#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 */
typedef int time_t;
typedef int suseconds_t;
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
void timestamp() {
  struct timeval tv;
  static time_t sec0;
  static suseconds_t usec0;
  int s, us;
  if (gettimeofday(&tv, NULL) < 0) {
    perror("gettimeofday");
  }
  s = tv.tv_sec - sec0;
  us = tv.tv_usec - usec0;
  if (us < 0) {
    s -= 1;
    us += 1000000;
  }
  fprintf(stderr, ">> %d.%06d\n", s, us);
  sec0 = tv.tv_sec;
  usec0 = tv.tv_usec;
}


x86 assembly (clang, -O0):
<timestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
jge    115b <timestamp+0x2b>
lea    0xeaa(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%eax
sub    0x2ed8(%rip),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
sub    0x2ed0(%rip),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1191 <timestamp+0x61>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0xf4240,%eax
mov    %eax,-0x10(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe66(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,0x2e87(%rip)
mov    -0x4(%rbp),%eax
mov    %eax,0x2e82(%rip)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timestamp>:
push   %rbp
push   %rbx
push   %rax
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
js     1193 <timestamp+0x63>
mov    (%rsp),%ebx
mov    0x4(%rsp),%ebp
mov    %ebx,%eax
sub    0x2eeb(%rip),%eax
mov    %ebp,%esi
sub    0x2ee7(%rip),%esi
lea    0xf4240(%rsi),%ecx
mov    %esi,%edx
sar    $0x1f,%edx
add    %eax,%edx
test   %esi,%esi
cmovns %esi,%ecx
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
call   1040 <fprintf@plt>
mov    %ebx,0x2eb6(%rip)
mov    %ebp,0x2eb4(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe66(%rip),%rdi
call   1050 <perror@plt>
jmp    1142 <timestamp+0x12>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@timestamp.sec0 = internal global i32 0
@timestamp.usec0 = internal global i32 0
@.str = [13 x i8] c"gettimeofday\00"
@stderr = global i32 0
@.str.1 = [12 x i8] c">> %d.%06d\0A\00"
define void @timestamp() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = alloca i32
%4 = call i64 @gettimeofday(%struct.timeval* %1, i32* null)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
%10 = load i32, i32* %9
%11 = load i32, i32* @timestamp.sec0
%12 = sub nsw i32 %10, %11
store i32 %12, i32* %2
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
%14 = load i32, i32* %13
%15 = load i32, i32* @timestamp.usec0
%16 = sub nsw i32 %14, %15
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %8
%20 = load i32, i32* %2
%21 = sub nsw i32 %20, 1
store i32 %21, i32* %2
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1000000
store i32 %23, i32* %3
br label %24
24: ; preds = %19, %8
%25 = load i32, i32* @stderr
%26 = load i32, i32* %2
%27 = load i32, i32* %3
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27)
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
%30 = load i32, i32* %29
store i32 %30, i32* @timestamp.sec0
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
%32 = load i32, i32* %31
store i32 %32, i32* @timestamp.usec0
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@timestamp.sec0 = internal unnamed_addr global i32 0
@timestamp.usec0 = internal unnamed_addr global i32 0
@.str = [13 x i8] c"gettimeofday\00"
@stderr = global i32 0
@.str.1 = [12 x i8] c">> %d.%06d\0A\00"
define void @timestamp() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = call i64 @gettimeofday(%struct.timeval* %1, i32* null)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @timestamp.sec0
%11 = sub i32 %9, %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%13 = load i32, i32* %12
%14 = load i32, i32* @timestamp.usec0
%15 = sub nsw i32 %13, %14
%16 = icmp slt i32 %15, 0
%17 = add nsw i32 %15, 1000000
%18 = ashr i32 %15, 31
%19 = add nsw i32 %11, %18
%20 = select i1 %16, i32 %17, i32 %15
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %20)
store i32 %9, i32* @timestamp.sec0
store i32 %13, i32* @timestamp.usec0
ret void
}