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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int __attribute__((unused)) set_up_socket(void) {
  int sd = socket(AF_INET, SOCK_STREAM, 0);
  if (sd < 0)
    err(1, "unable to create socket");
  struct sockaddr_in addr_in = {
      .sin_family = AF_INET,
  };
  inet_pton(AF_INET, "127.0.0.1", &addr_in.sin_addr);
  addr_in.sin_port = htons(2159);
  int rc = bind(sd, (struct sockaddr *)&addr_in, sizeof(addr_in));
  if (rc < 0)
    err(2, "unable to bind to port");
  rc = listen(sd, 5);
  if (rc < 0)
    err(3, "unable to listen on socket");
  return sd;
}


x86 assembly (clang, -O0):
<set_up_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    119f <set_up_socket+0x3f>
mov    $0x1,%edi
lea    0xe66(%rip),%rsi
call   1050 <err@plt>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rdx
add    $0x4,%rdx
lea    0xe47(%rip),%rsi
call   1040 <inet_pton@plt>
mov    $0x86f,%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1212 <set_up_socket+0xb2>
mov    $0x2,%edi
lea    0xe15(%rip),%rsi
call   1050 <err@plt>
mov    -0x4(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    123d <set_up_socket+0xdd>
mov    $0x3,%edi
lea    0xe01(%rip),%rsi
call   1050 <err@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_up_socket>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e7a(%rip),%r14
mov    (%r14),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1198 <set_up_socket+0x38>
lea    0xe72(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
movq   $0x0,0x8(%rsp)
lea    0xc(%rsp),%rdx
mov    (%r14),%edi
mov    %edi,0x10(%rsp)
lea    0xe64(%rip),%rsi
call   1040 <inet_pton@plt>
mov    $0x86f,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
jns    11ed <set_up_socket+0x8d>
lea    0xe3f(%rip),%rsi
mov    $0x2,%edi
call   1050 <err@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
test   %eax,%eax
jns    120e <set_up_socket+0xae>
lea    0xe35(%rip),%rsi
mov    $0x3,%edi
call   1050 <err@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"unable to create socket\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [23 x i8] c"unable to bind to port\00"
@.str.3 = [27 x i8] c"unable to listen on socket\00"
define i32 @set_up_socket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @err(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 0, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
%15 = load i32, i32* @AF_INET
store i32 %15, i32* %14
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%18 = call i32 @inet_pton(i32 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %17)
%19 = call i32 @htons(i32 2159)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %1
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i32 @bind(i32 %21, %struct.sockaddr* %22, i32 12)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %11
%27 = call i32 @err(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %11
%29 = load i32, i32* %1
%30 = call i32 @listen(i32 %29, i32 5)
store i32 %30, i32* %3
%31 = load i32, i32* %3
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @err(i32 3, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"unable to create socket\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [23 x i8] c"unable to bind to port\00"
@.str.3 = [27 x i8] c"unable to listen on socket\00"
define i32 @set_up_socket() {
%1 = alloca %struct.sockaddr_in
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = bitcast %struct.sockaddr_in* %1 to i8*
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 0, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
%13 = load i32, i32* @AF_INET
store i32 %13, i32* %12
%14 = call i32 @inet_pton(i32 %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %11)
%15 = call i32 @htons(i32 2159)
store i32 %15, i32* %10
%16 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%17 = call i32 @bind(i32 %4, %struct.sockaddr* %16, i32 12)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %8
%20 = call i32 @err(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %8
%22 = call i32 @listen(i32 %4, i32 5)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @err(i32 3, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
ret i32 %4
}