#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(int /*<<< orphan*/, char *, struct addrinfo *,
                struct addrinfo **);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_server_socket_fd(char *portNo) {
  int sockfd;
  struct addrinfo hints;
  struct addrinfo *servinfo;
  struct addrinfo *p;
  int yes = 1;
  int rv;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((rv = getaddrinfo(0, portNo, &hints, &servinfo)) != 0) {
    return -1;
  }
  for (p = servinfo; p != 0; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      continue;
    }
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
      return -1;
    }
    if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sockfd);
      continue;
    }
    break;
  }
  freeaddrinfo(servinfo);
  if (p == 0) {
    return -1;
  }
  if (listen(sockfd, 10) == -1) {
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<get_server_socket_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x54(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
xor    %edi,%edi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x58(%rbp)
cmp    $0x0,%eax
je     11f2 <get_server_socket_fd+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <get_server_socket_fd+0x172>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12aa <get_server_socket_fd+0x12a>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1230 <get_server_socket_fd+0xb0>
jmp    1299 <get_server_socket_fd+0x119>
mov    -0x14(%rbp),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1269 <get_server_socket_fd+0xe9>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <get_server_socket_fd+0x172>
mov    -0x14(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1294 <get_server_socket_fd+0x114>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    1299 <get_server_socket_fd+0x119>
jmp    12aa <get_server_socket_fd+0x12a>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    11fa <get_server_socket_fd+0x7a>
mov    -0x48(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpq   $0x0,-0x50(%rbp)
jne    12ca <get_server_socket_fd+0x14a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <get_server_socket_fd+0x172>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    12ec <get_server_socket_fd+0x16c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <get_server_socket_fd+0x172>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_server_socket_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
movl   $0x1,0xc(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1080 <getaddrinfo@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
je     1200 <get_server_socket_fd+0x80>
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1275 <get_server_socket_fd+0xf5>
mov    0x2dd8(%rip),%r12
mov    0x2da9(%rip),%r13
lea    0xc(%rsp),%r14
jmp    1229 <get_server_socket_fd+0xa9>
xchg   %ax,%ax
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1281 <get_server_socket_fd+0x101>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1220 <get_server_socket_fd+0xa0>
mov    %eax,%ebp
mov    (%r12),%esi
mov    0x0(%r13),%edx
mov    %eax,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11ee <get_server_socket_fd+0x6e>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    128f <get_server_socket_fd+0x10f>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1220 <get_server_socket_fd+0xa0>
xor    %edi,%edi
call   1090 <freeaddrinfo@plt>
jmp    11ee <get_server_socket_fd+0x6e>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
jmp    11ee <get_server_socket_fd+0x6e>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebp
mov    %ebp,%r15d
jmp    11ee <get_server_socket_fd+0x6e>

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

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