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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(scalar_t__);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                       struct addrinfo **);
scalar_t__ listen(scalar_t__, int);
int /*<<< orphan*/ log_perror(char *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
scalar_t__ socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int32_t http_init(uint32_t port) {
  int32_t sock_fd = 0, err = 0;
  char port_buffer[8] = {0};
  sprintf(port_buffer, "%u", port);
  struct addrinfo hints, *res = NULL, *addr = NULL;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  err = getaddrinfo(NULL, port_buffer, &hints, &res);
  if (err != 0) {
    log_perror("nyx: getaddrinfo");
    return 0;
  }
  for (addr = res; addr != NULL; addr = addr->ai_next) {
    sock_fd = socket(addr->ai_family, addr->ai_socktype, 0);
    if (sock_fd == -1)
      continue;
    if (bind(sock_fd, addr->ai_addr, addr->ai_addrlen) == 0)
      break;
    close(sock_fd);
  }
  freeaddrinfo(res);
  if (addr == NULL)
    return 0;
  if (listen(sock_fd, 64) != 0) {
    log_perror("nyx: listen");
    close(sock_fd);
    return 0;
  }
  return sock_fd;
}


x86 assembly (clang, -O0):
<http_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
lea    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe37(%rip),%rsi
call   1080 <sprintf@plt>
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     124f <http_init+0xbf>
lea    0xdc6(%rip),%rdi
call   1070 <log_perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    132d <http_init+0x19d>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12ca <http_init+0x13a>
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    128b <http_init+0xfb>
jmp    12b9 <http_init+0x129>
mov    -0x18(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jne    12b0 <http_init+0x120>
jmp    12ca <http_init+0x13a>
mov    -0x18(%rbp),%rdi
call   1040 <close@plt>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1257 <http_init+0xc7>
mov    -0x50(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
cmpq   $0x0,-0x58(%rbp)
jne    12eb <http_init+0x15b>
movq   $0x0,-0x8(%rbp)
jmp    132d <http_init+0x19d>
mov    -0x18(%rbp),%rdi
mov    $0x40,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
je     1325 <http_init+0x195>
lea    0xd0a(%rip),%rdi
call   1070 <log_perror@plt>
mov    -0x18(%rbp),%rdi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    132d <http_init+0x19d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_init>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %edi,%edx
movq   $0x0,0x10(%rsp)
lea    0xe57(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1080 <sprintf@plt>
movq   $0x0,0x8(%rsp)
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1090 <getaddrinfo@plt>
test   %rax,%rax
je     122b <http_init+0x9b>
lea    0xdeb(%rip),%rdi
call   1070 <log_perror@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    1249 <http_init+0xb9>
xor    %edi,%edi
call   10a0 <freeaddrinfo@plt>
jmp    121d <http_init+0x8d>
xchg   %ax,%ax
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     127b <http_init+0xeb>
mov    0x8(%rbx),%esi
mov    0xc(%rbx),%edi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffffffffffff,%rax
je     1240 <http_init+0xb0>
mov    %rax,%r14
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %rax,%rdi
call   1060 <bind@plt>
test   %rax,%rax
je     1287 <http_init+0xf7>
mov    %r14,%rdi
call   1040 <close@plt>
jmp    1240 <http_init+0xb0>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
jmp    121d <http_init+0x8d>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %r14,%rdi
mov    $0x40,%esi
call   1050 <listen@plt>
test   %rax,%rax
je     1220 <http_init+0x90>
lea    0xd66(%rip),%rdi
call   1070 <log_perror@plt>
mov    %r14,%rdi
call   1040 <close@plt>
jmp    121d <http_init+0x8d>

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

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