<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>List of English posts on Hossein Naghdbishi</title><link>https://hossein.naghdbishi.com/posts/en/</link><description>Recent content in List of English posts on Hossein Naghdbishi</description><generator>Hugo</generator><language>en-us</language><managingEditor>hossein@naghdbishi.com (Hossein Naghdbishi)</managingEditor><webMaster>hossein@naghdbishi.com (Hossein Naghdbishi)</webMaster><lastBuildDate>Mon, 11 May 2026 23:19:04 +0330</lastBuildDate><atom:link href="https://hossein.naghdbishi.com/posts/en/index.xml" rel="self" type="application/rss+xml"/><item><title>The Courage to Be Disliked Summary</title><link>https://hossein.naghdbishi.com/posts/en/the-courage-to-be-disliked-summary/</link><pubDate>Mon, 11 May 2026 23:19:04 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/the-courage-to-be-disliked-summary/</guid><description>
&lt;p&gt;
The Courage to Be Disliked is a book written in a dialogue form between a young man and a philosopher. The book uses this Socratic form to teach us about Adlerian psychology.&lt;/p&gt;
&lt;div id="outline-container-headline-1" class="outline-2"&gt;
&lt;h2 id="headline-1"&gt;
We are All Subjective Beings
&lt;/h2&gt;
&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;
&lt;p&gt;The book posists that we can never escape from our subjectivity, and that we ourselves choose how to interpret what we see and what meaning to attribute to it. For example a person who is short can see it as an advantage or a disadvantage, it&amp;#39;s totally up to them. A diamond, too, could be seen as nothing but a small stone. Objective facts cannot be altered, but we can change our subjective interpretation as much as we like. Therefore, our attitude, how we see the world, is of utmost importance. Our present is not determined by our experiences in the past, but by the meaning we have attributed to them: “No matter what has occurred in your life up to this point, it should have no bearing at all on how you live from now on.”&lt;/p&gt;
&lt;p&gt;
The most important thing is not in what circumstances we were born, but on how we utilize the hands we&amp;#39;re dealt. “It’s not what one is born with but what use one makes of that equipment”. Instead of focusing on the past and what could have been, we must focus on what we can do with our current equipment:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Freudian etiology is a psychology of possession, and eventually it arrives at determinism. Adlerian psychology, on the other hand, is a psychology of use, and it is you who decides it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-2" class="outline-2"&gt;
&lt;h2 id="headline-2"&gt;
Etiology vs Teleology
&lt;/h2&gt;
&lt;div id="outline-text-headline-2" class="outline-text-2"&gt;
&lt;p&gt;We can approach psychology either through etiology or teleology (then there is logotherapy which focuses on the future). In Freudian psychology we tell ourselves stories of the past to study why we behave in a certain way. Etiology is studying the past to understand the causes of a behavior. In Adlerian psychology, however, it is suggested that we decide our behavior right now, and any decision on our part is because we have chosen to benefit from it.
For example, the reason a NEET doesn&amp;#39;t leave the house is not because of a past trauma or fear, but rather because they enjoy the comfort and attention they are paid by their parents, even if they&amp;#39;re miserable in all other respects. The book stresses that there is no such thing as trauma and every one of our behaviors is used for a purpose. Studying what we hope to achieve by our current behavior is teleology, in other words: &lt;strong&gt;&amp;#34;People are not driven by past causes, but move toward goals that they themselves set&amp;#34;&lt;/strong&gt;. Instead of asking &amp;#39;Why am I like this?&amp;#39; (Etiology), Adler asks &amp;#39;What is my goal in acting this way?&amp;#39; (Teleology). This shifts the power back to the individual.
Another example is being unhappy. If we&amp;#39;re unhappy, it&amp;#39;s because we actively choose being unhappy, and that we&amp;#39;ve judged it to be good for us. If there&amp;#39;s something we want to change in ourselves, again, we&amp;#39;re always able to do so, and the only reason we don&amp;#39;t is because we are making the decision not to. By holding this view, we don&amp;#39;t wait for others to change or the situation to change, we take the first step ourselves. The past and how we got here don&amp;#39;t matter, and how we do from here on is our responsibilty.
Here&amp;#39;s another example from the book, about the son of a father who hit him in his childhood:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;As long as I use etiology to think, It is because he hit me that I have a bad relationship with my father, it would be a matter that was impossible for me to do anything about. But if I can think, I brought out the memory of being hit because I don’t want my relationship with my father to get better, then I will be holding the card to repair relations. Because if I can just change the goal, that fixes everything.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
&amp;#34;Lifestyle&amp;#34;, one&amp;#39;s worldview and outlook on life, is something the person chooses for themselves. If we don&amp;#39;t change it, it&amp;#39;s mostly because we&amp;#39;ve grow accustomed to it, and we fear that by going to uncharted territories we may get hurt. &lt;strong&gt;Anxiety is generated by changing, and disappointment by not changing, and most people usually choose the latter.&lt;/strong&gt; In other words, most people are lacking in the courage to change and be happy.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-3" class="outline-2"&gt;
&lt;h2 id="headline-3"&gt;
Apparent Cause and Effect
&lt;/h2&gt;
&lt;div id="outline-text-headline-3" class="outline-text-2"&gt;
&lt;p&gt;The book offecs a poignant observation on &amp;#34;apparent cause and effect&amp;#34;—the way we contruct logical-sounding excuses to justify our stagnation. For example the book tells the story of a girl who created a fear of blushing to avoid proposing to a boy she liked and risking rejection. Many of us are guilty of the same crime. We come up with excuses every day, and force ourselves to see a as door closed, in order to escape the pain of trying and risking.&lt;/p&gt;
&lt;p&gt;
A parabole for an unwritten and procrastinating life itself:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I have a young friend who dreams of becoming a novelist, but he never seems to be able to complete his work. According to him, his job keeps him too busy, and he can never find enough time to write novels, and that’s why he can’t complete work and enter it for writing awards. But is that the real reason? No! It’s actually that he wants to leave the possibility of “I can do it if I try” open, by not committing to anything. He doesn’t want to expose his work to criticism, and he certainly doesn’t want to face the reality that he might produce an inferior piece of writing and face rejection. He wants to live inside that realm of possibilities, where he can say that he could do it if he only had the time, or that he could write if he just had the proper environment, and that he really does have the talent for it. In another five or ten years, he will probably start using other excuses like “I’m not young anymore” or “I’ve got a family to think about now.”
YOUTH: I can relate all too well to how he must feel.
PHILOSOPHER: He should just enter his writing for an award, and if he gets rejected, so be it. If he did, he might grow, or discover that he should pursue something different. Either way, he would be able to move on. That is what changing your current lifestyle is about. He won’t get anywhere by not submitting anything.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-4" class="outline-2"&gt;
&lt;h2 id="headline-4"&gt;
Life Is Not a Competition
&lt;/h2&gt;
&lt;div id="outline-text-headline-4" class="outline-text-2"&gt;
&lt;p&gt;Human beings are equal but not the same; this means that a math genius is no more valuable than a child who can&amp;#39;t do simple arithmetic, or even a bedridden person with no activity. If we have a view that leads us to believe there is a competition going on, it will make us say words like &amp;#34;I lost to that person&amp;#34; or &amp;#34;I beat that person&amp;#34;. In turn, we will see other people as enemies. We can&amp;#39;t celebrate their happiness because their victory is our defeat, and this deprives us of a sense of community.&lt;/p&gt;
&lt;p&gt;
But every person has a sense that they need to grow even more, and a small amount of feeling lacking in a good quality is healthy for growth. Since we&amp;#39;re equals, this healthy sense comes not from comparing ourselves with other people, but with comparing us with our ideal selves. &lt;strong&gt;Life is not a competition&lt;/strong&gt; and we should move forward without competing with others. However, a problem is created when a healthy sense of inferiority becomes inferiority complex. In this case, we will use our sense of inferiority as an excuse, we will say things like &amp;#34;A happened so B can&amp;#39;t&amp;#34;, or &amp;#34;If it only weren&amp;#39;t for A, I could have achieved B&amp;#34;. These are all excuses/life-lies.&lt;/p&gt;
&lt;p&gt;
One may even not aim for success—perhaps to protect their leisure time or out of a deep-seated fear of failure. So while one might claim &amp;#34;the reality is that having a good education makes it easier to be successful in society.&amp;#34; but &amp;#34;The real issue is how one confronts that reality. If what you are thinking is, I’m not well educated, so I can’t succeed, then instead of I can’t succeed, you should think, I don’t want to succeed.&amp;#34;
If we don&amp;#39;t lack the courage, we will be diligent and find a solution to our problem. However, if we lack the courage, we will either boast about our misfortunes (saying things like &amp;#39;you don&amp;#39;t know what I&amp;#39;ve been through&amp;#39;, like a baby who rules with his weakness), or get caught up in a superiority complex, boasting about the memories past.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-5" class="outline-2"&gt;
&lt;h2 id="headline-5"&gt;
Life-tasks
&lt;/h2&gt;
&lt;div id="outline-text-headline-5" class="outline-text-2"&gt;
&lt;p&gt;According to Adler, all problems are interpersonal problems. But not only our unhappiness is caused by interpersonal relationship, so is our happiness.&lt;/p&gt;
&lt;p&gt;
In Adlerian psychology, there are 2 objectives for human behavior: to be self-reliant, and to live in harmony with the society, which are supported by the two objectives of psychology: &amp;#34;I have the ability&amp;#34; and &amp;#34;People are my comrades&amp;#34;. These two objectives can be achieved by facing &amp;#34;life tasks&amp;#34;: tasks of work, tasks of love and tasks of friendship, all of which involve interpersonal relationships.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-6" class="outline-2"&gt;
&lt;h2 id="headline-6"&gt;
Courage and Worth
&lt;/h2&gt;
&lt;div id="outline-text-headline-6" class="outline-text-2"&gt;
&lt;p&gt;If one is not following through with their life-tasks, it is not because they&amp;#39;re not able to, but because they have lost the &lt;em&gt;courage&lt;/em&gt; to face their tasks. They should first realize that it is them who holds all the cards and decides what to do with them (have a teleological attitude and change the goal), and other people who want to help should use &amp;#39;encouragement&amp;#39;. They should not judge the person or let assistance become intervention, but show by gratitude that they believe in them, that they value their relationship with them, and respect them. In Adler&amp;#39;s view, &amp;#34;It is only when a person is able to feel that he has worth that he can possess courage&amp;#34;, and courage is a prerequisite to change.
But how can one feel like they have worth? It is when they feel &amp;#34;I am beneficial and making a contribution to the community&amp;#34; or that &amp;#34;I am of use to someone&amp;#34;. However this feeling of worthiness doesn&amp;#39;t have to be something at the level of acts, but &lt;strong&gt;at the level of being&lt;/strong&gt;. We love our grandparents just by them being present, not but the things that they do for us. The same can be true for anyone, even NEETs. “People can be of use to someone else simply by being alive, and have a true sense of their worth just by being alive.”&lt;/p&gt;
&lt;p&gt;
How should we increase courage then? Acceptance, Confidence, Focusing on the Strength.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We should first accept who we are, just the way we are, and feel at ease with ourselves. We should also believe in ourselves and have the courage to take the step forward regardless of the outcome. This focus on the process of growth and journey, rather than the outcome is called encouragement.&lt;/li&gt;
&lt;li&gt;We should form a belief within, that we&amp;#39;re capable and we&amp;#39;re &amp;#39;enough&amp;#39; as long as we have the courage try. It is the antidote to inferiority complex and moves us from praise based on the outcome &amp;#34;I like it when I/you do well/achieve.&amp;#34; to &amp;#34;I believe in my/your ability to handle this.&amp;#34;, &amp;#34;I am worthy because I had the courage, regardless of the outcome&amp;#34;.&lt;/li&gt;
&lt;li&gt;By noticing on every small step that we get right, our confidence in ourselves increases and we may grow even more.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&amp;#34;The state of coming up with all manner of pretexts [excuses] in order to avoid the life tasks&amp;#34; is a “life-lie”. And life-lies aren&amp;#39;t about being bad or evil, but about lacking courage. When telling a life-lie:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;One shifts one’s responsibility for the situation one is currently in to someone else. One is running away from one’s life tasks by saying that everything is the fault of other people, or the fault of one’s environment. It’s exactly the same as with the story I mentioned earlier about the female student with the fear of blushing. One lies to oneself, and one lies to the people around one, too. When you really think about it, it’s a pretty severe term.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-7" class="outline-2"&gt;
&lt;h2 id="headline-7"&gt;
Happiness
&lt;/h2&gt;
&lt;div id="outline-text-headline-7" class="outline-text-2"&gt;
&lt;p&gt;Happiness deeply relates to the matter of self-acceptance. We are the unhappiest of all when we dislike ourself, but we can begin to like ourselves by using the feeling of community and being of use to other people, and by self-acceptance. Our effect of contribution need not be visible either, its just a feeling that we decide to have:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Does one accept oneself on the level of acts, or on the level of being? This is truly a question that relates to the courage to be happy&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;You are not the one who decides if your contributions are of use. That is the task of other people, and is not an issue in which you can intervene. In principle, there is not even any way you can know whether you have really made a contribution. That is to say, when we are engaging in this contribution to others, the contribution does not have to be a visible one—all we need is &lt;strong&gt;the subjective sense&lt;/strong&gt; that “I am of use to someone,” or in other words, a feeling of contribution.
…
All human beings can be happy. But it must be understood that this does not mean all human beings are happy. Whether it is on the level of acts or on the level of being, one needs to feel that one is of use to someone. That is to say, one needs a feeling of contribution.
…
In a word, happiness is &lt;em&gt;the feeling&lt;/em&gt; of contribution. That is the definition of happiness.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Note that this sense of being of use to the community is intrinsic, and is deeply different from being recognized and valued by others.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If one really has a feeling of contribution, one will no longer have any need for recognition from others. Because one will already have the real awareness that “I am of use to someone,” without needing to go out of one’s way to be acknowledged by others. In other words, a person who is obsessed with the desire for recognition does not have any community feeling yet, and has not managed to engage in self-acceptance, confidence in others, or contribution to others.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-8" class="outline-2"&gt;
&lt;h2 id="headline-8"&gt;
Separation of Tasks, and Avoiding of Judgement
&lt;/h2&gt;
&lt;div id="outline-text-headline-8" class="outline-text-2"&gt;
&lt;p&gt;Adlerian psychology denies the need for recognition from others, and states that one should neither rebuke, nor praise. If we seek recognition from others, we are living other people&amp;#39;s lives, and letting them have the interpersonal relationship cards. &amp;#34;If you don&amp;#39;t live your life, who is going to live it for you?&amp;#34; One might imagine that we at least need recognition from our parents, or God to live a good life, but this is false.&lt;/p&gt;
&lt;p&gt;
We need to continually separate our tasks from those of others. A simple way to see whose task it is, is to ask who&amp;#39;s going to ultimately get the result from the choice being made. Even things that seem mundane, such as urging a child to study is not recommended, it is not the parent&amp;#39;s tasks to study, just to lend a hand whenever they&amp;#39;re asked. The child has no obligation to fulfill their parents&amp;#39; expectations either. When doing the right thing, how people construe our actions and feel as a result is not our task either, it is their task to choose their attitude. Also, we must be astute enough to know when to resign, if it is not our task, or if we&amp;#39;re unable to change something, we must know when to resign. To be able to do so shows that we have a firm grasp on the truth of things.&lt;/p&gt;
&lt;p&gt;
Freedom is being disliked by other people. We must not be afraid of being disliked. Behaving in a decent manner is our task, but whether other people like us or not is not. It is not our task to fulfill other people&amp;#39;s expectations.
Freedom is resisting one&amp;#39;s instincts and impulses, a stone is powerless once it has begun to roll downhill, but we are not stones.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-9" class="outline-2"&gt;
&lt;h2 id="headline-9"&gt;
Community
&lt;/h2&gt;
&lt;div id="outline-text-headline-9" class="outline-text-2"&gt;
&lt;p&gt;Adler put great emphasis on the feeling of community, on the feeling of &amp;#34;having one&amp;#39;s refuge&amp;#34; and &amp;#34;it&amp;#39;s okay to be here&amp;#34;, that other people (and even animals and objects) are our comrades. It is when we realize that &amp;#34;I&amp;#34; is not the center of the world and when we switch from self-interest to concern for others that are we are the most happy. It is through active commitment to the community of one&amp;#39;s accord that this happens, not by simply being there. Instead of &amp;#34;What will this person give me?&amp;#34;, we will ask &amp;#34;What can I give to this person&amp;#34;.&lt;/p&gt;
&lt;p&gt;
The relationships we build in the community should be horizontal, meaning that in its basis we must treat and be treated as equals. It is for this reason that we don&amp;#39;t praise other people, praising (as well as rebuke) is a form of manipulation. We must not praise or rebuke, but encourage and express confidence in one another and gratitude. We could even treat our friends in a vertical manner:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Even if you are not treating them in a boss-or-subordinate kind of way, it is as if you are saying, “A is above me, and B is below me,” for example, or “I’ll follow A’s advice, but ignore what B says,” or “I don’t mind breaking my promise to C.”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;But this undermines the very purpose of relationships, as &amp;#34;the final goal of interpersonal relationships is a feeling of community&amp;#34;.&lt;/p&gt;
&lt;p&gt;
For community feeling, &amp;#34;self-acceptance&amp;#34;, &amp;#34;confidence in others&amp;#34;, and &amp;#34;contribution to others&amp;#34; is required.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Self-acceptance is not &amp;#34;I can&amp;#34; or &amp;#34;I am strong&amp;#34;, it is not self-affirmation, but acknowledging our not being perfect and trying anyway.&lt;/li&gt;
&lt;li&gt;Confidence is different from trust, in the sense that confidence is a form of belief that requires no surety and is unconditional. The opposite of confidence is doubt, and one should not doubt the ability of their friends and loved ones.&lt;/li&gt;
&lt;li&gt;Contribution to others happens not as a self-sacrifice which is something Adler denies, but as a way to get a sense of community, and feeling worthy, dedicated to a cause greater than ourselves.&lt;/li&gt;
&lt;/ul&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Concept&lt;/td&gt;
&lt;td&gt;Definition&lt;/td&gt;
&lt;td&gt;The &amp;#34;Shift&amp;#34;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;strong&gt;Self-Acceptance&lt;/strong&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Acknowledging imperfection without self-affirmation lies.&lt;/td&gt;
&lt;td&gt;From &amp;#34;I must be special&amp;#34; to &amp;#34;I am enough as I am.&amp;#34;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;strong&gt;Confidence&lt;/strong&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Unconditional belief in others without requiring surety.&lt;/td&gt;
&lt;td&gt;From &amp;#34;I trust you if…&amp;#34; to &amp;#34;I believe in you regardless.&amp;#34;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;strong&gt;Contribution&lt;/strong&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The subjective sense of being of use to the community.&lt;/td&gt;
&lt;td&gt;From &amp;#34;What do I get?&amp;#34; to &amp;#34;What can I give?&amp;#34;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;strong&gt;Teleology&lt;/strong&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The study of the purpose behind current behaviors.&lt;/td&gt;
&lt;td&gt;From &amp;#34;I am stuck because of my past&amp;#34; to &amp;#34;I choose this for a goal.&amp;#34;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-10" class="outline-2"&gt;
&lt;h2 id="headline-10"&gt;
Life is a Series of Dots
&lt;/h2&gt;
&lt;div id="outline-text-headline-10" class="outline-text-2"&gt;
&lt;p&gt;There is much merit in accepting to be normal, and normal doesn&amp;#39;t mean incapable. Both children that try to get excellent grades and those who try to attract attention by problem behavior are in search of being a &amp;#34;special being&amp;#34;. However, pursuit of superiority is an unhealthy attitude. Avoiding perfectionism and accepting to be a normal being is an important part of self-acceptance. People who pursue superiority are tentative in their being, &amp;#39;en route&amp;#39; to some destination. They can&amp;#39;t rest until they arrive, but life is not a competition but a journey to be enjoyed. Life is not a line (kinetic) but a series of dots (energeial). If you finished a marathon using a helicopter that&amp;#39;d be pretty meaningless, so life is not a marathon, but a dance:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Life is a series of moments, which one lives as if one were dancing, right now, around and around each passing instant. And when one happens to survey one’s surroundings, one realizes, I guess I’ve made it this far. Among those who have danced the dance of the violin, there are people who stay the course and become professional musicians. Among those who have danced the dance of the bar examination, there are people who become lawyers. There are people who have danced the dance of writing and become authors. Of course, it also happens that people end up in entirely different places. But none of these lives came to an end “en route.” It is enough if one finds fulfillment in the here and now one is dancing.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In a dance one is engaged in each moment, and it is enough that we do what we can do now, earnestly and conscientiously, but in doing so we must not get too serious and keep our lives simple.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;For example, one wants to get into a university but makes no attempt to study. This an attitude of not living earnestly here and now. Of course, maybe the entrance examination is still far off. Maybe one is not sure what needs to be studied or how thoroughly, and one finds it troublesome. However, it is enough to do it little by little—every day one can work out some mathematical formulas, one can memorize some words. In short, one can dance the dance. By doing so, one is sure to have a sense of “this is what I did today”; this is what today, this single day, was for. Clearly, today is not for an entrance examination in the distant future.
…
And the same may be said with regard to your own life. You set objectives for the distant future, and think of now as your preparatory period. You think, I really want to do this, and I’ll do it when the time comes. This is a way of living that postpones life. As long as we postpone life, we can never go anywhere and will pass our days only one after the next in dull monotony, because we think of here and now as just a preparatory period, as a time for patience. But a “here and now” in which one is studying for an entrance examination in the distant future, for example, is the real thing.
…
&lt;strong&gt;The greatest life-lie of all is to not live here and now.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
Life&amp;#39;s meaning is what we attribute to it. We have freedom in the meaning we assign to it and the choices we make, and our guiding star in these is contributing to the community:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Regardless of the circumstances, we must take some form of action. We must stand up to Kant’s “inclination.”
Now, suppose one experiences a major natural disaster, and one’s response is to look back at the past in an etiological manner and say, “What could have caused such a thing to happen?” How meaningful would that be? An experience of hardship should be an opportunity to look ahead and think, What can I do from now on?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-11" class="outline-2"&gt;
&lt;h2 id="headline-11"&gt;
Misc
&lt;/h2&gt;
&lt;div id="outline-text-headline-11" class="outline-text-2"&gt;
&lt;p&gt;Adler believed in holism, meaning that we can&amp;#39;t separate ourselves from our emotion, or somehow make us think of us as being cosisted of different systems.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;PHILOSOPHER: Suppose, for instance, that there is a certain Mr. A whom you don’t like because he has some flaws that are hard to forgive.
YOUTH: Ha-ha, if we’re looking for people I don’t like, there’s no shortage of candidates.
PHILOSOPHER: But it isn’t that you dislike Mr. A because you can’t forgive his flaws. You had the goal of taking a dislike to Mr. A beforehand and then started looking for the flaws to satisfy that goal.
YOUTH: That’s ridiculous! Why would I do that?
PHILOSOPHER: So that you could avoid an interpersonal relationship with Mr. A.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>Summary of Man's Search for Meaning</title><link>https://hossein.naghdbishi.com/posts/en/mans-search-for-meaning-summary/</link><pubDate>Sun, 10 May 2026 14:54:47 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/mans-search-for-meaning-summary/</guid><description>&lt;p&gt;
&amp;#34;Man&amp;#39;s Search for Meaning&amp;#34; is about the experiences and philosophy of Viktor Frankl, a concentration camp survivor. As a psychiatrist who endured many atrocities, he uses his observations in the camp to claim that life remains meaningful and worthwhile even under the most extreme circumstances. As we have freedom, not in conditions of life, but in how we respond to life, we get to choose to become a saint or a swine, not as the effect of the environment, but because of our freedom to choose. One&amp;#39;s attitude is the ultimate liberty that cannot be taken away from us, and what makes life meaningful under the most extreme circumstances.&lt;/p&gt;
&lt;p&gt;
His answer to the famous question of &amp;#34;life&amp;#39;s meaning&amp;#34; is similar to that of Tolstoy&amp;#39;s famous &amp;#39;&lt;a href="https://en.wikipedia.org/wiki/The_Three_Questions"&gt;Three Questions&lt;/a&gt;&amp;#39; story. Just like a chess game that has no &lt;em&gt;best move&lt;/em&gt; in general, but a best move at every moment, so does life. The best thing we can do, and the meaning of our life is determined by the moment we are facing now and how we decide to respond to life&amp;#39;s circumstances. Therefore, it is not that we ask what&amp;#39;s the meaning of life, but that we get to know what life asks of us, and whether we wish to endure hardships and live it with much dignity.&lt;/p&gt;
&lt;p&gt;
He claims that human life usually derives its meaning from one of these:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Being creative and actually creating something, such as authoring a book&lt;/li&gt;
&lt;li&gt;Interpersonal relationships, like the joy of seeing one&amp;#39;s child, or loving another person&lt;/li&gt;
&lt;li&gt;While the most important thing is to be creative and change the root cause of our suffering, if need be, just facing an unchangeable hardship with great dignity and courage, learning to change ourselves and grow spiritually as a result is a worthy meaning.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;As a result, life remains valuable in all circumstances, and we can &amp;#34;say yes to life, in spite of everything&amp;#34;. One reason that every life is valuable and meaningful is that we don&amp;#39;t know everything. He points out that since we do not know everything about this world, there might be a supermeaning to our life, a meaning beyond our comprehension. Therefore, we must try our best at all times.&lt;/p&gt;
&lt;p&gt;
He also warns against trying to live in the past. While one can do so in moderation, doing so excessively may cause us to lose sight of the opportunity of growth that is in front of us. Every difficulty is a chance to test our inner strength and a chance to grow and reach inner triumph.
It is of utmost importance that we &lt;strong&gt;believe&lt;/strong&gt; (a belief, a kind of faith as William James would put it) in the future. &amp;#34;The prisoner who had lost faith in the future—his future—was doomed. With his loss of belief in the future, he also lost his spiritual hold; he let himself decline and became subject to mental and physical decay.&amp;#34;&lt;/p&gt;
&lt;p&gt;
Frankl believes that human&amp;#39;s main concern is not to seek pleasure or avoid pain, but to see a meaning in one&amp;#39;s life. He reports that it wasn&amp;#39;t the bulky men who survived at the end, but those who had a goal beyond themselves such as writing a book only they could write, or seeing their children again.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;That is why people sometimes even choose to suffer. And it is how one can restore a man’s inner strength in the camp, by showing him some future goal. Nietzsche’s words, “He who has a why to live for can bear with almost any how,” could be the guiding motto for all psychotherapeutic and psychohygienic efforts regarding prisoners. Whenever there was an opportunity for it, one had to give them a why—an aim—for their lives, in order to strengthen them to bear the terrible how of their existence. Woe to him who saw no more sense in his life, no aim, no purpose, and therefore no point in carrying on. He was soon lost. The typical reply with which such a man rejected all encouraging arguments was, “I have nothing to expect from life any more.” What sort of answer can one give to that?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;What was really needed was a fundamental change in our attitude toward life. We had to learn ourselves and, furthermore, we had to teach the despairing men, that it did not really matter what we expected from life, but rather what life expected from us. We needed to stop asking about the meaning of life, and instead to think of ourselves as those who were being questioned by life—daily and hourly. Our answer must consist, not in talk and meditation, but in right action and in right conduct. Life ultimately means taking the responsibility to find the right answer to its problems and to fulfill the tasks which it constantly sets for each individual.&amp;#34;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
Frankl&amp;#39;s philosophy extends beyond the camps. Jerry Long is another logotherapist. He, a quadriplegic who became a university professor, says:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Once, after speaking to a large audience, I was asked if I ever felt sad because I could no longer walk. I replied, &amp;#34;Professor Frankl can hardly see, I cannot walk at all, and many of you can hardly cope with life. What is crucial to remember is this - We don&amp;#39;t need just our eyes, just our legs, or just our minds. All we need are the wings of our souls and together we can fly.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
Reading this book I think I found my answer to a question I have had for a long time. While people are equal and cannot be compared due to different circumstances, ceteris paribus, what makes another person better than the other? &amp;#34;It is a question of the attitude one takes toward life’s challenges and opportunities, both large and small.&amp;#34; People should be an active designer of their life story by making good decisions in every single moment of their life, determining how they want their life to look like.&lt;/p&gt;</description></item><item><title>Poor Man's VPN: How to Use Reverse SSH for Quick NAT Traversal</title><link>https://hossein.naghdbishi.com/posts/en/poor-mans-vpn-autossh/</link><pubDate>Sat, 03 Jan 2026 23:53:49 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/poor-mans-vpn-autossh/</guid><description>&lt;p&gt;
Suppose you want to connect from PC &lt;em&gt;A&lt;/em&gt; to PC &lt;em&gt;B&lt;/em&gt;, and neither have a pubilc IP. How can you do that? One way is to use &lt;a href="https://github.com/rustonbsd/iroh-ssh"&gt;iroh-ssh&lt;/a&gt;, it&amp;#39;s neat but currently they don&amp;#39;t support custom relays so it&amp;#39;s a deadend in more restricted networks. But if you own a VPS, you can actually can connect these two.&lt;/p&gt;
&lt;p&gt;
Assuming you have installed &lt;code class="verbatim"&gt;autossh&lt;/code&gt; on &lt;em&gt;B&lt;/em&gt;, create a systemd service on it as well &lt;code class="verbatim"&gt;/etc/systemd/system/autossh-ssh.service&lt;/code&gt;:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[Unit]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Description=AutoSSH Service
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;After=network.target
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[Service]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Type=simple
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;User=user
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Environment=AUTOSSH_GATETIME=0
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ExecStart=/usr/bin/autossh -M 0 -N -o &amp;#34;ServerAliveInterval 30&amp;#34; -o &amp;#34;ServerAliveCountMax 10&amp;#34; -o &amp;#34;ExitOnForwardFailure yes&amp;#34; -o &amp;#34;AddressFamily inet&amp;#34; -o &amp;#34;TCPKeepAlive no&amp;#34; -R localhost:REMOTE_SSH_PORT_ON_VPS:localhost:SSH_PORT_OF_B -i YOUR_KEY_ADDRESS USER@VPS -p VPS_SSH_PORT
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Restart=always
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;RestartSec=5
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;KillMode=process # Ensure child processes are not killed
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[Install]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;WantedBy=multi-user.target&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Then enable it using &lt;code class="verbatim"&gt;sudo systemctl enable autossh-ssh&lt;/code&gt;. Now it will always keep a connection to your VPS and expose its SSH port on your VPS&amp;#39;s &lt;code class="verbatim"&gt;localhost:REMOTE_SSH_PORT_ON_VPS&lt;/code&gt; (localhost can be changed to your VPS&amp;#39;s static IP if that&amp;#39;s what you need). Now you can connect to &lt;em&gt;B&lt;/em&gt; on your VPS.&lt;/p&gt;
&lt;p&gt;
If you need &lt;em&gt;B&lt;/em&gt;&amp;#39;s SSH port on &lt;em&gt;A&lt;/em&gt;, you have to port forward: &lt;code class="verbatim"&gt;ssh user@VPS_IP -p VP_SSH_PORT -L localhost:REMOTE_SSH_PORT_ON_A:localhost:REMOTE_SSH_PORT_ON_VPS&lt;/code&gt;, which means you can connect to &lt;em&gt;B&lt;/em&gt; from &lt;em&gt;A&lt;/em&gt;: &lt;code class="verbatim"&gt;ssh user@localhost -P REMOTE_SSH_PORT_ON_A&lt;/code&gt;.
Q.E.D!&lt;/p&gt;
&lt;p&gt;
Side note: If for whatever reason you can&amp;#39;t connect to &lt;em&gt;B&lt;/em&gt;, then go to your VPS, kill the SSH that is responsible for the &lt;code class="verbatim"&gt;REMOTE_SSH_PORT_ON_VPS&lt;/code&gt;. After some time &lt;em&gt;B&lt;/em&gt; will create a fresh connection which has a better likelihood of good connection.&lt;/p&gt;
&lt;p&gt;
Side note: If you own a VPS maybe you&amp;#39;re not so poor monetarily but rather temporarily, since you don&amp;#39;t have time to deal with Wireguard and so on.&lt;/p&gt;
&lt;p&gt;
Update: Just use &lt;a href="https://yggdrasil-network.github.io/"&gt;yggdrasil&lt;/a&gt; instead.&lt;/p&gt;</description></item><item><title>PWN Cheatsheet</title><link>https://hossein.naghdbishi.com/posts/en/pwn-cheatsheet/</link><pubDate>Sun, 16 Nov 2025 23:30:51 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/pwn-cheatsheet/</guid><description>
&lt;div id="outline-container-headline-1" class="outline-2"&gt;
&lt;h2 id="headline-1"&gt;
x86_64 Assembly
&lt;/h2&gt;
&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;%rax&lt;/code&gt; is a reference to &lt;code class="verbatim"&gt;rax&lt;/code&gt; register.&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;$0x4&lt;/code&gt; is a constant.&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;0(%esp)&lt;/code&gt; or &lt;code class="verbatim"&gt;(%esp)&lt;/code&gt; is the value loaded from the memory address that is stored in &lt;code class="verbatim"&gt;%esp&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;12(%esp)&lt;/code&gt; dereferences memory 12 bytes above the address contained in ESP&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code class="verbatim"&gt;mov A,B&lt;/code&gt;, B is changed to become A. A&amp;#39;s value is -&amp;gt; put into B.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This is AT&amp;amp;T x86 syntax (what GDB uses). In this syntax the destination register comes last. In RISC-V assembly the opposite is true.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;push A&lt;/code&gt; adds &lt;code class="verbatim"&gt;A&lt;/code&gt; as the last element of the stack, decreases the value of &lt;code class="verbatim"&gt;rsp/esp&lt;/code&gt; by one word (since the stack grows from high to low).&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;pop rax/eax&lt;/code&gt; removes the last element of the stack, copies into &lt;code class="verbatim"&gt;rax/eax&lt;/code&gt; increases the value of &lt;code class="verbatim"&gt;rsp/esp&lt;/code&gt; by one word (since the stack grows from high to low).&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;jmp&lt;/code&gt; is equivalent to &lt;code class="verbatim"&gt;mov rip, target_address&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;call&lt;/code&gt; is equivalent to &lt;code class="verbatim"&gt;push rip(old return address)&lt;/code&gt; and &lt;code class="verbatim"&gt;jmp dst_func.&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;ret&lt;/code&gt; is equivalent to &lt;code class="verbatim"&gt;pop rip(old return address)&lt;/code&gt; which is &lt;code class="verbatim"&gt;jmp src_func&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Because stack grows from high addresses to low addresses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;sub $8,%rsp&lt;/code&gt; increases stack size.&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;add $8,%rsp&lt;/code&gt; decreases stack size.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-2" class="outline-2"&gt;
&lt;h2 id="headline-2"&gt;
Stack
&lt;/h2&gt;
&lt;div id="outline-text-headline-2" class="outline-text-2"&gt;
&lt;div id="outline-container-headline-3" class="outline-3"&gt;
&lt;h3 id="headline-3"&gt;
Stack
&lt;/h3&gt;
&lt;div id="outline-text-headline-3" class="outline-text-3"&gt;
&lt;img src="https://hossein.naghdbishi.com/images/stack.svg" alt="This figure shows the memory layout created by C." title="/images/stack.svg" style="height:100%;width:100%;"/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-4" class="outline-3"&gt;
&lt;h3 id="headline-4"&gt;
Backgroud
&lt;/h3&gt;
&lt;div id="outline-text-headline-4" class="outline-text-3"&gt;
&lt;ul&gt;
&lt;li&gt;Each byte is 8 bits.&lt;/li&gt;
&lt;li&gt;Each word is 4 bytes in 32-bit systems and 8 bytes in 64-bit systems.&lt;/li&gt;
&lt;li&gt;Each register can store one word.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-5" class="outline-3"&gt;
&lt;h3 id="headline-5"&gt;
Registers
&lt;/h3&gt;
&lt;div id="outline-text-headline-5" class="outline-text-3"&gt;
&lt;div id="outline-container-headline-6" class="outline-4"&gt;
&lt;h4 id="headline-6"&gt;
Pointing to the code section
&lt;/h4&gt;
&lt;div id="outline-text-headline-6" class="outline-text-4"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;rip/eip&lt;/code&gt; is the instruction pointer, and it stores the address of the machine instruction currently being executed (code section). In RISC-V, this register is called the PC (program counter).&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-7" class="outline-4"&gt;
&lt;h4 id="headline-7"&gt;
Pointing to the stack section
&lt;/h4&gt;
&lt;div id="outline-text-headline-7" class="outline-text-4"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;rbp/ebp&lt;/code&gt; is the base pointer, and it stores the address of the base of the current stack frame. In RISC-V systems, this register is called the FP (frame pointer).&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;rsp/esp&lt;/code&gt; is the stack pointer, and it stores the address of the last element of the current stack frame. In RISC-V, this register is called the SP (stack pointer).&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-8" class="outline-4"&gt;
&lt;h4 id="headline-8"&gt;
General Purpose Registers
&lt;/h4&gt;
&lt;div id="outline-text-headline-8" class="outline-text-4"&gt;
&lt;ul&gt;
&lt;li&gt;16 general purpose registers in 64-bit systems: &lt;code class="verbatim"&gt;RAX&lt;/code&gt;, &lt;code class="verbatim"&gt;RBX&lt;/code&gt;, &lt;code class="verbatim"&gt;RCX&lt;/code&gt;, &lt;code class="verbatim"&gt;RDX&lt;/code&gt;, &lt;code class="verbatim"&gt;RSI&lt;/code&gt;, &lt;code class="verbatim"&gt;RDI&lt;/code&gt; and &lt;code class="verbatim"&gt;R8&lt;/code&gt; to &lt;code class="verbatim"&gt;R15&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;6 general puprose registers in 32-bit systems: &lt;code class="verbatim"&gt;EAX&lt;/code&gt;, &lt;code class="verbatim"&gt;EBX&lt;/code&gt;, &lt;code class="verbatim"&gt;ECX&lt;/code&gt;, &lt;code class="verbatim"&gt;EDX&lt;/code&gt;, &lt;code class="verbatim"&gt;ESI&lt;/code&gt;, &lt;code class="verbatim"&gt;EDI&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;table style="height:80%;width:50%; text-align:center; margin:auto;"&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Argument&lt;/th&gt;
&lt;th&gt;Register (64-bit)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Arg 1&lt;/td&gt;
&lt;td&gt;%rdi&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Arg 2&lt;/td&gt;
&lt;td&gt;%rsi&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Arg 3&lt;/td&gt;
&lt;td&gt;%rdx&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Arg 4&lt;/td&gt;
&lt;td&gt;%rcx&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Arg 5&lt;/td&gt;
&lt;td&gt;%r8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Arg 6&lt;/td&gt;
&lt;td&gt;%r9&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-9" class="outline-3"&gt;
&lt;h3 id="headline-9"&gt;
Little Endian or Big Endian
&lt;/h3&gt;
&lt;div id="outline-text-headline-9" class="outline-text-3"&gt;
&lt;ul&gt;
&lt;li&gt;Address numbers point to bytes.&lt;/li&gt;
&lt;li&gt;Using little endian &lt;code class="verbatim"&gt;0x44332211&lt;/code&gt; will be stored like &lt;code class="verbatim"&gt;0x11 0x22 0x33 0x44&lt;/code&gt; in memory, meaning that the most signifcant byte has the highest address number (the memory starts from &lt;code class="verbatim"&gt;0x0&lt;/code&gt; to &lt;code class="verbatim"&gt;0x3&lt;/code&gt;, and &lt;code class="verbatim"&gt;0x44&lt;/code&gt; is stored at &lt;code class="verbatim"&gt;0x3&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-10" class="outline-2"&gt;
&lt;h2 id="headline-10"&gt;
Function Call in x86
&lt;/h2&gt;
&lt;div id="outline-text-headline-10" class="outline-text-2"&gt;
&lt;p alt="This figure shows the memory layout created by C, after the function foo below is called (almost)." style="height:100%;width:100%;"&gt;&lt;img src="https://hossein.naghdbishi.com/images/stack-func-call.svg" alt="/images/stack-func-call.svg" title="/images/stack-func-call.svg"/&gt;
See the photos in &lt;a href="https://textbook.cs161.org/memory-safety/x86.html"&gt;UC Berkeley&amp;#39;s CS161 Textbook&lt;/a&gt; &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Function args are pushed into the stack. (or onto the general registers such as &lt;code class="verbatim"&gt;rdi&lt;/code&gt;, &lt;code class="verbatim"&gt;rsi&lt;/code&gt;, &lt;code class="verbatim"&gt;rdx&lt;/code&gt;, &lt;code class="verbatim"&gt;rcx&lt;/code&gt; if SysV x86-64)&lt;/li&gt;
&lt;li&gt;Return address (old &lt;code class="verbatim"&gt;rip&lt;/code&gt;) is pushed into the stack. &lt;/li&gt;
&lt;li&gt;Function jump happens (&lt;code class="verbatim"&gt;rip&lt;/code&gt; = new &lt;code class="verbatim"&gt;rip&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;The new function pushes old &lt;code class="verbatim"&gt;rbp&lt;/code&gt; onto the stack.&lt;/li&gt;
&lt;li&gt;Previous frame&amp;#39;s &lt;code class="verbatim"&gt;rbp&lt;/code&gt; becomes the current &lt;code class="verbatim"&gt;rsp&lt;/code&gt; (old function&amp;#39;s last element is the new functions bottom)&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;rsp&lt;/code&gt; is grown to fit the new function.&lt;/li&gt;
&lt;li&gt;The new function does whatever it does&lt;/li&gt;
&lt;li&gt;At the end of the new function&amp;#39;s run, we are at its &lt;code class="verbatim"&gt;rbp&lt;/code&gt;, which is the old one&amp;#39;s &lt;code class="verbatim"&gt;rsp&lt;/code&gt;. Previous frame&amp;#39;s &lt;code class="verbatim"&gt;rsp&lt;/code&gt; is the present frame&amp;#39;s &lt;code class="verbatim"&gt;rbp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Old &lt;code class="verbatim"&gt;rbp&lt;/code&gt; is poped back.&lt;/li&gt;
&lt;li&gt;Old &lt;code class="verbatim"&gt;rip&lt;/code&gt; is poped back.&lt;/li&gt;
&lt;li&gt;Decrease the size of the stack by the size of args.&lt;/li&gt;
&lt;/ol&gt;
&lt;div id="outline-container-headline-11" class="outline-3"&gt;
&lt;h3 id="headline-11"&gt;
Example C code
&lt;/h3&gt;
&lt;div id="outline-text-headline-11" class="outline-text-3"&gt;
&lt;div class="src src-c"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;foo&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; a, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; b) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; bar[&lt;span style="color:#ae81ff"&gt;4&lt;/span&gt;];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;main&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;foo&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;2&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Which gives the following assembly:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;0000000000001129 &amp;lt;foo&amp;gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 1129:	55 	push %rbp // Step 4. RBP is saved on the stack right below RIP (saved in the previous step).
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 112a:	48 89 e5 	mov %rsp,%rbp // Step 5. The new stack base is the previous stack pointer.
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 112d:	89 7d ec 	mov %edi,-0x14(%rbp) // Foo saves first arg into its own stack to free up %edi 
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 1130:	89 75 e8 	mov %esi,-0x18(%rbp) // Foo saves second arg into its own stack to free up %esi
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 1133:	90 	nop // Alignment, could be Step 6. =sub $N, %rsp= &amp;amp; Step. 7 if our function involved anything.
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; // Since Step. 6 didn&amp;#39;t take place, rsp didn&amp;#39;t change.
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; // therefore =mov %rsp, %rbp= (Step 8) is not run either.
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; // We could change this behavior using gcc flag -mno-red-zone
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 1134:	5d 	pop %rbp // Step 9. Old RBP is restored
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 1135:	c3 	ret // == pop %rip which is Step 10. Old RIP is restored
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; // Step 11. doesn&amp;#39;t happen since in the x86-64 ABI, since the caller
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; // passed arguments in registers, there is nothing on the stack for
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; // the caller to clean up
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;0000000000001136 &amp;lt;main&amp;gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 1136:	55 	push %rbp // This is step 4 since main is not the starting function and is being called from _start
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 1137:	48 89 e5 	mov %rsp,%rbp // this is also step 5 but we don&amp;#39;t analyze main.
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 113a:	be 02 00 00 00 	mov $0x2,%esi // Step 1. Saving args (from right to left): 2 
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 113f:	bf 01 00 00 00 	mov $0x1,%edi // Step 1. Saving args (from right to left): 1 
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 1144:	e8 e0 ff ff ff 	call 1129 &amp;lt;foo&amp;gt; // Step 2. RIP is pushed on the stack. Step 3. RIP is changed to the func address of foo (00001129).
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 1149:	b8 00 00 00 00 	mov $0x0,%eax
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 114e:	5d 	pop %rbp
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 114f:	c3 	ret&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-12" class="outline-2"&gt;
&lt;h2 id="headline-12"&gt;
Binary Background Check
&lt;/h2&gt;
&lt;div id="outline-text-headline-12" class="outline-text-2"&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Purpose&lt;/td&gt;
&lt;td&gt;Command&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Check security properties (NX bit, dynamic vs static)&lt;/td&gt;
&lt;td&gt;&lt;code class="verbatim"&gt;rabin2 -I a.out&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;List functions imported from shared libraries&lt;/td&gt;
&lt;td&gt;&lt;code class="verbatim"&gt;rabin2 -i a.out&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Find functions likely written by the programmer&lt;/td&gt;
&lt;td&gt;&lt;code class="verbatim"&gt;rabin2 -qs a.out&lt;/code&gt; piped to &lt;code class="verbatim"&gt;grep -ve imp -e &amp;#39; 0 &amp;#39;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Find strings&lt;/td&gt;
&lt;td&gt;&lt;code class="verbatim"&gt;strings a.out&lt;/code&gt; or (better) &lt;code class="verbatim"&gt;rabin2 -z split&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-13" class="outline-2"&gt;
&lt;h2 id="headline-13"&gt;
Return Oriented Programming
&lt;/h2&gt;
&lt;div id="outline-text-headline-13" class="outline-text-2"&gt;
&lt;div id="outline-container-headline-14" class="outline-3"&gt;
&lt;h3 id="headline-14"&gt;
Finding ROPs
&lt;/h3&gt;
&lt;div id="outline-text-headline-14" class="outline-text-3"&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;With ROPGadget:&lt;/p&gt;
&lt;div class="src src-bash"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ROPgadget --binary libc.so.6 --only &lt;span style="color:#e6db74"&gt;&amp;#34;add|jmp&amp;#34;&lt;/span&gt; &amp;gt; output &lt;span style="color:#75715e"&gt;# in case you&amp;#39;re looking for add followed by jump for example, otherwise remove&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cat output | grep &lt;span style="color:#e6db74"&gt;&amp;#39;add rdi, 0x10 ; jmp rc&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;With Ropper:&lt;/p&gt;
&lt;div class="src src-bash"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ropper --file ./libc.so.6 --search &lt;span style="color:#e6db74"&gt;&amp;#34;add rdi, 0x10; jmp rcx&amp;#34;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-15" class="outline-2"&gt;
&lt;h2 id="headline-15"&gt;
Misc
&lt;/h2&gt;
&lt;div id="outline-text-headline-15" class="outline-text-2"&gt;
&lt;ul&gt;
&lt;li&gt;PLT (Procedure Linkage Table): a small trampoline in the binary used for calls to external (dynamically linked) functions. Calls in the code go to func@plt; the PLT entry jumps to the real function address found in the GOT (or invokes the dynamic resolver on first call).&lt;/li&gt;
&lt;li&gt;GOT (Global Offset Table): a writable table of addresses in the binary. Each GOT slot holds the runtime address of a library function (or other dynamic symbol). The dynamic linker/loader fills GOT entries the first time a PLT entry is used (or at load time, depending on RELRO).&lt;/li&gt;
&lt;li&gt;They let the loader resolve and redirect calls to shared-library functions without embedding fixed library addresses in the code (works with ASLR and dynamic linking).&lt;/li&gt;
&lt;/ul&gt;
&lt;div id="outline-container-headline-16" class="outline-3"&gt;
&lt;h3 id="headline-16"&gt;
Memory Alignment
&lt;/h3&gt;
&lt;div id="outline-text-headline-16" class="outline-text-3"&gt;
&lt;ul&gt;
&lt;li&gt;Why it happens: &lt;a href="https://stackoverflow.com/a/381368"&gt;to give data to the CPU/CPU cache in one read instead of two&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-17" class="outline-2"&gt;
&lt;h2 id="headline-17"&gt;
Sources
&lt;/h2&gt;
&lt;div id="outline-text-headline-17" class="outline-text-2"&gt;
&lt;p&gt;Disclaimer: Some of the sentences here are a direct copy of the links below, so I don&amp;#39;t own their copyright. Follow the original sources.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://ropemporium.com/guide.html"&gt;https://ropemporium.com/guide.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/7060970/substitutes-for-x86-assembly-call-instruction"&gt;https://stackoverflow.com/questions/7060970/substitutes-for-x86-assembly-call-instruction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://textbook.cs161.org/memory-safety/x86.html#fnref:2"&gt;https://textbook.cs161.org/memory-safety/x86.html#fnref:2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>How to Share a Secret: Summary of Shamir's Paper</title><link>https://hossein.naghdbishi.com/posts/en/how-to-share-a-secret/</link><pubDate>Mon, 11 Aug 2025 13:15:47 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/how-to-share-a-secret/</guid><description>&lt;p&gt;
In his 1979 paper, Adi Shamir explains an ingenious way to share a secret between &lt;code class="verbatim"&gt;n&lt;/code&gt; people, such that only if &lt;code class="verbatim"&gt;k&lt;/code&gt; of them cooperate will the secret be revealed. And that even if &lt;code class="verbatim"&gt;k-1&lt;/code&gt; people cooperated, nothing would be revealed.&lt;/p&gt;
&lt;p&gt;
How it works is simple.
For a polynomial of degree 1 (i.e, $f(x)=a_{1}x+a_{0}$), how many &lt;code class="verbatim"&gt;(x,y)&lt;/code&gt; points do you need to find $a_{1}$ and $a_{0}$? Two. If you had only one point, you could find a relationship between $a_{0}$ and $a_{1}$ but that would be all.
For a polynomial of degree 2 (i.e, $f(x)=a_{2}x^2+a_{1}x+a_{0}$), how many &lt;code class="verbatim"&gt;(x,y)&lt;/code&gt; points do you need to find $a_{2}, a_{1}$ and $a_{0}$? Three. If you had only two points, there would still be an infinite pairs of $a_{2}$, $a_{1}$ and $a_{0}$.&lt;/p&gt;
&lt;p&gt;
Knowing what we know about polynomials, suppose &lt;code class="verbatim"&gt;n&lt;/code&gt; people need to be able to be part of our trusted group, such that if &lt;code class="verbatim"&gt;k&lt;/code&gt; of them cooperated, they could work out the secret.&lt;/p&gt;
&lt;p&gt;
We set $a_{0}$ as our secret. We generate a polynomial of degree &lt;code class="verbatim"&gt;k-1&lt;/code&gt; so there are &lt;code class="verbatim"&gt;k&lt;/code&gt; coefficients and at least &lt;code class="verbatim"&gt;k&lt;/code&gt; points or cooperating members are needed. Except for $a_{0}$ which is our secret, we generate the coefficients ($a_{1}$ to $a_{n}$) randomly. Then we index our &lt;code class="verbatim"&gt;n&lt;/code&gt; members give them each the &lt;code class="verbatim"&gt;f(x)&lt;/code&gt; of their index, so member one gets &lt;code class="verbatim"&gt;f(1)&lt;/code&gt;, member two gets &lt;code class="verbatim"&gt;f(2)&lt;/code&gt; and so on up to member n who gets &lt;code class="verbatim"&gt;f(n)&lt;/code&gt;. Now if any &lt;code class="verbatim"&gt;k&lt;/code&gt; members cooperate, they&amp;#39;ll have the numbers to find out all the coefficients, and work out $a_{0}$. Easy as that. We will also use modulo arithmetic for our polynomial equations so every number is equally likely in it.&lt;/p&gt;</description></item><item><title>Tail Recursion</title><link>https://hossein.naghdbishi.com/posts/en/tail-recursion/</link><pubDate>Sun, 10 Aug 2025 11:18:51 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/tail-recursion/</guid><description>
&lt;p&gt;
As nice as recursion is, sometimes our stack may grow too large, causing our program to crash due to running out of stack space. However, some languages support tail call optimization. That is, if you write your function in a way that leaves no need to use the previous stack, the compiler will use the same stack for its job and you&amp;#39;ll never hit the stack overflow problem.&lt;/p&gt;
&lt;p&gt;
For example, here&amp;#39;s a Fibonacci implementation in C.&lt;/p&gt;
&lt;div class="src src-c"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// badfib.C
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Non-tail recursive function for Fibonacci
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;fibonacci&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (n &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (n &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;fibonacci&lt;/span&gt;(n &lt;span style="color:#f92672"&gt;-&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;) &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;fibonacci&lt;/span&gt;(n &lt;span style="color:#f92672"&gt;-&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;2&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;main&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Enter a number: &amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;scanf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;%d&amp;#34;&lt;/span&gt;, &lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;n);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Fibonacci of %d is %d&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;\n&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;, n, &lt;span style="color:#a6e22e"&gt;fibonacci&lt;/span&gt;(n));
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;img src="https://hossein.naghdbishi.com/images/fibonacci-stack.svg" alt="This figure shows how fibonacci&amp;#39;s 5th term is calculated." title="/images/fibonacci-stack.svg" style="height:100%;width:100%;color:red;"/&gt;
&lt;p&gt;
Using this code, calculating the 5th term (&lt;code class="verbatim"&gt;1,1,2,3,5&lt;/code&gt;) can be done like the picture above. You can see the stack frames grow too long. Each call of the function waits for the return of at least two other function calls, and neither of the first two branches can be calculated until we reach &lt;code class="verbatim"&gt;fibonacci(0)&lt;/code&gt; and &lt;code class="verbatim"&gt;fibonacci(1)&lt;/code&gt;. Since we need to keep track of what two numbers are being added together, we also can&amp;#39;t reduce the amount of data we keep.&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;❯ gcc -O0 badfib.c -o badfib
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;❯ ./badfib
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Enter a number: 500000
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[1] 43221 segmentation fault (core dumped) ./badfib
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;❯ gcc -O1 badfib.c -o badfib_optimized
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;❯ ./badfib_optimized
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Enter a number: 500000
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[1] 43283 segmentation fault (core dumped) ./badfib_optimized
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;❯ gcc -O3 badfib.c -o badfib_super_optimized
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;❯ ./badfib_super_optimized
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Enter a number: 500000
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# takes too long :)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
As you can see, no matter the level of optimization, the code doesn&amp;#39;t finish in the way we want it to.&lt;/p&gt;
&lt;p&gt;
Now suppose we refactor our code like this:&lt;/p&gt;
&lt;div class="src src-c"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Tail recursive function for Fibonacci
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;fibonacci_tail&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; a, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; b) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (n &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; a;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (n &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; b;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;fibonacci_tail&lt;/span&gt;(n &lt;span style="color:#f92672"&gt;-&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, b, a &lt;span style="color:#f92672"&gt;+&lt;/span&gt; b);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;fibonacci&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;fibonacci_tail&lt;/span&gt;(n, &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;main&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Enter a number: &amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;scanf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;%d&amp;#34;&lt;/span&gt;, &lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;n);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Fibonacci&amp;#39;s %d term is %d&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;\n&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;, n, &lt;span style="color:#a6e22e"&gt;fibonacci&lt;/span&gt;(n));
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Here&amp;#39;s how the functions are called:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;fib(5)&lt;/code&gt; : &lt;code class="verbatim"&gt;fib_tail(5, 0, 1)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;fib_tail(5,0,1)&lt;/code&gt; : &lt;code class="verbatim"&gt;fib_tail(4,1,1)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;fib_tail(4,1,1)&lt;/code&gt; : &lt;code class="verbatim"&gt;fib_tail(3,1,2)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;fib_tail(3,1,2)&lt;/code&gt; : &lt;code class="verbatim"&gt;fib_tail(2,2,3)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;fib_tail(2,2,3)&lt;/code&gt; : &lt;code class="verbatim"&gt;fib_tail(1,3,5)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code class="verbatim"&gt;fib_tail(1,3,5)&lt;/code&gt; : &lt;code class="verbatim"&gt;5&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Or in a nice little picture:&lt;/p&gt;
&lt;img src="https://hossein.naghdbishi.com/images/fibonacci-optimized-stack.svg" alt="This figure shows how fibonacci&amp;#39;s 5th term is calculated using the tail recursive optimized code." title="/images/fibonacci-optimized-stack.svg" style="width:35%;"/&gt;
&lt;p&gt;
You may have noticed that this time is no different. It is less messy, sure, but each function still needs to wait for the result of another function so that it can finally return the value and remove its stack. The nice thing about this function, though, is that it &lt;em&gt;can&lt;/em&gt; be optimized. At each turn we&amp;#39;re accumulating the values we need inside two variables, and even if we only keep track of the last function call, we will still have the correct result.&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Key idea:&lt;/strong&gt; You can refractor your recursive functions, such that the recursive function call is being done at the end of the function, and the ultimate result that you will return isn&amp;#39;t dependent on anything but the data in the very last function call. Then some languages like C will discard your stack at each new call and will just keep the last function call at each step. This will result in less memory usage and prevention of stack overflow. This is the essence of tail recursion optimization.&lt;/p&gt;
&lt;p&gt;
Let&amp;#39;s see if the optimization makes a difference:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;❯ ./fib_tail_unoptimized
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Enter a number: 500000
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[1] 42515 segmentation fault (core dumped) ./fib_tail_unoptimized
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;❯ gcc -O1 fib_tail.c -o fib_tail_optimized
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;❯ ./fib_tail_optimized
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Enter a number: 500000
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Fibonacci&amp;#39;s 500000 term is -1975568635&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;It did! The answer is incorrect because &lt;code class="verbatim"&gt;int&lt;/code&gt; can&amp;#39;t such a large number, but we didn&amp;#39;t run into a stack overflow segfault either!&lt;/p&gt;
&lt;p&gt;
Not all programming languages support tail recursion optimization though. And you need to know that discarding the stack will make it harder to debug your code because the stack won&amp;#39;t be in its complete form.&lt;/p&gt;
&lt;p&gt;
The moral of the story is that whenever you feel like you&amp;#39;re at risk of stack overflow, go ahead and rewrite your code using an accumulator so that it can be tail optimized, or just use a loop. In the case of relying on an accumulator the compiler should support TCO optimizaiton. If it doesn&amp;#39;t no worries. You could use GOTO (it doesn&amp;#39;t call new functions then, and it&amp;#39;ll reuse the same variables so it&amp;#39;s nice) to use the same stack frame, or write a loop.&lt;/p&gt;
&lt;p&gt;
For example, here&amp;#39;s the Fibonacci function written using a loop, nice and easy:&lt;/p&gt;
&lt;div class="src src-c"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt;&lt;span style="color:#75715e"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;long&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;fib&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; term){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(term &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(term &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;2&lt;/span&gt; &lt;span style="color:#f92672"&gt;||&lt;/span&gt; term &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;3&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;long&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; a&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, b&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, c&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;i&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;term&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;2&lt;/span&gt;;i&lt;span style="color:#f92672"&gt;++&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; c &lt;span style="color:#f92672"&gt;=&lt;/span&gt; a&lt;span style="color:#f92672"&gt;+&lt;/span&gt;b;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; a &lt;span style="color:#f92672"&gt;=&lt;/span&gt; b;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; b &lt;span style="color:#f92672"&gt;=&lt;/span&gt; c;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;%d: %ld&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;\n&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;, i&lt;span style="color:#f92672"&gt;+&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;3&lt;/span&gt;, c);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; c;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;main&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fib&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;11&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-1" class="outline-3"&gt;
&lt;h3 id="headline-1"&gt;
Further Reading
&lt;/h3&gt;
&lt;div id="outline-text-headline-1" class="outline-text-3"&gt;
&lt;ul&gt;
&lt;li&gt;Tail Call Optimization in &lt;a href="https://inventwithpython.com/recursion/chapter8.html"&gt;The Recursive Book of Recursion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>How to share Mullvad VPN from Android on all your network?</title><link>https://hossein.naghdbishi.com/posts/en/mullvad-termux-android-vpn-share/</link><pubDate>Sat, 02 Aug 2025 20:53:23 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/mullvad-termux-android-vpn-share/</guid><description>&lt;p&gt;Suppose you want to share your &lt;a href="https://mullvad.net"&gt;Mullvad VPN&lt;/a&gt; over the network, you&amp;rsquo;re connected to it on your Android phone but not on any other device. How cool would it be if you could share it with other devices? Well you can do that.&lt;/p&gt;
&lt;p&gt;Once you connect to Mullvad VPN on your device, you&amp;rsquo;ll have access to a socks5 proxy on &lt;code&gt;socks5://10.64.0.1:1080&lt;/code&gt;, however this IP is only accessible from your Android phone. To share it with your other devices, first install &lt;a href="https://termux.dev/"&gt;Termux&lt;/a&gt;, then download the correct &lt;a href="https://gost.run/en/"&gt;gost&lt;/a&gt; binary according to your phone&amp;rsquo;s CPU architecture. Then you can instruct gost to forward all the packets it gets on port 8090 for example, to the socks5 provided by Mullvad. Done!&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;wget https://github.com/go-gost/gost/releases/download/v3.1.0/gost_3.1.0_linux_arm64.tar.gz &lt;span style="color:#75715e"&gt;# change the version and architecture accordingly&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;tar xfv *.tar.gz
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;./gost -L :8090 -F socks5://10.64.0.1:1080 &lt;span style="color:#75715e"&gt;# If you&amp;#39;re in a hostile environment, use -L socks5://user:password@:8090 instead to have a socks5 proxy with authentication.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now if inside your Mullvad app settings, &amp;ldquo;Local network sharing&amp;rdquo; is turned on, then you can easily connect to your phone&amp;rsquo;s IP from other devices and use the socks5/http/https proxy provided on its port 8090.
You might think that since &lt;a href="https://mullvad.net/en/help/socks5-proxy"&gt;all servers in Mullvad offer socks5 proxy&lt;/a&gt;, you can access them all, but sadly this is not possbile because folks at Mullvad have decided that as soon as you turn on local network sharing, your connection to other servers should be lost.&lt;/p&gt;
&lt;p&gt;You can use this socks5 proxy using &lt;a href="https://addons.mozilla.org/en-US/android/addon/foxyproxy-standard/"&gt;FoxyProxy&lt;/a&gt; on Firefox on both Android (yes! you can install extensions on Firefox Android) and GNU\Linux/Windows/MacOS.&lt;/p&gt;</description></item><item><title>You Can Resolve Domains via curl Using DoH</title><link>https://hossein.naghdbishi.com/posts/en/doh-and-odoh-using-curl/</link><pubDate>Tue, 29 Jul 2025 18:37:04 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/doh-and-odoh-using-curl/</guid><description>&lt;p&gt;
Did you know you can use curl to get DNS information? Here&amp;#39;s how:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ curl -X GET &amp;#34;https://cloudflare-dns.com/dns-query?name=example.com&amp;amp;type=A&amp;#34; -H &amp;#34;Accept: application/dns-json&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&amp;#34;Status&amp;#34;:0,&amp;#34;TC&amp;#34;:false,&amp;#34;RD&amp;#34;:true,&amp;#34;RA&amp;#34;:true,&amp;#34;AD&amp;#34;:true,&amp;#34;CD&amp;#34;:false,&amp;#34;Question&amp;#34;:[{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1}],&amp;#34;Answer&amp;#34;:[{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:276,&amp;#34;data&amp;#34;:&amp;#34;23.192.228.84&amp;#34;},{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:276,&amp;#34;data&amp;#34;:&amp;#34;23.215.0.136&amp;#34;},{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:276,&amp;#34;data&amp;#34;:&amp;#34;23.192.228.80&amp;#34;},{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:276,&amp;#34;data&amp;#34;:&amp;#34;23.215.0.138&amp;#34;},{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:276,&amp;#34;data&amp;#34;:&amp;#34;96.7.128.175&amp;#34;},{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:276,&amp;#34;data&amp;#34;:&amp;#34;96.7.128.198&amp;#34;}]}%&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
It works on Cloudflare&amp;#39;s &lt;a href="https://research.cloudflare.com/projects/network-privacy/odns/"&gt;ODOH&lt;/a&gt; server too!&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ curl -X GET &amp;#34;https://odoh.cloudflare-dns.com/dns-query?name=example.com&amp;amp;type=A&amp;#34; -H &amp;#34;Accept: application/dns-json&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&amp;#34;Status&amp;#34;:0,&amp;#34;TC&amp;#34;:false,&amp;#34;RD&amp;#34;:true,&amp;#34;RA&amp;#34;:true,&amp;#34;AD&amp;#34;:false,&amp;#34;CD&amp;#34;:false,&amp;#34;Question&amp;#34;:[{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1}],&amp;#34;Answer&amp;#34;:[{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:35,&amp;#34;data&amp;#34;:&amp;#34;23.192.228.84&amp;#34;},{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:35,&amp;#34;data&amp;#34;:&amp;#34;23.215.0.138&amp;#34;},{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:35,&amp;#34;data&amp;#34;:&amp;#34;23.215.0.136&amp;#34;},{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:35,&amp;#34;data&amp;#34;:&amp;#34;96.7.128.198&amp;#34;},{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:35,&amp;#34;data&amp;#34;:&amp;#34;96.7.128.175&amp;#34;},{&amp;#34;name&amp;#34;:&amp;#34;example.com&amp;#34;,&amp;#34;type&amp;#34;:1,&amp;#34;TTL&amp;#34;:35,&amp;#34;data&amp;#34;:&amp;#34;23.192.228.80&amp;#34;}]}%&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
If you can&amp;#39;t bootstrap a DoH server, you can always use &lt;a href="https://9.9.9.9/dns-query"&gt;https://9.9.9.9/dns-query&lt;/a&gt; instead (though not in this format) or &lt;a href="https://1.1.1.1/dns-query."&gt;https://1.1.1.1/dns-query.&lt;/a&gt; If you already know the IP of your target, you can use curl&amp;#39;s `–resolve` option too. curl has a `–doh-url` as well which can be used if you want curl to use DoH itself.&lt;/p&gt;
&lt;p&gt;
Note that although this method works for Cloudlare DoH servers, it is not part of any RFC so you can&amp;#39;t really rely on them and should refer to the &lt;a href="https://datatracker.ietf.org/doc/html/rfc8484"&gt;original RFC&lt;/a&gt; instead if you want to send your request to other servers.&lt;/p&gt;</description></item><item><title>So You Want to Learn Quicksort: Step-by-Step Guide to Basic, Lomuto &amp; Hoare Partitioning.</title><link>https://hossein.naghdbishi.com/posts/en/so-you-want-to-learn-quicksort/</link><pubDate>Tue, 29 Jul 2025 11:54:15 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/so-you-want-to-learn-quicksort/</guid><description>&lt;p&gt;I don&amp;rsquo;t have a Bachelor&amp;rsquo;s degree in computer science so I haven&amp;rsquo;t had any formal algorithm or data structure training. However, I am trying to remedy that by reading books and solving algorithmic questions on online platforms. One question in Hackerrank needed a sort implementation, which I couldn&amp;rsquo;t do without the help of AI, so I decided to learn about sort algorithms. I searched and found that quicksort has widespread usage, so I decided to learn it. However it was just too hard for me to grasp what was going on, so like a good programmer I decided to divide the problem into smaller parts and solve those :P.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The quicksort algorithm asks: what if we had an algorithm that takes a number (called the pivot) and divides our array into two parts, the ones smaller than the pivot and the ones larger than it? If an algorithm does this, we now have two arrays of numbers that we know don&amp;rsquo;t need to be compared. We could do the very same to those two arrays, then we keep doing this with the subarrays that are getting smaller and smaller in size, et voila! The resulting array is sorted. This is the essence of the quicksort algorithm.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The algorithm that partitions our arrays into subarrays that are all bigger or smaller than a certain number is called a partitioning algorithm. Most educational materials on quicksort conflate Lomuto&amp;rsquo;s and Hoare&amp;rsquo;s partitioning algorithms with the quicksort algorithm itself, so we could easily lose sight of the big picture. I have written a C quicksort program that is inefficient, yet easy to understand.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s learn about the silliest partitioning algorithm possible; then we can easily learn quicksort, even when it uses the right partitioning algorithms.&lt;/p&gt;
&lt;h2 id="basic-partitioning"&gt;Basic Partitioning&lt;/h2&gt;
&lt;p&gt;In the following code, we&amp;rsquo;ll partition an array of numbers into those bigger than the pivot and those smaller than the pivot in the silliest way: we go through all the numbers in the array one by one and put each number in one of the three temporary arrays we make — one temporary array for numbers bigger than the pivot, one for those smaller than the pivot, and one for those equal to it.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-C" data-lang="C"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt;&lt;span style="color:#75715e"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt;&lt;span style="color:#75715e"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#define MAX_INTS 100
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Counts from zero
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;printArray&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[], &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; place){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;i&lt;span style="color:#f92672"&gt;&amp;lt;=&lt;/span&gt;place;i&lt;span style="color:#f92672"&gt;++&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;%d,&amp;#34;&lt;/span&gt;,arr[i]);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;puts&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Counts from zero
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Partitions everything around the last array element (_high_ is the pivot) for elements **low** to high (not zero to high), returns the last array&amp;#39;s (pivot&amp;#39;s) new placement in the partitioned array.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;basicPartition&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[], &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; low, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; high){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;//nE/L/H == number of equals/lows/highs
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;//idx is defined at a function scope here and used later to place the elements in their correct position
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; idx&lt;span style="color:#f92672"&gt;=&lt;/span&gt;low,nE&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;,nL&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;,nH&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; pivot&lt;span style="color:#f92672"&gt;=&lt;/span&gt;arr[high];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; Ls[MAX_INTS]&lt;span style="color:#f92672"&gt;=&lt;/span&gt;{},Hs[MAX_INTS]&lt;span style="color:#f92672"&gt;=&lt;/span&gt;{},Ps[MAX_INTS]&lt;span style="color:#f92672"&gt;=&lt;/span&gt;{};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Element enumeration
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Here we place the numbers into temporary high, low, or equal arrays. 
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i&lt;span style="color:#f92672"&gt;=&lt;/span&gt;low;i&lt;span style="color:#f92672"&gt;&amp;lt;=&lt;/span&gt;high;i&lt;span style="color:#f92672"&gt;++&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(arr[i]&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt;pivot){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Hs[nH]&lt;span style="color:#f92672"&gt;=&lt;/span&gt;arr[i];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; nH&lt;span style="color:#f92672"&gt;++&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; } &lt;span style="color:#66d9ef"&gt;else&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (arr[i]&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;pivot){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Ls[nL]&lt;span style="color:#f92672"&gt;=&lt;/span&gt;arr[i];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; nL&lt;span style="color:#f92672"&gt;++&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; } &lt;span style="color:#66d9ef"&gt;else&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (arr[i] &lt;span style="color:#f92672"&gt;==&lt;/span&gt; pivot){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Ps[nE]&lt;span style="color:#f92672"&gt;=&lt;/span&gt;arr[i];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; nE&lt;span style="color:#f92672"&gt;++&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Place from temp to arr
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;; i&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;nL;i&lt;span style="color:#f92672"&gt;++&lt;/span&gt;,idx&lt;span style="color:#f92672"&gt;++&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; arr[idx]&lt;span style="color:#f92672"&gt;=&lt;/span&gt;Ls[i];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;; i&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;nE;i&lt;span style="color:#f92672"&gt;++&lt;/span&gt;,idx&lt;span style="color:#f92672"&gt;++&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; arr[idx]&lt;span style="color:#f92672"&gt;=&lt;/span&gt;Ps[i];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;; i&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;nH;i&lt;span style="color:#f92672"&gt;++&lt;/span&gt;,idx&lt;span style="color:#f92672"&gt;++&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; arr[idx]&lt;span style="color:#f92672"&gt;=&lt;/span&gt;Hs[i];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// return the new position of the pivot 
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; low&lt;span style="color:#f92672"&gt;+&lt;/span&gt;nL&lt;span style="color:#f92672"&gt;+&lt;/span&gt;nE&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Counts from zero
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[], &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; low, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; high){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(low&lt;span style="color:#f92672"&gt;&amp;gt;=&lt;/span&gt;high){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; piv&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;basicPartition&lt;/span&gt;(arr, low, high);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(arr, low, piv&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(arr, piv&lt;span style="color:#f92672"&gt;+&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, high);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;main&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {&lt;span style="color:#ae81ff"&gt;12&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;899&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;93&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;789&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;1000&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;95&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;4&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;56&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;11&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;18&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;12&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;180&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;43&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;93&lt;/span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;sizeof&lt;/span&gt;(arr)&lt;span style="color:#f92672"&gt;/&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;sizeof&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;/* printArray(arr,n-1); */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;/* int res = basicPartition(arr, 0, n-1); */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; res &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(arr, &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;, n&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printArray&lt;/span&gt;(arr,n&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; EXIT_SUCCESS;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If I were to draw a picture it would look like this:
&lt;img src="https://hossein.naghdbishi.com/images/quicksort-basic_partitioning.svg" alt="img"&gt;&lt;/p&gt;
&lt;p&gt;The problem with this implementation is that it uses O(n) in its partitioning. How can we improve that? Maybe instead of keeping everything in a temporary array of equal size, we could do in-place replacement? That&amp;rsquo;s what the Lomuto&amp;rsquo;s partitioning algorithm does. Let&amp;rsquo;s learn that.&lt;/p&gt;
&lt;h2 id="lomutos-partitioning-algorithm"&gt;Lomuto&amp;rsquo;s Partitioning Algorithm&lt;/h2&gt;
&lt;p&gt;So far we made a simple partitioning algorithm, but it left much to be desired in its memory usage. If we want to use less memory, how about we swap the elements instead of keeping them in temporary arrays? That&amp;rsquo;s what Lomuto&amp;rsquo;s algorithm does.
Imagine two little soldiers that we hire for sorting. The first one is named &lt;em&gt;i&lt;/em&gt;, and the second one is named &lt;em&gt;j&lt;/em&gt;.
Our little guy &lt;em&gt;i&lt;/em&gt;&amp;rsquo;s job is to protect the weak. He creates a firm boundary and all the elements smaller than the pivot line up before him.
Our adventurous fella &lt;em&gt;j&lt;/em&gt;&amp;rsquo;s job is to foray into the unknown, find those weaker than or equal to the pivot, and tell them they can find refuge if they line up before &lt;em&gt;i&lt;/em&gt;.
So here is how the algorithm works:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the last element of the array is chosen as our pivot&lt;/li&gt;
&lt;li&gt;&lt;em&gt;i&lt;/em&gt; stands at &lt;code&gt;low-1&lt;/code&gt;, behind where the array even begins.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;j&lt;/em&gt; stands at &lt;code&gt;low&lt;/code&gt;, where the array begins.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;j&lt;/em&gt; marches forward, on its way forward, it finds an element that is lower than or equal to the pivot, once it does, it stops to let &lt;em&gt;i&lt;/em&gt; know.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;j&lt;/em&gt; and &lt;em&gt;i&lt;/em&gt; cooperate, i moves one step forward to mark the new boundary, the element at the new boundary is swapped with the weak/equal element.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;j&lt;/em&gt; keeps doing this and marching on until it reaches &lt;code&gt;arr[high]&lt;/code&gt; which is our pivot.&lt;/li&gt;
&lt;li&gt;When &lt;em&gt;j&lt;/em&gt; is at one step behind &lt;code&gt;arr[high]&lt;/code&gt;, we already know that &lt;code&gt;arr[high]&lt;/code&gt; is going to be equal to the pivot since the last element is the pivot itself, so we swap &lt;code&gt;arr[i+1]&lt;/code&gt; with &lt;code&gt;arr[high]&lt;/code&gt; manually without doing a condition check.&lt;/li&gt;
&lt;li&gt;At the end of the process, we return &lt;em&gt;i&lt;/em&gt;+1, which is where our boundary (i.e. our pivot&amp;rsquo;s position in array) is after this one final swap.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Intuition: In Lomuto&amp;rsquo;s algorithm we use &lt;em&gt;i&lt;/em&gt; as a boundary, and everything before &lt;em&gt;i&lt;/em&gt; is smaller than or equal to the pivot. So at each step if &lt;code&gt;arr[j]&lt;/code&gt; is smaller than the pivot we increase our boundary and move the smaller numbers inside our boundary. Now, since everything before our pivot is smaller than or equal to it and everything after that is bigger than it, our pivot is in fact in its sorted position by the definition of sort.&lt;/p&gt;
&lt;p&gt;How does this relate to quicksort? Using Lomuto&amp;rsquo;s algorithm, you have an algorithm that efficiently partitions an array, such that one number of your array (the pivot) is already in its sorted position, and you have two separate arrays before and after the pivot, in a way that they don&amp;rsquo;t need to be compared with each other since we already know some of them are always bigger than the others. Isn&amp;rsquo;t that great? If we now ignore the pivot which is now in its sorted position and keep partitioning for arrays that come before and after it, we&amp;rsquo;ll have a fully sorted array.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s my implementation of quicksort using Lomuto&amp;rsquo;s partitioning algorithm.
Try not to repeat the mistakes I made during writing this code:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;I used &lt;code&gt;i=-1&lt;/code&gt; instead of &lt;code&gt;i=low-1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;I swapped &lt;code&gt;arr[high]&lt;/code&gt; and &lt;code&gt;arr[i]&lt;/code&gt; instead of &lt;code&gt;arr[high]&lt;/code&gt; and &lt;code&gt;arr[i+1]&lt;/code&gt; at the last step. The boundary is at &lt;em&gt;i&lt;/em&gt;+1, since we&amp;rsquo;re not doing that &lt;code&gt;i++&lt;/code&gt; which is inside our loop. Everything up to &lt;code&gt;i+1&lt;/code&gt; is lower than or equal to the pivot which is always at &lt;code&gt;arr[i+1]&lt;/code&gt; at the end of the algorithm.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-C" data-lang="C"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt;&lt;span style="color:#75715e"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt;&lt;span style="color:#75715e"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#define MAX_INTS 100
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Counts from zero
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;printArray&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[], &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; place){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;i&lt;span style="color:#f92672"&gt;&amp;lt;=&lt;/span&gt;place;i&lt;span style="color:#f92672"&gt;++&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;%d,&amp;#34;&lt;/span&gt;,arr[i]);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;puts&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;arrSwapInt&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[], &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; a, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; b){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; temp;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; temp &lt;span style="color:#f92672"&gt;=&lt;/span&gt; arr[b];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; arr[b] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; arr[a];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; arr[a] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; temp;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Counts from zero
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;lomutoPartition&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[], &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; low, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; high){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i&lt;span style="color:#f92672"&gt;=&lt;/span&gt;low&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; piv&lt;span style="color:#f92672"&gt;=&lt;/span&gt;arr[high];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; j&lt;span style="color:#f92672"&gt;=&lt;/span&gt;low;j&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;high;j&lt;span style="color:#f92672"&gt;++&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(arr[j]&lt;span style="color:#f92672"&gt;&amp;lt;=&lt;/span&gt;piv){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; i&lt;span style="color:#f92672"&gt;++&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;arrSwapInt&lt;/span&gt;(arr,i,j);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;arrSwapInt&lt;/span&gt;(arr,high,i&lt;span style="color:#f92672"&gt;+&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; i&lt;span style="color:#f92672"&gt;+&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Counts from zero
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[], &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; low, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; high){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(low&lt;span style="color:#f92672"&gt;&amp;gt;=&lt;/span&gt;high){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; piv&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;lomutoPartition&lt;/span&gt;(arr, low, high);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(arr, low, piv&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(arr, piv&lt;span style="color:#f92672"&gt;+&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, high);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;main&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {&lt;span style="color:#ae81ff"&gt;12&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;899&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;93&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;789&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;1000&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;95&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;4&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;56&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;11&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;18&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;12&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;180&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;43&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;93&lt;/span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;sizeof&lt;/span&gt;(arr)&lt;span style="color:#f92672"&gt;/&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;sizeof&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; res &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(arr, &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;, n&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printArray&lt;/span&gt;(arr,n&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; EXIT_SUCCESS;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="hoares-partitioning-algorithm"&gt;Hoare&amp;rsquo;s Partitioning Algorithm&lt;/h2&gt;
&lt;p&gt;Now let&amp;rsquo;s learn Hoare&amp;rsquo;s partitioning algorithm.&lt;/p&gt;
&lt;p&gt;In Lomuto&amp;rsquo;s our soldiers &lt;em&gt;i&lt;/em&gt; and &lt;em&gt;j&lt;/em&gt; had different jobs, now they have the same job description. The only difference between &lt;em&gt;i&lt;/em&gt; and &lt;em&gt;j&lt;/em&gt; is that &lt;em&gt;i&lt;/em&gt; starts one step behind the beginning of the array and &lt;em&gt;j&lt;/em&gt; starts from one step after the end of the array. They take rounds to march on and stop where they find elements that shouldn&amp;rsquo;t be where they are. That is, first &lt;em&gt;i&lt;/em&gt; marches towards the end of the array and stops if it sees any element that is bigger than the pivot, and then &lt;em&gt;j&lt;/em&gt; marches towards the beginning of the array and stops if it sees any element that is smaller than the pivot. Once they&amp;rsquo;re both stopped, we can very astutely move two elements to their correct place with one swap and that&amp;rsquo;s what we do. Our soldiers &lt;em&gt;i&lt;/em&gt; and &lt;em&gt;j&lt;/em&gt; both keep moving until they face each other (&lt;code&gt;i==j&lt;/code&gt;) or get past one another (&lt;code&gt;i&amp;gt;j&lt;/code&gt;). That is when they know they&amp;rsquo;re done.&lt;/p&gt;
&lt;p&gt;In short, in Hoare&amp;rsquo;s algorithm, you move from both sides to the other, stop at the wrong elements from each side, and swap them.
Note that just like the two algorithms above, at the end of the Hoare&amp;rsquo;s algorithm it is guaranteed that all items left of the returned &lt;em&gt;j&lt;/em&gt; are smaller than the pivot, and all items right of the retuned &lt;em&gt;j&lt;/em&gt; are bigger than the pivot. But unlike those two, the pivot itself doesn&amp;rsquo;t end up at the returned &lt;em&gt;j&lt;/em&gt; and it won&amp;rsquo;t be at its sorted position. That is, while Hoare&amp;rsquo;s partitioning algorithm returns the boundary of our partitioned arrays (&lt;em&gt;j&lt;/em&gt;), our pivot is not at &lt;em&gt;j&lt;/em&gt; (even though we can move it with one extra swap since we already know the boundary, we don&amp;rsquo;t do this since at the end of the recursion each element will be in its sorted position and this is unnecessary). So this time in the quicksort algorithm, we don&amp;rsquo;t do quicksort on &lt;code&gt;(low,piv-1)&lt;/code&gt; and &lt;code&gt;(piv+1,high)&lt;/code&gt; which ignores the element at piv, instead we do &lt;code&gt;(low,piv)&lt;/code&gt; &lt;code&gt;(piv+1,high)&lt;/code&gt; which includes the element at piv.&lt;/p&gt;
&lt;p&gt;Try not to repeat the mistakes I made while writing this code:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;I forgot to put the = in the loop break condition, so my code would get stuck in a loop.&lt;/li&gt;
&lt;li&gt;I forgot to make the pivot equal to &lt;code&gt;arr[low]&lt;/code&gt; and made it equal to &lt;code&gt;arr[0]&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-C" data-lang="C"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt;&lt;span style="color:#75715e"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt;&lt;span style="color:#75715e"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#define MAX_INTS 100
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Counts from zero
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;printArray&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[], &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; place){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;i&lt;span style="color:#f92672"&gt;&amp;lt;=&lt;/span&gt;place;i&lt;span style="color:#f92672"&gt;++&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;%d,&amp;#34;&lt;/span&gt;,arr[i]);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;puts&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;arrSwapInt&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[], &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; a, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; b){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; temp;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; temp &lt;span style="color:#f92672"&gt;=&lt;/span&gt; arr[b];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; arr[b] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; arr[a];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; arr[a] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; temp;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Counts from zero
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Returns piv_index instead of where the pivot is
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// pivot is not in its sorted position
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;hoarePartition&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[], &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; low, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; high){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// low, not zero!
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; piv&lt;span style="color:#f92672"&gt;=&lt;/span&gt;arr[low];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i&lt;span style="color:#f92672"&gt;=&lt;/span&gt;low&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;,j&lt;span style="color:#f92672"&gt;=&lt;/span&gt;high&lt;span style="color:#f92672"&gt;+&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;while&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;do&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; i&lt;span style="color:#f92672"&gt;++&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; } &lt;span style="color:#66d9ef"&gt;while&lt;/span&gt; (arr[i]&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;piv);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;do&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; j&lt;span style="color:#f92672"&gt;--&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; } &lt;span style="color:#66d9ef"&gt;while&lt;/span&gt; (arr[j]&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt;piv);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// if you don&amp;#39;t put the equal, for 11,4,18,13 it will get stuck in a loop since the j that is returned will be 0 instead of 1.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// since the pivot index is the same as the one that was called, it will get stuck in a loop, unless you break when both i and j are equal
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// breaking on j==i makes sense, since if i==j then we are already done comparing boundaries and at the correct pivot index.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(i&lt;span style="color:#f92672"&gt;&amp;gt;=&lt;/span&gt;j){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;break&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;arrSwapInt&lt;/span&gt;(arr, i, j);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; j;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Counts from zero
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[], &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; low, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; high){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(low&lt;span style="color:#f92672"&gt;&amp;gt;=&lt;/span&gt;high){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; piv_index&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;hoarePartition&lt;/span&gt;(arr, low, high);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Different from Lomuto&amp;#39;s and basic partitioning
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(arr, low, piv_index);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(arr, piv_index&lt;span style="color:#f92672"&gt;+&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, high);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;main&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;){
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; arr[] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {&lt;span style="color:#ae81ff"&gt;12&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;899&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;93&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;789&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;1000&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;95&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;4&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;56&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;11&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;18&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;12&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;180&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;43&lt;/span&gt;,&lt;span style="color:#ae81ff"&gt;93&lt;/span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;/* int arr[] = {11,4,18,13}; */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;sizeof&lt;/span&gt;(arr)&lt;span style="color:#f92672"&gt;/&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;sizeof&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;/* int res = hoarePartition(arr, 0, n-1); */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; res &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;quickSort&lt;/span&gt;(arr, &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;, n&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;puts&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Sorted array:&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printArray&lt;/span&gt;(arr,n&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; EXIT_SUCCESS;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h1 id="extra-tips"&gt;Extra Tips&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Pivots:&lt;/strong&gt; In Lomuto&amp;rsquo;s algorithm, the last element (&lt;code&gt;arr[high]&lt;/code&gt;) must be our pivot, because during the quicksort call on &lt;code&gt;(low,piv-1)&lt;/code&gt; and &lt;code&gt;(piv+1,high)&lt;/code&gt; we assume that the pivot is in its sorted position (that is, at the end of the boundary, where we have done our last swap), so we must choose the last element as our pivot. But if we need another array element to be our pivot, we can always swap it with the last element before we&amp;rsquo;re calling the algorithms and make the chosen element the last element. But in Hoare&amp;rsquo;s all we need is an arbitrary number that, ideally, is larger that half of the numbers and smaller than the other half, but we can choose any number really.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Best case and worse case:&lt;/strong&gt; Running the quicksort algorithm on an already sorted array will have a time complexity of O(n^2). So we usually choose a random element as our pivot (and if it is Lomuto&amp;rsquo;s, we need to swap it and make it the last element).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stability of the sort algorithms:&lt;/strong&gt; The basic partitioning algorithm is stable. But neither Lomuto&amp;rsquo;s nor Hoare&amp;rsquo;s algorithms are stable, meaning that for equal items (to which we have attached extra data) the order could be changed. That is &lt;code&gt;{(1,red),(2,blue),(1,blue)}&lt;/code&gt; could become &lt;code&gt;{(1,blue), (1,red), (2,blue)}&lt;/code&gt;. This would be really bad if we were sorting data in a database with timestamps for example.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id="conclusion"&gt;Conclusion&lt;/h1&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Partition Method&lt;/th&gt;
 &lt;th&gt;Pivot Choice&lt;/th&gt;
 &lt;th&gt;Extra Memory&lt;/th&gt;
 &lt;th&gt;Pivot Position After Partition&lt;/th&gt;
 &lt;th&gt;Stability&lt;/th&gt;
 &lt;th&gt;Average Time&lt;/th&gt;
 &lt;th&gt;Worst-Case Time&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Basic (temp arrays)&lt;/td&gt;
 &lt;td&gt;Doesn&amp;rsquo;t matter&lt;/td&gt;
 &lt;td&gt;O(n)&lt;/td&gt;
 &lt;td&gt;Pivot(s) end up in the middle of the concatenated lows-equals-highs block&lt;/td&gt;
 &lt;td&gt;Yes&lt;/td&gt;
 &lt;td&gt;O(n log n)&lt;/td&gt;
 &lt;td&gt;O(n²)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Lomuto&lt;/td&gt;
 &lt;td&gt;Must be the last element&lt;/td&gt;
 &lt;td&gt;O(1)&lt;/td&gt;
 &lt;td&gt;Pivot placed at its final sorted index (i+1)&lt;/td&gt;
 &lt;td&gt;No&lt;/td&gt;
 &lt;td&gt;O(n log n)&lt;/td&gt;
 &lt;td&gt;O(n²)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Hoare&lt;/td&gt;
 &lt;td&gt;Doesn&amp;rsquo;t matter but usually the first element&lt;/td&gt;
 &lt;td&gt;O(1)&lt;/td&gt;
 &lt;td&gt;Pivot not guaranteed in final position; partitions around boundary j&lt;/td&gt;
 &lt;td&gt;No&lt;/td&gt;
 &lt;td&gt;O(n log n)&lt;/td&gt;
 &lt;td&gt;O(n²)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Did you see what we did for sorting? There is much to be learned from what we did here. We used &lt;strong&gt;Divide and Conquer&lt;/strong&gt; to solve our problem. At each section we only had an algorithm that could do a single sort (a partitioning algorithm can put at most one element in its sorted position) but we used this algorithm - which can&amp;rsquo;t be seen as a sorting algorithm - 1. multiple times and 2. on smaller and smaller subsets of our problem, so at the end sorting happened efficiently. Any time you&amp;rsquo;re thinking about a problem that can&amp;rsquo;t be solved efficiently, maybe you could think about single items and then use D&amp;amp;C to solve the problem.&lt;/p&gt;
&lt;h1 id="todo"&gt;TODO&lt;/h1&gt;
&lt;p&gt;I asked AI what I should learn next and here are its opinions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; You never mention the importance of pivot choice on expected running time or practical performance (cache behavior, branch prediction, etc.). Real-world quicksort typically uses “median-of-three” or introspective quicksort (Introsort) that switches to heapsort if recursion gets too deep.&lt;/li&gt;
&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; Neither Lomuto’s nor Hoare’s implementations do tail recursion elimination. In the worst case (unbalanced splits), your recursion depth is O(n) and you risk a stack overflow. Best practice is to recurse on the smaller subarray first, then iterate on the larger one.&lt;/li&gt;
&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; Discuss three-way partitioning for many-duplicate inputs.
And I think I should:&lt;/li&gt;
&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; Add pictures of our valiant soldiers &lt;em&gt;i&lt;/em&gt; and &lt;em&gt;j&lt;/em&gt; performing their duties in Lomuoto&amp;rsquo;s and Hoare&amp;rsquo;s algorithms.&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Dining Cryptographers</title><link>https://hossein.naghdbishi.com/posts/en/dining-cryptographers/</link><pubDate>Sat, 26 Jul 2025 01:02:24 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/dining-cryptographers/</guid><description>&lt;p&gt;
Today I learned about the &lt;a href="https://en.wikipedia.org/wiki/Dining_cryptographers_problem"&gt;Dining cryptographers problem&lt;/a&gt;. It is such a great usage of XOR!&lt;/p&gt;
&lt;p&gt;
Suppose we have three cryptographers-a,b and c- seated in a circle. They all flip a coin and see the coin of the person sitting to the right of them. The goal: anonymously signal “I paid” (bit 1) or “Nobody paid” (bit 0) by publicly announcing XORs of coin flips, without revealing who paid. One of them can relay a message by saying the opposite of what the protocol usually expects. If a b and c gather round a table here&amp;#39;s how everyone&amp;#39;s positioned:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; a
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;b c&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
for a: a XOR c&lt;/p&gt;
&lt;p&gt;
for b: b XOR a&lt;/p&gt;
&lt;p&gt;
for c: c XOR b&lt;/p&gt;
&lt;p&gt;
Now you can easily see that if a, b and c toss a coin, and always say truth about the XOR of their coin&amp;#39;s state with the state of their neighbour&amp;#39;s coins, the result is always 0 because each element cancel itself out. That is: &lt;code class="verbatim"&gt;(a XOR c) XOR (b XOR a) XOR (c XOR b)&lt;/code&gt; is always zero. However if any of them want to say that a state is true (that at least one of them has paid for the dinner instead of the NSA for example) they can do so by opting to announce the NOT of their XORs instead. That way the answer would be 1, a message has been collaborated to the whole group, however the sender of the message is private. But the problem is that there can be only one sender, and one bit at each round to be sent.&lt;/p&gt;</description></item><item><title>How to Convert Telegram Stickers to Signal</title><link>https://hossein.naghdbishi.com/posts/en/telegram-sticker-to-signal-conversion/</link><pubDate>Mon, 26 May 2025 23:29:20 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/telegram-sticker-to-signal-conversion/</guid><description>&lt;p&gt;As Telegram is one of the most used messengers in Iran, it has a large pool of stickers. I don&amp;rsquo;t want my contacts to miss out on them, so I always try to bring good stickers to Signal. Luckily there is an awesome free (libre) app named &lt;a href="https://github.com/laggykiller/sticker-convert"&gt;sticker-convert&lt;/a&gt; that takes care of this.&lt;/p&gt;
&lt;p&gt;I use its command line version like this:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;./sticker-convert-x86_64.AppImage --download-telegram https://t.me/addstickers/GreatMindsColor --author 'None' --title 'None' --export-signal --signal-uuid UUID --signal-password 'PASSWORD' --telegram-userid USER_ID --telegram-token 'TELEGRAM_TOKEN'&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Now there is good documentation on how you can find and generate the tokens for both &lt;a href="https://github.com/laggykiller/sticker-convert/blob/master/docs/guide_signal.md"&gt;Signal&lt;/a&gt; and &lt;a href="https://github.com/laggykiller/sticker-convert/blob/master/docs/guide_telegram.md"&gt;Telegram&lt;/a&gt;. But for better security, I enter fake Signal credentials and provide only a bot token to the script. Obviously the script throws out an error, but the APNG output is there. So you can easily use Signal Desktop (File menu -&amp;gt; Create/upload sticker pack), import the APNG files into it, and upload them. The only downside is that the script takes care of emojis automatically, but you have to enter them manually if you use Signal Desktop for importing APNG files.&lt;/p&gt;
&lt;p&gt;After you&amp;rsquo;re done converting the stickers, please make sure you share them on &lt;a href="https://signalstickers.org"&gt;https://signalstickers.org&lt;/a&gt; as well with good tags, so everyone can easily find and use them.&lt;/p&gt;</description></item><item><title>The Secret Interplay Between OpenSnitch and nftables</title><link>https://hossein.naghdbishi.com/posts/en/how-does-opensnitch-work-under-the-hood/</link><pubDate>Wed, 16 Apr 2025 12:20:13 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/how-does-opensnitch-work-under-the-hood/</guid><description>&lt;p&gt;I use NixOS and I have enabled its &lt;code&gt;networking.nftables.enable = true;&lt;/code&gt; and &lt;code&gt;networking.firewall.enable = true;&lt;/code&gt;. This means I get to use &lt;a href="https://github.com/NixOS/nixpkgs/blob/1750f3c1c89488e2ffdd47cab9d05454dddfb734/nixos/modules/services/networking/nftables.nix"&gt;the default configuration of nftables on NixOS&lt;/a&gt;. Now, there are times when I wish to disable it. It is fairly easy to stop the firewall service: &lt;code&gt;systemctl stop nftables&lt;/code&gt; does it. This automatically flushes the nftables ruleset and gives me freedom to do whatever I want.&lt;/p&gt;
&lt;p&gt;Today, I wanted to run a small Debian VM, and I was in no mood to configure its networking, using QEMU&amp;rsquo;s SLiRP was the way. After creating the appropriate cloud-init files, here&amp;rsquo;s the command I ran:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;qemu-system-x86_64 &lt;span style="color:#ae81ff"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; -m &lt;span style="color:#ae81ff"&gt;2048&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; -smp &lt;span style="color:#ae81ff"&gt;2&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; -drive file&lt;span style="color:#f92672"&gt;=&lt;/span&gt;debian-12-genericcloud-amd64.qcow2,format&lt;span style="color:#f92672"&gt;=&lt;/span&gt;qcow2 &lt;span style="color:#ae81ff"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; -netdev user,id&lt;span style="color:#f92672"&gt;=&lt;/span&gt;net0,hostfwd&lt;span style="color:#f92672"&gt;=&lt;/span&gt;tcp::2222-:22 &lt;span style="color:#ae81ff"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; -device virtio-net,netdev&lt;span style="color:#f92672"&gt;=&lt;/span&gt;net0 &lt;span style="color:#ae81ff"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; -nographic -drive file&lt;span style="color:#f92672"&gt;=&lt;/span&gt;cloud-init.iso,media&lt;span style="color:#f92672"&gt;=&lt;/span&gt;cdrom
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But strangely, there was no Internet access inside the VM except for ICMP packets. Everything went fine when I flushed the nftables ruleset, but even after stopping nftables, no matter how many times I flushed the table via &lt;code&gt;nft flush ruleset&lt;/code&gt;, these two tables kept coming back in a matter of seconds:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code class="language-nft" data-lang="nft"&gt;table inet mangle {
 chain output {
 type route hook output priority mangle; policy accept;
 meta l4proto != tcp ct state related,new queue flags bypass to 0
 tcp flags &amp;amp; (fin | syn | rst | ack) == syn queue flags bypass to 0
 }
}
table inet filter {
 chain input {
 type filter hook input priority filter; policy accept;
 udp sport 53 queue flags bypass to 0
 }
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;What could it possibly be? At the first step, I learned about what queues are for in nftables. I had never seen them before. Queue statements in nftables are used to pass packets to userspace, just at that moment I knew which application it was. I use &lt;a href="https://github.com/evilsocket/opensnitch"&gt;OpenSnitch&lt;/a&gt; to monitor which applications make what requests. OpenSnitch automatically added all these rules, no matter how many times I flushed the ruleset. As a program that needs to process packets, it makes sense for OpenSnitch to check for its rules, but it seemed frightening at first, when I didn&amp;rsquo;t know what was going on.&lt;/p&gt;
&lt;p&gt;I was under the assumption that using eBPF, &lt;em&gt;all&lt;/em&gt; packets are checked under OpenSnitch, but apparently only TCP packets with SYN flag, and new and related packets of other type are sent to OpenSnitch for further processing. Could a person use this to their advantage? If they have root access and can send malformed packets to initiate connections, then I guess so.&lt;/p&gt;</description></item><item><title>Hosting a Hugo Site Using Codeberg Pages with One-Click Deployment</title><link>https://hossein.naghdbishi.com/posts/en/make-a-hugo-blog-on-codeberg/</link><pubDate>Sat, 12 Apr 2025 12:41:00 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/make-a-hugo-blog-on-codeberg/</guid><description>&lt;p&gt;How to host a Hugo site using Codeberg Pages with one-click deployment?&lt;/p&gt;
&lt;p&gt;Yesterday I made a few changes to my site, namely I decided to merge my Persian and English blog into one, and write more blog posts.&lt;/p&gt;
&lt;p&gt;To write more, it is necessary for the act of publishing a blog post to be easy. It would be easy on Github, which has its own CI, but I don&amp;rsquo;t want to host my blog there (they&amp;rsquo;re not a big help to libre software). Not using Github means I lose access to a nice CI. I could request one from Codeberg but seeing that it&amp;rsquo;d take their resources I decided against that as well.&lt;/p&gt;
&lt;h2 id="using-bash-for-automatic-deployment"&gt;Using Bash for Automatic Deployment&lt;/h2&gt;
&lt;p&gt;To automate a series of repetitive shell commands, the ever-present bash is the first option to consider. So that&amp;rsquo;s what I went with.&lt;/p&gt;
&lt;p&gt;My blog is on a single repo, but has two different branches. This meant that I had to go through some extra loops (&lt;code&gt;git switch&lt;/code&gt;, &lt;code&gt;git clean&lt;/code&gt;, &amp;hellip;) to publish my blog. Wouldn&amp;rsquo;t it be nice if I could have both my branches, my source directory and its build results in separate directories? A simple yet stupid approach is to clone the repo twice and use each branch in a different folder. However the best practice is to use git worktrees. And that&amp;rsquo;s what I went with.&lt;/p&gt;
&lt;p&gt;First we need to create one directory for each of the branches. You can learn this trick from &lt;a href="https://morgan.cugerone.com/blog/workarounds-to-git-worktree-using-bare-repository-and-cannot-fetch-remote-branches/"&gt;here&lt;/a&gt;. Note that you may have to also allow for relative paths (&lt;code&gt;git config status.relativePaths true&lt;/code&gt;) if you&amp;rsquo;re moving the base folder around, or use the same folder on multiple devices (for example by using &lt;a href="https://syncthing.net/"&gt;Syncthing&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;After being done with the process, you will have this directory structure:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;.
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;├── deploy.sh
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;├── main
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;└── src
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Make sure to go inside each worktree and do a &lt;code&gt;git pull&lt;/code&gt;. You may need to set up upstream branches as well.&lt;/p&gt;
&lt;p&gt;Then you can run the following deployment code for &lt;code&gt;deploy.sh&lt;/code&gt; when you&amp;rsquo;re in the parent directory:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;set -ux
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;BOLD&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;\e[1m&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;RED&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;\e[31m&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;RESET&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;\e[0m&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo -e &lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;&lt;span style="color:#e6db74"&gt;${&lt;/span&gt;BOLD&lt;span style="color:#e6db74"&gt;}${&lt;/span&gt;RED&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;&lt;span style="color:#e6db74"&gt;Make sure to push to src, as it does not happen automatically&lt;/span&gt;&lt;span style="color:#e6db74"&gt;${&lt;/span&gt;RESET&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Currently at &lt;/span&gt;$PWD&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cd src
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Removing old files&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;rm -rf ./public/*
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Building...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;hugo build
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cd ..
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cd main
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Removing all files in main&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;rm -rf *
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; &lt;span style="color:#f92672"&gt;[&lt;/span&gt; ! -f .domains &lt;span style="color:#f92672"&gt;]&lt;/span&gt;; &lt;span style="color:#66d9ef"&gt;then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;Error: File &amp;#39;.domains&amp;#39; does not exist.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; exit &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;fi&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Restoring main&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cp -r ../src/public/* .
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Git add&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git add .
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Git diff, do you verify?&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git diff HEAD
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;COMMIT_MESSAGE&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;&lt;span style="color:#e6db74"&gt;${&lt;/span&gt;1&lt;span style="color:#66d9ef"&gt;:-$(&lt;/span&gt;git -C ../src log -1 --pretty&lt;span style="color:#f92672"&gt;=&lt;/span&gt;%B&lt;span style="color:#66d9ef"&gt;)&lt;/span&gt;&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Using &lt;/span&gt;$COMMIT_MESSAGE&lt;span style="color:#e6db74"&gt; as commit message.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;read -p &lt;span style="color:#e6db74"&gt;&amp;#34;Do you want to continue? (y/n): &amp;#34;&lt;/span&gt; answer
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; &lt;span style="color:#f92672"&gt;[[&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;$answer&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;n&amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;]]&lt;/span&gt;; &lt;span style="color:#66d9ef"&gt;then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;You said no. Exiting...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; exit &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;elif&lt;/span&gt; &lt;span style="color:#f92672"&gt;[[&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;$answer&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;y&amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;]]&lt;/span&gt;; &lt;span style="color:#66d9ef"&gt;then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;Continuing...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;Invalid input. Please enter &amp;#39;y&amp;#39; or &amp;#39;n&amp;#39;.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; exit &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;fi&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Git commit...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git commit -m &lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;$COMMIT_MESSAGE&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Git push...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git push
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With this setup, I push to the src branch manually, but I can then easily deploy the static build results locally without using any CI tooling.&lt;/p&gt;</description></item><item><title>Make Capslock Great Again: XKB Customization for Sway</title><link>https://hossein.naghdbishi.com/posts/en/xkb-customization-swaywm/</link><pubDate>Sun, 26 May 2024 01:20:00 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/xkb-customization-swaywm/</guid><description>&lt;p&gt;As I&amp;rsquo;m on the unstable branch of NixOS, one day I woke up to a NixOS config that was failing to build. Specifically, the derivation for &lt;code&gt;sway.conf&lt;/code&gt; wouldn&amp;rsquo;t build.
The keyboard layout I use is special. I usually switch between two customized keyboard layouts. I use the Dvorak layout to write English, and Persian (with Persian numpad) as my second layout. All my attempts to use my previous config, where I just put two modified &lt;code&gt;xkb_symbol&lt;/code&gt; files in &lt;code&gt;~/.config/xkb/&lt;/code&gt; failed. I used to have some line like this in my &lt;a href="https://swaywm.org/"&gt;Sway&lt;/a&gt; config: &lt;code&gt;xkb_layout = &amp;quot;us_with_vim,ir_with_vim&amp;quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But now the only option that I could use without NixOS throwing an error at me was the &lt;code&gt;xkb_file&lt;/code&gt; which, as explained in &lt;a href="https://man.archlinux.org/man/sway-input.5"&gt;sway-input(5)&lt;/a&gt;, this option overrides the two previous options that I was using.&lt;/p&gt;
&lt;p&gt;It was hard to find an &lt;code&gt;xkb_file&lt;/code&gt; example with two layouts in it on the web. So, here you are, a XKB keymap that I wrote that not only has two layouts in it, but also &lt;a href="https://github.com/Vonng/Capslock"&gt;&lt;em&gt;makes capslock great again&lt;/em&gt;&lt;/a&gt; by changing capslock+hjkl into arrow keys. Also, the numpad is never turned off.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;xkb_keymap {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; xkb_keycodes { include &amp;#34;evdev+aliases(qwerty)&amp;#34; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; xkb_types { include &amp;#34;complete+numpad(mac)&amp;#34; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; xkb_compat { include &amp;#34;complete&amp;#34; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; xkb_symbols {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; include &amp;#34;pc+us(dvorak)+inet(evdev)+group(alts_toggle)&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; name[Group1] = &amp;#34;dvorak with vim&amp;#34;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; include &amp;#34;ir(pes_keypad):2+group(alts_toggle):2&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; name[Group2] = &amp;#34;persian with vim&amp;#34;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; // Changes for us(dvorak)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;CAPS&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;TWO_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ ISO_Level3_Shift ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ ISO_Level3_Shift ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;AC02&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;FOUR_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ o, O, KP_Enter, KP_Enter ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ Arabic_seen, Arabic_hamzaonyeh, KP_Enter, KP_Enter ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;AC03&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;FOUR_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ e, E, Next, Next ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ Farsi_yeh, Arabic_yeh, Arabic_alefmaksura ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;AC04&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;FOUR_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ u, U, BackSpace, BackSpace ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ Arabic_beh, Arabic_hamzaunderalef, BackSpace, BackSpace ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;AC06&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;FOUR_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ d, D, Left, Left ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ Arabic_alef, Arabic_maddaonalef, Left, Left ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;AC07&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;FOUR_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ h, H, Down, Down ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ Arabic_teh, Arabic_tatweel, Down, Down ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;AC08&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;FOUR_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ t, T, Up, Up ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ Arabic_noon, guillemotright, Up, Up ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;AC09&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;FOUR_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ n, N, Right, Right ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ Arabic_meem, guillemotleft, Right, Right ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;AD07&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;FOUR_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ g, G, Prior, Prior ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ Arabic_ain, Arabic_fatha, Prior, Prior ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;AD08&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;FOUR_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ c, C, Delete, Delete ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ Arabic_heh, Arabic_shadda, Delete, Delete ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;AE03&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;FOUR_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ 3, numbersign, Home, Home ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ Farsi_3, 0x100066b, Home, Home ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; key &amp;lt;AE04&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; type= &amp;#34;FOUR_LEVEL&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group1]= [ 4, dollar, End, End ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; symbols[Group2]= [ Farsi_4, 0x100fdfc, End, End ]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; xkb_geometry { include &amp;#34;pc(pc105)&amp;#34; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Update (May 2025):&lt;/p&gt;
&lt;p&gt;Don&amp;rsquo;t use sway? You can use &lt;a href="https://github.com/rvaiya/keyd"&gt;keyd&lt;/a&gt; with the following &lt;code&gt;/etc/keyd/default.conf&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[ids]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;*
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[main]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# Define capslock behavior:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# - When held with another key, activate the &amp;#39;nav&amp;#39; layer.
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# - When tapped alone, behave as normal capslock.
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;capslock = overload(nav, capslock)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[nav]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;h = left
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;j = down
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;k = up
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;l = right
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;f = backspace
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;backspace = delete
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item><item><title>Buddhism</title><link>https://hossein.naghdbishi.com/posts/en/buddhism/</link><pubDate>Fri, 24 Nov 2023 18:33:58 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/buddhism/</guid><description>&lt;blockquote&gt;
&lt;p&gt;Gautama found that there was a way to exit this vicious circle. If, when the mind experiences something pleasant or unpleasant, it simply understands things as they are, then there is no suffering. If you experience sadness without craving that the sadness go away, you continue to feel sadness but you do not suffer from it. There can actually be richness in the sadness. If you experience joy without craving that the joy linger and intensify, you continue to feel joy without losing your peace of mind.&lt;/p&gt;
&lt;p&gt;But how do you get the mind to accept things as they are, without craving? To accept sadness as sadness, joy as joy, pain as pain? Gautama developed a set of meditation techniques that train the mind to experience reality as it is, without craving. These practices train the mind to focus all its attention on the question, ‘What am I experiencing now?’ rather than on ‘What would I rather be experiencing?’ It is difficult to achieve this state of mind, but not impossible.&lt;/p&gt;
&lt;p&gt;Gautama grounded these meditation techniques in a set of ethical rules meant to make it easier for people to focus on actual experience and to avoid falling into cravings and fantasies. He instructed his followers to avoid killing, promiscuous sex and theft, since such acts necessarily stoke the fire of craving (for power, for sensual pleasure, or for wealth). When the flames are completely extinguished, craving is replaced by a state of perfect contentment and serenity, known as nirvana (the literal meaning of which is ‘extinguishing the fire’). Those who have attained nirvana are fully liberated from all suffering. They experience reality with the utmost clarity, free of fantasies and delusions. While they will most likely still encounter unpleasantness and pain, such experiences cause them no misery. A person who does not crave cannot suffer.&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="footnotes" role="doc-endnotes"&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Sapiens: A Brief History of Humankind, Sapiens: A Brief History of Humankind&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description></item><item><title>VPS Troubleshooting: A Quick Guide</title><link>https://hossein.naghdbishi.com/posts/en/vps-troubleshooting/</link><pubDate>Sun, 14 May 2023 18:10:53 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/vps-troubleshooting/</guid><description>&lt;p&gt;Many problems may occur on a VPS, and solving them is no easy task. It&amp;rsquo;s like being a doctor, you can only see the symptoms and have to give educated guesses about what the cause of the problem may be.&lt;/p&gt;
&lt;p&gt;First you diagnose the problem by different tools you have. Then, depending on your guess about the root cause, you try different remedies until you find one that actually solves the problem.&lt;/p&gt;
&lt;p&gt;This article will go over some general problems that VPSes may have. We will also learn how one may solve them. Let&amp;rsquo;s go!&lt;/p&gt;
&lt;h2 id="unable-to-connect-to-the-server-via-ssh"&gt;Unable to connect to the server via SSH&lt;/h2&gt;
&lt;h3 id="use-the-web-console"&gt;Use the web console.&lt;/h3&gt;
&lt;p&gt;All VPS providers give you the ability to manage your VPS even if it doesn&amp;rsquo;t have an Internet connection. Find that section (usually called web console) in your VPS provider&amp;rsquo;s site and log in to your server, follow the rest of the article then.&lt;/p&gt;
&lt;h3 id="are-you-connecting-to-the-correct-port-and-ip"&gt;Are you connecting to the correct port and IP?&lt;/h3&gt;
&lt;p&gt;Check the IP you&amp;rsquo;re trying to connect to; is it the same as the one on your VPS provider site? If so, the problem may be that you&amp;rsquo;re trying to connect to the wrong port.&lt;/p&gt;
&lt;p&gt;First, try to see the port that your SSH server listens to. You can use this command on your server to find that out: &lt;code&gt;sudo ss -tulpn | grep ssh&lt;/code&gt; if it shows any port other than 22, try connecting to that port from your PC, for example for port 2222, we&amp;rsquo;ll have &lt;code&gt;ssh -p 2222 user@IP&lt;/code&gt;&lt;/p&gt;
&lt;h3 id="is-your-sever-up-and-running"&gt;Is your sever up and running?&lt;/h3&gt;
&lt;p&gt;If you haven&amp;rsquo;t blocked ICMP packets with your firewall, you can use ping (or if you only have IPv6, ping6) to see whether your server is accessible over the Internet. Try: &lt;code&gt;ping x&lt;/code&gt; or &lt;code&gt;ping6 y&lt;/code&gt; where x is your IPv4 and y your IPv6 address.&lt;/p&gt;
&lt;p&gt;Can you ping your server? Or, equivalently, can you open a service hosted on it? If so, it&amp;rsquo;s good news! At least now you know that the OS on the server is up and is responding to the packets it receives. If it&amp;rsquo;s not, ping your server with online tools such as &lt;a href="https://ping.pe"&gt;ping.pe&lt;/a&gt; to make sure your server is really down, and it&amp;rsquo;s not a problem specific to your PC. If your server is down, you must reach out to your VPS provider. It&amp;rsquo;s most likely that they have turned off your VPS because you haven&amp;rsquo;t payed the bills, if it&amp;rsquo;s not that, then open a ticket and ask them for help.&lt;/p&gt;
&lt;h3 id="use-another-server-or-computer-for-ssh-connection"&gt;Use another server or computer for SSH connection&lt;/h3&gt;
&lt;p&gt;If your server&amp;rsquo;s up, but you can&amp;rsquo;t SSH to it, it may be your PC&amp;rsquo;s Internet connection that has problems. Try SSHing on other servers or computers and see if the problem&amp;rsquo;s on your server&amp;rsquo;s side. Ideally, you can use &lt;a href="https://tcp.ping.pe"&gt;tcp.ping.pe&lt;/a&gt; to verify that your server&amp;rsquo;s SSH port is open from multiple locations.&lt;/p&gt;
&lt;h3 id="use-the-verbose-mode-of-ssh"&gt;Use the verbose mode of SSH&lt;/h3&gt;
&lt;p&gt;Your SSH client itself can provide you with more details if you ask it to, use the &lt;code&gt;-vvv&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;option for the SSH command, it&amp;rsquo;ll become more verbose and you may find the problem just by doing that.&lt;/p&gt;
&lt;h3 id="is-your-firewall-allowing-tcp-packets-to-that-port"&gt;Is your firewall allowing TCP packets to that port?&lt;/h3&gt;
&lt;p&gt;It may be that you have enabled a firewall and it&amp;rsquo;s drops any packets sent to the port SSH server is currently listening to. It all really depends on your distro, or the firewall you have chosen to use, but on most distros, iptables is the default firewall, so try: &lt;code&gt;sudo iptables -L&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;If you have installed nftables, try this one: &lt;code&gt;sudo nft list ruleset&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;There must be nothing that indicates dropping of packets on the port that your SSH server is listening to. If there is, just change the configuration so that it allows TCP packets to that port.&lt;/p&gt;
&lt;h3 id="is-your-ssh-server-configured-well-is-it-running"&gt;Is your SSH server configured well? Is it running?&lt;/h3&gt;
&lt;p&gt;Sometimes it&amp;rsquo;s not about the firewall, it&amp;rsquo;s just that there&amp;rsquo;s no SSH server listening on any port, or if there is, it&amp;rsquo;s not allowing you to log in. Most GNU/Linux distros use OpenSSH as their SSH server and manage it with systemd. I&amp;rsquo;ll assume these tools for this article, but for example Alpine Linux and Gentoo use &lt;a href="https://en.wikipedia.org/wiki/OpenRC"&gt;openrc&lt;/a&gt; and not systemd, or some may use &lt;a href="https://en.wikipedia.org/wiki/Dropbear_(software)"&gt;dropbear&lt;/a&gt; as their SSH server. Keep these differences in mind if you&amp;rsquo;re not using mainstream distros such as Ubuntu and Debian.&lt;/p&gt;
&lt;p&gt;First let&amp;rsquo;s see if your SSH server is running with no problems. As before, &lt;code&gt;ss -tulpn | grep ssh&lt;/code&gt; will show if there&amp;rsquo;s a process with ssh in its name is running and listening to any port. OpenSSH&amp;rsquo;s server part is called &amp;ldquo;sshd&amp;rdquo; so if it&amp;rsquo;s listening it will show up.&lt;/p&gt;
&lt;p&gt;If it&amp;rsquo;s running but you can&amp;rsquo;t connect to it; or if it&amp;rsquo;s not running, you can look for some simple logs using this command: &lt;code&gt;journalctl -u ssh.service&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;If you can&amp;rsquo;t diagnose and solve the problem just by reading those logs, try looking up OpenSSH logs directly. You can do this by executing &lt;code&gt;cat /var/log/auth.log&lt;/code&gt; if this doesn&amp;rsquo;t work, try &lt;code&gt;cat /var/log/secure&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://en.wikibooks.org/wiki/OpenSSH/Logging_and_Troubleshooting"&gt;This book&lt;/a&gt; will help you understand what the log is saying.&lt;/p&gt;
&lt;p&gt;After reading logs and searching about them, you should have a general sense of what&amp;rsquo;s wrong with your sshd configuration, if you need to change anything, try using &lt;code&gt;sudo nano /etc/ssh/sshd_config&lt;/code&gt;
After saving your changes to this configuration file, run &lt;code&gt;sudo systemctl restart sshd&lt;/code&gt; for the changes to take place, repeat reading logs and making changes until you have a working SSH server.&lt;/p&gt;
&lt;p&gt;Some lines that you must pay attention to are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;PermitRootLogin&lt;/code&gt; If it&amp;rsquo;s &amp;ldquo;no&amp;rdquo;, then obviously you can&amp;rsquo;t log in as root and should try logging in as other users.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;PasswordAuthentication&lt;/code&gt; If it&amp;rsquo;s set to no, you have to always log in using your private key (a good thing!). This is done by -i option in your ssh client: &lt;code&gt;ssh -i /path/to/privkey&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;PubkeyAuthentication&lt;/code&gt; This must be set to yes if you&amp;rsquo;re trying to authenticate yourself by a private key as shown above.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ListenAddress&lt;/code&gt; This part specifies on which ports the SSH server listens to. Any IP other than &lt;code&gt;0.0.0.0&lt;/code&gt; (and it&amp;rsquo;s IPv6 counterpart &lt;code&gt;::&lt;/code&gt;) or the IP you&amp;rsquo;re currently SSHing to will cause the packets to drop since the IP you request and the IP the SSH server is listening to don&amp;rsquo;t match.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To check if the SSH server is running you can try: &lt;code&gt;sudo systemctl status sshd&lt;/code&gt;&lt;/p&gt;
&lt;h2 id="high-cpu-usage"&gt;High CPU usage&lt;/h2&gt;
&lt;h3 id="a-one-shot-or-a-chronic-problem"&gt;A one-shot or a chronic problem?&lt;/h3&gt;
&lt;p&gt;Look at the CPU usage graph of your VPS. You either have server monitoring tools such as Prometheus, or if you don&amp;rsquo;t, just use the one your VPS provider (probably) gives you.&lt;/p&gt;
&lt;p&gt;In the graph, if you see a sudden bump, it may be that you have suddenly too many people using your services, your VPS has a malware, or that you&amp;rsquo;re under a DDoS attack. For the latter, using a CDN is a good idea.&lt;/p&gt;
&lt;p&gt;If the graph is smooth and showing consistently high CPU usage, follow the steps below. If the processes that are using lots of CPU are legitimate, think about upgrading your VPS resources.&lt;/p&gt;
&lt;h3 id="find-the-cpu-consuming-processes"&gt;Find the CPU-consuming processes.&lt;/h3&gt;
&lt;p&gt;You can see which processes are using the most CPU by executing: &lt;code&gt;sudo top -o %CPU&lt;/code&gt;. You can find the culprit here. Once found, you can kill it or renice it. If the process is critical, you shouldn&amp;rsquo;t do either, just upgrade your VPS resources.&lt;/p&gt;
&lt;p&gt;To kill a process, press &lt;em&gt;k&lt;/em&gt; while top is open. It will ask you for a process ID (PID) and it will close that process for you.&lt;/p&gt;
&lt;p&gt;Instead of killing processes, you can renice them. Linux comes with the ability to change the priority of processes in CPU scheduling. It&amp;rsquo;s called renicing. While top is running press &lt;em&gt;r&lt;/em&gt; , then enter a PID and change its nice value. The nice value indicates the priority of the processes in getting CPU time. Nice can be any value from +19 to -20, the higher it is, the less CPU resource that process will get. When you&amp;rsquo;re done, press q to quit.&lt;/p&gt;
&lt;h3 id="kill-restart-or-renice-the-processes"&gt;Kill, restart, or renice the processes&lt;/h3&gt;
&lt;p&gt;If you don&amp;rsquo;t find the CPU consuming processes critical, kill them. To kill a process, press &lt;em&gt;k&lt;/em&gt; while top is open. It will ask you for a process ID (PID) and it will close that process for you. If they don&amp;rsquo;t go away gracefull, press &lt;code&gt;q&lt;/code&gt; to quit from top, then execute &lt;code&gt;kill -9 x&lt;/code&gt; where x is the PID. This will force kill the process. You can try restarting the services that run the processes (e.g. if nginx uses too much CPU: &lt;code&gt;systemctl restart nginx&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Instead of killing or restarting processes, you can renice them. Linux comes with the ability to change the priority of processes in CPU scheduling, it&amp;rsquo;s called renicing. While top is running press &lt;em&gt;r&lt;/em&gt; , then enter a PID and change its nice value. The nice value indicates the priority of the processes in getting CPU time. Nice can be any value from +19 to -20, the higher it is, the less CPU resource that process will get. When you&amp;rsquo;re done, press q to quit.&lt;/p&gt;
&lt;h3 id="case-by-case-solutions"&gt;Case-by-case solutions&lt;/h3&gt;
&lt;p&gt;Sometimes the problem is more subtle, a misconfiguration in, for example, your MySQL server, or a badly written Wordpress plugin might be the whole reason your VPS is consuming so much CPU. If you have a guess about the culprit (that shows up in the result of top command), try searching. Other users may have the same problem with the service, and you can ask them about how they have solved the issue.&lt;/p&gt;
&lt;h3 id="updating-everything"&gt;Updating everything&lt;/h3&gt;
&lt;p&gt;If the problem persists, try updating everything (from your entire OS to every docker container) as a last shot. If you have recently installed or upgraded something and guess that may be the cause, you can downgrade or reinstall that too.&lt;/p&gt;
&lt;h2 id="unexpected-reboot"&gt;Unexpected reboot&lt;/h2&gt;
&lt;p&gt;This one&amp;rsquo;s hard to catch! But let&amp;rsquo;s try.&lt;/p&gt;
&lt;h3 id="how-frequent-are-these-reboots-and-when-do-they-happen"&gt;How frequent are these reboots? and when do they happen?&lt;/h3&gt;
&lt;p&gt;First see your uptime, use the &lt;code&gt;uptime&lt;/code&gt; command, it will show you the current time and following that, how long your system has been up.&lt;/p&gt;
&lt;p&gt;Then try to see the frequency of the reboots. You can do this by: &lt;code&gt;sudo last -x | tac&lt;/code&gt; the &lt;code&gt;| tac&lt;/code&gt; part is optional and only shows you the latest info at the bottom of the terminal, instead of at the top.&lt;/p&gt;
&lt;p&gt;You may understand at this level that your system is not facing a reboot, it may just be that &lt;a href="https://href.li/?https://neo4j.com/developer/kb/linux-out-of-memory-killer/"&gt;OOM killer&lt;/a&gt; is killing your processes because you&amp;rsquo;re running out of memory and your process doesn&amp;rsquo;t restart automatically.&lt;/p&gt;
&lt;h3 id="look-through-the-logs"&gt;Look through the logs&lt;/h3&gt;
&lt;p&gt;After you get a rough idea of the times your server reboots, it&amp;rsquo;s time to dive into the logs. Try checking out either &lt;code&gt;/var/log/messages&lt;/code&gt; or &lt;code&gt;/var/log/syslog&lt;/code&gt;. Since you know exactly when you have had reboots and these files have timestamps, you know where you should look at. You can also use &lt;code&gt;cat&lt;/code&gt; and &lt;code&gt;grep&lt;/code&gt; to look for specific keywords.&lt;/p&gt;
&lt;p&gt;Mae sure you look into journalctl logs at a little bit before and after reboots using this command: &lt;code&gt;journalctl -S &amp;quot;2023-01-01 00:00:00&amp;quot; -U &amp;quot;2023-01-01 01:00:00&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Things get a lot easier once you use the journalctl command, first list the boots you have had by &lt;code&gt;journalctl --list-boots&lt;/code&gt; there will be a list of latest boots with a descending number assigned to each of them. If you want to see the logs for your current boot (which has the number 0) You would type: &lt;code&gt;journalctl -b 0&lt;/code&gt; and for the one before that &lt;code&gt;journalctl -b -1&lt;/code&gt; and so on &lt;a href="https://geekflare.com/check-linux-reboot-reason"&gt;(src)&lt;/a&gt;. Please note that journalctl uses a &lt;a href="https://en.wikipedia.org/wiki/Terminal_pager"&gt;pager&lt;/a&gt;. Use arrow keys to navigate, but if you want to jump to the latest logs just press &lt;em&gt;Shift+g&lt;/em&gt;.&lt;/p&gt;
&lt;h3 id="check-for-common-culprits"&gt;Check for common culprits&lt;/h3&gt;
&lt;p&gt;Linux sometimes panics, and if you make it to, it will reboot on panics. You can see if this is the case by executing &lt;code&gt;sysctl kernel.panic&lt;/code&gt;. If the result is a negative number, Linux will immediately reboot after a panic, if it&amp;rsquo;s a positive number, it will reboot after that many seconds, and if it&amp;rsquo;s 0, it won&amp;rsquo;t reboot.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re using a Debian based distro, and have enabled &lt;em&gt;Unattended-Upgrade::Automatic-Reboot&lt;/em&gt; in your apt configuration, then apt will reboot the system on its own after upgrades.&lt;/p&gt;
&lt;p&gt;Are you running out of resources? Check for that too, in the following part of the article you&amp;rsquo;ll learn how.&lt;/p&gt;
&lt;h3 id="ask-for-your-vps-providers-uptime"&gt;Ask for your VPS provider&amp;rsquo;s uptime&lt;/h3&gt;
&lt;p&gt;There may be nothing with your VPS after all, maybe it&amp;rsquo;s just that the dedicated server hosting your server is not configured well, or that it&amp;rsquo;s having reboots, ask your VPS provider about this.&lt;/p&gt;
&lt;h2 id="slow-response-time--network-connection-instability"&gt;Slow response time / network connection instability&lt;/h2&gt;
&lt;h3 id="do-you-have-enough-cpudiskram-resources"&gt;Do you have enough CPU/Disk/RAM resources?&lt;/h3&gt;
&lt;p&gt;Network packets need to be processed, if your system is overwhelmed and there&amp;rsquo;s not enough RAM and CPU, your VPS may have problem with processing the incoming packets. So use the following commands to see how much available resource you have.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;free -h&lt;/code&gt; use this to see how much RAM and swap you have left.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;top&lt;/code&gt; see which processes are hogging resources&lt;/li&gt;
&lt;li&gt;&lt;code&gt;df -h&lt;/code&gt; see if you have enough space left. The usage percentage better be less than 98%.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;df -hi&lt;/code&gt; see how much inodes you have left on each partition. The usage percentage better be less than 98%.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You may wonder what disk has got to do with response time. Two things come to mind. First, your programs need disk space to function well (for example a SQL server needs to write to disk) and second, sometimes your RAM is full and the OS starts writing to swap, if there&amp;rsquo;s too much I/O going on, writing and reading may be slow and it will hurt all the running processes that use swap. You should not lean on swap and if it&amp;rsquo;s always full, just buy more RAM.&lt;/p&gt;
&lt;h3 id="is-your-hard-disks-io-speed-okay"&gt;Is your hard disk&amp;rsquo;s I/O speed okay?&lt;/h3&gt;
&lt;p&gt;Sometimes the root of slow response time lies within slow disk read and write. Check for that as well.&lt;/p&gt;
&lt;h2 id="is-your-network-okay"&gt;Is your network okay?&lt;/h2&gt;
&lt;p&gt;Sometimes it may not be that your provider is having problems, but that your server is being flooded with network requests. You can check that out an app called &lt;a href="https://github.com/raboof/nethogs"&gt;nethogs&lt;/a&gt;. If you&amp;rsquo;re using too much network resources, ask for a dedicated Internet port from your server provider. But if you&amp;rsquo;re not, try to see what&amp;rsquo;s happening under the hood.&lt;/p&gt;
&lt;p&gt;Try pinging yourself and some other IP: &lt;code&gt;ping -c 10 your_ip&lt;/code&gt; and &lt;code&gt;ping -c 1.1.1.1&lt;/code&gt;. Ping shows you how long (in milliseconds) a server takes to respond to your packets, the less it is, the better. Pinging yourself should take less than a millisecond and pinging 1.1.1.1, less than 200ms at most, although it usually is a lot less than 200ms. Other than average round trip time that is displayed at the bottom, there&amp;rsquo;s also another factor you should pay attention to: packet loss. Ideally you should have a 0% packet loss.&lt;/p&gt;
&lt;p&gt;Try testing your Internet speed, first &lt;a href="https://www.speedtest.net/apps/cli"&gt;install speedtest-cli&lt;/a&gt; and then run it. Depending on your VPS provider&amp;rsquo;s network and what plan you have bought, the speed you get will vary, but if it&amp;rsquo;s too slow (and other processes are not downloading anything either), open a ticket with your provider.&lt;/p&gt;
&lt;h3 id="check-for-software-issues"&gt;Check for software issues&lt;/h3&gt;
&lt;p&gt;If your network connection is okay, but you&amp;rsquo;re still having slow response time, then it&amp;rsquo;s time that you look into the configuration of the specific service that is slow, try searching about that specific piece of software, and then optimize it.&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary&lt;/h2&gt;
&lt;p&gt;In this article we went through some of the common problems that you may face by owning a VPS, introduced some diagnostic tools for the problems and offered solutions. I hope you find them useful. If you have any questions or comments, don&amp;rsquo;t hesitate to send me an email.&lt;/p&gt;</description></item><item><title>Hypothesis Testing, P-Value: Things We Learn From an Anime</title><link>https://hossein.naghdbishi.com/posts/en/hypothesis-testing/</link><pubDate>Mon, 07 Mar 2022 16:14:30 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/hypothesis-testing/</guid><description>&lt;p&gt;They say it is only everybody that knows &lt;em&gt;almost&lt;/em&gt; everything and I agree. I rarely claim to know about a concept. The only thing that I&amp;rsquo;m certain about, is that there is always something, somewhere about that concept that I don&amp;rsquo;t know. But this is good news, this means that there&amp;rsquo;s always room for learning more, and I can keep learning and understanding.&lt;/p&gt;
&lt;p&gt;Hypothesis testing is one of the pillars of scientific discoveries. After observations, hypotheses are made, and surely, they need to be tested one way or another. Let&amp;rsquo;s try to understand what it takes to test a hypothesis.&lt;/p&gt;
&lt;p&gt;The way I&amp;rsquo;m going to structure this post and demonstrate how the statistics behind scientific discoveries works, comes from an unlikely source. In its first episode, the 2020 anime &lt;a href="https://en.wikipedia.org/wiki/Science_Fell_in_Love%2C_So_I_Tried_to_Prove_It"&gt;Science Fell in Love, So I Tried to Prove It&lt;/a&gt;, demonstrates the essence of hypothesis testing in a way none of my professors could. What I do here is building upon that demonstration and adding a very small amount of R and mathematical mojo.&lt;/p&gt;
&lt;p&gt;Suppose you&amp;rsquo;re a scientist living in a magical land and you want to prove that all the crows in this magical land are black, however your magical powers can only take a random sample of 100 crows and this can happen only once. You cast the spell, out of all the crows in the land, a &lt;a href="https://www.investopedia.com/terms/s/simple-random-sample.asp"&gt;random sample&lt;/a&gt; of 100 crows appear in front of you and you check them: they are all indeed black.&lt;/p&gt;
&lt;p&gt;Does this mean you can victoriously claim that all crows are black? You know very well that any critic, even you yourself, can always say that you have reached this conclusion, only because of your limited powers of magic, because of the small sample size, and not because all the crows are black like you&amp;rsquo;re implying. How do you know that there wouldn&amp;rsquo;t have been a crow of a different color, had your magical powers enabled you to take a larger sample of the crows of the land? This is a question to be considered.
&lt;img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/ad/Carrion_crows_Corvus_corone%2C_Tottenham%2C_London%2C_England_3.jpg/1024px-Carrion_crows_Corvus_corone%2C_Tottenham%2C_London%2C_England_3.jpg" alt="4 crows sitting on branches of a tree"&gt;&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Does your limited powers of observation mean that you can claim nothing unless you can do a full survey of all the crows in the world? Not really; &lt;strong&gt;even with limited powers of random sampling, you can still make very reliable claims using probabilities and &lt;a href="https://towardsdatascience.com/hypothesis-testing-demystified-e434a665e055"&gt;contradiction.&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To any critic, you can say, alright, probably you&amp;rsquo;re right, perhaps not all crows are black. What percentage of crows do you claim to be non-black? Let&amp;rsquo;s say they suggest that 50% of the crows are not black.&lt;/p&gt;
&lt;p&gt;But if this is the case, given that your sample is truly randomly chosen, you can calculate the possibility of getting such a sample from a population of crows that one half of it is black, and the other half is not.&lt;/p&gt;
&lt;p&gt;A crow can be either black or not. If we were dealing with just choosing one random crow from the population, we&amp;rsquo;d have a distribution called the &lt;a href="https://en.wikipedia.org/wiki/Bernoulli_distribution"&gt;Bernoulli distribution&lt;/a&gt;, since we have multiple crows drawn out from the population, we&amp;rsquo;re dealing with a &lt;a href="https://en.wikipedia.org/wiki/Binomial_distribution"&gt;Binomial distribution&lt;/a&gt;. In the formula below, p is the chance that a crow is black, k is the number of time&amp;rsquo;s we&amp;rsquo;ve seen a black crow in our sample and n is the total number of the times we&amp;rsquo;ve drawn out a crow from the population.&lt;/p&gt;
\[
P(X=k)=\binom{n}{k}(p)^k(1-p)^{n-k}
\]&lt;p&gt;Here&amp;rsquo;s the chance of getting 100 black crows &lt;strong&gt;given that&lt;/strong&gt; half of the crows are black and the other half are not:&lt;/p&gt;
\[
P(X=100)=\binom{100}{100}(\frac{1}{2})^{100}(\frac{1}{2})^{0}\approx 7.888\times10^{-31}
\]&lt;p&gt;This small values doesn&amp;rsquo;t sound very probable, does it?&lt;/p&gt;
&lt;p&gt;&lt;img src="https://hossein.naghdbishi.com/images/crow_dist_graph.png" alt="Distribution for when half the crows are non-black"&gt;&lt;/p&gt;
&lt;p&gt;You can even see it on the graph, the likelihood of getting 65 crows that are all black in this scenario is very unlikely, let alone 100 crows that are all black.&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;d get the same improbable results unless the critic increases their evaluation of the portion of crows they think are black. Let&amp;rsquo;s say they argue that, yes, most crows are black but there&amp;rsquo;s a very small portion of crows that are non-black, and this rarity is why we have missed the non-black ones.&lt;/p&gt;
&lt;p&gt;For example they might say that only 4% of the crows are non-black and that&amp;rsquo;s why our sample of 100 has missed them.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://hossein.naghdbishi.com/images/crow_dist_graph_2.png" alt="Distribution for when 4% of the crows are non-black"&gt;&lt;/p&gt;
&lt;p&gt;We can argue, once more, that even this time the chances that we have missed the non-black crows because of the sample size is really low, &lt;em&gt;the probability that a random sample of 100 black crows happens merely by chance, &lt;strong&gt;given that&lt;/strong&gt; 4% of the crows of the population are non-black_is less than 1.7%.&lt;/em&gt;&lt;/p&gt;
\[
P(X=100)=\binom{100}{100}(0.96)^{100}(0.4)^{0}\approx 0.01687
\]&lt;p&gt;If they&amp;rsquo;re smart enough, they can suggest a portion that actually undermines our findings, here&amp;rsquo;s how:&lt;/p&gt;
\[
P(X=100)=\binom{100}{100}(p)^{100}(1-p)^{0} = 0.05
\]&lt;p&gt;
&lt;/p&gt;
\[
p^{100} = 5 \times 10^{-2} \Rightarrow p=5^{\frac{1}{100}} \times 10^{\frac{-2}{100}} \approx 0.9705
\]&lt;p&gt;A binomial distribution with n=100 and p=0.9705 looks like this:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://hossein.naghdbishi.com/images/crow_dist_graph_3.png" alt="Distribution for when 97.05% of the crows are black"&gt;&lt;/p&gt;
&lt;p&gt;If they claim that 97.05% crows are black, now our sample size begins to make problems, &lt;em&gt;the probability that a random sample of 100 black crows happens, &lt;strong&gt;given that&lt;/strong&gt; 97.05% of the crows of the population are black is around 5%.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The sentences above in italic tell us what p-value means for &lt;em&gt;our question&lt;/em&gt;. But we can state it in a more general form: &lt;strong&gt;it&amp;rsquo;s the probability that our sample (or what&amp;rsquo;s even more extreme than our sample) happens, given that the hypothesis we tried to contradict is true.&lt;/strong&gt;&lt;/p&gt;
\[
p-value=P(\mbox{occurrence of our sample or more extreme case}|H_0)
\]&lt;p&gt;The use of contradiction happens right here: If this probability is very low, then maybe what we&amp;rsquo;re assuming is wrong? In different branches of science, 5% is what we call really low probability. If for our null hypothesis, our p-value drops to anything less than 5%, we say:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;We reject the null hypothesis, in favor of the alternative hypothesis at the significance level of 5%&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are two important points to be made here. First: failing to reject the null hypothesis doesn&amp;rsquo;t mean it&amp;rsquo;s true, because you&amp;rsquo;re already assuming the null hypothesis in the calculations. It doesn&amp;rsquo;t make a whole lot of sense to accept the null hypothesis when you&amp;rsquo;re already assuming it. Null hypothesis can never be accepted, we can only &lt;em&gt;reject it&lt;/em&gt; at a certain significance level or &lt;em&gt;fail to reject it&lt;/em&gt; and that&amp;rsquo;s it.&lt;/p&gt;
&lt;p&gt;The other point is that here we worked with the most extreme case, had we chosen another statement as our hypothesis, we would have to consider both our own case and the extremer ones to calculate the p-value.&lt;/p&gt;
&lt;p&gt;At last, I encourage you to read about &lt;a href="https://www.wired.com/story/were-all-p-hacking-now/"&gt;p-hacking&lt;/a&gt; and what it means for science in general. &lt;a href="https://faculty.elgin.edu/dkernler/statistics/ch10/10-1.html"&gt;From what I&amp;rsquo;ve come to understand p-value is a great tool, &lt;/a&gt; but it can also be &lt;a href="https://www.tandfonline.com/doi/full/10.1080/00031305.2019.1583913"&gt;misused and misunderstood&lt;/a&gt;&lt;a href="https://faculty.elgin.edu/dkernler/statistics/ch10/10-1.html"&gt;.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;And oh&amp;hellip; One more thing! Search for &amp;ldquo;p-value memes&amp;rdquo; or &amp;ldquo;null hypothesis memes&amp;rdquo; in your favorite web search engine, and have fun :)&lt;/p&gt;
&lt;div class="footnotes" role="doc-endnotes"&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Image © &lt;a href="https://commons.wikimedia.org/wiki/User:Acabashi"&gt;Acabashi&lt;/a&gt; &lt;a href="https://creativecommons.org/licenses/by/4.0/legalcode"&gt;CC-BY-SA 4.0&lt;/a&gt;&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description></item><item><title>How to Study</title><link>https://hossein.naghdbishi.com/posts/en/how-to-study/</link><pubDate>Fri, 14 Jan 2022 16:06:41 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/how-to-study/</guid><description>&lt;p&gt;See &lt;a href="https://cse.buffalo.edu/~rapaport/HOWTOSTUDY/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Lately I’ve been thinking of becoming even more serious in my goal of becoming a researcher. Mainly I’ve been asking myself: how many hours of studying is enough? The article above has some valuable tips and also answers this question.&lt;/p&gt;
&lt;p&gt;These two are great too: &lt;a href="https://learningrabbithole.com/how-many-hours-a-day-can-you-effectively-study/"&gt;+&lt;/a&gt; &lt;a href="https://learningrabbithole.com/how-many-hours-do-top-students-study/"&gt;+&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>Kant</title><link>https://hossein.naghdbishi.com/posts/en/kant/</link><pubDate>Fri, 08 Oct 2021 16:01:36 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/kant/</guid><description>&lt;p&gt;&lt;img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/Admiring_the_Galaxy.jpg/1024px-Admiring_the_Galaxy.jpg" alt="A person looking at the sky and admiring the galaxy"&gt;&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Lately I’ve been reading a little bit about Kant, mainly &lt;a href="http://www.roger-scruton.com/"&gt;Roger Scruton&lt;/a&gt;’s book: “Kant: A Very Short Introduction”. Here I intend to briefly explain things I find interesting about him.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Two things fill the mind with ever new and increasing admiration and reverence, the more often and more steadily one reflects on them: the starry heavens above me and the moral law within me.&lt;/strong&gt; I do not need to search for them and merely conjecture them as though they were veiled in obscurity or in the transcendent region beyond my horizon; I see them before me and connect them immediately with the consciousness of my existence. The first begins from the place I occupy in the external world of sense and extends the connection in which I stand into an unbounded magnitude with worlds upon worlds and systems of systems, and moreover into the unbounded times of their periodic motion, their beginning and their duration. The second begins from my invisible self, my personality, and presents me in a world which has true infinity but which can be discovered only by the understanding, and I cognize that my connection with that world (and thereby with all those visible worlds as well) is not merely contingent, as in the first case, but universal and necessary. The first view of a countless multitude of worlds annihilates, as it were, my importance as an animal creature, which after it has been for a short time provided with vital force (one knows not how) must give back to the planet (a mere speck in the universe) the matter from which it came. The second, on the contrary, raises my worth as an intelligence infinitely through my personality, in which the moral law reveals to me a life independent of animality and even of the whole sensible world, at least so far as this may be inferred from the purposive determination of my existence by this law.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is the most famous quote from Kant which, as &lt;a href="https://en.wikipedia.org/wiki/Paul_Guyer"&gt;Paul Guyer &lt;/a&gt;puts it, contains within itself references to two of the Kant’s greatest accomplishments in philosophy: his ideas about metaphysics and his ideas about ethics.&lt;/p&gt;
&lt;p&gt;What does Kant have to offer? He has already answered this question:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;All interest of my reason (the speculative as well as the practical) is united in the following three questions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;What can I know?&lt;/li&gt;
&lt;li&gt;What should I do?&lt;/li&gt;
&lt;li&gt;What may I hope?&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;
&lt;p&gt;Kant was born in 1724. He went on to become one of the greatest thinkers of &lt;a href="https://en.wikipedia.org/wiki/Age_of_Enlightenment"&gt;the Enlightenment era&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;During this era, thanks to the confidence brought by the great achievements of Newton, much emphasis was put into human reasoning. If one were to substitute, or even support, traditional authorities through reason, two basic disciplines were needed.
First, if we’re going to ever do scientific experiments, or use their results, we need to determine whether knowing anything about this world is possible at all (&lt;a href="https://en.wikipedia.org/wiki/Evil_demon"&gt;since my whole world may be an illusion made by an evil demon after al&lt;/a&gt;l). It’s obvious then, that we need a philosophy of epistemology.
Secondly, if we are not going to rely on religion, we’re going to need another moral compass. A philosophy of ethics is then needed.
Kant has came up with solutions in both areas.&lt;/p&gt;
&lt;p&gt;During the Enlightenment two main views were prevalent: Empiricism and Rationalism. Empiricists like Hume, threatened the foundations of science in a way. They had posited that we can’t have any knowledge of the world except through our own experience, and that our knowledge is always limited to our very own experience of the world. Leibniz and other rationalists however thought that the knowledge can be derived from reason independent of sensation and experience. Kant takes the middle way. He argues that knowledge is impossible without both our sensory input and our capacity to understand. Reason offers forms without content and sensation content without form. If I see something and call it a chair, it’s because I’ve used both the sensory input and my capacity to reason.&lt;/p&gt;
&lt;p&gt;What I like most about Kant is his creativity. For example in case of epistemology, empiricists claim that no knowledge can be possible that is not from my perspective, so it’s impossible for me to come up with a universal fact. They ask how is &lt;em&gt;synthetic a priori knowledge&lt;/em&gt; (knowledge that offers something new about the world that is both universal and necessary) possible? Kant says it’s true that nothing can be said about &lt;a href="https://en.wikipedia.org/wiki/Noumenon"&gt;the intelligible world&lt;/a&gt;, but what if we investigate what’s possible to say about the sensible world? He then goes on to show how synthetic a priori knowledge is possible in this case. He calls this “Copernican revolution in philosophy”.&lt;/p&gt;
&lt;p&gt;What Kant comes up with ultimately is nothing but what commonsense usually offers. For epistemology He tries his best to prove that things are as we see them and we see as they are. The same inclination towards commonsense is also true for his philosophy of ethics. Good will is central to his ethics.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It is impossible to conceive anything at all in the world, or even out of it, which can be taken as good without qualification, except a good will.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It’s an enormous task to provide philosophical grounds for what we have in ourselves intuitively, but what’s more interesting about Kant is how derives most of the information he needs from very basic ideas. He answers most of the questions of epistemology by examining the presuppositions of the simple fact that &lt;em&gt;we can ask&lt;/em&gt; the question “How is synthetic a priori knowledge possible?”, and for ethics he utilizes a basic claim of “Ought implies can” to get what he wants.&lt;/p&gt;
&lt;p&gt;I talked mostly about Kant’s ideas about metaphysics. Kant’s ethics is a lot more interesting indeed but you don’t need me to explain it to you, &lt;a href="https://www.youtube.com/watch?v=8bIys6JoEDw"&gt;there&lt;/a&gt; are &lt;a href="https://plato.stanford.edu/entries/kant-moral/"&gt;experts&lt;/a&gt; that explain it a lot better than I ever could.&lt;/p&gt;
&lt;p&gt;While I can’t claim to have understood him, I’m glad I took the time to read about him and his ideas. I am less interested in metaphysics, so my next goal would is to read more about ethics in general.&lt;/p&gt;
&lt;p&gt;Next book: &lt;a href="https://www.goodreads.com/book/show/7956661-ethics-for-dummies"&gt;Ethics for Dummies&lt;/a&gt;.&lt;/p&gt;
&lt;div class="footnotes" role="doc-endnotes"&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Photo by &lt;a href="http://www.eso.org/public/images/potw1320a/"&gt;ESO&lt;/a&gt;/A. Fitzsimmons licensed under &lt;a href="https://creativecommons.org/licenses/by/4.0"&gt;&lt;em&gt;CC BY 4.0&lt;/em&gt;&lt;/a&gt;&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description></item><item><title>Happiness Science</title><link>https://hossein.naghdbishi.com/posts/en/7-ways/</link><pubDate>Fri, 20 Nov 2020 15:57:05 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/7-ways/</guid><description>&lt;p&gt;Watch this &lt;a href="https://youtu.be/e9UVAafNvm4"&gt;great video&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Stoicism Is Awesome But...</title><link>https://hossein.naghdbishi.com/posts/en/on-stoicism/</link><pubDate>Fri, 22 May 2020 15:48:23 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/on-stoicism/</guid><description>&lt;p&gt;Stoicism is an awesome philosophy, one that I’ve spent some time reading about. Today however I was searching for what critics have to say. I stumbled upon &lt;a href="https://newideal.aynrand.org/the-false-promise-of-stoicism/"&gt;this piece&lt;/a&gt; from New Ideal which preaches Ayn Rand’s thoughts.&lt;/p&gt;
&lt;p&gt;When Stoic philosophers utter sentences like &lt;a href="https://youtu.be/5897dMWJiSM?t=579"&gt;“If you’re a slave then be a good slave.”&lt;/a&gt;, you don’t have a choice but to think that the underlying determinism in Stoicism is way too strong, and one should make way for what other philosophies have to say too: Every person is responsible for his or her own happiness and should try to pursue it.&lt;/p&gt;</description></item><item><title>What Makes Us Happy</title><link>https://hossein.naghdbishi.com/posts/en/what-makes-us-happy/</link><pubDate>Mon, 06 Apr 2020 15:39:07 +0330</pubDate><author>hossein@naghdbishi.com (Hossein Naghdbishi)</author><guid>https://hossein.naghdbishi.com/posts/en/what-makes-us-happy/</guid><description>&lt;blockquote&gt;
&lt;p&gt;Vaillant says his hopeful temperament is best summed up by the story of a father who on Christmas Eve puts into one son’s stocking a fine gold watch, and into another son’s, a pile of horse manure. The next morning, the first boy comes to his father and says glumly, “Dad, I just don’t know what I’ll do with this watch. It’s so fragile. It could break.” The other boy runs to him and says, “Daddy! Daddy! Santa left me a pony, if only I can just find it!”&lt;/p&gt;
&lt;p&gt;The story gets to the heart of Vaillant’s angle on the Grant Study. His central question is not how much or how little trouble these men met, but rather precisely how—and to what effect—they responded to that trouble. His main interpretive lens has been the psychoanalytic metaphor of “adaptations,” or unconscious responses to pain, conflict, or uncertainty.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;and also:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Perhaps in this, I thought, lies the key to the good life—not rules to follow, nor problems to avoid, but an engaged humility, an earnest acceptance of life’s pains and promises.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Excerpts from &lt;a href="https://www.theatlantic.com/magazine/archive/2009/06/what-makes-us-happy/"&gt;The Atlantic&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;[2026-02-01]: I just realized this wisdom is very similar to Stoicim&amp;rsquo;s &amp;ldquo;&lt;a href="https://en.wikipedia.org/wiki/Amor_fati"&gt;Amor fati&lt;/a&gt;&amp;rdquo;&lt;/p&gt;</description></item></channel></rss>