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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int inet_aton(char *, struct in_addr *);
int listen(int, int);
int /*<<< orphan*/ log_err(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int openServerSocket(int port) {
  struct sockaddr_in sockaddr;
  struct in_addr addr;
  int serverSocket = -1;
  int r = inet_aton("127.0.0.1", &addr);
  if (r == 0) {
    log_err("Daemon: Failed to convert '127.0.0.1' to an IP address...?!\n");
    return -1;
  }
  serverSocket = socket(AF_INET, SOCK_STREAM, 0);
  if (serverSocket == -1) {
    log_err("Daemon: Failed to create server socket\n");
    return -1;
  }
  memset((char *)&sockaddr, 0, sizeof(struct sockaddr_in));
  sockaddr.sin_family = AF_INET;
  sockaddr.sin_addr = addr;
  sockaddr.sin_port = htons(port);
  if (bind(serverSocket, (const struct sockaddr *)&sockaddr,
           sizeof(struct sockaddr_in)) == -1) {
    close(serverSocket);
    serverSocket = -1;
    log_err("Daemon: Failed to bind server socket\n");
    return -1;
  }
  if (listen(serverSocket, 10) == -1) {
    close(serverSocket);
    serverSocket = -1;
    log_err("Daemon: Failed to bind server socket\n");
    return -1;
  }
  return serverSocket;
}


x86 assembly (clang, -O0):
<openServerSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0x24(%rbp)
lea    0xe67(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <inet_aton@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11c7 <openServerSocket+0x47>
lea    0xe54(%rip),%rdi
call   1090 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <openServerSocket+0x136>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1205 <openServerSocket+0x85>
lea    0xe53(%rip),%rdi
call   1090 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <openServerSocket+0x136>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1273 <openServerSocket+0xf3>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x24(%rbp)
lea    0xe0d(%rip),%rdi
call   1090 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <openServerSocket+0x136>
mov    -0x24(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    12b0 <openServerSocket+0x130>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x24(%rbp)
lea    0xdd0(%rip),%rdi
call   1090 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <openServerSocket+0x136>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openServerSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
lea    0xe6d(%rip),%rdi
mov    %rsp,%rsi
call   1060 <inet_aton@plt>
test   %eax,%eax
je     1226 <openServerSocket+0xa6>
mov    0x2e3e(%rip),%r15
mov    (%r15),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     122f <openServerSocket+0xaf>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    (%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1216 <openServerSocket+0x96>
mov    %ebx,%edi
mov    $0xa,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    1240 <openServerSocket+0xc0>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xe4b(%rip),%rdi
jmp    1236 <openServerSocket+0xb6>
lea    0xddd(%rip),%rdi
jmp    1236 <openServerSocket+0xb6>
lea    0xe11(%rip),%rdi
call   1090 <log_err@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [61 x i8] c"Daemon: Failed to convert '127.0.0.1' to an IP address...?!\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [40 x i8] c"Daemon: Failed to create server socket\0A\00"
@.str.3 = [38 x i8] c"Daemon: Failed to bind server socket\0A\00"
define i32 @openServerSocket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.in_addr
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %6
%8 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.in_addr* %5)
store i32 %8, i32* %7
%9 = load i32, i32* %7
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @log_err(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %50
13: ; preds = %1
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @log_err(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %50
21: ; preds = %13
%22 = bitcast %struct.sockaddr_in* %4 to i8*
%23 = call i32 @memset(i8* %22, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%27 = bitcast %struct.in_addr* %26 to i8*
%28 = bitcast %struct.in_addr* %5 to i8*
%29 = load i32, i32* %3
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* %6
%33 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%34 = call i32 @bind(i32 %32, %struct.sockaddr* %33, i32 12)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %21
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %6
%39 = call i32 @log_err(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %50
40: ; preds = %21
%41 = load i32, i32* %6
%42 = call i32 @listen(i32 %41, i32 10)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %6
%47 = call i32 @log_err(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %50
48: ; preds = %40
%49 = load i32, i32* %6
store i32 %49, i32* %2
br label %50
50: ; preds = %48, %44, %36, %19, %11
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [61 x i8] c"Daemon: Failed to convert '127.0.0.1' to an IP address...?!\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [40 x i8] c"Daemon: Failed to create server socket\0A\00"
@.str.3 = [38 x i8] c"Daemon: Failed to bind server socket\0A\00"
define i32 @openServerSocket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.in_addr
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast %struct.in_addr* %3 to i8*
%6 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.in_addr* %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %30, label %8
8: ; preds = %1
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %30, label %13
13: ; preds = %8
%14 = call i32 @memset(i8* %4, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%19 = load i32, i32* %17
store i32 %19, i32* %18
%20 = call i32 @htons(i32 %0)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i32 @bind(i32 %11, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i32 %23, -1
br i1 %24, label %28, label %25
25: ; preds = %13
%26 = call i32 @listen(i32 %11, i32 10)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %25, %13
%29 = call i32 @close(i32 %11)
br label %30
30: ; preds = %28, %8, %1
%31 = phi i8* [ getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), %28 ]
%32 = call i32 @log_err(i8* %31)
br label %33
33: ; preds = %30, %25
%34 = phi i32 [ %11, %25 ], [ -1, %30 ]
ret i32 %34
}