#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*/ member_0;
};
struct sockaddr_in {
  int member_1;
  TYPE_1__ member_2;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int htons(unsigned int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setup_socket(unsigned int hport) {
  int sock_wait;
  int cresult = 0;
  int port = htons(hport);
  struct sockaddr_in addr = {AF_INET, port, {INADDR_ANY}};
  sock_wait = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  cresult += bind(sock_wait, (struct sockaddr *)&addr, sizeof(addr));
  cresult += listen(sock_wait, 20);
  if (sock_wait < 0 || cresult < 0) {
    printf("Unable to open socket on port %d.\n", hport);
    printf("Errno: %d\n", errno);
    exit(EXIT_FAILURE);
  }
  return sock_wait;
}


x86 assembly (clang, -O0):
<setup_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %rax,%rcx
movslq -0xc(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x14,%esi
call   1050 <listen@plt>
mov    %rax,%rcx
movslq -0xc(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     120a <setup_socket+0xaa>
cmpl   $0x0,-0xc(%rbp)
jge    123c <setup_socket+0xdc>
mov    -0x4(%rbp),%esi
lea    0xdec(%rip),%rdi
call   1040 <printf@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
lea    0xdfa(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
call   1030 <htons@plt>
mov    0x2e71(%rip),%rcx
mov    (%rcx),%edi
mov    %edi,(%rsp)
mov    %eax,0x4(%rsp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    $0x14,%esi
call   1050 <listen@plt>
test   %ebp,%ebp
js     11df <setup_socket+0x7f>
add    %ebx,%eax
and    $0x80000000,%eax
test   %rax,%rax
jne    11df <setup_socket+0x7f>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1a(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1070 <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
@IPPROTO_TCP = global i32 0
@.str = [35 x i8] c"Unable to open socket on port %d.\0A\00"
@.str.1 = [11 x i8] c"Errno: %d\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @setup_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %2
store i32 0, i32* %4
%7 = load i32, i32* %2
%8 = call i32 @htons(i32 %7)
store i32 %8, i32* %5
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%10 = load i32, i32* @AF_INET
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%12 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %11, i32 0, i32 0
%13 = load i32, i32* %5
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%15 = load i32, i32* @INADDR_ANY
store i32 %15, i32* %14
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = load i32, i32* @IPPROTO_TCP
%19 = call i32 @socket(i32 %16, i32 %17, i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%22 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 12)
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = add nsw i64 %24, %22
%26 = trunc i64 %25 to i32
store i32 %26, i32* %4
%27 = load i32, i32* %3
%28 = call i64 @listen(i32 %27, i32 20)
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = add nsw i64 %30, %28
%32 = trunc i64 %31 to i32
store i32 %32, i32* %4
%33 = load i32, i32* %3
%34 = icmp slt i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %1
%36 = load i32, i32* %4
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %35, %1
%39 = load i32, i32* %2
%40 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = load i32, i32* @errno
%42 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %35
%46 = load i32, i32* %3
ret i32 %46
}

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
@IPPROTO_TCP = global i32 0
@.str = [35 x i8] c"Unable to open socket on port %d.\0A\00"
@.str.1 = [11 x i8] c"Errno: %d\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @setup_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = tail call i32 @htons(i32 %0)
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%6 = load i32, i32* @AF_INET
store i32 %6, i32* %5
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %3, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
%9 = load i32, i32* @INADDR_ANY
store i32 %9, i32* %8
%10 = load i32, i32* @SOCK_STREAM
%11 = load i32, i32* @IPPROTO_TCP
%12 = tail call i32 @socket(i32 %6, i32 %10, i32 %11)
%13 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%14 = call i64 @bind(i32 %12, %struct.sockaddr* %13, i32 12)
%15 = call i64 @listen(i32 %12, i32 20)
%16 = add i64 %15, %14
%17 = icmp slt i32 %12, 0
%18 = and i64 %16, 2147483648
%19 = icmp ne i64 %18, 0
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %21, label %27
21: ; preds = %1
%22 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %0)
%23 = load i32, i32* @errno
%24 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %1
ret i32 %12
}