#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*/ serv;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
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*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ listen(int, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror_l(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ xerr(char *, char *);
int xlisten(int port) {
  char serv[32];
  snprintf(serv, sizeof(serv), "%d", port);
  struct addrinfo hints = {
      .ai_flags = AI_PASSIVE,
      .ai_family = AF_UNSPEC,
      .ai_socktype = SOCK_STREAM,
  };
  struct addrinfo *res;
  int rv;
  if ((rv = getaddrinfo(NULL, serv, &hints, &res)) != 0)
    xerr("getaddrinfo: %s", gai_strerror(rv));
  struct addrinfo *ressave = res;
  int fd = -1;
  for (; res; res = res->ai_next) {
    fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (fd < 0)
      continue;
    int on = 1;
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    if (bind(fd, res->ai_addr, res->ai_addrlen) == 0)
      break;
    close(fd);
    fd = -1;
  }
  freeaddrinfo(ressave);
  if (fd < 0)
    xerr("bind: failed to bind port %s", serv);
  if (listen(fd, 1024) < 0)
    xerr("listen: %s", strerror_l(errno, 0));
  return fd;
}


x86 assembly (clang, -O0):
<xlisten>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x20,%esi
lea    0xe2f(%rip),%rdx
call   1040 <snprintf@plt>
movl   $0x0,-0x58(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x50(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x58(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x64(%rbp)
cmp    $0x0,%eax
je     124f <xlisten+0x9f>
mov    -0x64(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xdb9(%rip),%rdi
call   10a0 <xerr@plt>
mov    -0x60(%rbp),%rax
mov    %rax,-0x70(%rbp)
movl   $0xffffffff,-0x74(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     1304 <xlisten+0x154>
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    1295 <xlisten+0xe5>
jmp    12f3 <xlisten+0x143>
movl   $0x1,-0x78(%rbp)
mov    -0x74(%rbp),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
mov    0x2d09(%rip),%rax
mov    (%rax),%edx
lea    -0x78(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x74(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x60(%rbp),%rax
mov    (%rax),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jne    12e4 <xlisten+0x134>
jmp    1304 <xlisten+0x154>
mov    -0x74(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x74(%rbp)
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    125e <xlisten+0xae>
mov    -0x70(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
cmpl   $0x0,-0x74(%rbp)
jge    1327 <xlisten+0x177>
lea    -0x30(%rbp),%rsi
lea    0xcf1(%rip),%rdi
call   10a0 <xerr@plt>
mov    -0x74(%rbp),%edi
mov    $0x400,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    135d <xlisten+0x1ad>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1080 <strerror_l@plt>
mov    %rax,%rsi
lea    0xcd8(%rip),%rdi
call   10a0 <xerr@plt>
mov    -0x74(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xlisten>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x60,%rsp
mov    %edi,%ecx
lea    0xe3a(%rip),%rdx
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1040 <snprintf@plt>
movq   $0x0,0x18(%rsp)
movl   $0x0,0x20(%rsp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x30(%rsp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
lea    0x18(%rsp),%rdx
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     1247 <xlisten+0x97>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdc4(%rip),%rdi
mov    %rax,%rsi
call   10a0 <xerr@plt>
mov    0x8(%rsp),%r14
test   %r14,%r14
je     12db <xlisten+0x12b>
mov    0x2d8c(%rip),%r12
mov    0x2d55(%rip),%r13
lea    0x14(%rsp),%r15
mov    %r14,%rax
jmp    1283 <xlisten+0xd3>
nopl   (%rax)
mov    0x8(%rsp),%rax
mov    0x18(%rax),%rax
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     12e0 <xlisten+0x130>
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10d0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1270 <xlisten+0xc0>
movl   $0x1,0x14(%rsp)
mov    (%r12),%esi
mov    0x0(%r13),%edx
mov    %ebx,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1090 <bind@plt>
test   %rax,%rax
je     12fb <xlisten+0x14b>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    1270 <xlisten+0xc0>
mov    $0xffffffff,%ebx
mov    %r14,%rdi
call   10c0 <freeaddrinfo@plt>
lea    0xd24(%rip),%rdi
lea    0x40(%rsp),%rsi
call   10a0 <xerr@plt>
jmp    1303 <xlisten+0x153>
mov    %r14,%rdi
call   10c0 <freeaddrinfo@plt>
mov    %ebx,%edi
mov    $0x400,%esi
call   1070 <listen@plt>
test   %rax,%rax
jns    1333 <xlisten+0x183>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1080 <strerror_l@plt>
lea    0xd05(%rip),%rdi
mov    %rax,%rsi
call   10a0 <xerr@plt>
mov    %ebx,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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