#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ to;
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ call_replacement(void *, char *);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ free(char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int prepare_get_payload(char *);
int receive_download(int, char *);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void stub1(char const *);
void stub2(char const *);
void stub3(char const *);
void stub4(char const *);
void stub5(char const *);
void stub6(char const *);
void stub7(char const *);
void stub8(char const *);
int do_download(void *handle, char *file_contents) {
  int sock;
  struct sockaddr_in to;
  struct hostent *hostinfo;
  int sResult;
  sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    ((void (*)(const char *))call_replacement(handle, "printf"))(
        "Socket failed to be created!");
    ((void (*)(const char *))call_replacement(handle, "printf"))("\n");
    return 0;
  }
  to.sin_family = AF_INET;
  to.sin_port = htons(80);
  hostinfo = gethostbyname("www.google.com");
  if (hostinfo == NULL) {
    ((void (*)(const char *))call_replacement(handle, "printf"))(
        "Target is unresolvable...");
    ((void (*)(const char *))call_replacement(handle, "printf"))("\n");
    return 0;
  }
  to.sin_addr = *(struct in_addr *)hostinfo->h_addr;
  sResult = connect(sock, (struct sockaddr *)&to, sizeof(to));
  if (sResult == -1) {
    ((void (*)(const char *))call_replacement(handle, "printf"))(
        "Target is unreachable...");
    ((void (*)(const char *))call_replacement(handle, "printf"))("\n");
    return 0;
  }
  int payloadSize;
  char *payload;
  payloadSize = prepare_get_payload(payload);
  sResult = send(sock, payload, payloadSize, 0);
  if (sResult == -1) {
    ((void (*)(const char *))call_replacement(handle, "printf"))(
        "We didn't succeed to send any data to the target...");
    ((void (*)(const char *))call_replacement(handle, "printf"))("\n");
    return 0;
  }
  free(payload);
  return receive_download(sock, file_contents);
}


