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

/* Variables and functions */
int AI_ADDRCONFIG;
int AI_NUMERICSERV;
int AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ close(int);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                       struct addrinfo **);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void const *,
                      int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_listenfd(const char *port) {
  struct addrinfo hints, *listp, *p;
  int listenfd, optval = 1;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
  hints.ai_flags |= AI_NUMERICSERV;
  if (getaddrinfo(NULL, port, &hints, &listp) != 0) {
    err_sys("[open_listenfd] getaddrinfo");
  }
  for (p = listp; p; p = p->ai_next) {
    if ((listenfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
      continue;
    }
    if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval,
                   sizeof(int)) != 0) {
      err_sys("[open_listenfd] setsockopt");
    }
    if (bind(listenfd, p->ai_addr, p->ai_addrlen) == 0) {
      break;
    }
    if (close(listenfd) < 0) {
      err_sys("[open_listenfd] close");
    }
  }
  freeaddrinfo(listp);
  if (!p) {
    return -1;
  }
  if (listen(listenfd, 1024) < 0) {
    if (close(listenfd) < 0) {
      err_sys("[open_listenfd] close");
    }
    return -1;
  }
  return listenfd;
}


x86 assembly (clang, -O0):
<open_listenfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    0x2de1(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1090 <getaddrinfo@plt>
cmp    $0x0,%rax
je     120e <open_listenfd+0x7e>
lea    0xdf7(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12df <open_listenfd+0x14f>
mov    -0x40(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jge    124c <open_listenfd+0xbc>
jmp    12ce <open_listenfd+0x13e>
mov    -0x44(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
je     1286 <open_listenfd+0xf6>
lea    0xd9b(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jne    12ab <open_listenfd+0x11b>
jmp    12df <open_listenfd+0x14f>
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    12c9 <open_listenfd+0x139>
lea    0xd73(%rip),%rdi
call   1030 <err_sys@plt>
jmp    12ce <open_listenfd+0x13e>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1216 <open_listenfd+0x86>
mov    -0x38(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
cmpq   $0x0,-0x40(%rbp)
jne    12ff <open_listenfd+0x16f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1346 <open_listenfd+0x1b6>
mov    -0x44(%rbp),%edi
mov    $0x400,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1340 <open_listenfd+0x1b0>
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    1334 <open_listenfd+0x1a4>
lea    0xd08(%rip),%rdi
call   1030 <err_sys@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1346 <open_listenfd+0x1b6>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_listenfd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
movl   $0x1,0x14(%rsp)
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2e07(%rip),%rax
mov    0x2dd8(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e15(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x18(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %rax,%rax
je     120c <open_listenfd+0x7c>
lea    0xdf9(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     12b4 <open_listenfd+0x124>
mov    0x2dbf(%rip),%r13
mov    0x2d98(%rip),%r15
lea    0x14(%rsp),%r14
lea    0xde8(%rip),%r12
jmp    1249 <open_listenfd+0xb9>
cs nopw 0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12ad <open_listenfd+0x11d>
mov    0x14(%rbx),%edi
mov    0xc(%rbx),%edx
mov    0x10(%rbx),%esi
call   10b0 <socket@plt>
test   %eax,%eax
js     1240 <open_listenfd+0xb0>
mov    %eax,%ebp
mov    0x0(%r13),%esi
mov    (%r15),%edx
mov    %eax,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
je     1281 <open_listenfd+0xf1>
mov    %r12,%rdi
call   1030 <err_sys@plt>
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
mov    %ebp,%edi
call   1080 <bind@plt>
test   %rax,%rax
je     12d1 <open_listenfd+0x141>
mov    %ebp,%edi
call   1060 <close@plt>
test   %rax,%rax
jns    1240 <open_listenfd+0xb0>
lea    0xd91(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1240 <open_listenfd+0xb0>
mov    0x8(%rsp),%rdi
jmp    12b6 <open_listenfd+0x126>
xor    %edi,%edi
call   10a0 <freeaddrinfo@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%edi
mov    $0x400,%esi
call   1070 <listen@plt>
test   %rax,%rax
jns    12c0 <open_listenfd+0x130>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jns    12c0 <open_listenfd+0x130>
lea    0xd33(%rip),%rdi
call   1030 <err_sys@plt>
jmp    12c0 <open_listenfd+0x130>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32, %struct.addrinfo* }
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@AI_ADDRCONFIG = global i32 0
@AI_NUMERICSERV = global i32 0
@.str = [28 x i8] c"[open_listenfd] getaddrinfo\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [27 x i8] c"[open_listenfd] setsockopt\00"
@.str.2 = [22 x i8] c"[open_listenfd] close\00"
define i32 @open_listenfd(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 32)
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = load i32, i32* @AI_ADDRCONFIG
%13 = or i32 %12, %11
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 0
%15 = load i32, i32* @AI_NUMERICSERV
%16 = or i32 %15, %13
store i32 %16, i32* %14
%17 = call i64 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %1
%20 = call i32 @err_sys(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %1
%22 = load %struct.addrinfo*, %struct.addrinfo** %3
%23 = icmp eq %struct.addrinfo* %22, null
br i1 %23, label %26, label %29
24: ; preds = %58
%25 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %26
26: ; preds = %24, %21
%27 = phi %struct.addrinfo* [ %25, %24 ], [ null, %21 ]
%28 = call i32 @freeaddrinfo(%struct.addrinfo* %27)
br label %72
29: ; preds = %21, %58
%30 = phi %struct.addrinfo* [ %60, %58 ], [ %22, %21 ]
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 5
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 4
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 3
%36 = load i32, i32* %35
%37 = call i32 @socket(i32 %32, i32 %34, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %58, label %39
39: ; preds = %29
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_REUSEADDR
%42 = call i64 @setsockopt(i32 %37, i32 %40, i32 %41, i8* %7, i32 4)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %39
%45 = call i32 @err_sys(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %46
46: ; preds = %44, %39
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 2
%48 = load i32, i32* %47
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 1
%50 = load i32, i32* %49
%51 = call i64 @bind(i32 %37, i32 %48, i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %62, label %53
53: ; preds = %46
%54 = call i64 @close(i32 %37)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %53, %56, %29
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 6
%60 = load %struct.addrinfo*, %struct.addrinfo** %59
%61 = icmp eq %struct.addrinfo* %60, null
br i1 %61, label %24, label %29
62: ; preds = %46
%63 = load %struct.addrinfo*, %struct.addrinfo** %3
%64 = call i32 @freeaddrinfo(%struct.addrinfo* %63)
%65 = call i64 @listen(i32 %37, i32 1024)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %72
67: ; preds = %62
%68 = call i64 @close(i32 %37)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %67
%71 = call i32 @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %72
72: ; preds = %26, %62, %67, %70
%73 = phi i32 [ -1, %70 ], [ -1, %67 ], [ %37, %62 ], [ -1, %26 ]
ret i32 %73
}