#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 */

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_open() {
  int opt;
  int res;
  int sock;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("could not create server socket");
    exit(1);
  }
  opt = 1;
  res = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
  if (res < 0) {
    perror("could not set server socket options");
    exit(1);
  }
  return sock;
}


x86 assembly (clang, -O0):
<socket_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1184 <socket_open+0x44>
lea    0xe8b(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
movl   $0x1,-0x4(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
lea    -0x4(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d2 <socket_open+0x92>
lea    0xe5c(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_open>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     119c <socket_open+0x5c>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     11a5 <socket_open+0x65>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe5d(%rip),%rdi
jmp    11ac <socket_open+0x6c>
lea    0xe73(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [31 x i8] c"could not create server socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [36 x i8] c"could not set server socket options\00"
define i32 @socket_open() {
%1 = alloca i32
%2 = alloca i32
%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* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
store i32 1, i32* %1
%13 = load i32, i32* %3
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %13, i32 %14, i32 %15, i32* %1, i32 4)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [31 x i8] c"could not create server socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [36 x i8] c"could not set server socket options\00"
define i32 @socket_open() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%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 slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
store i32 1, i32* %1
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_REUSEADDR
%13 = call i32 @setsockopt(i32 %5, i32 %11, i32 %12, i32* %1, i32 4)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10
ret i32 %5
}