#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;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct timeval *,
               int *);
int get_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,
  };
  socklen_t koptlen = sizeof(tv);
  rc = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, &koptlen);
  if (rc == 0)
    *(int *)optval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
  return rc;
}


x86 assembly (clang, -O0):
<get_rcvtimeo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x24(%rbp)
movl   $0x8,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
lea    -0x2c(%rbp),%r8
call   1030 <getsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    119e <get_rcvtimeo+0x8e>
imul   $0x3e8,-0x28(%rbp),%ecx
mov    -0x24(%rbp),%eax
mov    $0x3e8,%esi
cltd
idiv   %esi
add    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_rcvtimeo>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
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)
movl   $0x8,0xc(%rsp)
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
lea    0xc(%rsp),%r8
call   1030 <getsockopt@plt>
test   %eax,%eax
jne    1193 <get_rcvtimeo+0x83>
imul   $0x3e8,(%rsp),%ecx
movslq 0x4(%rsp),%rdx
imul   $0x10624dd3,%rdx,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0x26,%rdx
add    %esi,%edx
add    %ecx,%edx
mov    %edx,(%rbx)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @get_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
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* %2, i32** %6
%11 = load i8*, i8** %5
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
store i32 %13, i32* %8
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%15 = load i32, i32* %8
%16 = sdiv i32 %15, 1000
store i32 %16, i32* %14
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%18 = load i32, i32* %8
%19 = srem i32 %18, 1000
%20 = mul nsw i32 %19, 1000
store i32 %20, i32* %17
store i32 8, i32* %10
%21 = load i32, i32* %4
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_RCVTIMEO
%24 = call i32 @getsockopt(i32 %21, i32 %22, i32 %23, %struct.timeval* %9, i32* %10)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %3
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%29 = load i32, i32* %28
%30 = mul nsw i32 %29, 1000
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%32 = load i32, i32* %31
%33 = sdiv i32 %32, 1000
%34 = add nsw i32 %30, %33
%35 = load i8*, i8** %5
%36 = bitcast i8* %35 to i32*
store i32 %34, i32* %36
br label %37
37: ; preds = %27, %3
%38 = load i32, i32* %7
ret i32 %38
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @get_rcvtimeo(i32 %0, i8* nocapture %1, i32* nocapture readnone %2) {
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = bitcast i8* %1 to i32*
%7 = load i32, i32* %6
%8 = bitcast %struct.timeval* %4 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%10 = sdiv i32 %7, 1000
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%12 = srem i32 %7, 1000
%13 = mul nsw i32 %12, 1000
store i32 %13, i32* %11
%14 = bitcast i32* %5 to i8*
store i32 8, i32* %5
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_RCVTIMEO
%17 = call i32 @getsockopt(i32 %0, i32 %15, i32 %16, %struct.timeval* %4, i32* %5)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %3
%20 = load i32, i32* %9
%21 = mul nsw i32 %20, 1000
%22 = load i32, i32* %11
%23 = sdiv i32 %22, 1000
%24 = add nsw i32 %23, %21
store i32 %24, i32* %6
br label %25
25: ; preds = %19, %3
ret i32 %17
}