#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 tm {
  int tm_year;
  int tm_mon;
  int tm_mday;
  int tm_hour;
  int tm_min;
  int tm_sec;
};
struct timespec {
  int tv_nsec;
  int tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, int, int, int,
                           int, long);
int /*<<< orphan*/ localtime_r(int *, struct tm *);
int /*<<< orphan*/ stderr;
void print_timestamp(struct timespec *timestamp) {
  long usec = (timestamp->tv_nsec + 500) / 1000;
  if (usec >= 1000000) {
    usec -= 1000000;
    timestamp->tv_sec += 1;
  }
  struct tm tm;
  localtime_r(&timestamp->tv_sec, &tm);
  fprintf(stderr, "%04d-%02d-%02d %02d:%02d:%02d.%06ld: ", 1900 + tm.tm_year,
          tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, usec);
}


x86 assembly (clang, -O0):
<print_timestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
add    $0x1f4,%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x10(%rbp)
cmpq   $0xf4240,-0x10(%rbp)
jl     116e <print_timestamp+0x4e>
mov    -0x10(%rbp),%rax
sub    $0xf4240,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rdi
add    $0x4,%rdi
lea    -0x28(%rbp),%rsi
call   1030 <localtime_r@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
add    $0x76c,%edx
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    -0x20(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
mov    -0x18(%rbp),%r11d
mov    -0x14(%rbp),%r10d
mov    -0x10(%rbp),%rax
lea    0xe4e(%rip),%rsi
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1040 <fprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_timestamp>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    (%rdi),%eax
lea    0x1f4(%rax),%ecx
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
movslq %ecx,%r14
cmp    $0x3b9ac80c,%eax
jl     115b <print_timestamp+0x3b>
add    $0xfffffffffff0bdc0,%r14
addl   $0x1,0x4(%rdi)
add    $0x4,%rdi
mov    %rsp,%rsi
call   1030 <localtime_r@plt>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    $0x76c,%edx
add    (%rsp),%edx
mov    0x4(%rsp),%ecx
mov    0x8(%rsp),%r8d
add    $0x1,%ecx
mov    0xc(%rsp),%r9d
mov    0x10(%rsp),%eax
mov    0x14(%rsp),%ebx
sub    $0x8,%rsp
lea    0xe64(%rip),%rsi
push   %r14
push   %rbx
push   %rax
call   1040 <fprintf@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [38 x i8] c"%04d-%02d-%02d %02d:%02d:%02d.%06ld: \00"
define void @print_timestamp(%struct.timespec* %0) {
%2 = alloca %struct.timespec*
%3 = alloca i64
%4 = alloca %struct.tm
store %struct.timespec* %0, %struct.timespec** %2
%5 = load %struct.timespec*, %struct.timespec** %2
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%7 = load i32, i32* %6
%8 = add nsw i32 %7, 500
%9 = sdiv i32 %8, 1000
%10 = sext i32 %9 to i64
store i64 %10, i64* %3
%11 = load i64, i64* %3
%12 = icmp sge i64 %11, 1000000
br i1 %12, label %13, label %20
13: ; preds = %1
%14 = load i64, i64* %3
%15 = sub nsw i64 %14, 1000000
store i64 %15, i64* %3
%16 = load %struct.timespec*, %struct.timespec** %2
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = add nsw i32 %18, 1
store i32 %19, i32* %17
br label %20
20: ; preds = %13, %1
%21 = load %struct.timespec*, %struct.timespec** %2
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %21, i32 0, i32 1
%23 = call i32 @localtime_r(i32* %22, %struct.tm* %4)
%24 = load i32, i32* @stderr
%25 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 0
%26 = load i32, i32* %25
%27 = add nsw i32 1900, %26
%28 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 1
%29 = load i32, i32* %28
%30 = add nsw i32 %29, 1
%31 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 2
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 3
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 4
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 5
%38 = load i32, i32* %37
%39 = load i64, i64* %3
%40 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %30, i32 %32, i32 %34, i32 %36, i32 %38, i64 %39)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [38 x i8] c"%04d-%02d-%02d %02d:%02d:%02d.%06ld: \00"
define void @print_timestamp(%struct.timespec* %0) {
%2 = alloca %struct.tm
%3 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = add nsw i32 %4, 500
%6 = sdiv i32 %5, 1000
%7 = sext i32 %6 to i64
%8 = icmp sgt i32 %4, 999999499
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = add nsw i64 %7, -1000000
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1
store i32 %13, i32* %11
br label %14
14: ; preds = %9, %1
%15 = phi i64 [ %10, %9 ], [ %7, %1 ]
%16 = bitcast %struct.tm* %2 to i8*
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%18 = call i32 @localtime_r(i32* %17, %struct.tm* %2)
%19 = load i32, i32* @stderr
%20 = getelementptr inbounds %struct.tm, %struct.tm* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1900
%23 = getelementptr inbounds %struct.tm, %struct.tm* %2, i64 0, i32 1
%24 = load i32, i32* %23
%25 = add nsw i32 %24, 1
%26 = getelementptr inbounds %struct.tm, %struct.tm* %2, i64 0, i32 2
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.tm, %struct.tm* %2, i64 0, i32 3
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.tm, %struct.tm* %2, i64 0, i32 4
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.tm, %struct.tm* %2, i64 0, i32 5
%33 = load i32, i32* %32
%34 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %25, i32 %27, i32 %29, i32 %31, i32 %33, i64 %15)
ret void
}