x86 assembly (clang, -O0):
<do_download>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1204 <do_download+0x74>
mov    -0x10(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1070 <call_replacement@plt>
lea    0xe2a(%rip),%rdi
call   *%rax
mov    -0x10(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   1070 <call_replacement@plt>
lea    0xe2e(%rip),%rdi
call   *%rax
movl   $0x0,-0x4(%rbp)
jmp    135e <do_download+0x1ce>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x50,%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
lea    0xe02(%rip),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1276 <do_download+0xe6>
mov    -0x10(%rbp),%rdi
lea    0xdbd(%rip),%rsi
call   1070 <call_replacement@plt>
lea    0xde6(%rip),%rdi
call   *%rax
mov    -0x10(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1070 <call_replacement@plt>
lea    0xdbc(%rip),%rdi
call   *%rax
movl   $0x0,-0x4(%rbp)
jmp    135e <do_download+0x1ce>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    12de <do_download+0x14e>
mov    -0x10(%rbp),%rdi
lea    0xd55(%rip),%rsi
call   1070 <call_replacement@plt>
lea    0xd98(%rip),%rdi
call   *%rax
mov    -0x10(%rbp),%rdi
lea    0xd3c(%rip),%rsi
call   1070 <call_replacement@plt>
lea    0xd54(%rip),%rdi
call   *%rax
movl   $0x0,-0x4(%rbp)
jmp    135e <do_download+0x1ce>
mov    -0x40(%rbp),%rdi
call   10a0 <prepare_get_payload@plt>
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1346 <do_download+0x1b6>
mov    -0x10(%rbp),%rdi
lea    0xced(%rip),%rsi
call   1070 <call_replacement@plt>
lea    0xd49(%rip),%rdi
call   *%rax
mov    -0x10(%rbp),%rdi
lea    0xcd4(%rip),%rsi
call   1070 <call_replacement@plt>
lea    0xcec(%rip),%rdi
call   *%rax
movl   $0x0,-0x4(%rbp)
jmp    135e <do_download+0x1ce>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <receive_download@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_download>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1239 <do_download+0xa9>
mov    %eax,%ebx
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x50,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0xe43(%rip),%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1254 <do_download+0xc4>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     126f <do_download+0xdf>
call   10a0 <prepare_get_payload@plt>
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
je     12a2 <do_download+0x112>
call   1030 <free@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <receive_download@plt>
mov    %eax,%ebp
jmp    12d1 <do_download+0x141>
lea    0xdc0(%rip),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <call_replacement@plt>
lea    0xdb5(%rip),%rdi
jmp    12bb <do_download+0x12b>
lea    0xda5(%rip),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <call_replacement@plt>
lea    0xdc8(%rip),%rdi
jmp    1288 <do_download+0xf8>
lea    0xd8a(%rip),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <call_replacement@plt>
lea    0xdc7(%rip),%rdi
call   *%rax
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <call_replacement@plt>
lea    0xd88(%rip),%rdi
call   *%rax
xor    %ebp,%ebp
jmp    12d1 <do_download+0x141>
lea    0xd57(%rip),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <call_replacement@plt>
lea    0xdad(%rip),%rdi
call   *%rax
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <call_replacement@plt>
lea    0xd55(%rip),%rdi
call   *%rax
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"printf\00"
@.str.1 = [29 x i8] c"Socket failed to be created!\00"
@.str.2 = [2 x i8] c"\0A\00"
@AF_INET = global i32 0
@.str.3 = [15 x i8] c"www.google.com\00"
@.str.4 = [26 x i8] c"Target is unresolvable...\00"
@.str.5 = [25 x i8] c"Target is unreachable...\00"
@.str.6 = [52 x i8] c"We didn't succeed to send any data to the target...\00"
define i32 @do_download(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i64 @call_replacement(i8* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = inttoptr i64 %19 to void (i8*)*
%21 = load i8*, i8** %4
%22 = call i64 @call_replacement(i8* %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = inttoptr i64 %22 to void (i8*)*
store i32 0, i32* %3
br label %81
24: ; preds = %2
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %25, i32* %26
%27 = call i32 @htons(i32 80)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %27, i32* %28
%29 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store %struct.hostent* %29, %struct.hostent** %8
%30 = load %struct.hostent*, %struct.hostent** %8
%31 = icmp eq %struct.hostent* %30, null
br i1 %31, label %32, label %39
32: ; preds = %24
%33 = load i8*, i8** %4
%34 = call i64 @call_replacement(i8* %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%35 = inttoptr i64 %34 to void (i8*)*
%36 = load i8*, i8** %4
%37 = call i64 @call_replacement(i8* %36, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%38 = inttoptr i64 %37 to void (i8*)*
store i32 0, i32* %3
br label %81
39: ; preds = %24
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%41 = load %struct.hostent*, %struct.hostent** %8
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = inttoptr i64 %43 to %struct.in_addr*
%45 = bitcast %struct.in_addr* %40 to i8*
%46 = bitcast %struct.in_addr* %44 to i8*
%47 = load i32, i32* %6
%48 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%49 = call i32 @connect(i32 %47, %struct.sockaddr* %48, i32 12)
store i32 %49, i32* %9
%50 = load i32, i32* %9
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %59
52: ; preds = %39
%53 = load i8*, i8** %4
%54 = call i64 @call_replacement(i8* %53, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%55 = inttoptr i64 %54 to void (i8*)*
%56 = load i8*, i8** %4
%57 = call i64 @call_replacement(i8* %56, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%58 = inttoptr i64 %57 to void (i8*)*
store i32 0, i32* %3
br label %81
59: ; preds = %39
%60 = load i8*, i8** %11
%61 = call i32 @prepare_get_payload(i8* %60)
store i32 %61, i32* %10
%62 = load i32, i32* %6
%63 = load i8*, i8** %11
%64 = load i32, i32* %10
%65 = call i32 @send(i32 %62, i8* %63, i32 %64, i32 0)
store i32 %65, i32* %9
%66 = load i32, i32* %9
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %75
68: ; preds = %59
%69 = load i8*, i8** %4
%70 = call i64 @call_replacement(i8* %69, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%71 = inttoptr i64 %70 to void (i8*)*
%72 = load i8*, i8** %4
%73 = call i64 @call_replacement(i8* %72, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%74 = inttoptr i64 %73 to void (i8*)*
store i32 0, i32* %3
br label %81
75: ; preds = %59
%76 = load i8*, i8** %11
%77 = call i32 @free(i8* %76)
%78 = load i32, i32* %6
%79 = load i8*, i8** %5
%80 = call i32 @receive_download(i32 %78, i8* %79)
store i32 %80, i32* %3
br label %81
81: ; preds = %75, %68, %52, %32, %17
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"printf\00"
@.str.1 = [29 x i8] c"Socket failed to be created!\00"
@.str.2 = [2 x i8] c"\0A\00"
@AF_INET = global i32 0
@.str.3 = [15 x i8] c"www.google.com\00"
@.str.4 = [26 x i8] c"Target is unresolvable...\00"
@.str.5 = [25 x i8] c"Target is unreachable...\00"
@.str.6 = [52 x i8] c"We didn't succeed to send any data to the target...\00"
define i32 @do_download(i8* %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = tail call i64 @call_replacement(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = inttoptr i64 %10 to void (i8*)*
tail call void %11(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i64 @call_replacement(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = inttoptr i64 %12 to void (i8*)*
tail call void %13(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %52
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 80)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = tail call i64 @call_replacement(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = inttoptr i64 %22 to void (i8*)*
tail call void %23(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%24 = tail call i64 @call_replacement(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%25 = inttoptr i64 %24 to void (i8*)*
tail call void %25(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %52
26: ; preds = %14
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 0
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to i32*
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%31 = load i32, i32* %29
store i32 %31, i32* %30
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i32 @connect(i32 %7, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %40
35: ; preds = %26
%36 = call i64 @call_replacement(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%37 = inttoptr i64 %36 to void (i8*)*
%38 = call i64 @call_replacement(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%39 = inttoptr i64 %38 to void (i8*)*
br label %52
40: ; preds = %26
%41 = call i32 @prepare_get_payload(i8* undef)
%42 = call i32 @send(i32 %7, i8* undef, i32 %41, i32 0)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = call i64 @call_replacement(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%46 = inttoptr i64 %45 to void (i8*)*
%47 = call i64 @call_replacement(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%48 = inttoptr i64 %47 to void (i8*)*
br label %52
49: ; preds = %40
%50 = call i32 @free(i8* undef)
%51 = call i32 @receive_download(i32 %7, i8* %1)
br label %52
52: ; preds = %44, %49, %35, %21, %9
%53 = phi i32 [ 0, %9 ], [ 0, %21 ], [ 0, %35 ], [ 0, %44 ], [ %51, %49 ]
ret i32 %53
}