#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ _makeNonBlocking(int);
int /*<<< orphan*/ _reuseAddr(int);
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ logWrite(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int socketOpenServer(const char *host, const char *port) {
  int fd = -1, res;
  struct addrinfo addrInfoHints = {0}, *addrInfo, *curInfo;
  addrInfoHints.ai_family = AF_UNSPEC;
  addrInfoHints.ai_socktype = SOCK_STREAM;
  addrInfoHints.ai_flags = AI_PASSIVE;
  if ((res = getaddrinfo(host, port, &addrInfoHints, &addrInfo)) == 0) {
    for (curInfo = addrInfo; curInfo != NULL; curInfo = curInfo->ai_next) {
      fd = socket(curInfo->ai_family, curInfo->ai_socktype,
                  curInfo->ai_protocol);
      if (fd < 0) {
        logWrite("ERROR socket()");
        logWrite(strerror(errno));
        continue;
      }
      _reuseAddr(fd);
      if (bind(fd, curInfo->ai_addr, curInfo->ai_addrlen) < 0) {
        logWrite("ERROR bind()");
        logWrite(strerror(errno));
        close(fd);
        continue;
      }
      break;
    }
    freeaddrinfo(addrInfo);
    if (curInfo != NULL) {
      _makeNonBlocking(fd);
      if (listen(fd, SOMAXCONN) == 0) {
        return fd;
      }
      logWrite("ERROR listen()");
      logWrite(strerror(errno));
      close(fd);
    } else {
      logWrite("ERROR getaddrinfo(): returned unusable results");
    }
  } else {
    logWrite("ERROR getaddrinfo()");
    logWrite(gai_strerror(res));
  }
  return (-1);
}


x86 assembly (clang, -O0):
<socketOpenServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    137b <socketOpenServer+0x1bb>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12f9 <socketOpenServer+0x139>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    128d <socketOpenServer+0xcd>
lea    0xd93(%rip),%rdi
call   1030 <logWrite@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdi
call   1030 <logWrite@plt>
jmp    12e8 <socketOpenServer+0x128>
mov    -0x1c(%rbp),%edi
call   1080 <_reuseAddr@plt>
mov    -0x1c(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12e3 <socketOpenServer+0x123>
lea    0xd54(%rip),%rdi
call   1030 <logWrite@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdi
call   1030 <logWrite@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
jmp    12e8 <socketOpenServer+0x128>
jmp    12f9 <socketOpenServer+0x139>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1234 <socketOpenServer+0x74>
mov    -0x50(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
cmpq   $0x0,-0x58(%rbp)
je     136a <socketOpenServer+0x1aa>
mov    -0x1c(%rbp),%edi
call   1040 <_makeNonBlocking@plt>
mov    -0x1c(%rbp),%edi
mov    0x2cb1(%rip),%rax
mov    (%rax),%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jne    133b <socketOpenServer+0x17b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139e <socketOpenServer+0x1de>
lea    0xcda(%rip),%rdi
call   1030 <logWrite@plt>
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdi
call   1030 <logWrite@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
jmp    1376 <socketOpenServer+0x1b6>
lea    0xcba(%rip),%rdi
call   1030 <logWrite@plt>
jmp    1397 <socketOpenServer+0x1d7>
lea    0xcd8(%rip),%rdi
call   1030 <logWrite@plt>
mov    -0x20(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rdi
call   1030 <logWrite@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketOpenServer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x30(%rsp)
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     1229 <socketOpenServer+0x79>
mov    %eax,%ebx
lea    0xe45(%rip),%rdi
call   1030 <logWrite@plt>
mov    %ebx,%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rdi
jmp    12d5 <socketOpenServer+0x125>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     12c7 <socketOpenServer+0x117>
lea    0xdc2(%rip),%r14
mov    0x2d7b(%rip),%r12
lea    0xdc3(%rip),%r15
jmp    1272 <socketOpenServer+0xc2>
xchg   %ax,%ax
mov    %r14,%rdi
call   1030 <logWrite@plt>
mov    (%r12),%edi
call   10b0 <strerror@plt>
mov    %rax,%rdi
call   1030 <logWrite@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12c0 <socketOpenServer+0x110>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10d0 <socket@plt>
test   %eax,%eax
js     1250 <socketOpenServer+0xa0>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <_reuseAddr@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1090 <bind@plt>
test   %rax,%rax
jns    12ee <socketOpenServer+0x13e>
mov    %r15,%rdi
call   1030 <logWrite@plt>
mov    (%r12),%edi
call   10b0 <strerror@plt>
mov    %rax,%rdi
call   1030 <logWrite@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1269 <socketOpenServer+0xb9>
mov    0x8(%rsp),%rdi
jmp    12c9 <socketOpenServer+0x119>
xor    %edi,%edi
call   10c0 <freeaddrinfo@plt>
lea    0xd56(%rip),%rdi
call   1030 <logWrite@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    %ebp,%edi
call   1040 <_makeNonBlocking@plt>
mov    0x2cca(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1080 <listen@plt>
test   %rax,%rax
je     12df <socketOpenServer+0x12f>
lea    0xd01(%rip),%rdi
call   1030 <logWrite@plt>
mov    (%r12),%edi
call   10b0 <strerror@plt>
mov    %rax,%rdi
call   1030 <logWrite@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    12da <socketOpenServer+0x12a>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [15 x i8] c"ERROR socket()\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"ERROR bind()\00"
@SOMAXCONN = global i32 0
@.str.2 = [15 x i8] c"ERROR listen()\00"
@.str.3 = [47 x i8] c"ERROR getaddrinfo(): returned unusable results\00"
@.str.4 = [20 x i8] c"ERROR getaddrinfo()\00"
define i32 @socketOpenServer(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 -1, i32* %6
%11 = bitcast %struct.addrinfo* %8 to i8*
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %14, i32* %15
%16 = load i32, i32* @AI_PASSIVE
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 6
store i32 %16, i32* %17
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = call i32 @getaddrinfo(i8* %18, i8* %19, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %20, i32* %7
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %93
22: ; preds = %2
%23 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %23, %struct.addrinfo** %10
br label %24
24: ; preds = %65, %22
%25 = load %struct.addrinfo*, %struct.addrinfo** %10
%26 = icmp ne %struct.addrinfo* %25, null
br i1 %26, label %27, label %69
27: ; preds = %24
%28 = load %struct.addrinfo*, %struct.addrinfo** %10
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 4
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %10
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 3
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %10
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 2
%36 = load i32, i32* %35
%37 = call i32 @socket(i32 %30, i32 %33, i32 %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %27
%41 = call i32 @logWrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @logWrite(i8* %43)
br label %65
45: ; preds = %27
%46 = load i32, i32* %6
%47 = call i32 @_reuseAddr(i32 %46)
%48 = load i32, i32* %6
%49 = load %struct.addrinfo*, %struct.addrinfo** %10
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load %struct.addrinfo*, %struct.addrinfo** %10
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i64 @bind(i32 %48, i32 %51, i32 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %64
57: ; preds = %45
%58 = call i32 @logWrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 @logWrite(i8* %60)
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
br label %65
64: ; preds = %45
br label %69
65: ; preds = %57, %40
%66 = load %struct.addrinfo*, %struct.addrinfo** %10
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 5
%68 = load %struct.addrinfo*, %struct.addrinfo** %67
store %struct.addrinfo* %68, %struct.addrinfo** %10
br label %24
69: ; preds = %64, %24
%70 = load %struct.addrinfo*, %struct.addrinfo** %9
%71 = call i32 @freeaddrinfo(%struct.addrinfo* %70)
%72 = load %struct.addrinfo*, %struct.addrinfo** %10
%73 = icmp ne %struct.addrinfo* %72, null
br i1 %73, label %74, label %90
74: ; preds = %69
%75 = load i32, i32* %6
%76 = call i32 @_makeNonBlocking(i32 %75)
%77 = load i32, i32* %6
%78 = load i32, i32* @SOMAXCONN
%79 = call i64 @listen(i32 %77, i32 %78)
%80 = icmp eq i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %74
%82 = load i32, i32* %6
store i32 %82, i32* %3
br label %99
83: ; preds = %74
%84 = call i32 @logWrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%85 = load i32, i32* @errno
%86 = call i8* @strerror(i32 %85)
%87 = call i32 @logWrite(i8* %86)
%88 = load i32, i32* %6
%89 = call i32 @close(i32 %88)
br label %92
90: ; preds = %69
%91 = call i32 @logWrite(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %92
92: ; preds = %90, %83
br label %98
93: ; preds = %2
%94 = call i32 @logWrite(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%95 = load i32, i32* %7
%96 = call i8* @gai_strerror(i32 %95)
%97 = call i32 @logWrite(i8* %96)
br label %98
98: ; preds = %93, %92
store i32 -1, i32* %3
br label %99
99: ; preds = %98, %81
%100 = load i32, i32* %3
ret i32 %100
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [15 x i8] c"ERROR socket()\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"ERROR bind()\00"
@SOMAXCONN = global i32 0
@.str.2 = [15 x i8] c"ERROR listen()\00"
@.str.3 = [47 x i8] c"ERROR getaddrinfo(): returned unusable results\00"
@.str.4 = [20 x i8] c"ERROR getaddrinfo()\00"
define i32 @socketOpenServer(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = load i32, i32* @AF_UNSPEC
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %70
15: ; preds = %2
%16 = load %struct.addrinfo*, %struct.addrinfo** %4
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %66, label %18
18: ; preds = %15, %47
%19 = phi %struct.addrinfo* [ %49, %47 ], [ %16, %15 ]
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 4
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 3
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 2
%25 = load i32, i32* %24
%26 = call i32 @socket(i32 %21, i32 %23, i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %18
%29 = call i32 @logWrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @logWrite(i8* %31)
br label %47
33: ; preds = %18
%34 = call i32 @_reuseAddr(i32 %26)
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 1
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i64 @bind(i32 %26, i32 %36, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %33
%42 = call i32 @logWrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 @logWrite(i8* %44)
%46 = call i32 @close(i32 %26)
br label %47
47: ; preds = %41, %28
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 5
%49 = load %struct.addrinfo*, %struct.addrinfo** %48
%50 = icmp eq %struct.addrinfo* %49, null
br i1 %50, label %64, label %18
51: ; preds = %33
%52 = load %struct.addrinfo*, %struct.addrinfo** %4
%53 = call i32 @freeaddrinfo(%struct.addrinfo* %52)
%54 = call i32 @_makeNonBlocking(i32 %26)
%55 = load i32, i32* @SOMAXCONN
%56 = call i64 @listen(i32 %26, i32 %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %74, label %58
58: ; preds = %51
%59 = call i32 @logWrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 @logWrite(i8* %61)
%63 = call i32 @close(i32 %26)
br label %74
64: ; preds = %47
%65 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %66
66: ; preds = %64, %15
%67 = phi %struct.addrinfo* [ %65, %64 ], [ null, %15 ]
%68 = call i32 @freeaddrinfo(%struct.addrinfo* %67)
%69 = call i32 @logWrite(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %74
70: ; preds = %2
%71 = call i32 @logWrite(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%72 = call i8* @gai_strerror(i32 %13)
%73 = call i32 @logWrite(i8* %72)
br label %74
74: ; preds = %70, %66, %58, %51
%75 = phi i32 [ %26, %51 ], [ -1, %58 ], [ -1, %66 ], [ -1, %70 ]
ret i32 %75
}