#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 /*<<< orphan*/ s_addr;
};
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*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int inet_aton(char const *, 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*/);
scalar_t__ strcmp(char const *, char *);
int openServerSocket(const char *address, int port) {
  struct sockaddr_in sockaddr;
  struct in_addr addr;
  int serverSocket = -1;
  if ((address != NULL) && (strcmp(address, "any") != 0)) {
    int r = inet_aton(address, &addr);
    if (r == 0) {
      log_err("Daemon: Failed to convert '%s' to an IP address...?!\n",
              address);
      return -1;
    }
  } else {
    addr.s_addr = htonl(INADDR_ANY);
  }
  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    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1218 <openServerSocket+0x78>
mov    -0x10(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1218 <openServerSocket+0x78>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <inet_aton@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    1213 <openServerSocket+0x73>
mov    -0x10(%rbp),%rsi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   10b0 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131e <openServerSocket+0x17e>
jmp    1229 <openServerSocket+0x89>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1269 <openServerSocket+0xc9>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   10b0 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131e <openServerSocket+0x17e>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12d9 <openServerSocket+0x139>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x2c(%rbp)
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   10b0 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131e <openServerSocket+0x17e>
mov    -0x2c(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1318 <openServerSocket+0x178>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x2c(%rbp)
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   10b0 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131e <openServerSocket+0x17e>
mov    -0x2c(%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    %esi,%r14d
test   %rdi,%rdi
je     11eb <openServerSocket+0x4b>
mov    %rdi,%rbp
lea    0xe44(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11eb <openServerSocket+0x4b>
mov    %rsp,%rsi
mov    %rbp,%rdi
call   1080 <inet_aton@plt>
test   %eax,%eax
jne    11fc <openServerSocket+0x5c>
lea    0xe28(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   10b0 <log_err@plt>
jmp    128c <openServerSocket+0xec>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,(%rsp)
mov    0x2de5(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     127e <openServerSocket+0xde>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    (%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     126e <openServerSocket+0xce>
mov    %ebp,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1291 <openServerSocket+0xf1>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xde6(%rip),%rdi
jmp    1285 <openServerSocket+0xe5>
lea    0xdb5(%rip),%rdi
xor    %eax,%eax
call   10b0 <log_err@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%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 = [4 x i8] c"any\00"
@.str.1 = [54 x i8] c"Daemon: Failed to convert '%s' to an IP address...?!\0A\00"
@INADDR_ANY = global i32 0
@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(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.in_addr
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 -1, i32* %8
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %25
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %25
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = call i32 @inet_aton(i8* %17, %struct.in_addr* %7)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 -1, i32* %3
br label %66
24: ; preds = %16
br label %29
25: ; preds = %12, %2
%26 = load i32, i32* @INADDR_ANY
%27 = call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %7, i32 0, i32 0
store i32 %27, i32* %28
br label %29
29: ; preds = %25, %24
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
37: ; preds = %29
%38 = bitcast %struct.sockaddr_in* %6 to i8*
%39 = call i32 @memset(i8* %38, i32 0, i32 12)
%40 = load i32, i32* @AF_INET
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %40, i32* %41
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%43 = bitcast %struct.in_addr* %42 to i8*
%44 = bitcast %struct.in_addr* %7 to i8*
%45 = load i32, i32* %5
%46 = call i32 @htons(i32 %45)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %46, i32* %47
%48 = load i32, i32* %8
%49 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%50 = call i32 @bind(i32 %48, %struct.sockaddr* %49, i32 12)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %56
52: ; preds = %37
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
store i32 -1, i32* %8
%55 = call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
56: ; preds = %37
%57 = load i32, i32* %8
%58 = call i32 @listen(i32 %57, i32 10)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = load i32, i32* %8
%62 = call i32 @close(i32 %61)
store i32 -1, i32* %8
%63 = call i32 (i8*, ...) @log_err(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
64: ; preds = %56
%65 = load i32, i32* %8
store i32 %65, i32* %3
br label %66
66: ; preds = %64, %60, %52, %35, %21
%67 = load i32, i32* %3
ret i32 %67
}

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