#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 */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int getMS() {
  static int firstTime = 1;
  static struct timeval previous_time;
  struct timeval new_time;
  if (firstTime) {
    firstTime = 0;
    gettimeofday(&previous_time, NULL);
    return 0;
  }
  gettimeofday(&new_time, NULL);
  int ms = (new_time.tv_sec - previous_time.tv_sec) * 1000 +
           (new_time.tv_usec - previous_time.tv_usec) / 1000;
  return ms;
}


x86 assembly (clang, -O0):
<getMS>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
cmpl   $0x0,0x2f09(%rip)
je     114b <getMS+0x3b>
movl   $0x0,0x2ef9(%rip)
lea    0x2efa(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
movl   $0x0,-0x4(%rbp)
jmp    118b <getMS+0x7b>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x10(%rbp),%eax
sub    0x2ecf(%rip),%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    0x2ec1(%rip),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getMS>:
push   %rbx
sub    $0x10,%rsp
cmpb   $0x0,0x2f10(%rip)
je     115f <getMS+0x4f>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x8(%rsp),%eax
mov    0xc(%rsp),%ecx
sub    0x2ef8(%rip),%eax
imul   $0x3e8,%eax,%eax
sub    0x2ef0(%rip),%ecx
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rbx
mov    %rbx,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rbx
add    %ecx,%ebx
add    %eax,%ebx
jmp    1176 <getMS+0x66>
movb   $0x1,0x2ec6(%rip)
lea    0x2ec3(%rip),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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