// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rsi, %r14
pushq $0x11
popq %rdi
pushq $0x1
popq %rsi
callq 0x20a0
xorl %ebx, %ebx
cmpl $0x3, %ebx
je 0x23b2
callq 0x2180
testl %eax, %eax
je 0x23be
incl %ebx
jmp 0x23a0
xorl %edi, %edi
callq 0x2030
jmp 0x25e2
leaq 0xc3f(%rip), %rsi       # 0x3004
leaq -0x180(%rbp), %r15
movq %r15, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x21a0
movq (%r14), %rax
movaps (%r15), %xmm0
movaps 0x10(%r15), %xmm1
movups %xmm0, (%rax)
movups %xmm1, 0x10(%rax)
leaq 0xc2d(%rip), %rsi       # 0x301f
leaq -0xc0(%rbp), %r14
leaq -0x160(%rbp), %rdx
movq %r14, %rdi
callq 0x2646
leaq -0x90(%rbp), %rdi
movq %r14, %rsi
callq 0x20e0
leaq -0x160(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x2748
leaq -0x90(%rbp), %rdi
callq 0x2120
leaq 0xbf9(%rip), %rdx       # 0x3036
leaq -0x160(%rbp), %rdi
movl $0x2762, %esi           # imm = 0x2762
callq 0x2986
leaq 0xbe1(%rip), %rdx       # 0x3036
leaq -0x160(%rbp), %rdi
movl $0x272f, %esi           # imm = 0x272F
callq 0x2986
leaq -0x160(%rbp), %rdi
callq 0x28be
movl $0x400, %edi            # imm = 0x400
callq 0x2150
leaq -0x30(%rbp), %rdx
movq %rax, (%rdx)
leaq -0x160(%rbp), %rdi
movl $0x100001, %esi         # imm = 0x100001
callq 0x290e
movq -0x30(%rbp), %rsi
leaq 0xba3(%rip), %rdi       # 0x3042
xorl %eax, %eax
callq 0x2060
leaq -0x70(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x2944
leaq -0x160(%rbp), %rdi
callq 0x297c
movq %rax, %r14
movq -0x70(%rbp), %rsi
leaq 0xb89(%rip), %rdi       # 0x3059
xorl %eax, %eax
callq 0x2060
leaq 0xb94(%rip), %rdi       # 0x3072
movq %r14, %rsi
xorl %eax, %eax
callq 0x2060
leaq -0x160(%rbp), %rdi
callq 0x291e
testl %eax, %eax
je 0x2525
leaq -0x50(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x2928
leaq -0x50(%rbp), %rbx
movq (%rbx), %rsi
leaq 0xbb9(%rip), %rdi       # 0x30cf
xorl %eax, %eax
callq 0x2060
movq %rbx, %rdi
jmp 0x25b4
leaq -0x50(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x2960
leaq 0xb4a(%rip), %rsi       # 0x3086
leaq -0x9a(%rbp), %r15
movq %r15, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x21a0
leaq 0xb3b(%rip), %rsi       # 0x3091
movq %r15, %rdi
callq 0x2200
movq %rax, %r12
leaq -0x50(%rbp), %rax
movq (%rax), %r13
movq %r13, %rdi
callq 0x20b0
movq %rax, %r14
pushq $0x1
popq %rdx
movq %r13, %rdi
movq %rax, %rsi
movq %r12, %rcx
callq 0x21b0
leaq 0xb08(%rip), %rdi       # 0x3093
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2060
movq %r12, %rdi
callq 0x20c0
leaq 0xb03(%rip), %rdi       # 0x30aa
movl %ebx, %esi
xorl %eax, %eax
callq 0x2060
leaq -0x50(%rbp), %rdi
callq 0x2120
pushq $0xa
popq %rdi
callq 0x21c0
leaq -0x70(%rbp), %rdi
callq 0x2120
leaq -0x160(%rbp), %rdi
callq 0x29c0
leaq -0xc0(%rbp), %rdi
callq 0x2120
xorl %eax, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2614
jmp 0x2614
jmp 0x2622
jmp 0x2622
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x2120
jmp 0x2631
movq %rax, %rbx
jmp 0x2631
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x2120
jmp 0x2625
movq %rax, %rbx
leaq -0x160(%rbp), %rdi
callq 0x29c0
leaq -0xc0(%rbp), %rdi
callq 0x2120
movq %rbx, %rdi
callq 0x20f0
nop