#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 {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ ms2timeval(struct timeval *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct timeval *, int);
int sam3tcpSetTimeoutReceive(int fd, int timeoutms) {
  if (fd >= 0 && timeoutms >= 0) {
    struct timeval tv;
    ms2timeval(&tv, timeoutms);
    return (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0 ? -1
                                                                         : 0);
  }
  return -1;
}


x86 assembly (clang, -O0):
<sam3tcpSetTimeoutReceive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     118b <sam3tcpSetTimeoutReceive+0x6b>
cmpl   $0x0,-0xc(%rbp)
jl     118b <sam3tcpSetTimeoutReceive+0x6b>
mov    -0xc(%rbp),%esi
lea    -0x10(%rbp),%rdi
call   1040 <ms2timeval@plt>
mov    -0x8(%rbp),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %rax,%rdx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%rdx
cmovl  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1192 <sam3tcpSetTimeoutReceive+0x72>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sam3tcpSetTimeoutReceive>:
mov    %esi,%eax
or     %edi,%eax
js     1165 <sam3tcpSetTimeoutReceive+0x45>
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <ms2timeval@plt>
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
mov    0x2e91(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
sar    $0x3f,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @sam3tcpSetTimeoutReceive(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %22
9: ; preds = %2
%10 = load i32, i32* %5
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = load i32, i32* %5
%14 = call i32 @ms2timeval(%struct.timeval* %6, i32 %13)
%15 = load i32, i32* %4
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_RCVTIMEO
%18 = call i64 @setsockopt(i32 %15, i32 %16, i32 %17, %struct.timeval* %6, i32 4)
%19 = icmp slt i64 %18, 0
%20 = zext i1 %19 to i64
%21 = select i1 %19, i32 -1, i32 0
store i32 %21, i32* %3
br label %23
22: ; preds = %9, %2
store i32 -1, i32* %3
br label %23
23: ; preds = %22, %12
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @sam3tcpSetTimeoutReceive(i32 %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = or i32 %1, %0
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %14
6: ; preds = %2
%7 = bitcast %struct.timeval* %3 to i8*
%8 = call i32 @ms2timeval(%struct.timeval* %3, i32 %1)
%9 = load i32, i32* @SOL_SOCKET
%10 = load i32, i32* @SO_RCVTIMEO
%11 = call i64 @setsockopt(i32 %0, i32 %9, i32 %10, %struct.timeval* %3, i32 4)
%12 = ashr i64 %11, 63
%13 = trunc i64 %12 to i32
br label %14
14: ; preds = %2, %6
%15 = phi i32 [ %13, %6 ], [ -1, %2 ]
ret i32 %15
}