#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 */
struct sockaddr_in {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int connect_to_socket(char *, struct sockaddr_in *, int);
scalar_t__ download_file(int, char *);
struct hostent *gethostbyname(char *);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    puts("usage: ./download <url> <filepath>");
    return 1;
  }
  struct hostent *h = gethostbyname(argv[1]);
  char *ip = inet_ntoa(*((struct in_addr *)h->h_addr));
  struct sockaddr_in dest;
  int my_socket;
  if ((my_socket = connect_to_socket(ip, &dest, 80)) == -1) {
    perror("couldn't connect. ");
    return 1;
  }
  puts("connected.");
  if (download_file(my_socket, argv[2])) {
    perror("unable to download");
    return 1;
  }
  puts("download completed");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1198 <main+0x38>
lea    0xe79(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1242 <main+0xe2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x50,%edx
call   1030 <connect_to_socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    11f1 <main+0x91>
lea    0xe43(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1242 <main+0xe2>
lea    0xe3e(%rip),%rdi
call   1040 <puts@plt>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1060 <download_file@plt>
cmp    $0x0,%rax
je     122f <main+0xcf>
lea    0xe23(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1242 <main+0xe2>
lea    0xe1e(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     117b <main+0x1b>
lea    0xe91(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    11d2 <main+0x72>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1070 <gethostbyname@plt>
mov    (%rax),%rax
mov    (%rax),%edi
call   1050 <inet_ntoa@plt>
mov    %rsp,%rsi
mov    %rax,%rdi
mov    $0x50,%edx
call   1030 <connect_to_socket@plt>
cmp    $0xffffffff,%eax
je     11d9 <main+0x79>
mov    %eax,%ebp
lea    0xe87(%rip),%rdi
call   1040 <puts@plt>
mov    0x10(%rbx),%rsi
mov    %ebp,%edi
call   1060 <download_file@plt>
test   %rax,%rax
jne    11e2 <main+0x82>
lea    0xe89(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe43(%rip),%rdi
jmp    11e9 <main+0x89>
lea    0xe58(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    11d2 <main+0x72>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { i32 }
%struct.in_addr = type { i32 }
@.str = [35 x i8] c"usage: ./download <url> <filepath>\00"
@.str.1 = [19 x i8] c"couldn't connect. \00"
@.str.2 = [11 x i8] c"connected.\00"
@.str.3 = [19 x i8] c"unable to download\00"
@.str.4 = [19 x i8] c"download completed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.hostent*
%7 = alloca i8*
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @puts(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %43
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %6
%19 = load %struct.hostent*, %struct.hostent** %6
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to %struct.in_addr*
%23 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i8* @inet_ntoa(i32 %24)
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = call i32 @connect_to_socket(i8* %26, %struct.sockaddr_in* %8, i32 80)
store i32 %27, i32* %9
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %14
%30 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %43
31: ; preds = %14
%32 = call i32 @puts(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* %9
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i64 @download_file(i32 %33, i8* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %43
41: ; preds = %31
%42 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %43
43: ; preds = %41, %39, %29, %12
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
@.str = [35 x i8] c"usage: ./download <url> <filepath>\00"
@.str.1 = [19 x i8] c"couldn't connect. \00"
@.str.2 = [11 x i8] c"connected.\00"
@.str.3 = [19 x i8] c"unable to download\00"
@.str.4 = [19 x i8] c"download completed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %34
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call %struct.hostent* @gethostbyname(i8* %9)
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%12 = load i64, i64* %11
%13 = inttoptr i64 %12 to %struct.in_addr*
%14 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %13, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i8* @inet_ntoa(i32 %15)
%17 = bitcast %struct.sockaddr_in* %3 to i8*
%18 = call i32 @connect_to_socket(i8* %16, %struct.sockaddr_in* %3, i32 80)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %7
%21 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %32
22: ; preds = %7
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = call i64 @download_file(i32 %18, i8* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %32
30: ; preds = %22
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %28, %20
%33 = phi i32 [ 1, %20 ], [ 1, %28 ], [ 0, %30 ]
br label %34
34: ; preds = %32, %5
%35 = phi i32 [ 1, %5 ], [ %33, %32 ]
ret i32 %35
}