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

/* Variables and functions */
struct timeval *esp_transport_utils_ms_to_timeval(int timeout_ms,
                                                  struct timeval *tv) {
  if (timeout_ms == -1) {
    return NULL;
  }
  tv->tv_sec = timeout_ms / 1000;
  tv->tv_usec = (timeout_ms - (tv->tv_sec * 1000)) * 1000;
  return tv;
}


x86 assembly (clang, -O0):
<esp_transport_utils_ms_to_timeval>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1122 <esp_transport_utils_ms_to_timeval+0x22>
movq   $0x0,-0x8(%rbp)
jmp    1159 <esp_transport_utils_ms_to_timeval+0x59>
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%rcx
imul   $0x3e8,(%rcx),%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<esp_transport_utils_ms_to_timeval>:
cmp    $0xffffffff,%edi
je     1133 <esp_transport_utils_ms_to_timeval+0x33>
mov    %rsi,%rax
movslq %edi,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,(%rsi)
imul   $0xfffffc18,%ecx,%ecx
add    %edi,%ecx
imul   $0x3e8,%ecx,%ecx
mov    %ecx,0x4(%rsi)
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define %struct.timeval* @esp_transport_utils_ms_to_timeval(i32 %0, %struct.timeval* %1) {
%3 = alloca %struct.timeval*
%4 = alloca i32
%5 = alloca %struct.timeval*
store i32 %0, i32* %4
store %struct.timeval* %1, %struct.timeval** %5
%6 = load i32, i32* %4
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %9
8: ; preds = %2
store %struct.timeval* null, %struct.timeval** %3
br label %24
9: ; preds = %2
%10 = load i32, i32* %4
%11 = sdiv i32 %10, 1000
%12 = load %struct.timeval*, %struct.timeval** %5
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i32, i32* %4
%15 = load %struct.timeval*, %struct.timeval** %5
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = mul nsw i32 %17, 1000
%19 = sub nsw i32 %14, %18
%20 = mul nsw i32 %19, 1000
%21 = load %struct.timeval*, %struct.timeval** %5
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %21, i32 0, i32 1
store i32 %20, i32* %22
%23 = load %struct.timeval*, %struct.timeval** %5
store %struct.timeval* %23, %struct.timeval** %3
br label %24
24: ; preds = %9, %8
%25 = load %struct.timeval*, %struct.timeval** %3
ret %struct.timeval* %25
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define %struct.timeval* @esp_transport_utils_ms_to_timeval(i32 %0, %struct.timeval* writeonly %1) {
%3 = icmp eq i32 %0, -1
br i1 %3, label %11, label %4
4: ; preds = %2
%5 = sdiv i32 %0, 1000
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
store i32 %5, i32* %6
%7 = mul nsw i32 %5, -1000
%8 = add i32 %7, %0
%9 = mul nsw i32 %8, 1000
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
store i32 %9, i32* %10
br label %11
11: ; preds = %2, %4
%12 = phi %struct.timeval* [ %1, %4 ], [ null, %2 ]
ret %struct.timeval* %12
}