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

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ SO_SNDBUF;
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
void sock_optimize(int sock, int flags) {
  if (flags & 0x0001) {
    int sockbuf = (1024 * 1024);
    while (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &sockbuf, sizeof(int))) {
      sockbuf >>= 1;
    }
  }
  if (flags & 0x0002) {
    int sockbuf = (1024 * 1024);
    while (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sockbuf, sizeof(int))) {
      sockbuf >>= 1;
    }
  }
  if (flags & 0x0008) {
    int val = 1;
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) == -1) {
      perror("SO_REUSEADDR");
    }
  }
}


x86 assembly (clang, -O0):
<sock_optimize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1184 <sock_optimize+0x64>
movl   $0x100000,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%eax
je     117f <sock_optimize+0x5f>
mov    -0xc(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1144 <sock_optimize+0x24>
jmp    1184 <sock_optimize+0x64>
mov    -0x8(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11da <sock_optimize+0xba>
movl   $0x100000,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%eax
je     11d5 <sock_optimize+0xb5>
mov    -0x10(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    119a <sock_optimize+0x7a>
jmp    11da <sock_optimize+0xba>
mov    -0x8(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     122e <sock_optimize+0x10e>
movl   $0x1,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1229 <sock_optimize+0x109>
lea    0xddc(%rip),%rdi
call   1040 <perror@plt>
jmp    122e <sock_optimize+0x10e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_optimize>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
test   $0x1,%r14b
je     118f <sock_optimize+0x6f>
movl   $0x100000,0x8(%rsp)
mov    0x2ea2(%rip),%r15
mov    (%r15),%esi
mov    0x2e80(%rip),%r12
mov    (%r12),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
je     118f <sock_optimize+0x6f>
lea    0x8(%rsp),%rbp
nop
sarl   0x8(%rsp)
mov    (%r15),%esi
mov    (%r12),%edx
mov    %ebx,%edi
mov    %rbp,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
jne    1170 <sock_optimize+0x50>
test   $0x2,%r14b
je     11ef <sock_optimize+0xcf>
movl   $0x100000,0x4(%rsp)
mov    0x2e44(%rip),%r15
mov    (%r15),%esi
mov    0x2e32(%rip),%r12
mov    (%r12),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
je     11ef <sock_optimize+0xcf>
lea    0x4(%rsp),%rbp
nopl   (%rax)
sarl   0x4(%rsp)
mov    (%r15),%esi
mov    (%r12),%edx
mov    %ebx,%edi
mov    %rbp,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
jne    11d0 <sock_optimize+0xb0>
test   $0x8,%r14b
je     1226 <sock_optimize+0x106>
movl   $0x1,0xc(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1233 <sock_optimize+0x113>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc6(%rip),%rdi
call   1040 <perror@plt>
jmp    1226 <sock_optimize+0x106>

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@SO_SNDBUF = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [13 x i8] c"SO_REUSEADDR\00"
define void @sock_optimize(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
%8 = load i32, i32* %4
%9 = and i32 %8, 1
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %2
store i32 1048576, i32* %5
br label %12
12: ; preds = %18, %11
%13 = load i32, i32* %3
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_RCVBUF
%16 = call i32 @setsockopt(i32 %13, i32 %14, i32 %15, i32* %5, i32 4)
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i32, i32* %5
%20 = ashr i32 %19, 1
store i32 %20, i32* %5
br label %12
21: ; preds = %12
br label %22
22: ; preds = %21, %2
%23 = load i32, i32* %4
%24 = and i32 %23, 2
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %22
store i32 1048576, i32* %6
br label %27
27: ; preds = %33, %26
%28 = load i32, i32* %3
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_SNDBUF
%31 = call i32 @setsockopt(i32 %28, i32 %29, i32 %30, i32* %6, i32 4)
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = load i32, i32* %6
%35 = ashr i32 %34, 1
store i32 %35, i32* %6
br label %27
36: ; preds = %27
br label %37
37: ; preds = %36, %22
%38 = load i32, i32* %4
%39 = and i32 %38, 8
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %37
store i32 1, i32* %7
%42 = load i32, i32* %3
%43 = load i32, i32* @SOL_SOCKET
%44 = load i32, i32* @SO_REUSEADDR
%45 = call i32 @setsockopt(i32 %42, i32 %43, i32 %44, i32* %7, i32 4)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %49
49: ; preds = %47, %41
br label %50
50: ; preds = %49, %37
ret void
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@SO_SNDBUF = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [13 x i8] c"SO_REUSEADDR\00"
define void @sock_optimize(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = and i32 %1, 1
%7 = icmp eq i32 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %2
%9 = bitcast i32* %3 to i8*
store i32 1048576, i32* %3
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_RCVBUF
%12 = call i32 @setsockopt(i32 %0, i32 %10, i32 %11, i32* %3, i32 4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %8, %14
%15 = load i32, i32* %3
%16 = ashr i32 %15, 1
store i32 %16, i32* %3
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_RCVBUF
%19 = call i32 @setsockopt(i32 %0, i32 %17, i32 %18, i32* %3, i32 4)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %14
21: ; preds = %14, %8
br label %22
22: ; preds = %21, %2
%23 = and i32 %1, 2
%24 = icmp eq i32 %23, 0
br i1 %24, label %39, label %25
25: ; preds = %22
%26 = bitcast i32* %4 to i8*
store i32 1048576, i32* %4
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_SNDBUF
%29 = call i32 @setsockopt(i32 %0, i32 %27, i32 %28, i32* %4, i32 4)
%30 = icmp eq i32 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %25, %31
%32 = load i32, i32* %4
%33 = ashr i32 %32, 1
store i32 %33, i32* %4
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_SNDBUF
%36 = call i32 @setsockopt(i32 %0, i32 %34, i32 %35, i32* %4, i32 4)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %31
38: ; preds = %31, %25
br label %39
39: ; preds = %38, %22
%40 = and i32 %1, 8
%41 = icmp eq i32 %40, 0
br i1 %41, label %51, label %42
42: ; preds = %39
%43 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_REUSEADDR
%46 = call i32 @setsockopt(i32 %0, i32 %44, i32 %45, i32* %5, i32 4)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %50
50: ; preds = %48, %42
br label %51
51: ; preds = %50, %39
ret void
}