#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*/ time_t;
struct timeval {
  scalar_t__ tv_usec;
  int /*<<< orphan*/ tv_sec;
};
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
size_t recv(int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              struct timeval *, int);
int tracker_recv_dgram(int sockfd, char *buff, size_t max, size_t *dgram_size,
                       time_t timeout) {
  struct timeval tv;
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));
  ssize_t nb = recv(sockfd, buff, max, 0);
  if (nb < 0)
    return -1;
  *dgram_size = nb;
  return 0;
}


x86 assembly (clang, -O0):
<tracker_recv_dgram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jae    119a <tracker_recv_dgram+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <tracker_recv_dgram+0x8c>
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tracker_recv_dgram>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %r8d,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,(%r14)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @tracker_recv_dgram(i32 %0, i8* %1, i64 %2, i64* nocapture writeonly %3, i32 %4) {
%6 = alloca %struct.timeval
%7 = bitcast %struct.timeval* %6 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i32 %4, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i64 0, i64* %9
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_RCVTIMEO
%12 = call i32 @setsockopt(i32 %0, i32 %10, i32 %11, %struct.timeval* %6, i32 16)
%13 = call i64 @recv(i32 %0, i8* %1, i64 %2, i32 0)
store i64 %13, i64* %3
ret i32 0
}