#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*/ uint32_t;
typedef int /*<<< orphan*/ enabled;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ *boxUnsigned(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
uint32_t *socketOpenTCP(uint32_t context_len, uint32_t *stackvar) {
  int sd;
  int status;
  int enabled = 1;
  sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (-1 == sd) {
    perror("socket:");
    exit(1);
  }
  status = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (const char *)&enabled,
                      sizeof(enabled));
  if (-1 == status) {
    perror("setsockopt(...,SO_REUSEADDR,...)");
  }
  return boxUnsigned(context_len, (uint32_t)sd);
}


x86 assembly (clang, -O0):
<socketOpenTCP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11ad <socketOpenTCP+0x5d>
lea    0xe62(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    11ee <socketOpenTCP+0x9e>
lea    0xe1f(%rip),%rdi
call   1050 <perror@plt>
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <boxUnsigned@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketOpenTCP>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11cb <socketOpenTCP+0x7b>
mov    %eax,%ebp
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11bd <socketOpenTCP+0x6d>
mov    %ebx,%edi
mov    %ebp,%esi
call   1030 <boxUnsigned@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe44(%rip),%rdi
call   1050 <perror@plt>
jmp    11ad <socketOpenTCP+0x5d>
lea    0xe2e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [8 x i8] c"socket:\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [33 x i8] c"setsockopt(...,SO_REUSEADDR,...)\00"
define i32* @socketOpenTCP(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 1, i32* %7
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = load i32, i32* @IPPROTO_TCP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 -1, %12
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* %5
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = bitcast i32* %7 to i8*
%22 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i8* %21, i32 4)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 -1, %23
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %17
%28 = load i32, i32* %3
%29 = load i32, i32* %5
%30 = call i32* @boxUnsigned(i32 %28, i32 %29)
ret i32* %30
}

LLVM IR (-O2):
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [8 x i8] c"socket:\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [33 x i8] c"setsockopt(...,SO_REUSEADDR,...)\00"
define i32* @socketOpenTCP(i32 %0, i32* nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %8, i32 %14, i32 %15, i8* %4, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = call i32* @boxUnsigned(i32 %0, i32 %8)
ret i32* %21
}