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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/);
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ listen(int, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int start_server(const char *portstr) {
  struct addrinfo hints = {0}, *res;
  int sockfd;
  int e, opt = 1;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((e = getaddrinfo(NULL, portstr, &hints, &res)))
    errx(1, "getaddrinfo: %s", gai_strerror(e));
  if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
    err(1, "socket");
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))
    err(1, "setsockopt");
  if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) < 0)
    err(1, "fcntl");
  if (bind(sockfd, res->ai_addr, res->ai_addrlen))
    err(1, "bind");
  if (listen(sockfd, 5))
    err(1, "listen");
  freeaddrinfo(res);
  return sockfd;
}


x86 assembly (clang, -O0):
<start_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1070 <memset@plt>
movl   $0x1,-0x3c(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
je     1233 <start_server+0x83>
mov    -0x38(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xdd2(%rip),%rsi
call   1050 <errx@plt>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    126a <start_server+0xba>
mov    $0x1,%edi
lea    0xdab(%rip),%rsi
call   1080 <err@plt>
mov    -0x34(%rbp),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
lea    -0x3c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     12a9 <start_server+0xf9>
mov    $0x1,%edi
lea    0xd73(%rip),%rsi
call   1080 <err@plt>
mov    -0x34(%rbp),%edi
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
mov    0x2cfc(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    12de <start_server+0x12e>
mov    $0x1,%edi
lea    0xd49(%rip),%rsi
call   1080 <err@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     130e <start_server+0x15e>
mov    $0x1,%edi
lea    0xd1f(%rip),%rsi
call   1080 <err@plt>
mov    -0x34(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
je     1336 <start_server+0x186>
mov    $0x1,%edi
lea    0xcfc(%rip),%rsi
call   1080 <err@plt>
mov    -0x30(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_server>:
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rsi
xorps  %xmm0,%xmm0
movups %xmm0,0x1c(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
lea    0x10(%rsp),%rdx
mov    %rsp,%rcx
xor    %edi,%edi
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     1211 <start_server+0x71>
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xdfb(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1050 <errx@plt>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10c0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    123a <start_server+0x9a>
lea    0xde0(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2d66(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     1274 <start_server+0xd4>
lea    0xdad(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
jns    12a3 <start_server+0x103>
lea    0xd89(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1090 <bind@plt>
test   %rax,%rax
je     12c9 <start_server+0x129>
lea    0xd69(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
test   %rax,%rax
je     12eb <start_server+0x14b>
lea    0xd4c(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    (%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
ret

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

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