#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 */
struct linger {
  int l_onoff;
  int l_linger;
};
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ len;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ SO_LINGER;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ SO_SNDBUF;
int /*<<< orphan*/ close(int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
void x_set_sock_default_opt(int s) {
  int on = 1;
  int result = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
  if (result < 0) {
    close(s);
    return;
  }
  on = 1;
  result = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof(on));
  struct linger optval;
  optval.l_onoff = 1;
  optval.l_linger = 60;
  result = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&optval,
                      sizeof(struct linger));
  if (result < 0) {
    close(s);
    return;
  }
  int len = 2 * 1024 * 1024;
  setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&len, sizeof(len));
  setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&len, sizeof(len));
  return;
}


x86 assembly (clang, -O0):
<x_set_sock_default_opt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1170 <x_set_sock_default_opt+0x50>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
jmp    1239 <x_set_sock_default_opt+0x119>
movl   $0x1,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0xc(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x3c,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ea <x_set_sock_default_opt+0xca>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
jmp    1239 <x_set_sock_default_opt+0x119>
movl   $0x200000,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x4(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x_set_sock_default_opt>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movl   $0x1,(%rsp)
mov    0x2eaf(%rip),%r15
mov    (%r15),%esi
mov    0x2e7d(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     11f6 <x_set_sock_default_opt+0xd6>
movl   $0x1,(%rsp)
mov    (%r15),%esi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movabs $0x3c00000001,%rax
mov    %rax,0x8(%rsp)
mov    (%r15),%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     11f6 <x_set_sock_default_opt+0xd6>
movl   $0x200000,0x4(%rsp)
mov    (%r15),%esi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%r15),%esi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
jmp    11fd <x_set_sock_default_opt+0xdd>
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.linger = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_KEEPALIVE = global i32 0
@SO_LINGER = global i32 0
@SO_RCVBUF = global i32 0
@SO_SNDBUF = global i32 0
define void @x_set_sock_default_opt(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.linger
%6 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %3
%7 = load i32, i32* %2
%8 = load i32, i32* @SOL_SOCKET
%9 = load i32, i32* @SO_REUSEADDR
%10 = bitcast i32* %3 to i8*
%11 = call i32 @setsockopt(i32 %7, i32 %8, i32 %9, i8* %10, i32 4)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* %2
%16 = call i32 @close(i32 %15)
br label %46
17: ; preds = %1
store i32 1, i32* %3
%18 = load i32, i32* %2
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_KEEPALIVE
%21 = bitcast i32* %3 to i8*
%22 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i8* %21, i32 4)
store i32 %22, i32* %4
%23 = getelementptr inbounds %struct.linger, %struct.linger* %5, i32 0, i32 0
store i32 1, i32* %23
%24 = getelementptr inbounds %struct.linger, %struct.linger* %5, i32 0, i32 1
store i32 60, i32* %24
%25 = load i32, i32* %2
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_LINGER
%28 = bitcast %struct.linger* %5 to i8*
%29 = call i32 @setsockopt(i32 %25, i32 %26, i32 %27, i8* %28, i32 8)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %17
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
br label %46
35: ; preds = %17
store i32 2097152, i32* %6
%36 = load i32, i32* %2
%37 = load i32, i32* @SOL_SOCKET
%38 = load i32, i32* @SO_RCVBUF
%39 = bitcast i32* %6 to i8*
%40 = call i32 @setsockopt(i32 %36, i32 %37, i32 %38, i8* %39, i32 4)
%41 = load i32, i32* %2
%42 = load i32, i32* @SOL_SOCKET
%43 = load i32, i32* @SO_SNDBUF
%44 = bitcast i32* %6 to i8*
%45 = call i32 @setsockopt(i32 %41, i32 %42, i32 %43, i8* %44, i32 4)
br label %46
46: ; preds = %35, %32, %14
ret void
}

LLVM IR (-O2):
%struct.linger = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_KEEPALIVE = global i32 0
@SO_LINGER = global i32 0
@SO_RCVBUF = global i32 0
@SO_SNDBUF = global i32 0
define void @x_set_sock_default_opt(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.linger
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%6 = load i32, i32* @SOL_SOCKET
%7 = load i32, i32* @SO_REUSEADDR
%8 = call i32 @setsockopt(i32 %0, i32 %6, i32 %7, i8* %5, i32 4)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @close(i32 %0)
br label %34
12: ; preds = %1
store i32 1, i32* %2
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_KEEPALIVE
%15 = call i32 @setsockopt(i32 %0, i32 %13, i32 %14, i8* %5, i32 4)
%16 = bitcast %struct.linger* %3 to i8*
%17 = getelementptr inbounds %struct.linger, %struct.linger* %3, i64 0, i32 0
store i32 1, i32* %17
%18 = getelementptr inbounds %struct.linger, %struct.linger* %3, i64 0, i32 1
store i32 60, i32* %18
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_LINGER
%21 = call i32 @setsockopt(i32 %0, i32 %19, i32 %20, i8* %16, i32 8)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %12
%24 = call i32 @close(i32 %0)
br label %33
25: ; preds = %12
%26 = bitcast i32* %4 to i8*
store i32 2097152, i32* %4
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_RCVBUF
%29 = call i32 @setsockopt(i32 %0, i32 %27, i32 %28, i8* %26, i32 4)
%30 = load i32, i32* @SOL_SOCKET
%31 = load i32, i32* @SO_SNDBUF
%32 = call i32 @setsockopt(i32 %0, i32 %30, i32 %31, i8* %26, i32 4)
br label %33
33: ; preds = %25, %23
br label %34
34: ; preds = %33, %10
ret void
}