#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 {
  long long tv_sec;
  long long tv_usec;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ buildtag(char *, char const *, long long);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int writetag(int, int /*<<< orphan*/, char *, int);
int writetimestamp(int fd, off_t offset, const char *id, char *tag,
                   long long *ts) {
  struct timeval tv;
  long long t;
  int r;
  gettimeofday(&tv, 0);
  t = tv.tv_sec * 1000000ll + tv.tv_usec;
  buildtag(tag, id, t);
  r = writetag(fd, offset, tag, 1);
  if (r > 0)
    *ts = t;
  return r;
}


x86 assembly (clang, -O0):
<writetimestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
imul   $0xf4240,-0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <buildtag@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    $0x1,%ecx
call   1040 <writetag@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    11a2 <writetimestamp+0x72>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writetimestamp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %esi,%r13d
mov    %edi,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
imul   $0xf4240,0x8(%rsp),%rbx
add    0x10(%rsp),%rbx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <buildtag@plt>
mov    %ebp,%edi
mov    %r13d,%esi
mov    %r15,%rdx
mov    $0x1,%ecx
call   1040 <writetag@plt>
test   %eax,%eax
jle    118d <writetimestamp+0x5d>
mov    %rbx,(%r14)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @writetimestamp(i32 %0, i32 %1, i8* %2, i8* %3, i64* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64*
%11 = alloca %struct.timeval
%12 = alloca i64
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i64* %4, i64** %10
%14 = call i32 @gettimeofday(%struct.timeval* %11, i32 0)
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
%16 = load i64, i64* %15
%17 = mul nsw i64 %16, 1000000
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%19 = load i64, i64* %18
%20 = add nsw i64 %17, %19
store i64 %20, i64* %12
%21 = load i8*, i8** %9
%22 = load i8*, i8** %8
%23 = load i64, i64* %12
%24 = call i32 @buildtag(i8* %21, i8* %22, i64 %23)
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = load i8*, i8** %9
%28 = call i32 @writetag(i32 %25, i32 %26, i8* %27, i32 1)
store i32 %28, i32* %13
%29 = load i32, i32* %13
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %5
%32 = load i64, i64* %12
%33 = load i64*, i64** %10
store i64 %32, i64* %33
br label %34
34: ; preds = %31, %5
%35 = load i32, i32* %13
ret i32 %35
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @writetimestamp(i32 %0, i32 %1, i8* %2, i8* %3, i64* nocapture writeonly %4) {
%6 = alloca %struct.timeval
%7 = bitcast %struct.timeval* %6 to i8*
%8 = call i32 @gettimeofday(%struct.timeval* %6, i32 0)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%10 = load i64, i64* %9
%11 = mul nsw i64 %10, 1000000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%13 = load i64, i64* %12
%14 = add nsw i64 %11, %13
%15 = call i32 @buildtag(i8* %3, i8* %2, i64 %14)
%16 = call i32 @writetag(i32 %0, i32 %1, i8* %3, i32 1)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %5
store i64 %14, i64* %4
br label %19
19: ; preds = %18, %5
ret i32 %16
}