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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *, char const *);
int /*<<< orphan*/ errx(int, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char const *, 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 open_server(const char *port) {
  struct addrinfo hints;
  struct addrinfo *res, *rp;
  int ret, sock;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  ret = getaddrinfo(NULL, port, &hints, &res);
  if (ret)
    errx(1, "getaddrinfo error %s\n", gai_strerror(ret));
  for (rp = res; rp != NULL; rp = rp->ai_next) {
    sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sock == -1)
      continue;
    if (bind(sock, rp->ai_addr, rp->ai_addrlen) == 0)
      break;
    close(sock);
  }
  freeaddrinfo(res);
  if (rp == NULL)
    errx(1, "failed to bind to port %s\n", port);
  int opt = 1;
  ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  if (ret)
    errx(1, "Could not set SO_REUSEADDR\n");
  ret = listen(sock, 1);
  if (ret)
    err(1, "listen error port %s\n", port);
  return sock;
}


x86 assembly (clang, -O0):
<open_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     122f <open_server+0x7f>
mov    -0x44(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12ab <open_server+0xfb>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    126e <open_server+0xbe>
jmp    129a <open_server+0xea>
mov    -0x48(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jne    1292 <open_server+0xe2>
jmp    12ab <open_server+0xfb>
mov    -0x48(%rbp),%edi
call   1070 <close@plt>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1237 <open_server+0x87>
mov    -0x38(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
cmpq   $0x0,-0x40(%rbp)
jne    12d6 <open_server+0x126>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xd47(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
movl   $0x1,-0x4c(%rbp)
mov    -0x48(%rbp),%edi
mov    0x2d01(%rip),%rax
mov    (%rax),%esi
mov    0x2cd0(%rip),%rax
mov    (%rax),%edx
lea    -0x4c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1321 <open_server+0x171>
mov    $0x1,%edi
lea    0xd17(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
mov    -0x48(%rbp),%edi
mov    $0x1,%esi
call   1090 <listen@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1350 <open_server+0x1a0>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xd02(%rip),%rsi
call   1080 <err@plt>
mov    -0x48(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rdi,%r14
lea    0x18(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %r14,%rsi
mov    %rbp,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     1228 <open_server+0x78>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xde6(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <errx@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    1249 <open_server+0x99>
xor    %edi,%edi
jmp    127d <open_server+0xcd>
cs nopw 0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1278 <open_server+0xc8>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10d0 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1240 <open_server+0x90>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   10a0 <bind@plt>
test   %rax,%rax
je     129a <open_server+0xea>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1240 <open_server+0x90>
mov    0x8(%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
lea    0xd8d(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <errx@plt>
jmp    12a4 <open_server+0xf4>
mov    0x8(%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
movl   $0x1,0x14(%rsp)
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
mov    0x2d04(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
je     12e7 <open_server+0x137>
lea    0xd56(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <errx@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1090 <listen@plt>
test   %eax,%eax
je     130b <open_server+0x15b>
lea    0xd4f(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
call   1080 <err@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [22 x i8] c"getaddrinfo error %s\0A\00"
@.str.1 = [27 x i8] c"failed to bind to port %s\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [28 x i8] c"Could not set SO_REUSEADDR\0A\00"
@.str.3 = [22 x i8] c"listen error port %s\0A\00"
define i32 @open_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 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%8 = load i32, i32* @AF_INET
%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 6
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 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %1
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %50, label %22
22: ; preds = %19, %41
%23 = phi %struct.addrinfo* [ %43, %41 ], [ %20, %19 ]
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 4
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 3
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%29 = load i32, i32* %28
%30 = call i32 @socket(i32 %25, i32 %27, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %41, label %32
32: ; preds = %22
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @bind(i32 %30, i32 %34, i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %45, label %39
39: ; preds = %32
%40 = call i32 @close(i32 %30)
br label %41
41: ; preds = %22, %39
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%43 = load %struct.addrinfo*, %struct.addrinfo** %42
%44 = icmp eq %struct.addrinfo* %43, null
br i1 %44, label %48, label %22
45: ; preds = %32
%46 = load %struct.addrinfo*, %struct.addrinfo** %3
%47 = call i32 @freeaddrinfo(%struct.addrinfo* %46)
br label %55
48: ; preds = %41
%49 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %50
50: ; preds = %48, %19
%51 = phi %struct.addrinfo* [ null, %19 ], [ %49, %48 ]
%52 = phi i32 [ undef, %19 ], [ %30, %48 ]
%53 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
%54 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %55
55: ; preds = %45, %50
%56 = phi i32 [ %52, %50 ], [ %30, %45 ]
%57 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%58 = load i32, i32* @SOL_SOCKET
%59 = load i32, i32* @SO_REUSEADDR
%60 = call i32 @setsockopt(i32 %56, i32 %58, i32 %59, i32* %4, i32 4)
%61 = icmp eq i32 %60, 0
br i1 %61, label %64, label %62
62: ; preds = %55
%63 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %64
64: ; preds = %62, %55
%65 = call i32 @listen(i32 %56, i32 1)
%66 = icmp eq i32 %65, 0
br i1 %66, label %69, label %67
67: ; preds = %64
%68 = call i32 @err(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %69
69: ; preds = %67, %64
ret i32 %56
}