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

/* Variables and functions */
int /*<<< orphan*/ crash(char *);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int64_t now_ms() {
  struct timeval tv;
  if (gettimeofday(&tv, NULL) < 0)
    crash("no working clock");
  int64_t ms1 = tv.tv_sec;
  ms1 *= 1000;
  int64_t ms2 = tv.tv_usec;
  ms2 /= 1000;
  return ms1 + ms2;
}


x86 assembly (clang, -O0):
<now_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
cmp    $0x0,%rax
jge    114b <now_ms+0x2b>
lea    0xeba(%rip),%rdi
call   1030 <crash@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
imul   $0x3e8,-0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<now_ms>:
push   %rax
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
test   %rax,%rax
jns    113c <now_ms+0x1c>
lea    0xec9(%rip),%rdi
call   1030 <crash@plt>
imul   $0x3e8,(%rsp),%ecx
movslq 0x4(%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
add    %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [17 x i8] c"no working clock\00"
define i32 @now_ms() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = alloca i32
%4 = call i64 @gettimeofday(%struct.timeval* %1, i32* null)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @crash(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
%10 = load i32, i32* %9
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = mul nsw i32 %11, 1000
store i32 %12, i32* %2
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
%14 = load i32, i32* %13
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = sdiv i32 %15, 1000
store i32 %16, i32* %3
%17 = load i32, i32* %2
%18 = load i32, i32* %3
%19 = add nsw i32 %17, %18
ret i32 %19
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [17 x i8] c"no working clock\00"
define i32 @now_ms() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = call i64 @gettimeofday(%struct.timeval* %1, i32* null)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @crash(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%9 = load i32, i32* %8
%10 = mul nsw i32 %9, 1000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%12 = load i32, i32* %11
%13 = sdiv i32 %12, 1000
%14 = add nsw i32 %13, %10
ret i32 %14
}