#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 */
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int linux_get_ms(unsigned long *count) {
  struct timeval t;
  if (!count)
    return -1;
  if (gettimeofday(&t, NULL) < 0) {
    perror("gettimeofday");
    return -1;
  }
  *count = (t.tv_sec * 1000) + (t.tv_usec / 1000);
  return 0;
}


x86 assembly (clang, -O0):
<linux_get_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1143 <linux_get_ms+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <linux_get_ms+0x7f>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
jge    1172 <linux_get_ms+0x52>
lea    0xe9f(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <linux_get_ms+0x7f>
imul   $0x3e8,-0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
movslq %eax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linux_get_ms>:
push   %rbp
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     116e <linux_get_ms+0x4e>
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
js     1177 <linux_get_ms+0x57>
movslq (%rsp),%rax
imul   $0x3e8,%rax,%rax
movslq 0x4(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
movslq %ecx,%rcx
add    %rax,%rcx
mov    %rcx,(%rbx)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe82(%rip),%rdi
call   1040 <perror@plt>
jmp    116e <linux_get_ms+0x4e>

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

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