#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 sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(unsigned short);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcplisten(unsigned short port) {
  int listener = socket(PF_INET, SOCK_STREAM, 0);
  if (listener == -1) {
    perror("noq: socket");
    exit(1);
  }
  int one = 1;
  int r = setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
  if (r == -1) {
    perror("noq: setsockopt SO_REUSEADDR");
    exit(1);
  }
  struct sockaddr_in addr = {};
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  r = bind(listener, (struct sockaddr *)&addr, sizeof addr);
  if (r == -1) {
    perror("noq: bind");
    exit(1);
  }
  r = listen(listener, 0);
  if (r == -1) {
    perror("noq: listen");
    exit(1);
  }
  return listener;
}


x86 assembly (clang, -O0):
<tcplisten>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11cb <tcplisten+0x4b>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1219 <tcplisten+0x99>
lea    0xe02(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movzwl -0x2(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1275 <tcplisten+0xf5>
lea    0xdc3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1060 <listen@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    12a2 <tcplisten+0x122>
lea    0xda0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcplisten>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     120f <tcplisten+0x9f>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1218 <tcplisten+0xa8>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     1221 <tcplisten+0xb1>
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
je     122a <tcplisten+0xba>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdea(%rip),%rdi
jmp    1231 <tcplisten+0xc1>
lea    0xded(%rip),%rdi
jmp    1231 <tcplisten+0xc1>
lea    0xe01(%rip),%rdi
jmp    1231 <tcplisten+0xc1>
lea    0xe02(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"noq: socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [29 x i8] c"noq: setsockopt SO_REUSEADDR\00"
@AF_INET = global i32 0
@.str.2 = [10 x i8] c"noq: bind\00"
@.str.3 = [12 x i8] c"noq: listen\00"
define i32 @tcplisten(i16 zeroext %0) {
%2 = alloca i16
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i16 %0, i16* %2
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
store i32 1, i32* %4
%16 = load i32, i32* %3
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i32* %4, i32 4)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
%26 = bitcast %struct.sockaddr_in* %6 to i8*
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i16, i16* %2
%30 = call i32 @htons(i16 zeroext %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* %3
%33 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%34 = call i32 @bind(i32 %32, %struct.sockaddr* %33, i32 8)
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %25
%38 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %25
%41 = load i32, i32* %3
%42 = call i32 @listen(i32 %41, i32 0)
store i32 %42, i32* %5
%43 = load i32, i32* %5
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %40
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"noq: socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [29 x i8] c"noq: setsockopt SO_REUSEADDR\00"
@AF_INET = global i32 0
@.str.2 = [10 x i8] c"noq: bind\00"
@.str.3 = [12 x i8] c"noq: listen\00"
define i32 @tcplisten(i16 zeroext %0) {
%2 = alloca i32
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.sockaddr_in*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %7, i32 %14, i32 %15, i32* %2, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = bitcast i64* %3 to i8*
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %23, i32* %24
%25 = call i32 @htons(i16 zeroext %0)
%26 = bitcast i64* %3 to i32*
store i32 %25, i32* %26
%27 = bitcast i64* %3 to %struct.sockaddr*
%28 = call i32 @bind(i32 %7, %struct.sockaddr* %27, i32 8)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %21
%34 = call i32 @listen(i32 %7, i32 0)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %33
ret i32 %7
}