#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*/ *);
long logging_get_ticks() {
  static struct timeval tv0;
  static int inited = 0;
  if (!inited) {
    gettimeofday(&tv0, NULL);
    inited = 1;
  }
  struct timeval tv;
  if (gettimeofday(&tv, NULL) == 0) {
    return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
  }
  return 0;
}


x86 assembly (clang, -O0):
<logging_get_ticks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
cmpl   $0x0,0x2f15(%rip)
jne    113f <logging_get_ticks+0x2f>
lea    0x2f00(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
movl   $0x1,0x2ef5(%rip)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
jne    118b <logging_get_ticks+0x7b>
mov    -0x10(%rbp),%eax
sub    0x2ecd(%rip),%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
sub    0x2ebf(%rip),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
add    %ecx,%eax
cltq
mov    %rax,-0x8(%rbp)
jmp    1193 <logging_get_ticks+0x83>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logging_get_ticks>:
push   %rbx
sub    $0x10,%rsp
cmpb   $0x0,0x2f18(%rip)
jne    1133 <logging_get_ticks+0x23>
lea    0x2f07(%rip),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movb   $0x1,0x2f01(%rip)
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
jne    117c <logging_get_ticks+0x6c>
mov    0x8(%rsp),%eax
mov    0xc(%rsp),%ecx
sub    0x2ed8(%rip),%eax
sub    0x2ed6(%rip),%ecx
imul   $0x3e8,%eax,%eax
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
add    %eax,%ecx
movslq %ecx,%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@logging_get_ticks.tv0 = internal global %struct.timeval zeroinitializer
@logging_get_ticks.inited = internal unnamed_addr global i1 false
define i64 @logging_get_ticks() {
%1 = alloca %struct.timeval
%2 = load i1, i1* @logging_get_ticks.inited
br i1 %2, label %5, label %3
3: ; preds = %0
%4 = tail call i64 @gettimeofday(%struct.timeval* @logging_get_ticks.tv0, i32* null)
store i1 true, i1* @logging_get_ticks.inited
br label %5
5: ; preds = %3, %0
%6 = bitcast %struct.timeval* %1 to i8*
%7 = call i64 @gettimeofday(%struct.timeval* %1, i32* null)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %22
9: ; preds = %5
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @logging_get_ticks.tv0, i64 0, i32 0)
%13 = sub nsw i32 %11, %12
%14 = mul nsw i32 %13, 1000
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%16 = load i32, i32* %15
%17 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @logging_get_ticks.tv0, i64 0, i32 1)
%18 = sub nsw i32 %16, %17
%19 = sdiv i32 %18, 1000
%20 = add nsw i32 %19, %14
%21 = sext i32 %20 to i64
br label %22
22: ; preds = %5, %9
%23 = phi i64 [ %21, %9 ], [ 0, %5 ]
ret i64 %23
}