#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;
};
typedef int /*<<< orphan*/ send_timeval;
typedef int /*<<< orphan*/ recv_timeval;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ SO_SNDTIMEO;
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct timeval *,
               int);
int set_sock_time(int fd, int read_sec, int write_sec) {
  struct timeval send_timeval;
  struct timeval recv_timeval;
  if (fd <= 0)
    return -1;
  send_timeval.tv_sec = write_sec < 0 ? 0 : write_sec;
  send_timeval.tv_usec = 0;
  recv_timeval.tv_sec = read_sec < 0 ? 0 : read_sec;
  ;
  recv_timeval.tv_usec = 0;
  if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &send_timeval,
                 sizeof(send_timeval)) == -1) {
    return -1;
  }
  if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &recv_timeval,
                 sizeof(recv_timeval)) == -1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_sock_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     1137 <set_sock_time+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <set_sock_time+0xf0>
cmpl   $0x0,-0x10(%rbp)
jge    114b <set_sock_time+0x3b>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    1151 <set_sock_time+0x41>
mov    -0x10(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1173 <set_sock_time+0x63>
xor    %eax,%eax
mov    %eax,-0x38(%rbp)
jmp    1179 <set_sock_time+0x69>
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11c0 <set_sock_time+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <set_sock_time+0xf0>
mov    -0x8(%rbp),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11f9 <set_sock_time+0xe9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <set_sock_time+0xf0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_sock_time>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    $0xffffffff,%ebx
test   %edi,%edi
jle    1196 <set_sock_time+0x86>
mov    %edi,%ebp
xor    %eax,%eax
test   %edx,%edx
cmovs  %eax,%edx
mov    %edx,0x10(%rsp)
movq   $0x0,0x18(%rsp)
test   %esi,%esi
cmovns %esi,%eax
mov    %eax,(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e91(%rip),%r14
mov    (%r14),%esi
mov    0x2e97(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1196 <set_sock_time+0x86>
mov    (%r14),%esi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebp,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
sete   %bl
neg    %ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @set_sock_time(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca %struct.timeval
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = icmp sle i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -1, i32* %4
br label %46
13: ; preds = %3
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
br label %19
17: ; preds = %13
%18 = load i32, i32* %7
br label %19
19: ; preds = %17, %16
%20 = phi i32 [ 0, %16 ], [ %18, %17 ]
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %22
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
br label %28
26: ; preds = %19
%27 = load i32, i32* %6
br label %28
28: ; preds = %26, %25
%29 = phi i32 [ 0, %25 ], [ %27, %26 ]
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %31
%32 = load i32, i32* %5
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_SNDTIMEO
%35 = call i32 @setsockopt(i32 %32, i32 %33, i32 %34, %struct.timeval* %8, i32 16)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %28
store i32 -1, i32* %4
br label %46
38: ; preds = %28
%39 = load i32, i32* %5
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_RCVTIMEO
%42 = call i32 @setsockopt(i32 %39, i32 %40, i32 %41, %struct.timeval* %9, i32 16)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %38
store i32 -1, i32* %4
br label %46
45: ; preds = %38
store i32 0, i32* %4
br label %46
46: ; preds = %45, %44, %37, %12
%47 = load i32, i32* %4
ret i32 %47
}

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