// File: ammarfaizi2[P]TeaCurlCpp/main.cpp
// Function: main
int main(int argc, char *argv[]) {

	// Prevent defunct
	signal(SIGCHLD, SIG_IGN);

	for(int i = 0; i < 3; i++) {
		int pid = fork();
		if (pid == 0) {

			char process_name[32];
			sprintf(process_name, "child_%d --daemon --no-tty", i);
			memcpy(argv[0], process_name, sizeof(process_name));

			std::string url = "https://www.google.com";
			
			TeaCurl tch(url);
			tch.setOpt(CURLOPT_COOKIEJAR, "/tmp/cookie");
			tch.setOpt(CURLOPT_COOKIEFILE, "/tmp/cookie");
			tch.exec();
			
			char *effectiveURL = (char*)malloc(1024);
			tch.getInfo(CURLINFO_EFFECTIVE_URL, &effectiveURL);

			printf("Got effective URL: %s\n", effectiveURL);

			std::string responseHeader = tch.getResponseHeader();
			long httpCode = tch.getHttpCode();

			printf("Got response header:\n%s\n", responseHeader.c_str());
			printf("Got http code: %ld\n", httpCode);

			if (tch.getRes() == CURLE_OK) {
				std::string body = tch.getBody();
				char filename[10];
				sprintf(filename, "out_%d.tmp", i);
				FILE *h = fopen(filename, "w");
				const char *cstrBody = body.c_str();

				size_t length = strlen(cstrBody);
				fwrite(cstrBody, length, 1, h);
				printf("Wrote %ld bytes to %s\n", length, filename);
				fclose(h);
				printf("Child %d has finished its execution\n", i);
			} else {
				printf("An error occued: %s\n", tch.getError().c_str());
			}
			sleep(10);
			return 0;
		}
	}

	wait(NULL);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x11, %edi
movl $0x1, %esi
callq 0x20f0
movl $0x0, -0x14(%rbp)
cmpl $0x3, -0x14(%rbp)
jge 0x28a1
callq 0x2240
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x2891
movl -0x14(%rbp), %edx
leaq 0x1b3f(%rip), %rsi      # 0x4004
xorl %eax, %eax
leaq -0x40(%rbp), %rdi
callq 0x2260
movq -0x10(%rbp), %rax
movq (%rax), %rax
movaps -0x40(%rbp), %xmm0
movaps -0x30(%rbp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x61(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x2210
movq -0x1d8(%rbp), %rdx
leaq 0x1b1b(%rip), %rsi      # 0x401f
leaq -0x60(%rbp), %rdi
callq 0x28d0
jmp 0x250f
leaq -0x61(%rbp), %rdi
callq 0x22e0
leaq -0x138(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x20a0
jmp 0x252a
leaq -0x118(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x2c60
jmp 0x253f
leaq -0x138(%rbp), %rdi
callq 0x2180
leaq 0x1ae4(%rip), %rdx      # 0x4036
leaq -0x118(%rbp), %rdi
movl $0x2762, %esi           # imm = 0x2762
callq 0x2fc0
jmp 0x2565
leaq 0x1aca(%rip), %rdx      # 0x4036
leaq -0x118(%rbp), %rdi
movl $0x272f, %esi           # imm = 0x272F
callq 0x2fc0
jmp 0x257f
leaq -0x118(%rbp), %rdi
callq 0x2e40
jmp 0x258d
movl $0x400, %edi            # imm = 0x400
callq 0x21e0
movq %rax, -0x140(%rbp)
leaq -0x118(%rbp), %rdi
movl $0x100001, %esi         # imm = 0x100001
leaq -0x140(%rbp), %rdx
callq 0x2eb0
jmp 0x25b8
movq -0x140(%rbp), %rsi
leaq 0x1a7c(%rip), %rdi      # 0x4042
xorl %eax, %eax
callq 0x2070
jmp 0x25cf
leaq -0x160(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x2f30
jmp 0x25e4
leaq -0x118(%rbp), %rdi
callq 0x2fa0
movq %rax, -0x1e0(%rbp)
jmp 0x25f9
movq -0x1e0(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x2090
movq %rax, %rsi
leaq 0x1a3c(%rip), %rdi      # 0x4059
xorl %eax, %eax
callq 0x2070
jmp 0x2626
movq -0x168(%rbp), %rsi
leaq 0x1a3e(%rip), %rdi      # 0x4072
xorl %eax, %eax
callq 0x2070
jmp 0x263d
leaq -0x118(%rbp), %rdi
callq 0x2ee0
movl %eax, -0x1e4(%rbp)
jmp 0x2651
movl -0x1e4(%rbp), %eax
cmpl $0x0, %eax
jne 0x27dc
leaq -0x188(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x2f70
jmp 0x2675
movl -0x14(%rbp), %edx
leaq 0x1a07(%rip), %rsi      # 0x4086
xorl %eax, %eax
leaq -0x192(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x2260
movq -0x1f8(%rbp), %rdi
leaq 0x19ef(%rip), %rsi      # 0x4091
callq 0x22d0
movq %rax, -0x1f0(%rbp)
jmp 0x26b0
movq -0x1f0(%rbp), %rax
movq %rax, -0x1a0(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x2090
movq %rax, -0x1a8(%rbp)
movq -0x1a8(%rbp), %rdi
callq 0x2100
movq %rax, -0x1b0(%rbp)
movq -0x1a8(%rbp), %rdi
movq -0x1b0(%rbp), %rsi
movq -0x1a0(%rbp), %rcx
movl $0x1, %edx
callq 0x2270
jmp 0x2705
movq -0x1b0(%rbp), %rsi
leaq 0x1980(%rip), %rdi      # 0x4093
xorl %eax, %eax
leaq -0x192(%rbp), %rdx
callq 0x2070
jmp 0x2723
movq -0x1a0(%rbp), %rdi
callq 0x2130
jmp 0x2731
movl -0x14(%rbp), %esi
leaq 0x196f(%rip), %rdi      # 0x40aa
xorl %eax, %eax
callq 0x2070
jmp 0x2744
leaq -0x188(%rbp), %rdi
callq 0x2180
jmp 0x2838
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x61(%rbp), %rdi
callq 0x22e0
jmp 0x28bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x2886
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x2180
jmp 0x2886
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x287a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x286e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x2180
jmp 0x286e
leaq -0x1d0(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x2f00
jmp 0x27f1
leaq -0x1d0(%rbp), %rdi
callq 0x2090
movq %rax, %rsi
leaq 0x18c8(%rip), %rdi      # 0x40cf
xorl %eax, %eax
callq 0x2070
jmp 0x2810
leaq -0x1d0(%rbp), %rdi
callq 0x2180
jmp 0x2838
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x2180
jmp 0x286e
movl $0xa, %edi
callq 0x2280
jmp 0x2844
movl $0x0, -0x4(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x2180
leaq -0x118(%rbp), %rdi
callq 0x3040
leaq -0x60(%rbp), %rdi
callq 0x2180
jmp 0x28b1
leaq -0x160(%rbp), %rdi
callq 0x2180
leaq -0x118(%rbp), %rdi
callq 0x3040
leaq -0x60(%rbp), %rdi
callq 0x2180
jmp 0x28bd
jmp 0x2893
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x249f
xorl %eax, %eax
movl %eax, %edi
callq 0x2030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x2150
nopw %cs:(%rax,%rax)