[Thought] Is using while loops safer than for?

I have been reading up on some reverse engineering lately, and after reading up assembly codes of some of my C programs. I think, any kind of loop would actually boil down to a simple while, when you see its version in assembly.


for(i=0; i < 10; i++)
printf("Hello, world!\n");

would boil down to:

0x08048394 : mov DWORD PTR [ebp-4],0x0
0x0804839b : cmp DWORD PTR [ebp-4],0x9
0x0804839f : jle 0x80483a3
0x080483a1 :jmp 0x80483b6
0x080483a3 : mov DWORD PTR [esp],0x80484d4
0x080483aa : call 0x80482a8 <_init+56>
0x080483af : lea eax,[ebp-4]
0x080483b2 : inc DWORD PTR [eax]
0x080483b4 : jmp 0x804839b
0x080483b6 : leave

Which is pretty much like a while.

while ( 1 ) {
if (DWORD PTR [ebp-4] > 0x9) {
) else {
do the job;
inc pointer;


while (DWORD PTR [ebp-4] > 0x9) {
do the job;

I think using a while loop is a safer option, because you'd have a better sense of idea on how it'll look in assembly. Makes sense?

Placing bets on dota2lounge when the bots are busy

I was trying to place a bet on the Fnatic vs Titan match tonight. I tried for over 15 minutes with no success. Every time I clicked on “place bet”, it showed me this annoying error “All queues are full. Please try again later.”

I tried to automate this, and ended up with 2 lines of javascript in my console.

Here’s how to do it:

  1. Select the items and the team you are betting for.
  2. Open the javascript console of your browser. (Options->Javascript Console in Chrome)
  3. Copy paste the following code into the javascript console.
    alert = function(args) { console.log(args); }
    setInterval(function(){$("#placebut").click()}, 500);
  4. Let it rip. ;)

You should probably have your bet placed in less than 20 minutes. You’ll hear a beep confirming your bet and then you’d have to accept the trade offer, from one of their bots, in Steam.

Dumb automated Tomnod map traverser

Few hours ago I read about the Tomnod challenge. We all know about the missing Malaysia Airlines flight MH370 that vanished from radar while en route to Beijing from Kuala Lumpur. Tomnod came up with this pretty novel idea. They made satellite images of places in the route accessible, so that people can view the map tiles and report if they see something interesting. The webapp is pretty cool and the UI is fine, but I thought it would be cooler if they had a play button. Maybe play over a couple of satellite images and you could view them from a distance and report if something is fishy. At present they require user interaction, you need to press buttons to traverse. What if I want to put this up in a shopping mall on a big screen. People waiting to get their products billed can have an eye on it and can report if they see something interesting. I think this would help get a lot of more people involved. To test this out, I made a little javascript which tries to traverse through the map automatically – without any user inputs.

The Script

var count = 0;
var threshold = 10;
function some_function(){
 if (count > threshold) {
 $(".info-panel-group > p:eq(1) > a:eq(1)").click();
 count = 0;
 var e = jQuery.Event("keyup");
 e.which = 37 + Math.floor((Math.random()*4));;
 jQuery("#map-canvas").trigger( e );
 count += 1;

// load jQuery and execute the main function
var interval_handle = window.setInterval(function() {
}, 10000);
$(".tagger-progress-view").append("Stop Moving");

So, now you could display the tomnod webpage on a large screen, run this piece of javascript on top of that and you get a dumb but an automated map traverser. Views?

How it feels to have your project featured on LifeHacker

It was an average day. I woke up at 11, attended a boring DSA class, came back, worked on IIITD’s website, then on GSF’s website and then on Odyssey’s website. Was bored, planned to doze off, but couldn’t sleep. Thought reading about some projects over github would be fun, opened github and saw this:

Screen Shot 2014-01-28 at 5.20.38 pm

RasPod is one of my old baby projects. Most of it was made in about 2 days as an entry to Raspberry Pi’s Summer coding contest. I haven’t really worked on it since then that means around 1.5 years of inactivity. Getting 20-30 likes out of nowhere is strange for such an old hack. I thought the project was probably shared by one of my popular friends on Google Plus; so, I searched #RasPod on Google Plus and got no new posts. I, then googled “RasPod” to check if someone posted about it in a blog or something.

Screen Shot 2014-01-28 at 5.38.09 pm

I was Ecstatic! Flabbergasted! To see a post featuring RasPod on LifeHacker.

Screen Shot 2014-01-28 at 6.08.38 pm

One of the happiest moments of my life! One of the best tokens of appreciation I have received from anybody! Thank you so much LifeHacker you made my day.

Bucket List:

Get a project featured on LifeHacker
Do laundry
World Domination

Make a facebook group photo collage

IIITD students photo collage

That’s the picture of some of the students registered in the IIITD Students group on facebook. Its generated using a script located here. Here’s how to use the script to generate a collage for your facebook group:

Step 1: Go to Facebook’s graph API explorer and do the following:

1) Click on “Get Access Token”

Screen Shot 2013-12-29 at 9.32.12 pm

2) Check the user_groups permission and click on “Get Access Token”.

You should see a page asking for permissions. Just click “Okay”.

Screen Shot 2013-12-29 at 9.30.58 pm Screen Shot 2013-12-29 at 9.34.13 pm

3) Get the facebook group ID using this.

4) Enter “<group_id>?fields=members” in the graph API prompt.

