#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int createSocket(void) {
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1) {
    perror("Socket creation failed");
    exit(EXIT_FAILURE);
  }
  int enable = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) == -1) {
    perror("Socket options failed");
    exit(EXIT_FAILURE);
  }
  return sock;
}


x86 assembly (clang, -O0):
<createSocket>:
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,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1188 <createSocket+0x48>
lea    0xe8b(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
movl   $0x1,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11d6 <createSocket+0x96>
lea    0xe54(%rip),%rdi
call   1040 <perror@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createSocket>:
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>
cmp    $0xffffffff,%eax
je     119e <createSocket+0x5e>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11a7 <createSocket+0x67>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe5b(%rip),%rdi
jmp    11ae <createSocket+0x6e>
lea    0xe69(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Socket creation failed\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [22 x i8] c"Socket options failed\00"
define i32 @createSocket() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = call i32 @socket(i32 %3, i32 %4, i32 0)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
store i32 1, i32* %2
%13 = load i32, i32* %1
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %13, i32 %14, i32 %15, i32* %2, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %12
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Socket creation failed\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [22 x i8] c"Socket options failed\00"
define i32 @createSocket() {
%1 = alloca i32
%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 eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_REUSEADDR
%14 = call i32 @setsockopt(i32 %4, i32 %12, i32 %13, i32* %1, i32 4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %10
ret i32 %4
}