#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 tv_sec;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct timeval *,
               int);
int set_rcvtimeo(int fd, void *optval, int optlen) {
  int rc;
  int to = *(int *)optval;
  struct timeval tv = {
      .tv_sec = to / 1000,
      .tv_usec = (to % 1000) * 1000,
  };
  rc = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  return rc;
}


x86 assembly (clang, -O0):
<set_rcvtimeo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_rcvtimeo>:
push   %rax
movslq (%rsi),%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0x4(%rsp)
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
pop    %rcx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @set_rcvtimeo(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %5
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
store i32 %12, i32* %8
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%14 = load i32, i32* %8
%15 = sdiv i32 %14, 1000
store i32 %15, i32* %13
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%17 = load i32, i32* %8
%18 = srem i32 %17, 1000
%19 = mul nsw i32 %18, 1000
store i32 %19, i32* %16
%20 = load i32, i32* %4
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_RCVTIMEO
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, %struct.timeval* %9, i32 8)
store i32 %23, i32* %7
%24 = load i32, i32* %7
ret i32 %24
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @set_rcvtimeo(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = bitcast i8* %1 to i32*
%6 = load i32, i32* %5
%7 = bitcast %struct.timeval* %4 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%9 = sdiv i32 %6, 1000
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%11 = srem i32 %6, 1000
%12 = mul nsw i32 %11, 1000
store i32 %12, i32* %10
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_RCVTIMEO
%15 = call i32 @setsockopt(i32 %0, i32 %13, i32 %14, %struct.timeval* %4, i32 8)
ret i32 %15
}