#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;
};
typedef int /*<<< orphan*/ addrCriteria;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int /*<<< orphan*/ getSockName(int);
int getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sysError(int /*<<< orphan*/);
int createServerSocket(const char *service) {
  struct addrinfo addrCriteria;
  memset(&addrCriteria, 0, sizeof(addrCriteria));
  addrCriteria.ai_family = AF_UNSPEC;
  addrCriteria.ai_flags = AI_PASSIVE;
  addrCriteria.ai_socktype = SOCK_STREAM;
  addrCriteria.ai_protocol = IPPROTO_TCP;
  struct addrinfo *servAddr;
  int ret = getaddrinfo(NULL, service, &addrCriteria, &servAddr);
  if (ret < 0)
    sysError(gai_strerror(ret));
  int servSock = -1;
  for (struct addrinfo *addr = servAddr; addr != NULL; addr = addr->ai_next) {
    servSock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
    if (servSock < 0)
      continue;
    if (bind(servSock, addr->ai_addr, addr->ai_addrlen) == 0 &&
        listen(servSock, 10) == 0) {
      getSockName(servSock);
      break;
    }
    close(servSock);
    servSock = -1;
  }
  freeaddrinfo(servAddr);
  return servSock;
}


x86 assembly (clang, -O0):
<createServerSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    121d <createServerSocket+0x7d>
mov    -0x3c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %eax,%edi
call   1030 <sysError@plt>
movl   $0xffffffff,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12c6 <createServerSocket+0x126>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    1263 <createServerSocket+0xc3>
jmp    12b5 <createServerSocket+0x115>
mov    -0x40(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jne    12a6 <createServerSocket+0x106>
mov    -0x40(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jne    12a6 <createServerSocket+0x106>
mov    -0x40(%rbp),%edi
call   1070 <getSockName@plt>
jmp    12c6 <createServerSocket+0x126>
mov    -0x40(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x40(%rbp)
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    122c <createServerSocket+0x8c>
mov    -0x38(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x40(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createServerSocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
jns    1215 <createServerSocket+0x75>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
mov    %eax,%edi
call   1030 <sysError@plt>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    1245 <createServerSocket+0xa5>
mov    $0xffffffff,%ebp
jmp    1282 <createServerSocket+0xe2>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1282 <createServerSocket+0xe2>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10c0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     123c <createServerSocket+0x9c>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1090 <bind@plt>
test   %rax,%rax
jne    1230 <createServerSocket+0x90>
mov    %ebp,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
test   %rax,%rax
jne    1230 <createServerSocket+0x90>
mov    %ebp,%edi
call   1070 <getSockName@plt>
mov    (%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@AI_PASSIVE = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
define i32 @createServerSocket(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.addrinfo*
store i8* %0, i8** %2
%8 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 6
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @IPPROTO_TCP
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i8*, i8** %2
%18 = call i32 @getaddrinfo(i32* null, i8* %17, %struct.addrinfo* %3, %struct.addrinfo** %4)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %1
%22 = load i32, i32* %5
%23 = call i32 @gai_strerror(i32 %22)
%24 = call i32 @sysError(i32 %23)
br label %25
25: ; preds = %21, %1
store i32 -1, i32* %6
%26 = load %struct.addrinfo*, %struct.addrinfo** %4
store %struct.addrinfo* %26, %struct.addrinfo** %7
br label %27
27: ; preds = %64, %25
%28 = load %struct.addrinfo*, %struct.addrinfo** %7
%29 = icmp ne %struct.addrinfo* %28, null
br i1 %29, label %30, label %68
30: ; preds = %27
%31 = load %struct.addrinfo*, %struct.addrinfo** %7
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 4
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %7
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 3
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %7
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 2
%39 = load i32, i32* %38
%40 = call i32 @socket(i32 %33, i32 %36, i32 %39)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %30
br label %64
44: ; preds = %30
%45 = load i32, i32* %6
%46 = load %struct.addrinfo*, %struct.addrinfo** %7
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = load %struct.addrinfo*, %struct.addrinfo** %7
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i64 @bind(i32 %45, i32 %48, i32 %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %61
54: ; preds = %44
%55 = load i32, i32* %6
%56 = call i64 @listen(i32 %55, i32 10)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = load i32, i32* %6
%60 = call i32 @getSockName(i32 %59)
br label %68
61: ; preds = %54, %44
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
store i32 -1, i32* %6
br label %64
64: ; preds = %61, %43
%65 = load %struct.addrinfo*, %struct.addrinfo** %7
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 5
%67 = load %struct.addrinfo*, %struct.addrinfo** %66
store %struct.addrinfo* %67, %struct.addrinfo** %7
br label %27
68: ; preds = %58, %27
%69 = load %struct.addrinfo*, %struct.addrinfo** %4
%70 = call i32 @freeaddrinfo(%struct.addrinfo* %69)
%71 = load i32, i32* %6
ret i32 %71
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@AI_PASSIVE = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
define i32 @createServerSocket(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%6 = load i32, i32* @AF_UNSPEC
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %6, i32* %7
%8 = load i32, i32* @AI_PASSIVE
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 6
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %10, i32* %11
%12 = load i32, i32* @IPPROTO_TCP
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = bitcast %struct.addrinfo** %3 to i8*
%15 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = call i32 @gai_strerror(i32 %15)
%19 = call i32 @sysError(i32 %18)
br label %20
20: ; preds = %17, %1
%21 = load %struct.addrinfo*, %struct.addrinfo** %3
%22 = icmp eq %struct.addrinfo* %21, null
br i1 %22, label %52, label %23
23: ; preds = %20, %47
%24 = phi %struct.addrinfo* [ %50, %47 ], [ %21, %20 ]
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 4
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 3
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 2
%30 = load i32, i32* %29
%31 = call i32 @socket(i32 %26, i32 %28, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %47, label %33
33: ; preds = %23
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 0
%37 = load i32, i32* %36
%38 = call i64 @bind(i32 %31, i32 %35, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %33
%41 = call i64 @listen(i32 %31, i32 10)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @getSockName(i32 %31)
br label %52
45: ; preds = %40, %33
%46 = call i32 @close(i32 %31)
br label %47
47: ; preds = %23, %45
%48 = phi i32 [ %31, %23 ], [ -1, %45 ]
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 5
%50 = load %struct.addrinfo*, %struct.addrinfo** %49
%51 = icmp eq %struct.addrinfo* %50, null
br i1 %51, label %52, label %23
52: ; preds = %47, %20, %43
%53 = phi i32 [ %31, %43 ], [ -1, %20 ], [ %48, %47 ]
%54 = load %struct.addrinfo*, %struct.addrinfo** %3
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %54)
ret i32 %53
}