#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 {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
typedef int int64_t;

/* Variables and functions */
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int64_t timestamp(void) {
  struct timeval tv;
  int ret = gettimeofday(&tv, NULL);
  if (-1 == ret)
    return -1;
  return (int64_t)((int64_t)tv.tv_sec * 1000 + (int64_t)tv.tv_usec / 1000);
}


x86 assembly (clang, -O0):
<timestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    1142 <timestamp+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1165 <timestamp+0x55>
mov    -0x10(%rbp),%rax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timestamp>:
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmp    $0xffffffff,%eax
je     114d <timestamp+0x3d>
imul   $0x3e8,0x10(%rsp),%ecx
movslq 0x8(%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
add    %ecx,%eax
add    $0x18,%rsp
ret
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @timestamp() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = icmp eq i32 -1, %5
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 -1, i32* %1
br label %18
8: ; preds = %0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%10 = load i64, i64* %9
%11 = trunc i64 %10 to i32
%12 = mul nsw i32 %11, 1000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%14 = load i64, i64* %13
%15 = trunc i64 %14 to i32
%16 = sdiv i32 %15, 1000
%17 = add nsw i32 %12, %16
store i32 %17, i32* %1
br label %18
18: ; preds = %8, %7
%19 = load i32, i32* %1
ret i32 %19
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @timestamp() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%4 = icmp eq i32 %3, -1
br i1 %4, label %15, label %5
5: ; preds = %0
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%7 = load i64, i64* %6
%8 = trunc i64 %7 to i32
%9 = mul nsw i32 %8, 1000
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%11 = load i64, i64* %10
%12 = trunc i64 %11 to i32
%13 = sdiv i32 %12, 1000
%14 = add nsw i32 %13, %9
br label %15
15: ; preds = %0, %5
%16 = phi i32 [ %14, %5 ], [ -1, %0 ]
ret i32 %16
}