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

/* 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*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_server_socket(void) {
  int s, x;
  struct sockaddr_in ia;
  s = socket(AF_INET, SOCK_STREAM, 0);
  memset(&ia, 0, sizeof(ia));
  ia.sin_family = AF_INET;
  inet_pton(AF_INET, "127.0.0.1", &ia.sin_addr);
  ia.sin_port = htons(12345);
  if (bind(s, (struct sockaddr *)&ia, sizeof(ia)) < 0) {
    perror("bind");
    exit(77);
  }
  x = 1;
  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
  if (listen(s, 1) < 0) {
    perror("listen");
    exit(77);
  }
  return s;
}


x86 assembly (clang, -O0):
<open_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
add    $0x4,%rdx
lea    0xe18(%rip),%rsi
call   1060 <inet_pton@plt>
mov    $0x3039,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    122b <open_server_socket+0x9b>
lea    0xdee(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4d,%edi
call   10a0 <exit@plt>
movl   $0x1,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1283 <open_server_socket+0xf3>
lea    0xd9b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4d,%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_server_socket>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e40(%rip),%r15
mov    (%r15),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r15),%edi
mov    %edi,0x18(%rsp)
lea    0x14(%rsp),%rdx
lea    0xe24(%rip),%rsi
call   1060 <inet_pton@plt>
mov    $0x3039,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     124c <open_server_socket+0xbc>
movl   $0x1,0xc(%rsp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     1255 <open_server_socket+0xc5>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdb7(%rip),%rdi
jmp    125c <open_server_socket+0xcc>
lea    0xdb3(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4d,%edi
call   10a0 <exit@plt>

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 = [10 x i8] c"127.0.0.1\00"
@.str.1 = [5 x i8] c"bind\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [7 x i8] c"listen\00"
define i32 @open_server_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%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 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%12 = call i32 @inet_pton(i32 %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %11)
%13 = call i32 @htons(i32 12345)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* %1
%16 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%17 = call i64 @bind(i32 %15, %struct.sockaddr* %16, i32 12)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 77)
unreachable
22: ; preds = %0
store i32 1, i32* %2
%23 = load i32, i32* %1
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %2, i32 4)
%27 = load i32, i32* %1
%28 = call i64 @listen(i32 %27, i32 1)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 77)
unreachable
33: ; preds = %22
%34 = load i32, i32* %1
ret i32 %34
}

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 = [10 x i8] c"127.0.0.1\00"
@.str.1 = [5 x i8] c"bind\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [7 x i8] c"listen\00"
define i32 @open_server_socket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%12 = call i32 @inet_pton(i32 %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %11)
%13 = call i32 @htons(i32 12345)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %13, i32* %14
%15 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%16 = call i64 @bind(i32 %7, %struct.sockaddr* %15, i32 12)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 77)
unreachable
21: ; preds = %0
store i32 1, i32* %1
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %7, i32 %22, i32 %23, i32* %1, i32 4)
%25 = call i64 @listen(i32 %7, i32 1)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 77)
unreachable
30: ; preds = %21
ret i32 %7
}