#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*/ rcv_buffsize_base;
typedef int /*<<< orphan*/ rcv_buffsize;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int SO_RCVBUF;
int /*<<< orphan*/ errno;
scalar_t__ getsockopt(int, int /*<<< orphan*/, int, int *, int *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int, int *, int);
int strerror(int /*<<< orphan*/);
int /*<<< orphan*/ traceEvent(int, char *, int, char *, ...);
void maximize_socket_buffer(int sock_fd, int buf_type) {
  int i, rcv_buffsize_base, rcv_buffsize, max_buf_size = 1024 * 2 * 1024,
                                          debug = 0;
  socklen_t len = sizeof(rcv_buffsize_base);
  if (getsockopt(sock_fd, SOL_SOCKET, buf_type, &rcv_buffsize_base, &len) < 0) {
    traceEvent(0,
               "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
               "outputs/2022-01-22/02-19-57/repos/kvitaly2005/lprobe/refs/"
               "heads/master/utils/nf_reflector.c",
               638, "Unable to read socket receiver buffer size [%s]",
               strerror(errno));
    return;
  } else {
    if (debug)
      traceEvent(3,
                 "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                 "outputs/2022-01-22/02-19-57/repos/kvitaly2005/lprobe/refs/"
                 "heads/master/utils/nf_reflector.c",
                 642, "Default socket %s buffer size is %d",
                 buf_type == SO_RCVBUF ? "receive" : "send", rcv_buffsize_base);
  }
  for (i = 2;; i++) {
    rcv_buffsize = i * rcv_buffsize_base;
    if (rcv_buffsize > max_buf_size)
      break;
    if (setsockopt(sock_fd, SOL_SOCKET, buf_type, &rcv_buffsize,
                   sizeof(rcv_buffsize)) < 0) {
      if (debug)
        traceEvent(0,
                   "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                   "outputs/2022-01-22/02-19-57/repos/kvitaly2005/lprobe/refs/"
                   "heads/master/utils/nf_reflector.c",
                   652, "Unable to set socket %s buffer size [%s]",
                   buf_type == SO_RCVBUF ? "receive" : "send", strerror(errno));
      break;
    } else if (debug)
      traceEvent(3,
                 "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                 "outputs/2022-01-22/02-19-57/repos/kvitaly2005/lprobe/refs/"
                 "heads/master/utils/nf_reflector.c",
                 657, "%s socket buffer size set %d",
                 buf_type == SO_RCVBUF ? "Receive" : "Send", rcv_buffsize);
  }
}


