#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 */
typedef int /*<<< orphan*/ uint16_t;
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;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int bindAndListenOnServerSocket(uint16_t port) {
  int socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_desc == -1) {
    fprintf(stderr, "Error: Could not create socket!");
    return -1;
  }
  struct sockaddr_in server;
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(port);
  if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
    fprintf(stderr, "Error: Could not bind socket!");
    return -2;
  }
  if (listen(socket_desc, 3) < 0) {
    fprintf(stderr, "Error: Could not listen on socket!");
    return -3;
  }
  return socket_desc;
}


x86 assembly (clang, -O0):
<bindAndListenOnServerSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11a2 <bindAndListenOnServerSocket+0x52>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <bindAndListenOnServerSocket+0xef>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1201 <bindAndListenOnServerSocket+0xb1>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    123f <bindAndListenOnServerSocket+0xef>
mov    -0xc(%rbp),%edi
mov    $0x3,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    1239 <bindAndListenOnServerSocket+0xe9>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    123f <bindAndListenOnServerSocket+0xef>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bindAndListenOnServerSocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e87(%rip),%r14
mov    (%r14),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11cc <bindAndListenOnServerSocket+0x7c>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11e8 <bindAndListenOnServerSocket+0x98>
mov    %ebx,%edi
mov    $0x3,%esi
call   1050 <listen@plt>
test   %rax,%rax
jns    1202 <bindAndListenOnServerSocket+0xb2>
mov    $0xfffffffd,%ebx
lea    0xe74(%rip),%rsi
jmp    11f4 <bindAndListenOnServerSocket+0xa4>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1202 <bindAndListenOnServerSocket+0xb2>
mov    $0xfffffffe,%ebx
lea    0xe2c(%rip),%rsi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Error: Could not create socket!\00"
@INADDR_ANY = global i32 0
@.str.1 = [30 x i8] c"Error: Could not bind socket!\00"
@.str.2 = [35 x i8] c"Error: Could not listen on socket!\00"
define i32 @bindAndListenOnServerSocket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %39
14: ; preds = %1
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* %3
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %4
%24 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %14
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %2
br label %39
30: ; preds = %14
%31 = load i32, i32* %4
%32 = call i64 @listen(i32 %31, i32 3)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %2
br label %39
37: ; preds = %30
%38 = load i32, i32* %4
store i32 %38, i32* %2
br label %39
39: ; preds = %37, %34, %27, %11
%40 = load i32, i32* %2
ret i32 %40
}

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
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Error: Could not create socket!\00"
@INADDR_ANY = global i32 0
@.str.1 = [30 x i8] c"Error: Could not bind socket!\00"
@.str.2 = [35 x i8] c"Error: Could not listen on socket!\00"
define i32 @bindAndListenOnServerSocket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %31
10: ; preds = %1
%11 = bitcast %struct.sockaddr_in* %2 to i8*
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 %0)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = call i64 @bind(i32 %5, %struct.sockaddr* %18, i32 12)
%20 = icmp slt i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %10
%22 = call i64 @listen(i32 %5, i32 3)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21, %10
%25 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %10 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), %21 ]
%26 = phi i32 [ -2, %10 ], [ -3, %21 ]
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* %25)
br label %29
29: ; preds = %24, %21
%30 = phi i32 [ %5, %21 ], [ %26, %24 ]
br label %31
31: ; preds = %29, %7
%32 = phi i32 [ -1, %7 ], [ %30, %29 ]
ret i32 %32
}