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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_server(char *serverIP) {
  struct sockaddr_in serv_addr;
  int sockfd = 0;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("\n Error : Could not create socket \n");
    return 1;
  }
  memset(&serv_addr, '0', sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(5000);
  if (inet_pton(AF_INET, serverIP, &serv_addr.sin_addr) <= 0) {
    printf("\n inet_pton error occured\n");
    return 1;
  }
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    printf("\n Error : Connect Failed \n");
    return 1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11b0 <connect_to_server+0x50>
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    124d <connect_to_server+0xed>
lea    -0x20(%rbp),%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x1388,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <inet_pton@plt>
cmp    $0x0,%rax
jg     1214 <connect_to_server+0xb4>
lea    0xe21(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    124d <connect_to_server+0xed>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    1247 <connect_to_server+0xe7>
lea    0xe09(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    124d <connect_to_server+0xed>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e73(%rip),%r12
mov    (%r12),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11ef <connect_to_server+0x8f>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    $0x1388,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%r12),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <inet_pton@plt>
test   %rax,%rax
jle    11f8 <connect_to_server+0x98>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
jns    1209 <connect_to_server+0xa9>
lea    0xe13(%rip),%rdi
jmp    11ff <connect_to_server+0x9f>
lea    0xe3e(%rip),%rdi
jmp    11ff <connect_to_server+0x9f>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [36 x i8] c"\0A Error : Could not create socket \0A\00"
@.str.1 = [27 x i8] c"\0A inet_pton error occured\0A\00"
@.str.2 = [27 x i8] c"\0A Error : Connect Failed \0A\00"
define i32 @connect_to_server(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %5
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %34
12: ; preds = %1
%13 = call i32 @memset(%struct.sockaddr_in* %4, i32 48, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 5000)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = load i8*, i8** %3
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%21 = call i64 @inet_pton(i32 %18, i8* %19, i32* %20)
%22 = icmp sle i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %12
%24 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %34
25: ; preds = %12
%26 = load i32, i32* %5
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i64 @connect(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %34
32: ; preds = %25
%33 = load i32, i32* %5
store i32 %33, i32* %2
br label %34
34: ; preds = %32, %30, %23, %10
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@str = [26 x i8] c"\0A Error : Connect Failed \00"
@str.3 = [26 x i8] c"\0A inet_pton error occured\00"
@str.4 = [35 x i8] c"\0A Error : Could not create socket \00"
define i32 @connect_to_server(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.4, i64 0, i64 0))
br label %28
10: ; preds = %1
%11 = call i32 @memset(%struct.sockaddr_in* %2, i32 48, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @htons(i32 5000)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%18 = call i64 @inet_pton(i32 %16, i8* %0, i32* %17)
%19 = icmp slt i64 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.3, i64 0, i64 0))
br label %28
22: ; preds = %10
%23 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%24 = call i64 @connect(i32 %6, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %22, %26, %20, %8
%29 = phi i32 [ 1, %8 ], [ 1, %20 ], [ 1, %26 ], [ %6, %22 ]
ret i32 %29
}