#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 tms {
  int dummy;
};
struct timezone {
  int dummy;
};
struct timeval {
  long tv_sec;
  long tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ time(long *);
int times(struct tms *);
int gettimeofday(struct timeval *tvp, struct timezone *tzp) {
  struct tms tms;
  static long offset = 0;
  long ticks;
  if (!offset) {
    time(&offset);
    offset -= (times(&tms) / 100);
  }
  ticks = times(&tms);
  tvp->tv_sec = ticks / 100 + offset;
  tvp->tv_usec = (ticks % 100) * (1000 * 1000 / 100);
  return 0;
}


x86 assembly (clang, -O0):
<gettimeofday>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,0x2f00(%rip)
jne    116f <gettimeofday+0x4f>
lea    0x2ef3(%rip),%rdi
call   1030 <time@plt>
lea    -0x18(%rbp),%rdi
call   1040 <times@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
movslq %eax,%rcx
mov    0x2ed3(%rip),%rax
sub    %rcx,%rax
mov    %rax,0x2ec9(%rip)
lea    -0x18(%rbp),%rdi
call   1040 <times@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    $0x64,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
add    0x2ea2(%rip),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    $0x64,%ecx
cqto
idiv   %rcx
imul   $0x2710,%rdx,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x8(%rax)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettimeofday>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
cmpq   $0x0,0x2f08(%rip)
jne    1167 <gettimeofday+0x47>
lea    0x2eff(%rip),%rdi
call   1030 <time@plt>
lea    0x8(%rsp),%rdi
call   1040 <times@plt>
cltq
imul   $0x51eb851f,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x25,%rax
add    %ecx,%eax
cltq
sub    %rax,0x2ed1(%rip)
lea    0x8(%rsp),%rdi
call   1040 <times@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
movslq %ecx,%rcx
mov    0x2ea7(%rip),%rdx
add    %rcx,%rdx
mov    %rdx,(%rbx)
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
imul   $0x2710,%eax,%eax
cltq
mov    %rax,0x8(%rbx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
%struct.timezone = type { i32 }
%struct.tms = type { i32 }
@gettimeofday.offset = internal global i64 0
define i32 @gettimeofday(%struct.timeval* %0, %struct.timezone* %1) {
%3 = alloca %struct.timeval*
%4 = alloca %struct.timezone*
%5 = alloca %struct.tms
%6 = alloca i64
store %struct.timeval* %0, %struct.timeval** %3
store %struct.timezone* %1, %struct.timezone** %4
%7 = load i64, i64* @gettimeofday.offset
%8 = icmp ne i64 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %2
%10 = call i32 @time(i64* @gettimeofday.offset)
%11 = call i32 @times(%struct.tms* %5)
%12 = sdiv i32 %11, 100
%13 = sext i32 %12 to i64
%14 = load i64, i64* @gettimeofday.offset
%15 = sub nsw i64 %14, %13
store i64 %15, i64* @gettimeofday.offset
br label %16
16: ; preds = %9, %2
%17 = call i32 @times(%struct.tms* %5)
%18 = sext i32 %17 to i64
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = sdiv i64 %19, 100
%21 = load i64, i64* @gettimeofday.offset
%22 = add nsw i64 %20, %21
%23 = load %struct.timeval*, %struct.timeval** %3
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %23, i32 0, i32 0
store i64 %22, i64* %24
%25 = load i64, i64* %6
%26 = srem i64 %25, 100
%27 = mul nsw i64 %26, 10000
%28 = load %struct.timeval*, %struct.timeval** %3
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %28, i32 0, i32 1
store i64 %27, i64* %29
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
%struct.timezone = type { i32 }
%struct.tms = type { i32 }
@gettimeofday.offset = internal global i64 0
define i32 @gettimeofday(%struct.timeval* nocapture writeonly %0, %struct.timezone* nocapture readnone %1) {
%3 = alloca %struct.tms
%4 = bitcast %struct.tms* %3 to i8*
%5 = load i64, i64* @gettimeofday.offset
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %2
%8 = tail call i32 @time(i64* @gettimeofday.offset)
%9 = call i32 @times(%struct.tms* %3)
%10 = sdiv i32 %9, 100
%11 = sext i32 %10 to i64
%12 = load i64, i64* @gettimeofday.offset
%13 = sub nsw i64 %12, %11
store i64 %13, i64* @gettimeofday.offset
br label %14
14: ; preds = %7, %2
%15 = call i32 @times(%struct.tms* %3)
%16 = sdiv i32 %15, 100
%17 = sext i32 %16 to i64
%18 = load i64, i64* @gettimeofday.offset
%19 = add nsw i64 %18, %17
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
store i64 %19, i64* %20
%21 = srem i32 %15, 100
%22 = mul nsw i32 %21, 10000
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
store i64 %23, i64* %24
ret i32 0
}