#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*/ uint8_t;
struct timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
scalar_t__ recv(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char const *, int);
void clear_socket_buffer(int socket_fd) {
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = 100;
  setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof tv);
  int recv_buff_size = 2048;
  uint8_t recv_buff[recv_buff_size];
  while (recv(socket_fd, recv_buff, recv_buff_size, 0) > 0) {
  }
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof tv);
}


x86 assembly (clang, -O0):
<clear_socket_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x64,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
movl   $0x800,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jle    11ac <clear_socket_buffer+0x8c>
jmp    118c <clear_socket_buffer+0x6c>
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clear_socket_buffer>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2010,%rsp
mov    %edi,%ebx
movq   $0x0,0x8(%rsp)
movl   $0x64,(%rsp)
mov    0x2e9f(%rip),%r15
mov    (%r15),%esi
mov    0x2e85(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
mov    %rsp,%r13
lea    0x10(%rsp),%r14
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jg     1170 <clear_socket_buffer+0x50>
movq   $0x0,0x8(%rsp)
movl   $0x0,(%rsp)
mov    (%r15),%esi
mov    (%r12),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
mov    %r13,%rsp
add    $0x2010,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define void @clear_socket_buffer(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
store i32 %0, i32* %2
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 100, i32* %8
%9 = load i32, i32* %2
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_RCVTIMEO
%12 = bitcast %struct.timeval* %3 to i8*
%13 = call i32 @setsockopt(i32 %9, i32 %10, i32 %11, i8* %12, i32 16)
store i32 2048, i32* %4
%14 = load i32, i32* %4
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %5
%17 = alloca i32, i64 %15
store i64 %15, i64* %6
br label %18
18: ; preds = %23, %1
%19 = load i32, i32* %2
%20 = load i32, i32* %4
%21 = call i64 @recv(i32 %19, i32* %17, i32 %20, i32 0)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %18
24: ; preds = %18
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %25
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 0, i32* %26
%27 = load i32, i32* %2
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_RCVTIMEO
%30 = bitcast %struct.timeval* %3 to i8*
%31 = call i32 @setsockopt(i32 %27, i32 %28, i32 %29, i8* %30, i32 16)
%32 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define void @clear_socket_buffer(i32 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %4
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 100, i32* %5
%6 = load i32, i32* @SOL_SOCKET
%7 = load i32, i32* @SO_RCVTIMEO
%8 = call i32 @setsockopt(i32 %0, i32 %6, i32 %7, i8* %3, i32 16)
%9 = call i8* @llvm.stacksave()
%10 = alloca [2048 x i32]
%11 = getelementptr inbounds [2048 x i32], [2048 x i32]* %10, i64 0, i64 0
br label %12
12: ; preds = %12, %1
%13 = call i64 @recv(i32 %0, i32* %11, i32 2048, i32 0)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %12, label %15
15: ; preds = %12
store i64 0, i64* %4
store i32 0, i32* %5
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_RCVTIMEO
%18 = call i32 @setsockopt(i32 %0, i32 %16, i32 %17, i8* %3, i32 16)
ret void
}