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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ set_error(char *, int, char *, ...);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int tcp_socket(char *ebuf, int ebuf_sz) {
  int socket_fd;
  int one = 1;
  if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    set_error(ebuf, ebuf_sz, "Failed to create a socket");
    return -1;
  }
  if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) ==
      -1) {
    set_error(ebuf, ebuf_sz, "Failed to setsockopt: SO_REUSEADDR: %s",
              strerror(errno));
    return -1;
  }
  return socket_fd;
}


x86 assembly (clang, -O0):
<tcp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    119c <tcp_socket+0x5c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    0xe77(%rip),%rdx
mov    $0x0,%al
call   1040 <set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <tcp_socket+0xce>
mov    -0x18(%rbp),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1208 <tcp_socket+0xc8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    %eax,%ecx
lea    0xe25(%rip),%rdx
mov    $0x0,%al
call   1040 <set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <tcp_socket+0xce>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
movl   $0x1,0xc(%rsp)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11c5 <tcp_socket+0x85>
mov    %eax,%ebp
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11de <tcp_socket+0x9e>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe66(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <set_error@plt>
jmp    11d9 <tcp_socket+0x99>
lea    0xe34(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <set_error@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [26 x i8] c"Failed to create a socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [39 x i8] c"Failed to setsockopt: SO_REUSEADDR: %s\00"
@errno = global i32 0
define i32 @tcp_socket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %7
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i32, i32* %5
%15 = call i32 (i8*, i32, i8*, ...) @set_error(i8* %13, i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %30
16: ; preds = %2
%17 = load i32, i32* %6
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = call i32 @setsockopt(i32 %17, i32 %18, i32 %19, i32* %7, i32 4)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 (i8*, i32, i8*, ...) @set_error(i8* %23, i32 %24, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 -1, i32* %3
br label %30
28: ; preds = %16
%29 = load i32, i32* %6
store i32 %29, i32* %3
br label %30
30: ; preds = %28, %22, %12
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [26 x i8] c"Failed to create a socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [39 x i8] c"Failed to setsockopt: SO_REUSEADDR: %s\00"
@errno = global i32 0
define i32 @tcp_socket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (i8*, i32, i8*, ...) @set_error(i8* %0, i32 %1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %20
11: ; preds = %2
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_REUSEADDR
%14 = call i32 @setsockopt(i32 %7, i32 %12, i32 %13, i32* %3, i32 4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 (i8*, i32, i8*, ...) @set_error(i8* %0, i32 %1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %11, %16, %9
%21 = phi i32 [ -1, %9 ], [ -1, %16 ], [ %7, %11 ]
ret i32 %21
}