#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 socklen_t;
typedef int /*<<< orphan*/ option_value;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ SO_SNDBUF;
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *,
                      int *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
scalar_t__ socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int g_tcp_socket(void) {
  int rv;
  int option_value;
  socklen_t option_len;
  rv = (int)socket(AF_INET, SOCK_STREAM, 0);
  if (rv < 0) {
    return -1;
  }
  option_len = sizeof(option_value);
  if (getsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *)&option_value,
                 &option_len) == 0) {
    if (option_value == 0) {
      option_value = 1;
      option_len = sizeof(option_value);
      setsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *)&option_value,
                 option_len);
    }
  }
  option_len = sizeof(option_value);
  if (getsockopt(rv, SOL_SOCKET, SO_SNDBUF, (char *)&option_value,
                 &option_len) == 0) {
    if (option_value < (1024 * 32)) {
      option_value = 1024 * 32;
      option_len = sizeof(option_value);
      setsockopt(rv, SOL_SOCKET, SO_SNDBUF, (char *)&option_value, option_len);
    }
  }
  return rv;
}


x86 assembly (clang, -O0):
<g_tcp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    116a <g_tcp_socket+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <g_tcp_socket+0x127>
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1040 <getsockopt@plt>
cmp    $0x0,%rax
jne    11dc <g_tcp_socket+0xac>
cmpl   $0x0,-0xc(%rbp)
jne    11d7 <g_tcp_socket+0xa7>
movl   $0x1,-0xc(%rbp)
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    -0x10(%rbp),%r8d
call   1030 <setsockopt@plt>
jmp    11dc <g_tcp_socket+0xac>
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1040 <getsockopt@plt>
cmp    $0x0,%rax
jne    1251 <g_tcp_socket+0x121>
cmpl   $0x8000,-0xc(%rbp)
jge    124c <g_tcp_socket+0x11c>
movl   $0x8000,-0xc(%rbp)
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    -0x10(%rbp),%r8d
call   1030 <setsockopt@plt>
jmp    1251 <g_tcp_socket+0x121>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<g_tcp_socket>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2ea0(%rip),%rax
mov    (%rax),%edi
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %rax,%rbx
test   %ebx,%ebx
js     121d <g_tcp_socket+0xed>
movl   $0x4,0xc(%rsp)
mov    0x2e7c(%rip),%r14
mov    (%r14),%esi
mov    0x2e4a(%rip),%r15
mov    (%r15),%edx
lea    0x8(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %ebx,%edi
call   1040 <getsockopt@plt>
test   %rax,%rax
jne    11be <g_tcp_socket+0x8e>
cmpl   $0x0,0x8(%rsp)
jne    11be <g_tcp_socket+0x8e>
movl   $0x1,0x8(%rsp)
movl   $0x4,0xc(%rsp)
mov    (%r14),%esi
mov    (%r15),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movl   $0x4,0xc(%rsp)
mov    (%r14),%esi
mov    0x2e08(%rip),%r15
mov    (%r15),%edx
lea    0x8(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %ebx,%edi
call   1040 <getsockopt@plt>
test   %rax,%rax
jne    1222 <g_tcp_socket+0xf2>
cmpl   $0x7fff,0x8(%rsp)
jg     1222 <g_tcp_socket+0xf2>
movl   $0x8000,0x8(%rsp)
movl   $0x4,0xc(%rsp)
mov    (%r14),%esi
mov    (%r15),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
jmp    1222 <g_tcp_socket+0xf2>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_SNDBUF = global i32 0
define i32 @g_tcp_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i64 @socket(i32 %5, i32 %6, i32 0)
%8 = trunc i64 %7 to i32
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 -1, i32* %1
br label %50
12: ; preds = %0
store i32 4, i32* %4
%13 = load i32, i32* %2
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = bitcast i32* %3 to i8*
%17 = call i64 @getsockopt(i32 %13, i32 %14, i32 %15, i8* %16, i32* %4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %12
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %19
store i32 1, i32* %3
store i32 4, i32* %4
%23 = load i32, i32* %2
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = bitcast i32* %3 to i8*
%27 = load i32, i32* %4
%28 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i8* %26, i32 %27)
br label %29
29: ; preds = %22, %19
br label %30
30: ; preds = %29, %12
store i32 4, i32* %4
%31 = load i32, i32* %2
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_SNDBUF
%34 = bitcast i32* %3 to i8*
%35 = call i64 @getsockopt(i32 %31, i32 %32, i32 %33, i8* %34, i32* %4)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %48
37: ; preds = %30
%38 = load i32, i32* %3
%39 = icmp slt i32 %38, 32768
br i1 %39, label %40, label %47
40: ; preds = %37
store i32 32768, i32* %3
store i32 4, i32* %4
%41 = load i32, i32* %2
%42 = load i32, i32* @SOL_SOCKET
%43 = load i32, i32* @SO_SNDBUF
%44 = bitcast i32* %3 to i8*
%45 = load i32, i32* %4
%46 = call i32 @setsockopt(i32 %41, i32 %42, i32 %43, i8* %44, i32 %45)
br label %47
47: ; preds = %40, %37
br label %48
48: ; preds = %47, %30
%49 = load i32, i32* %2
store i32 %49, i32* %1
br label %50
50: ; preds = %48, %11
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_SNDBUF = global i32 0
define i32 @g_tcp_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i64 @socket(i32 %5, i32 %6, i32 0)
%8 = trunc i64 %7 to i32
%9 = icmp slt i32 %8, 0
br i1 %9, label %34, label %10
10: ; preds = %0
store i32 4, i32* %2
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_REUSEADDR
%13 = call i64 @getsockopt(i32 %8, i32 %11, i32 %12, i8* %3, i32* %2)
%14 = icmp eq i64 %13, 0
%15 = load i32, i32* %1
%16 = icmp eq i32 %15, 0
%17 = select i1 %14, i1 %16, i1 false
br i1 %17, label %18, label %22
18: ; preds = %10
store i32 1, i32* %1
store i32 4, i32* %2
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %8, i32 %19, i32 %20, i8* %3, i32 4)
br label %22
22: ; preds = %18, %10
store i32 4, i32* %2
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_SNDBUF
%25 = call i64 @getsockopt(i32 %8, i32 %23, i32 %24, i8* %3, i32* %2)
%26 = icmp eq i64 %25, 0
%27 = load i32, i32* %1
%28 = icmp slt i32 %27, 32768
%29 = select i1 %26, i1 %28, i1 false
br i1 %29, label %30, label %34
30: ; preds = %22
store i32 32768, i32* %1
store i32 4, i32* %2
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_SNDBUF
%33 = call i32 @setsockopt(i32 %8, i32 %31, i32 %32, i8* %3, i32 4)
br label %34
34: ; preds = %22, %30, %0
%35 = phi i32 [ -1, %0 ], [ %8, %30 ], [ %8, %22 ]
ret i32 %35
}