#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 /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ get_domain(char *, char **, char **, char **);
int /*<<< orphan*/ hostname_to_ip(char *, struct sockaddr_in *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  char buffer[1025];
  int portNum, sockfd = 0;
  struct sockaddr_in serv_addr;
  char *domain, *destination, *file;
  if (argc < 2) {
    portNum = atoi(argv[2]);
  } else {
    portNum = 80;
  }
  get_domain(argv[1], &domain, &destination, &file);
  printf("%s\n%s\n%s\n", domain, destination, file);
  hostname_to_ip(domain, &serv_addr);
  printf("%s resolved %s\n", domain, inet_ntoa(serv_addr.sin_addr));
  printf("Connecting to server\n");
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("\n Error : Could not create socket \n");
    return 1;
  }
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    printf("ERROR cannot connect to server\n");
    return 1;
  }
  printf("Connected\n");
  printf("Disconnecting...\n");
  close(sockfd);
  printf("Disconnected\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x428(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x424(%rbp)
jmp    11cf <main+0x4f>
movl   $0x50,-0x424(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x438(%rbp),%rsi
lea    -0x440(%rbp),%rdx
lea    -0x448(%rbp),%rcx
call   1050 <get_domain@plt>
mov    -0x438(%rbp),%rsi
mov    -0x440(%rbp),%rdx
mov    -0x448(%rbp),%rcx
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x438(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1070 <hostname_to_ip@plt>
mov    -0x438(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x430(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    -0x450(%rbp),%rsi
mov    %rax,%rdx
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x428(%rbp)
cmp    $0x0,%eax
jge    12a8 <main+0x128>
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131f <main+0x19f>
mov    -0x428(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    12e3 <main+0x163>
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131f <main+0x19f>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x428(%rbp),%edi
call   1060 <close@plt>
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    0x8(%rsi),%rdi
lea    0x10(%rsp),%rsi
lea    0x20(%rsp),%rdx
lea    0x18(%rsp),%rcx
call   1060 <get_domain@plt>
mov    0x10(%rsp),%rsi
mov    0x20(%rsp),%rdx
mov    0x18(%rsp),%rcx
lea    0xe4c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1080 <hostname_to_ip@plt>
mov    0x10(%rsp),%rbx
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xe2b(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0x1,%ebx
test   %eax,%eax
js     125c <main+0xdc>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1265 <main+0xe5>
lea    0xdf6(%rip),%rdi
call   1030 <puts@plt>
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %ebx,%ebx
lea    0xdf0(%rip),%rdi
jmp    126c <main+0xec>
lea    0xe13(%rip),%rdi
jmp    126c <main+0xec>
lea    0xdeb(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"%s\0A%s\0A%s\0A\00"
@.str.1 = [16 x i8] c"%s resolved %s\0A\00"
@.str.2 = [22 x i8] c"Connecting to server\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [36 x i8] c"\0A Error : Could not create socket \0A\00"
@.str.4 = [32 x i8] c"ERROR cannot connect to server\0A\00"
@.str.5 = [11 x i8] c"Connected\0A\00"
@.str.6 = [18 x i8] c"Disconnecting...\0A\00"
@.str.7 = [14 x i8] c"Disconnected\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1025 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %7
br label %21
20: ; preds = %2
store i32 80, i32* %7
br label %21
21: ; preds = %20, %15
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @get_domain(i8* %24, i8** %10, i8** %11, i8** %12)
%26 = load i8*, i8** %10
%27 = load i8*, i8** %11
%28 = load i8*, i8** %12
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27, i8* %28)
%30 = load i8*, i8** %10
%31 = call i32 @hostname_to_ip(i8* %30, %struct.sockaddr_in* %9)
%32 = load i8*, i8** %10
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i8* @inet_ntoa(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @AF_INET
%39 = load i32, i32* @SOCK_STREAM
%40 = call i32 @socket(i32 %38, i32 %39, i32 0)
store i32 %40, i32* %8
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %21
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %57
44: ; preds = %21
%45 = load i32, i32* %8
%46 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%47 = call i64 @connect(i32 %45, %struct.sockaddr* %46, i32 4)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %57
51: ; preds = %44
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
br label %57
57: ; preds = %51, %49, %42
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"%s\0A%s\0A%s\0A\00"
@.str.1 = [16 x i8] c"%s resolved %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@str = [21 x i8] c"Connecting to server\00"
@str.8 = [10 x i8] c"Connected\00"
@str.9 = [17 x i8] c"Disconnecting...\00"
@str.10 = [13 x i8] c"Disconnected\00"
@str.11 = [31 x i8] c"ERROR cannot connect to server\00"
@str.12 = [35 x i8] c"\0A Error : Could not create socket \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i8** %4 to i8*
%9 = bitcast i8** %5 to i8*
%10 = bitcast i8** %6 to i8*
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = call i32 @get_domain(i8* %12, i8** %4, i8** %5, i8** %6)
%14 = load i8*, i8** %4
%15 = load i8*, i8** %5
%16 = load i8*, i8** %6
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %15, i8* %16)
%18 = load i8*, i8** %4
%19 = call i32 @hostname_to_ip(i8* %18, %struct.sockaddr_in* %3)
%20 = load i8*, i8** %4
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i8* @inet_ntoa(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* %23)
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%26 = load i32, i32* @AF_INET
%27 = load i32, i32* @SOCK_STREAM
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %38, label %30
30: ; preds = %2
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i64 @connect(i32 %28, %struct.sockaddr* %31, i32 4)
%33 = icmp slt i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.8, i64 0, i64 0))
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.9, i64 0, i64 0))
%37 = call i32 @close(i32 %28)
br label %38
38: ; preds = %30, %2, %34
%39 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @str.10, i64 0, i64 0), %34 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @str.12, i64 0, i64 0), %2 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @str.11, i64 0, i64 0), %30 ]
%40 = phi i32 [ 0, %34 ], [ 1, %2 ], [ 1, %30 ]
%41 = call i32 @puts(i8* dereferenceable(1) %39)
ret i32 %40
}