Screen Shot 2013-12-29 at 9.49.15 pm

Step 2: Copy paste the result into a text file.

Use this script to generate a json file of user profile and their corresponding images.

Step 3: Clone this repo and save the JSON file generated in 5th step as user_photos.json.

Step 4: Just open index.html in your favorite browser and you should have something looking like this.

Invite all your friends to like a page on facebook

The other day, I wanted to invite all of my friends to like my college cultural fest’s page: Odyssey 2014. Now, facebook does have an option to invite all of your friends to an event, but not to a page. So, I had to manually click on a ton of “invite” buttons on the page and I have around 300 friends, so I wasn’t in no way clicking on 300 invite buttons; instead I tried to automate this. And here‘s a script to do that.


Just drag Invite all my friends to your bookmarks and click on it when you are on a page, you want your friends to like.

Video Demonstration

How I fixed the “Hash Sum mismatch” problem in apt.

Today, I installed a new Ubuntu system was trying to set up OpenCV on it. I was getting issues installing libopencv-dev package.

As suggested in ubuntu forums and other places, I tried cleaning my lists and tried to download them again:

apt-get clean
rm -rf /var/lib/apt/lists/*
rm -rf /var/lib/apt/lists/partial/*
apt-get clean
apt-get update

This however, didn’t work for me. I got the same “Hash Sum mismatch” error while downloading the amd64 and i386 list. After spending 2 hours trying to fix this problem. I checked what mirrors, I was using to download my packages, by going to Ubuntu Software Center -> Edit -> Software Sources. It was downloading from the Indian server. Then I tried to check with the mirror, if it was up to date with the main Ubuntu server. And it turns out it wasn’t; it was actually running 1 week behind the server. I then changed the Software Source to the Main Server and it worked.

I have a C program, which loops ‘n’ times calling fork each loop. How many child processes do this program create?

First let’s consider a program which loops 2 times:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define N 2
int main(){
 int i;
 for (i = 1; i <= N; i++) fork();
 printf("Hey! from %d\n", getpid());

This program prints “Hey”, 4 times to the console, hence it creates 3 child processes. Let’s figure out how!

Note: The child processes have their local copies of variables, hence any operations on the variables won’t affect the parent’s copy or the other children’s copy.

1) The parent process (P0) runs through the loop 2 times, hence creating 2 child processes (P1 and P2). These child processes get their local copies of ‘i’ which is 1 for the first child and 2 for the second child. Thus, P2 runs through the loop 1 time while the P1 runs through the loop 2 times.

Note: The child always start executing from the instruction immediately after fork, which is i++ in our case.

2) P2 increments i from 2 to 3, followed by the comparison “i <= N” which obviously evaluates to false. Hence, P2 doesn’t create any processes.

3) P1 increments i from 1 to 2, it then executes fork() which creates another child with i as 2, lets call this child “P1_1″. After that it increments i from 2 to 3 and exits out of the loop.

4) P1_1  increments i from 2 to 3, and exits out of the loop.

So, at the end of the loop we have 3 children (P1, P2, P1_1).

Let’s now figure out the number of children for a program which loops ‘N’ times.
Let Pn be the nth child of the parent process, Pn_n be the nth child of the nth child of the parent process and so on.

Children with ‘i’ as 1: 1 (P1)

Children with ‘i’ as 2: 2 (P1_1, P2)

Children with ‘i’ as 3: 4 (P1_2, P2_1, P3, P1_1_1)

Do you notice a pattern in the above written numbers?

Number of children with ‘i’ as 1: 1

Number of children with ‘i’ as 2: 2^1

Number of children with ‘i’ as 3: 2^2


Number of children with ‘i’ as N: 2^(n-1)

Now we know that sum of G.P is \dfrac {a (r^n - 1)}{r - 1}

Hence, total number of child precesses created is: \dfrac {1 (2^n - 1)}{2 - 1}2^n - 1

I have n fork calls followed by another instruction ‘a’ in a C program. How many times will ‘a’ get executed?

If your answer to the above asked question is \dfrac{(n)(n - 1)}{2} you are wrong. Consider the following C program.

#include <stdlib.h>
#include <stdio.h>
int main(){
 pid_t c1 = fork(); // parent
 pid_t c2 = fork(); // parent, c1
 pid_t c3 = fork(); // parent, c1, child of parent & c2, child of c1 & c2

For 1st fork call we have 1 caller i.e the parent, this creates 1 child that then starts executing the rest of the program.

For 2nd fork call we have 2 callers i.e the parent and c1 this creates 2 children these then start executing the rest of the program.

For 3rd fork call we have 4 callers i.e  the parent, c1, child of parent & c2 and child of c1 & c2.

At the end of 3 fork calls we have 1 (1st fork) + 2 (2nd fork) + 4  (3rd fork) = 7 children and 1 parent. Thus, The printf gets executed 8 (2^3) times. We can easily deduce that the pattern in the number of children is a G.P with 2 as its common ratio.

We know that sum of G.P is \dfrac{a (r^n - 1)}{r - 1} where n is the number of terms, a is the first term and r is the common ratio.

For our case: a = 1, r = 2. Hence the sum = \dfrac{(1) (2^n - 1)}{2 - 1} = 2^n - 1

So, for a program containing n fork calls followed by another instruction ‘a’. The instruction will be executed 2^n times (2^n -1 + 1).

Breaking and fixing ‘pipy_downloads’

Pipy_downloads, a part of PyUtilb, is a utility to calculate the total number of downloads that a particular PyPI package has received across all versions tracked by PyPI. I wanted to check the number of downloads for all pypybox2d packages, so I ran the following command:

pypi_downloads pypybox2d

which broke pypi_downloads and popped out the following error message:

Traceback (most recent call last):
 File "/usr/local/bin/pypi_downloads", line 8, in 
 load_entry_point('pyutilib.misc==5.3.1', 'console_scripts', 'pypi_downloads')()
 File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 166, in main
 _main(args, options.exact)
 File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 147, in _main
 PyPIDownloadAggregator(pkg, exact=exact).stats()
 File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 116, in stats
 keys.sort(key=lambda x: [int(y) for y in x.split('.')])
 File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 116, in 
 keys.sort(key=lambda x: [int(y) for y in x.split('.')])
ValueError: invalid literal for int() with base 10: '1-r331'

Looking into the code, it seems that Pypy-downloads will break for any package with version format other than x.x.x where x represents a number. It can be fixed with a simple hack:

keys.sort(key=lambda x: [int(y) for y in x.strip('-r331').split('.') if y.isdigit()])

Now, it strips off ‘-r331′ from the version number. And also checkes if each x in x.x.x is a digit, if it isn’t a digit it ignores that part of the version number.