#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int test_listen(int port) {
  int sock;
  int on = 1;
  struct sockaddr_in addr;
  int errno2;
  memset(&addr, 0, sizeof(struct sockaddr_in));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = inet_addr("0");
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("ERR socket(%m)\n");
    return -1;
  }
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) {
    printf("ERR getsockopt(%m)\n");
    goto err;
  }
  if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
    printf("ERR bind(%m)\n");
    goto err;
  }
  if (listen(sock, 5) < 0) {
    printf("ERR listen(%m)\n");
    goto err;
  }
  return (sock);
err:
  errno2 = errno;
  close(sock);
  errno = errno2;
  printf("ERR listen on 0:%d\n", port);
  exit(1);
  return -1;
}


x86 assembly (clang, -O0):
<test_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe20(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1227 <test_listen+0x87>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <test_listen+0x166>
mov    -0xc(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1268 <test_listen+0xc8>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12cb <test_listen+0x12b>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1296 <test_listen+0xf6>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12cb <test_listen+0x12b>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    12c0 <test_listen+0x120>
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12cb <test_listen+0x12b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1306 <test_listen+0x166>
mov    0x2cf6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2cdf(%rip),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%esi
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movl   $0x1,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e14(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0xe1b(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1260 <test_listen+0xc0>
mov    %eax,%ebp
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1280 <test_listen+0xe0>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     1289 <test_listen+0xe9>
mov    %ebp,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
jns    1273 <test_listen+0xd3>
lea    0xdd6(%rip),%rdi
jmp    1290 <test_listen+0xf0>
lea    0xd9b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8b(%rip),%rdi
jmp    1290 <test_listen+0xf0>
lea    0xd96(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d2a(%rip),%r14
mov    (%r14),%r15d
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r15d,(%r14)
lea    0xd92(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [2 x i8] c"0\00"
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"ERR socket(%m)\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [20 x i8] c"ERR getsockopt(%m)\0A\00"
@.str.3 = [14 x i8] c"ERR bind(%m)\0A\00"
@.str.4 = [16 x i8] c"ERR listen(%m)\0A\00"
@errno = global i32 0
@.str.5 = [20 x i8] c"ERR listen on 0:%d\0A\00"
define i32 @test_listen(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %5
%8 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* %3
%12 = call i32 @htons(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %12, i32* %13
%14 = call i32 @inet_addr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %55
23: ; preds = %1
%24 = load i32, i32* %4
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = bitcast i32* %5 to i8*
%28 = call i64 @setsockopt(i32 %24, i32 %25, i32 %26, i8* %27, i32 4)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %47
32: ; preds = %23
%33 = load i32, i32* %4
%34 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%35 = call i64 @bind(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %47
39: ; preds = %32
%40 = load i32, i32* %4
%41 = call i64 @listen(i32 %40, i32 5)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %47
45: ; preds = %39
%46 = load i32, i32* %4
store i32 %46, i32* %2
br label %55
47: ; preds = %43, %37, %30
%48 = load i32, i32* @errno
store i32 %48, i32* %7
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %7
store i32 %51, i32* @errno
%52 = load i32, i32* %3
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %52)
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %45, %21
%56 = load i32, i32* %2
ret i32 %56
}

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