#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*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ dieError(char *, int);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, struct in_addr *);
scalar_t__ listen(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int createServerConnection(int port) {
  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, "127.0.0.1", &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 (bind(sockid, sa_dst, sizeof(*sa_dst)))
    dieError("bind() failed", sockid);
  ;
  if (listen(sockid, 0))
    dieError("listen() failed", sockid);
  return sockid;
}


x86 assembly (clang, -O0):
<createServerConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a0 <createServerConnection+0x40>
mov    -0x8(%rbp),%esi
lea    0xe65(%rip),%rdi
call   1060 <dieError@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1040 <inet_pton@plt>
cmp    $0x1,%eax
jge    11d1 <createServerConnection+0x71>
mov    -0x8(%rbp),%esi
lea    0xe4e(%rip),%rdi
call   1060 <dieError@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     1220 <createServerConnection+0xc0>
mov    -0x8(%rbp),%esi
lea    0xe0d(%rip),%rdi
call   1060 <dieError@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
je     1243 <createServerConnection+0xe3>
mov    -0x8(%rbp),%esi
lea    0xdf8(%rip),%rdi
call   1060 <dieError@plt>
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createServerConnection>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
mov    0x2e76(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    119d <createServerConnection+0x3d>
lea    0xe6d(%rip),%rdi
mov    $0xffffffff,%esi
call   1060 <dieError@plt>
mov    0x0(%rbp),%edi
lea    0xe69(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1040 <inet_pton@plt>
test   %eax,%eax
jg     11c3 <createServerConnection+0x63>
lea    0xe5e(%rip),%rdi
mov    %ebx,%esi
call   1060 <dieError@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%rsp),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <bind@plt>
test   %rax,%rax
je     1202 <createServerConnection+0xa2>
lea    0xe2d(%rip),%rdi
mov    %ebx,%esi
call   1060 <dieError@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <listen@plt>
test   %rax,%rax
je     121e <createServerConnection+0xbe>
lea    0xe1f(%rip),%rdi
mov    %ebx,%esi
call   1060 <dieError@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
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 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [14 x i8] c"pton() failed\00"
@.str.3 = [14 x i8] c"bind() failed\00"
@.str.4 = [16 x i8] c"listen() failed\00"
define i32 @createServerConnection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.in_addr
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr*
store i32 %0, i32* %2
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* %3
%14 = call i32 @dieError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %1
%16 = load i32, i32* @AF_INET
%17 = call i32 @inet_pton(i32 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %struct.in_addr* %4)
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %3
%21 = call i32 @dieError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %19, %15
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* %2
%26 = call i32 @htons(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%29 = bitcast %struct.in_addr* %28 to i8*
%30 = bitcast %struct.in_addr* %4 to i8*
%31 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
store %struct.sockaddr* %31, %struct.sockaddr** %6
%32 = load i32, i32* %3
%33 = load %struct.sockaddr*, %struct.sockaddr** %6
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 4)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %22
%37 = load i32, i32* %3
%38 = call i32 @dieError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %36, %22
%40 = load i32, i32* %3
%41 = call i64 @listen(i32 %40, i32 0)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* %3
%45 = call i32 @dieError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %43, %39
%47 = load i32, i32* %3
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 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [14 x i8] c"pton() failed\00"
@.str.3 = [14 x i8] c"bind() failed\00"
@.str.4 = [16 x i8] c"listen() failed\00"
define i32 @createServerConnection(i32 %0) {
%2 = alloca %struct.in_addr
%3 = alloca %struct.sockaddr_in
%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 eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @dieError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 -1)
br label %10
10: ; preds = %8, %1
%11 = bitcast %struct.in_addr* %2 to i8*
%12 = load i32, i32* @AF_INET
%13 = call i32 @inet_pton(i32 %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %struct.in_addr* %2)
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @dieError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %6)
br label %17
17: ; preds = %15, %10
%18 = bitcast %struct.sockaddr_in* %3 to i8*
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 %0)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %2, i64 0, i32 0
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%25 = load i32, i32* %23
store i32 %25, i32* %24
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i64 @bind(i32 %6, %struct.sockaddr* %26, i32 4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %17
%30 = call i32 @dieError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %6)
br label %31
31: ; preds = %29, %17
%32 = call i64 @listen(i32 %6, i32 0)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 @dieError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %6)
br label %36
36: ; preds = %34, %31
ret i32 %6
}