#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*/ uint32_t;
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ reuse;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ debug_print(char *, ...);
int /*<<< orphan*/ errno;
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_server(char *serverIp, uint32_t serverPort) {
  struct hostent *he;
  struct in_addr inAddr;
  int sock = -1;
  int reuse = 1;
  int res = 0;
  struct sockaddr_in sinAddr;
  memset(&sinAddr, 0, sizeof(struct sockaddr_in));
  he = gethostbyname(serverIp);
  if (!he) {
    if (inet_aton(serverIp, &inAddr) == 0) {
      debug_print("ERROR: Unknown server address!\n");
      return -1;
    }
  } else {
    inAddr = *((struct in_addr *)he->h_addr);
  }
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    debug_print("ERROR: Failed to open socket!\n");
    return -1;
  }
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
  sinAddr.sin_family = AF_INET;
  sinAddr.sin_addr = inAddr;
  sinAddr.sin_port = htons(serverPort);
  res = connect(sock, (struct sockaddr *)&sinAddr, sizeof(struct sockaddr));
  if (res < 0) {
    debug_print("ERROR: Failed to connect socket! errno=%d\n", errno);
    perror("Could not connect socket");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<connect_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
movl   $0x1,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1212 <connect_to_server+0x82>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%rax
jne    120d <connect_to_server+0x7d>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1090 <debug_print@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <connect_to_server+0x162>
jmp    121e <connect_to_server+0x8e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    125e <connect_to_server+0xce>
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1090 <debug_print@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <connect_to_server+0x162>
mov    -0x2c(%rbp),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <connect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12ec <connect_to_server+0x15c>
mov    0x2d04(%rip),%rax
mov    (%rax),%esi
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1090 <debug_print@plt>
lea    0xd8f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <connect_to_server+0x162>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
movl   $0x1,0xc(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     11cf <connect_to_server+0x3f>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
jmp    11e5 <connect_to_server+0x55>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <inet_aton@plt>
test   %rax,%rax
je     1285 <connect_to_server+0xf5>
mov    0x2df4(%rip),%rbx
mov    (%rbx),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     127c <connect_to_server+0xec>
mov    %eax,%ebp
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%rsp),%eax
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   10a0 <connect@plt>
test   %eax,%eax
jns    1298 <connect_to_server+0x108>
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdi
xor    %eax,%eax
call   1090 <debug_print@plt>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
jmp    1293 <connect_to_server+0x103>
lea    0xd9d(%rip),%rdi
jmp    128c <connect_to_server+0xfc>
lea    0xd74(%rip),%rdi
xor    %eax,%eax
call   1090 <debug_print@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.sockaddr = type { i32 }
@.str = [32 x i8] c"ERROR: Unknown server address!\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [31 x i8] c"ERROR: Failed to open socket!\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [43 x i8] c"ERROR: Failed to connect socket! errno=%d\0A\00"
@errno = global i32 0
@.str.3 = [25 x i8] c"Could not connect socket\00"
define i32 @connect_to_server(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca %struct.in_addr
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 -1, i32* %8
store i32 1, i32* %9
store i32 0, i32* %10
%12 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 12)
%13 = load i8*, i8** %4
%14 = call %struct.hostent* @gethostbyname(i8* %13)
store %struct.hostent* %14, %struct.hostent** %6
%15 = load %struct.hostent*, %struct.hostent** %6
%16 = icmp ne %struct.hostent* %15, null
br i1 %16, label %24, label %17
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i64 @inet_aton(i8* %18, %struct.in_addr* %7)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @debug_print(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %64
23: ; preds = %17
br label %31
24: ; preds = %2
%25 = load %struct.hostent*, %struct.hostent** %6
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 0
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to %struct.in_addr*
%29 = bitcast %struct.in_addr* %7 to i8*
%30 = bitcast %struct.in_addr* %28 to i8*
br label %31
31: ; preds = %24, %23
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* @SOCK_STREAM
%34 = call i32 @socket(i32 %32, i32 %33, i32 0)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 (i8*, ...) @debug_print(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %64
39: ; preds = %31
%40 = load i32, i32* %8
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_REUSEADDR
%43 = bitcast i32* %9 to i8*
%44 = call i32 @setsockopt(i32 %40, i32 %41, i32 %42, i8* %43, i32 4)
%45 = load i32, i32* @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %45, i32* %46
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%48 = bitcast %struct.in_addr* %47 to i8*
%49 = bitcast %struct.in_addr* %7 to i8*
%50 = load i32, i32* %5
%51 = call i32 @htons(i32 %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %51, i32* %52
%53 = load i32, i32* %8
%54 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%55 = call i32 @connect(i32 %53, %struct.sockaddr* %54, i32 4)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %39
%59 = load i32, i32* @errno
%60 = call i32 (i8*, ...) @debug_print(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %59)
%61 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %64
62: ; preds = %39
%63 = load i32, i32* %8
store i32 %63, i32* %3
br label %64
64: ; preds = %62, %58, %37, %21
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [32 x i8] c"ERROR: Unknown server address!\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [31 x i8] c"ERROR: Failed to open socket!\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [43 x i8] c"ERROR: Failed to connect socket! errno=%d\0A\00"
@errno = global i32 0
@.str.3 = [25 x i8] c"Could not connect socket\00"
define i32 @connect_to_server(i8* %0, i32 %1) {
%3 = alloca %struct.in_addr
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.in_addr* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%10 = call %struct.hostent* @gethostbyname(i8* %0)
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = call i64 @inet_aton(i8* %0, %struct.in_addr* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = call i32 (i8*, ...) @debug_print(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %48
17: ; preds = %2
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%19 = load i64, i64* %18
%20 = inttoptr i64 %19 to i32*
%21 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
%22 = load i32, i32* %20
store i32 %22, i32* %21
br label %23
23: ; preds = %12, %17
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 (i8*, ...) @debug_print(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %48
30: ; preds = %23
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_REUSEADDR
%33 = call i32 @setsockopt(i32 %26, i32 %31, i32 %32, i8* %7, i32 4)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%38 = load i32, i32* %36
store i32 %38, i32* %37
%39 = call i32 @htons(i32 %1)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %39, i32* %40
%41 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%42 = call i32 @connect(i32 %26, %struct.sockaddr* %41, i32 4)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %30
%45 = load i32, i32* @errno
%46 = call i32 (i8*, ...) @debug_print(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %30, %44, %28, %15
%49 = phi i32 [ -1, %28 ], [ -1, %44 ], [ -1, %15 ], [ %26, %30 ]
ret i32 %49
}