#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;
};

/* Variables and functions */
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/);
unsigned int sat_timems(unsigned int subtract) {
  struct timeval tv;
  unsigned int r;
  if (gettimeofday(&tv, 0))
    return 0;
  r = (((unsigned int)tv.tv_sec >> 16) * 1000) << 16;
  r += ((unsigned int)tv.tv_sec & 0xffff) * 1000;
  r += (unsigned int)tv.tv_usec / 1000;
  return r - subtract;
}


x86 assembly (clang, -O0):
<sat_timems>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
je     113c <sat_timems+0x2c>
movl   $0x0,-0x4(%rbp)
jmp    1180 <sat_timems+0x70>
mov    -0x10(%rbp),%rax
shr    $0x10,%eax
imul   $0x3e8,%eax,%eax
shl    $0x10,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
and    $0xffff,%eax
imul   $0x3e8,%eax,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sat_timems>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
jne    1156 <sat_timems+0x46>
mov    0x8(%rsp),%eax
mov    0x10(%rsp),%ecx
movzwl %cx,%edx
shr    $0x10,%ecx
imul   $0x3e80000,%ecx,%ecx
imul   $0x3e8,%edx,%ebp
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
sub    %ebx,%ebp
add    %ecx,%ebp
add    %eax,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @sat_timems(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i32
store i32 %0, i32* %3
%6 = call i64 @gettimeofday(%struct.timeval* %4, i32 0)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 0, i32* %2
br label %32
9: ; preds = %1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%11 = load i64, i64* %10
%12 = trunc i64 %11 to i32
%13 = lshr i32 %12, 16
%14 = mul i32 %13, 1000
%15 = shl i32 %14, 16
store i32 %15, i32* %5
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%17 = load i64, i64* %16
%18 = trunc i64 %17 to i32
%19 = and i32 %18, 65535
%20 = mul i32 %19, 1000
%21 = load i32, i32* %5
%22 = add i32 %21, %20
store i32 %22, i32* %5
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%24 = load i64, i64* %23
%25 = trunc i64 %24 to i32
%26 = udiv i32 %25, 1000
%27 = load i32, i32* %5
%28 = add i32 %27, %26
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = load i32, i32* %3
%31 = sub i32 %29, %30
store i32 %31, i32* %2
br label %32
32: ; preds = %9, %8
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @sat_timems(i32 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = call i64 @gettimeofday(%struct.timeval* %2, i32 0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %21
6: ; preds = %1
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%8 = load i64, i64* %7
%9 = trunc i64 %8 to i32
%10 = lshr i32 %9, 16
%11 = mul i32 %10, 65536000
%12 = and i32 %9, 65535
%13 = mul nuw nsw i32 %12, 1000
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%15 = load i64, i64* %14
%16 = trunc i64 %15 to i32
%17 = udiv i32 %16, 1000
%18 = sub i32 %13, %0
%19 = add i32 %18, %11
%20 = add i32 %19, %17
br label %21
21: ; preds = %1, %6
%22 = phi i32 [ %20, %6 ], [ 0, %1 ]
ret i32 %22
}