#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 /*<<< orphan*/ value;
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*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ EXIT_FAILURE;
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*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int tcp_listen(void) {
  int listenfd;
  int value = 1;
  struct addrinfo hints, *res, *rp;
  int err;
  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  if ((err = getaddrinfo(NULL, "daytime", &hints, &res)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
    exit(EXIT_FAILURE);
  }
  for (rp = res; rp; rp = rp->ai_next) {
    listenfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (listenfd == -1)
      continue;
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));
    if (bind(listenfd, rp->ai_addr, rp->ai_addrlen) == 0)
      break;
    close(listenfd);
  }
  if (rp == NULL) {
    fprintf(stderr, "couldn't bind address\n");
    exit(EXIT_FAILURE);
  }
  if (listen(listenfd, 16) == -1)
    error(EXIT_FAILURE, errno, "listen");
  return listenfd;
}


x86 assembly (clang, -O0):
<tcp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x1,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe02(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     124d <tcp_listen+0x9d>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12ed <tcp_listen+0x13d>
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,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    128c <tcp_listen+0xdc>
jmp    12dc <tcp_listen+0x12c>
mov    -0x4(%rbp),%edi
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    0x2d09(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x4(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jne    12d4 <tcp_listen+0x124>
jmp    12ed <tcp_listen+0x13d>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1255 <tcp_listen+0xa5>
cmpq   $0x0,-0x40(%rbp)
jne    131d <tcp_listen+0x16d>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
lea    0xd11(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
mov    $0x10,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    1351 <tcp_listen+0x1a1>
mov    0x2c76(%rip),%rax
mov    (%rax),%edi
mov    0x2c75(%rip),%rax
mov    (%rax),%esi
lea    0xce4(%rip),%rdx
call   10a0 <error@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_listen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
movl   $0x1,0xc(%rsp)
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
lea    0xdfa(%rip),%rsi
lea    0x10(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rdx
call   10c0 <getaddrinfo@plt>
test   %eax,%eax
jne    12f0 <tcp_listen+0x140>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     12cb <tcp_listen+0x11b>
mov    0x2db6(%rip),%r15
mov    0x2d6f(%rip),%r12
lea    0xc(%rsp),%r14
jmp    1250 <tcp_listen+0xa0>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12cb <tcp_listen+0x11b>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1247 <tcp_listen+0x97>
mov    %eax,%ebp
mov    (%r15),%esi
mov    (%r12),%edx
mov    %eax,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1090 <bind@plt>
test   %rax,%rax
jne    1240 <tcp_listen+0x90>
mov    %ebp,%edi
mov    $0x10,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12bc <tcp_listen+0x10c>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
mov    0x2d0a(%rip),%rax
mov    (%rax),%esi
lea    0xd79(%rip),%rdx
call   10a0 <error@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
lea    0xd3e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2cd1(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xd01(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12e2 <tcp_listen+0x132>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AI_PASSIVE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"daytime\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [23 x i8] c"couldn't bind address\0A\00"
@errno = global i32 0
@.str.3 = [7 x i8] c"listen\00"
define i32 @tcp_listen() {
%1 = alloca i32
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%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* @AI_PASSIVE
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 6
store i32 %8, i32* %9
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %12, i32* %13
%14 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %51, label %25
19: ; preds = %0
%20 = load i32, i32* @stderr
%21 = call i8* @gai_strerror(i32 %14)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %16, %47
%26 = phi %struct.addrinfo* [ %49, %47 ], [ %17, %16 ]
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 4
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 3
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 2
%32 = load i32, i32* %31
%33 = call i32 @socket(i32 %28, i32 %30, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %47, label %35
35: ; preds = %25
%36 = load i32, i32* @SOL_SOCKET
%37 = load i32, i32* @SO_REUSEADDR
%38 = call i32 @setsockopt(i32 %33, i32 %36, i32 %37, i32* %1, i32 4)
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @bind(i32 %33, i32 %40, i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %56, label %45
45: ; preds = %35
%46 = call i32 @close(i32 %33)
br label %47
47: ; preds = %25, %45
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 5
%49 = load %struct.addrinfo*, %struct.addrinfo** %48
%50 = icmp eq %struct.addrinfo* %49, null
br i1 %50, label %51, label %25
51: ; preds = %47, %16
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %35
%57 = call i32 @listen(i32 %33, i32 16)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %63
59: ; preds = %56
%60 = load i32, i32* @EXIT_FAILURE
%61 = load i32, i32* @errno
%62 = call i32 @error(i32 %60, i32 %61, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %63
63: ; preds = %59, %56
ret i32 %33
}