#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*/ tv;
struct timeval {
  long long tv_sec;
  long long tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ anetSetError(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct timeval *,
               int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int anetRecvTimeout(char *err, int fd, long long ms) {
  struct timeval tv;
  tv.tv_sec = ms / 1000;
  tv.tv_usec = (ms % 1000) * 1000;
  if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
    anetSetError(err, "setsockopt SO_RCVTIMEO: %s", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<anetRecvTimeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
imul   $0x3e8,%rdx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11cf <anetRecvTimeout+0x9f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xe42(%rip),%rsi
call   1040 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <anetRecvTimeout+0xa6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetRecvTimeout>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rcx
mov    %esi,%ebx
mov    %rdi,%r14
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
mov    %rdx,0x8(%rsp)
imul   $0x3e8,%rdx,%rax
sub    %rax,%rcx
imul   $0x3e8,%rcx,%rax
mov    %rax,0x10(%rsp)
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11c9 <anetRecvTimeout+0x99>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe46(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1040 <anetSetError@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [27 x i8] c"setsockopt SO_RCVTIMEO: %s\00"
@errno = global i32 0
define i32 @anetRecvTimeout(i8* %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.timeval
store i8* %0, i8** %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%9 = load i64, i64* %7
%10 = sdiv i64 %9, 1000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i64 %10, i64* %11
%12 = load i64, i64* %7
%13 = srem i64 %12, 1000
%14 = mul nsw i64 %13, 1000
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 %14, i64* %15
%16 = load i32, i32* %6
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_RCVTIMEO
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, %struct.timeval* %8, i32 16)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = load i32, i32* @errno
%24 = call i32 @strerror(i32 %23)
%25 = call i32 @anetSetError(i8* %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %24)
store i32 -1, i32* %4
br label %27
26: ; preds = %3
store i32 0, i32* %4
br label %27
27: ; preds = %26, %21
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [27 x i8] c"setsockopt SO_RCVTIMEO: %s\00"
@errno = global i32 0
define i32 @anetRecvTimeout(i8* %0, i32 %1, i64 %2) {
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %4 to i8*
%6 = sdiv i64 %2, 1000
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i64 %6, i64* %7
%8 = srem i64 %2, 1000
%9 = mul nsw i64 %8, 1000
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 %9, i64* %10
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_RCVTIMEO
%13 = call i32 @setsockopt(i32 %1, i32 %11, i32 %12, %struct.timeval* %4, i32 16)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %3, %15
%20 = phi i32 [ -1, %15 ], [ 0, %3 ]
ret i32 %20
}