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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ log_message(char *, char *, char *, int /*<<< orphan*/,
                               int /*<<< orphan*/);
scalar_t__ set_nonblocking(int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int new_tcp_socket(int options) {
  int f;
  f = socket(AF_INET, SOCK_STREAM, 0);
  if (f < 0) {
    log_message("%s: %s: %s (errno %d)", "new_tcp_socket", "socket",
                strerror(errno), errno);
    return -1;
  }
  if (options & 1) {
    if (set_nonblocking(f)) {
      close(f);
      return -1;
    }
  }
  if (options & 2) {
    int i = 1;
    if (setsockopt(f, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) != 0) {
      close(f);
      log_message("%s: %s: %s (errno %d)", "new_tcp_socket", "setsockopt",
                  strerror(errno), errno);
      exit(1);
    }
  }
  return f;
}


x86 assembly (clang, -O0):
<new_tcp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e1 <new_tcp_socket+0x71>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%ecx
mov    0x2e10(%rip),%rax
mov    (%rax),%r8d
lea    0xe3e(%rip),%rdi
lea    0xe4d(%rip),%rsi
lea    0xe4e(%rip),%rdx
call   1050 <log_message@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <new_tcp_socket+0x140>
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     121b <new_tcp_socket+0xab>
mov    -0xc(%rbp),%edi
call   1060 <set_nonblocking@plt>
cmp    $0x0,%rax
je     1216 <new_tcp_socket+0xa6>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <new_tcp_socket+0x140>
jmp    121b <new_tcp_socket+0xab>
mov    -0x8(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     12aa <new_tcp_socket+0x13a>
movl   $0x1,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     12a5 <new_tcp_socket+0x135>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%ecx
mov    0x2d4a(%rip),%rax
mov    (%rax),%r8d
lea    0xd78(%rip),%rdi
lea    0xd87(%rip),%rsi
lea    0xd8f(%rip),%rdx
call   1050 <log_message@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    12aa <new_tcp_socket+0x13a>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_tcp_socket>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     122f <new_tcp_socket+0xbf>
mov    %eax,%ebx
test   $0x1,%bpl
je     11b6 <new_tcp_socket+0x46>
mov    %ebx,%edi
call   1060 <set_nonblocking@plt>
test   %rax,%rax
je     11b6 <new_tcp_socket+0x46>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    125c <new_tcp_socket+0xec>
test   $0x2,%bpl
je     1261 <new_tcp_socket+0xf1>
movl   $0x1,0x4(%rsp)
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     1261 <new_tcp_socket+0xf1>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2dc9(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
mov    (%rbx),%r8d
lea    0xdf0(%rip),%rdi
lea    0xdff(%rip),%rsi
lea    0xe07(%rip),%rdx
mov    %eax,%ecx
call   1050 <log_message@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d92(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
mov    (%rbx),%r8d
lea    0xdb9(%rip),%rdi
lea    0xdc8(%rip),%rsi
lea    0xdc9(%rip),%rdx
mov    %eax,%ecx
call   1050 <log_message@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"%s: %s: %s (errno %d)\00"
@.str.1 = [15 x i8] c"new_tcp_socket\00"
@.str.2 = [7 x i8] c"socket\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [11 x i8] c"setsockopt\00"
define i32 @new_tcp_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = call i32 @strerror(i32 %12)
%14 = load i32, i32* @errno
%15 = call i32 @log_message(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %14)
store i32 -1, i32* %2
br label %49
16: ; preds = %1
%17 = load i32, i32* %3
%18 = and i32 %17, 1
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %16
%21 = load i32, i32* %4
%22 = call i64 @set_nonblocking(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %2
br label %49
27: ; preds = %20
br label %28
28: ; preds = %27, %16
%29 = load i32, i32* %3
%30 = and i32 %29, 2
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %47
32: ; preds = %28
store i32 1, i32* %5
%33 = load i32, i32* %4
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEADDR
%36 = call i64 @setsockopt(i32 %33, i32 %34, i32 %35, i32* %5, i32 4)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %32
%39 = load i32, i32* %4
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = load i32, i32* @errno
%44 = call i32 @log_message(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %42, i32 %43)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %32
br label %47
47: ; preds = %46, %28
%48 = load i32, i32* %4
store i32 %48, i32* %2
br label %49
49: ; preds = %47, %24, %11
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"%s: %s: %s (errno %d)\00"
@.str.1 = [15 x i8] c"new_tcp_socket\00"
@.str.2 = [7 x i8] c"socket\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [11 x i8] c"setsockopt\00"
define i32 @new_tcp_socket(i32 %0) {
%2 = alloca i32
%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 %12
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = load i32, i32* @errno
%11 = tail call i32 @log_message(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %9, i32 %10)
br label %37
12: ; preds = %1
%13 = and i32 %0, 1
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %12
%16 = tail call i64 @set_nonblocking(i32 %5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 @close(i32 %5)
br label %37
20: ; preds = %15, %12
%21 = and i32 %0, 2
%22 = icmp eq i32 %21, 0
br i1 %22, label %37, label %23
23: ; preds = %20
%24 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i64 @setsockopt(i32 %5, i32 %25, i32 %26, i32* %2, i32 4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %36, label %29
29: ; preds = %23
%30 = call i32 @close(i32 %5)
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = load i32, i32* @errno
%34 = call i32 @log_message(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %32, i32 %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %23
br label %37
37: ; preds = %20, %36, %18, %7
%38 = phi i32 [ -1, %7 ], [ -1, %18 ], [ %5, %36 ], [ %5, %20 ]
ret i32 %38
}