#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*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ 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 /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_server_socket(char *port) {
  int server_socket, ret;
  struct addrinfo hints, *srvinfo, *p;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  ret = getaddrinfo(NULL, port, &hints, &srvinfo);
  if (ret != 0) {
    return ret;
  }
  for (p = srvinfo; p != NULL; p = p->ai_next) {
    server_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (server_socket == -1) {
      continue;
    }
    if ((bind(server_socket, p->ai_addr, p->ai_addrlen)) == 0) {
      break;
    }
    close(server_socket);
  }
  if (p == NULL) {
    return -1;
  }
  freeaddrinfo(srvinfo);
  return server_socket;
}


x86 assembly (clang, -O0):
<create_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11cd <create_server_socket+0x6d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <create_server_socket+0x10f>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1249 <create_server_socket+0xe9>
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   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120c <create_server_socket+0xac>
jmp    1238 <create_server_socket+0xd8>
mov    -0x14(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jne    1230 <create_server_socket+0xd0>
jmp    1249 <create_server_socket+0xe9>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    11d5 <create_server_socket+0x75>
cmpq   $0x0,-0x50(%rbp)
jne    1260 <create_server_socket+0x100>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <create_server_socket+0x10f>
mov    -0x48(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_server_socket>:
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   1030 <memset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <getaddrinfo@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1211 <create_server_socket+0xb1>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    11d9 <create_server_socket+0x79>
mov    $0xffffffff,%ebp
jmp    1211 <create_server_socket+0xb1>
nopl   0x0(%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     11c5 <create_server_socket+0x65>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11d0 <create_server_socket+0x70>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1050 <bind@plt>
test   %rax,%rax
je     1208 <create_server_socket+0xa8>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11d0 <create_server_socket+0x70>
mov    (%rsp),%rdi
call   1070 <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
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
define i32 @create_server_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
store i8* %0, i8** %3
%9 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 40)
%10 = load i32, i32* @AF_UNSPEC
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @AI_PASSIVE
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 6
store i32 %14, i32* %15
%16 = load i8*, i8** %3
%17 = call i32 @getaddrinfo(i32* null, i8* %16, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = load i32, i32* %5
store i32 %21, i32* %2
br label %67
22: ; preds = %1
%23 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %23, %struct.addrinfo** %8
br label %24
24: ; preds = %55, %22
%25 = load %struct.addrinfo*, %struct.addrinfo** %8
%26 = icmp ne %struct.addrinfo* %25, null
br i1 %26, label %27, label %59
27: ; preds = %24
%28 = load %struct.addrinfo*, %struct.addrinfo** %8
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 4
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %8
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 3
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %8
%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* %4
%38 = load i32, i32* %4
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %27
br label %55
41: ; preds = %27
%42 = load i32, i32* %4
%43 = load %struct.addrinfo*, %struct.addrinfo** %8
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load %struct.addrinfo*, %struct.addrinfo** %8
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i64 @bind(i32 %42, i32 %45, i32 %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %41
br label %59
52: ; preds = %41
%53 = load i32, i32* %4
%54 = call i32 @close(i32 %53)
br label %55
55: ; preds = %52, %40
%56 = load %struct.addrinfo*, %struct.addrinfo** %8
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 5
%58 = load %struct.addrinfo*, %struct.addrinfo** %57
store %struct.addrinfo* %58, %struct.addrinfo** %8
br label %24
59: ; preds = %51, %24
%60 = load %struct.addrinfo*, %struct.addrinfo** %8
%61 = icmp eq %struct.addrinfo* %60, null
br i1 %61, label %62, label %63
62: ; preds = %59
store i32 -1, i32* %2
br label %67
63: ; preds = %59
%64 = load %struct.addrinfo*, %struct.addrinfo** %7
%65 = call i32 @freeaddrinfo(%struct.addrinfo* %64)
%66 = load i32, i32* %4
store i32 %66, i32* %2
br label %67
67: ; preds = %63, %62, %20
%68 = load i32, i32* %2
ret i32 %68
}

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
define i32 @create_server_socket(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%7 = load i32, i32* @AF_UNSPEC
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 6
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %44
15: ; preds = %1
%16 = load %struct.addrinfo*, %struct.addrinfo** %3
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %44, label %18
18: ; preds = %15, %37
%19 = phi %struct.addrinfo* [ %39, %37 ], [ %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 eq i32 %26, -1
br i1 %27, label %37, label %28
28: ; preds = %18
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i64 @bind(i32 %26, i32 %30, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %28
%36 = call i32 @close(i32 %26)
br label %37
37: ; preds = %18, %35
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 5
%39 = load %struct.addrinfo*, %struct.addrinfo** %38
%40 = icmp eq %struct.addrinfo* %39, null
br i1 %40, label %44, label %18
41: ; preds = %28
%42 = load %struct.addrinfo*, %struct.addrinfo** %3
%43 = call i32 @freeaddrinfo(%struct.addrinfo* %42)
br label %44
44: ; preds = %37, %15, %1, %41
%45 = phi i32 [ %26, %41 ], [ %13, %1 ], [ -1, %15 ], [ -1, %37 ]
ret i32 %45
}