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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int listen_on_port(int port) {
  int srvfd;
  struct sockaddr_in srvskt;
  int adrlen;
  int flags;
  adrlen = sizeof(struct sockaddr_in);
  (void)memset((void *)&srvskt, 0, (size_t)adrlen);
  srvskt.sin_family = AF_INET;
  srvskt.sin_addr.s_addr = INADDR_ANY;
  srvskt.sin_port = htons(port);
  if ((srvfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "Unable to get socket for port %d.", port);
    exit(1);
  }
  flags = fcntl(srvfd, F_GETFL, 0);
  flags |= O_NONBLOCK;
  (void)fcntl(srvfd, F_SETFL, flags);
  if (bind(srvfd, (struct sockaddr *)&srvskt, adrlen) < 0) {
    fprintf(stderr, "Unable to bind to port %d\n", port);
    exit(1);
  }
  if (listen(srvfd, 1) < 0) {
    fprintf(stderr, "Unable to listen on port %d\n", port);
    exit(1);
  }
  return (srvfd);
}


x86 assembly (clang, -O0):
<listen_on_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xc,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    120b <listen_on_port+0x8b>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe04(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    127f <listen_on_port+0xff>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xdb2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    12b8 <listen_on_port+0x138>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xd94(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_on_port>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
mov    $0xc,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e48(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1235 <listen_on_port+0xb5>
mov    %eax,%ebp
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2def(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc6(%rip),%rcx
mov    (%rcx),%esi
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1247 <listen_on_port+0xc7>
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     1259 <listen_on_port+0xd9>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
jmp    1269 <listen_on_port+0xe9>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
jmp    1269 <listen_on_port+0xe9>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
mov    %ebx,%edx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"Unable to get socket for port %d.\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.1 = [27 x i8] c"Unable to bind to port %d\0A\00"
@.str.2 = [29 x i8] c"Unable to listen on port %d\0A\00"
define i32 @listen_on_port(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = call i32 @memset(i8* %3, i32 0, i64 12)
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %5, i32* %6
%7 = load i32, i32* @INADDR_ANY
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %7, i32* %8
%9 = call i32 @htons(i32 %0)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %0)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = load i32, i32* @F_GETFL
%21 = call i32 @fcntl(i32 %13, i32 %20, i32 0)
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %22, %21
%24 = load i32, i32* @F_SETFL
%25 = call i32 @fcntl(i32 %13, i32 %24, i32 %23)
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = call i64 @bind(i32 %13, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %19
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %19
%34 = call i64 @listen(i32 %13, i32 1)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %33
ret i32 %13
}