x86 assembly (clang, -O0):
<maximize_socket_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x200000,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x4,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
lea    -0x10(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1040 <getsockopt@plt>
cmp    $0x0,%rax
jge    11bb <maximize_socket_buffer+0x7b>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%r8d
xor    %edi,%edi
lea    0xe5d(%rip),%rsi
mov    $0x27e,%edx
lea    0xee5(%rip),%rcx
mov    $0x0,%al
call   1050 <traceEvent@plt>
jmp    1320 <maximize_socket_buffer+0x1e0>
cmpl   $0x0,-0x1c(%rbp)
je     1208 <maximize_socket_buffer+0xc8>
mov    -0x8(%rbp),%ecx
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
lea    0xf18(%rip),%r8
lea    0xf09(%rip),%rax
cmp    %edx,%ecx
cmove  %rax,%r8
mov    -0x10(%rbp),%r9d
mov    $0x3,%edi
lea    0xe0b(%rip),%rsi
mov    $0x282,%edx
lea    0xec3(%rip),%rcx
mov    $0x0,%al
call   1050 <traceEvent@plt>
jmp    120d <maximize_socket_buffer+0xcd>
movl   $0x2,-0xc(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    122f <maximize_socket_buffer+0xef>
jmp    1320 <maximize_socket_buffer+0x1e0>
mov    -0x4(%rbp),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    12bb <maximize_socket_buffer+0x17b>
cmpl   $0x0,-0x1c(%rbp)
je     12b6 <maximize_socket_buffer+0x176>
mov    -0x8(%rbp),%edx
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rax
lea    0xe6d(%rip),%rcx
cmp    %esi,%edx
cmove  %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%r8
mov    %eax,%r9d
xor    %edi,%edi
lea    0xd5d(%rip),%rsi
mov    $0x28c,%edx
lea    0xe46(%rip),%rcx
mov    $0x0,%al
call   1050 <traceEvent@plt>
jmp    1320 <maximize_socket_buffer+0x1e0>
cmpl   $0x0,-0x1c(%rbp)
je     1308 <maximize_socket_buffer+0x1c8>
mov    -0x8(%rbp),%ecx
mov    0x2d09(%rip),%rax
mov    (%rax),%edx
lea    0xe6b(%rip),%r8
lea    0xe5c(%rip),%rax
cmp    %edx,%ecx
cmove  %rax,%r8
mov    -0x14(%rbp),%r9d
mov    $0x3,%edi
lea    0xd0b(%rip),%rsi
mov    $0x291,%edx
lea    0xe1d(%rip),%rcx
mov    $0x0,%al
call   1050 <traceEvent@plt>
jmp    130d <maximize_socket_buffer+0x1cd>
jmp    1312 <maximize_socket_buffer+0x1d2>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1214 <maximize_socket_buffer+0xd4>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<maximize_socket_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r12d
mov    %edi,%ebp
movl   $0x4,0xc(%rsp)
mov    0x2e88(%rip),%r15
mov    (%r15),%esi
lea    0x8(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %r12d,%edx
call   1040 <getsockopt@plt>
test   %rax,%rax
js     11d2 <maximize_socket_buffer+0x92>
mov    0x8(%rsp),%eax
lea    (%rax,%rax,1),%ecx
mov    %ecx,0x4(%rsp)
cmp    $0x100000,%eax
jg     11ff <maximize_socket_buffer+0xbf>
mov    $0x3,%ebx
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
mov    (%r15),%esi
mov    %ebp,%edi
mov    %r12d,%edx
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11ff <maximize_socket_buffer+0xbf>
mov    0x8(%rsp),%eax
imul   %ebx,%eax
mov    %eax,0x4(%rsp)
add    $0x1,%ebx
cmp    $0x200000,%eax
jle    11a0 <maximize_socket_buffer+0x60>
jmp    11ff <maximize_socket_buffer+0xbf>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe19(%rip),%rsi
lea    0xea6(%rip),%rcx
xor    %edi,%edi
mov    $0x27e,%edx
mov    %eax,%r8d
xor    %eax,%eax
call   1050 <traceEvent@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@.str = [148 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/kvitaly2005/lprobe/refs/heads/master/utils/nf_reflector.c\00"
@.str.1 = [48 x i8] c"Unable to read socket receiver buffer size [%s]\00"
@errno = global i32 0
@.str.2 = [36 x i8] c"Default socket %s buffer size is %d\00"
@SO_RCVBUF = global i32 0
@.str.3 = [8 x i8] c"receive\00"
@.str.4 = [5 x i8] c"send\00"
@.str.5 = [41 x i8] c"Unable to set socket %s buffer size [%s]\00"
@.str.6 = [29 x i8] c"%s socket buffer size set %d\00"
@.str.7 = [8 x i8] c"Receive\00"
@.str.8 = [5 x i8] c"Send\00"
define void @maximize_socket_buffer(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 2097152, i32* %8
store i32 0, i32* %9
store i32 4, i32* %10
%11 = load i32, i32* %3
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* %4
%14 = call i64 @getsockopt(i32 %11, i32 %12, i32 %13, i32* %6, i32* %10)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 (i32, i8*, i32, i8*, ...) @traceEvent(i32 0, i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str, i64 0, i64 0), i32 638, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %76
20: ; preds = %2
%21 = load i32, i32* %9
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i32, i32* %4
%25 = load i32, i32* @SO_RCVBUF
%26 = icmp eq i32 %24, %25
%27 = zext i1 %26 to i64
%28 = select i1 %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0)
%29 = load i32, i32* %6
%30 = call i32 (i32, i8*, i32, i8*, ...) @traceEvent(i32 3, i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str, i64 0, i64 0), i32 642, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %28, i32 %29)
br label %31
31: ; preds = %23, %20
br label %32
32: ; preds = %31
store i32 2, i32* %5
br label %33
33: ; preds = %73, %32
%34 = load i32, i32* %5
%35 = load i32, i32* %6
%36 = mul nsw i32 %34, %35
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = load i32, i32* %8
%39 = icmp sgt i32 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %33
br label %76
41: ; preds = %33
%42 = load i32, i32* %3
%43 = load i32, i32* @SOL_SOCKET
%44 = load i32, i32* %4
%45 = call i64 @setsockopt(i32 %42, i32 %43, i32 %44, i32* %7, i32 4)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %60
47: ; preds = %41
%48 = load i32, i32* %9
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %59
50: ; preds = %47
%51 = load i32, i32* %4
%52 = load i32, i32* @SO_RCVBUF
%53 = icmp eq i32 %51, %52
%54 = zext i1 %53 to i64
%55 = select i1 %53, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0)
%56 = load i32, i32* @errno
%57 = call i32 @strerror(i32 %56)
%58 = call i32 (i32, i8*, i32, i8*, ...) @traceEvent(i32 0, i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str, i64 0, i64 0), i32 652, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %55, i32 %57)
br label %59
59: ; preds = %50, %47
br label %76
60: ; preds = %41
%61 = load i32, i32* %9
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %71
63: ; preds = %60
%64 = load i32, i32* %4
%65 = load i32, i32* @SO_RCVBUF
%66 = icmp eq i32 %64, %65
%67 = zext i1 %66 to i64
%68 = select i1 %66, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0)
%69 = load i32, i32* %7
%70 = call i32 (i32, i8*, i32, i8*, ...) @traceEvent(i32 3, i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str, i64 0, i64 0), i32 657, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %68, i32 %69)
br label %71
71: ; preds = %63, %60
br label %72
72: ; preds = %71
br label %73
73: ; preds = %72
%74 = load i32, i32* %5
%75 = add nsw i32 %74, 1
store i32 %75, i32* %5
br label %33
76: ; preds = %16, %59, %40
ret void
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@.str = [148 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/kvitaly2005/lprobe/refs/heads/master/utils/nf_reflector.c\00"
@.str.1 = [48 x i8] c"Unable to read socket receiver buffer size [%s]\00"
@errno = global i32 0
@SO_RCVBUF = global i32 0
define void @maximize_socket_buffer(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%9 = load i32, i32* @SOL_SOCKET
%10 = call i64 @getsockopt(i32 %0, i32 %9, i32 %1, i32* %3, i32* %5)
%11 = icmp slt i64 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i32, i32* %3
%14 = shl nsw i32 %13, 1
store i32 %14, i32* %4
%15 = icmp sgt i32 %13, 1048576
br i1 %15, label %30, label %24
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 (i32, i8*, i32, i8*, ...) @traceEvent(i32 0, i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str, i64 0, i64 0), i32 638, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %30
20: ; preds = %24
%21 = load i32, i32* %3
%22 = mul nsw i32 %21, %29
store i32 %22, i32* %4
%23 = icmp sgt i32 %22, 2097152
br i1 %23, label %30, label %24
24: ; preds = %12, %20
%25 = phi i32 [ %29, %20 ], [ 2, %12 ]
%26 = load i32, i32* @SOL_SOCKET
%27 = call i64 @setsockopt(i32 %0, i32 %26, i32 %1, i32* %4, i32 4)
%28 = icmp slt i64 %27, 0
%29 = add nuw nsw i32 %25, 1
br i1 %28, label %30, label %20
30: ; preds = %24, %20, %12, %16
ret void
}