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

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
void set_socket_timeout(int socketfd, int timeout) {
  if (timeout < 0)
    return;
  struct timeval tv;
  tv.tv_sec = timeout > 0 ? timeout : 0;
  tv.tv_usec = 0;
  setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
             sizeof(struct timeval));
}


x86 assembly (clang, -O0):
<set_socket_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    112d <set_socket_timeout+0x1d>
jmp    117e <set_socket_timeout+0x6e>
cmpl   $0x0,-0x8(%rbp)
jle    1142 <set_socket_timeout+0x32>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    114c <set_socket_timeout+0x3c>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    114c <set_socket_timeout+0x3c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_socket_timeout>:
test   %esi,%esi
js     114b <set_socket_timeout+0x3b>
sub    $0x18,%rsp
mov    %esi,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2ebc(%rip),%rax
mov    (%rax),%esi
mov    0x2ea3(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
add    $0x18,%rsp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define void @set_socket_timeout(i32 %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = icmp slt i32 %1, 0
br i1 %4, label %12, label %5
5: ; preds = %2
%6 = bitcast %struct.timeval* %3 to i8*
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 %1, i32* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %8
%9 = load i32, i32* @SOL_SOCKET
%10 = load i32, i32* @SO_RCVTIMEO
%11 = call i32 @setsockopt(i32 %0, i32 %9, i32 %10, i8* %6, i32 16)
br label %12
12: ; preds = %2, %5
ret void
}