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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ dieError(char *, int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, struct in_addr *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int createClientConnection(int port, char *address) {
  int sockid;
  sockid = socket(AF_INET, SOCK_STREAM, 0);
  if (sockid == -1)
    dieError("socket() failed", sockid);
  struct in_addr addr;
  if (inet_pton(AF_INET, address, &addr) < 1)
    dieError("pton() failed", sockid);
  struct sockaddr_in dst;
  dst.sin_family = AF_INET;
  dst.sin_port = htons(port);
  dst.sin_addr = addr;
  struct sockaddr *sa_dst = (struct sockaddr *)&dst;
  if (connect(sockid, sa_dst, sizeof(*sa_dst)))
    dieError("connect() failed", sockid);
  fcntl(sockid, F_SETFL, O_NONBLOCK);
  return sockid;
}


x86 assembly (clang, -O0):
<createClientConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a4 <createClientConnection+0x44>
mov    -0x14(%rbp),%esi
lea    0xe61(%rip),%rdi
call   1060 <dieError@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1050 <inet_pton@plt>
cmp    $0x1,%eax
jge    11d2 <createClientConnection+0x72>
mov    -0x14(%rbp),%esi
lea    0xe43(%rip),%rdi
call   1060 <dieError@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
je     1221 <createClientConnection+0xc1>
mov    -0x14(%rbp),%esi
lea    0xe02(%rip),%rdi
call   1060 <dieError@plt>
mov    -0x14(%rbp),%edi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createClientConnection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %edi,%r14d
mov    0x2e71(%rip),%r15
mov    (%r15),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11a2 <createClientConnection+0x42>
lea    0xe68(%rip),%rdi
mov    $0xffffffff,%esi
call   1060 <dieError@plt>
mov    (%r15),%edi
mov    %rsp,%rdx
mov    %rbp,%rsi
call   1050 <inet_pton@plt>
test   %eax,%eax
jg     11c2 <createClientConnection+0x62>
lea    0xe55(%rip),%rdi
mov    %ebx,%esi
call   1060 <dieError@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rsp),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <connect@plt>
test   %rax,%rax
je     1200 <createClientConnection+0xa0>
lea    0xe25(%rip),%rdi
mov    %ebx,%esi
call   1060 <dieError@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@.str.1 = [14 x i8] c"pton() failed\00"
@.str.2 = [17 x i8] c"connect() failed\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @createClientConnection(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.in_addr
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr*
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* %5
%16 = call i32 @dieError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %14, %2
%18 = load i32, i32* @AF_INET
%19 = load i8*, i8** %4
%20 = call i32 @inet_pton(i32 %18, i8* %19, %struct.in_addr* %6)
%21 = icmp slt i32 %20, 1
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* %5
%24 = call i32 @dieError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %17
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* %3
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%32 = bitcast %struct.in_addr* %31 to i8*
%33 = bitcast %struct.in_addr* %6 to i8*
%34 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
store %struct.sockaddr* %34, %struct.sockaddr** %8
%35 = load i32, i32* %5
%36 = load %struct.sockaddr*, %struct.sockaddr** %8
%37 = call i64 @connect(i32 %35, %struct.sockaddr* %36, i32 4)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %25
%40 = load i32, i32* %5
%41 = call i32 @dieError(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %39, %25
%43 = load i32, i32* %5
%44 = load i32, i32* @F_SETFL
%45 = load i32, i32* @O_NONBLOCK
%46 = call i32 @fcntl(i32 %43, i32 %44, i32 %45)
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@.str.1 = [14 x i8] c"pton() failed\00"
@.str.2 = [17 x i8] c"connect() failed\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @createClientConnection(i32 %0, i8* %1) {
%3 = alloca %struct.in_addr
%4 = alloca %struct.sockaddr_in
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @dieError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 -1)
br label %11
11: ; preds = %9, %2
%12 = bitcast %struct.in_addr* %3 to i8*
%13 = load i32, i32* @AF_INET
%14 = call i32 @inet_pton(i32 %13, i8* %1, %struct.in_addr* %3)
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @dieError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %18
18: ; preds = %16, %11
%19 = bitcast %struct.sockaddr_in* %4 to i8*
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @htons(i32 %0)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%26 = load i32, i32* %24
store i32 %26, i32* %25
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i64 @connect(i32 %7, %struct.sockaddr* %27, i32 4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %18
%31 = call i32 @dieError(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %32
32: ; preds = %30, %18
%33 = load i32, i32* @F_SETFL
%34 = load i32, i32* @O_NONBLOCK
%35 = call i32 @fcntl(i32 %7, i32 %33, i32 %34)
ret i32 %7
}