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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ exit(int);
struct tm *gmtime(int *);
int /*<<< orphan*/ perror(char *);
int printf(char *, int, int, int, int, int, int, int, char, int, int);
void timestamp(void) {
  int res, usec;
  int hh = 0, mm = 0, ss = 0;
  time_t tnow = 0;
  struct timespec ts = {.tv_sec = 0, .tv_nsec = 0};
  struct tm *tm = NULL;
  res = clock_gettime(CLOCK_REALTIME, &ts);
  if (res != 0) {
    perror("Cannot get current time");
    exit(1);
  }
  tnow = ts.tv_sec;
  usec = (int)((ts.tv_nsec + 500) / 1000);
  while (usec > 1000000) {
    usec -= 1000000;
    tnow += 1;
  }
  tm = gmtime(&tnow);
  if (tm == (struct tm *)NULL)
    exit(1);
  res = printf("%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%02d:%02d\t",
               tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
               tm->tm_min, tm->tm_sec, usec, ss < 0 ? '-' : '+', hh, mm);
  if (res != 33)
    exit(1);
}


x86 assembly (clang, -O0):
<timestamp>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movq   $0x0,-0x38(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11d4 <timestamp+0x74>
lea    0xe3b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1f4,%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
cmpl   $0xf4240,-0x18(%rbp)
jle    1213 <timestamp+0xb3>
mov    -0x18(%rbp),%eax
sub    $0xf4240,%eax
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11ed <timestamp+0x8d>
lea    -0x28(%rbp),%rdi
call   1070 <gmtime@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1235 <timestamp+0xd5>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
add    $0x76c,%esi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%edx
add    $0x1,%edx
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%r8d
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%r9d
mov    -0x38(%rbp),%rax
mov    0x14(%rax),%r14d
mov    -0x18(%rbp),%ebx
mov    -0x24(%rbp),%r10d
mov    $0x2b,%eax
mov    $0x2d,%edi
cmp    $0x0,%r10d
cmovl  %edi,%eax
mov    %al,%r11b
mov    -0x1c(%rbp),%r10d
mov    -0x20(%rbp),%eax
lea    0xd85(%rip),%rdi
mov    %r14d,(%rsp)
mov    %ebx,0x8(%rsp)
movsbl %r11b,%r11d
mov    %r11d,0x10(%rsp)
mov    %r10d,0x18(%rsp)
mov    %eax,0x20(%rsp)
call   1040 <printf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x21,-0x14(%rbp)
je     12c9 <timestamp+0x169>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

x86 assembly (clang, -O2):
<timestamp>:
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
test   %eax,%eax
jne    122d <timestamp+0xdd>
mov    (%rsp),%eax
mov    0x4(%rsp),%ecx
lea    0x1f4(%rcx),%edx
movslq %edx,%rdx
imul   $0x10624dd3,%rdx,%rbx
mov    %rbx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rbx
add    %edx,%ebx
cmp    $0x3b9acbf4,%ecx
jl     11d8 <timestamp+0x88>
cmp    $0x1e8480,%ebx
mov    $0x1e8480,%ecx
cmovl  %ebx,%ecx
neg    %ecx
add    %ebx,%ecx
add    $0xf423f,%ecx
imul   $0x431bde83,%rcx,%rcx
shr    $0x32,%rcx
imul   $0xfff0bdc0,%ecx,%edx
add    %edx,%ebx
add    $0xfff0bdc0,%ebx
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1060 <gmtime@plt>
test   %rax,%rax
je     1239 <timestamp+0xe9>
mov    $0x76c,%esi
add    (%rax),%esi
mov    0x4(%rax),%edx
mov    0x8(%rax),%ecx
add    $0x1,%edx
mov    0xc(%rax),%r8d
mov    0x10(%rax),%r9d
mov    0x14(%rax),%eax
sub    $0x8,%rsp
lea    0xe07(%rip),%rdi
push   $0x0
push   $0x0
push   $0x2b
push   %rbx
push   %rax
call   1040 <printf@plt>
add    $0x30,%rsp
cmp    $0x21,%eax
jne    1239 <timestamp+0xe9>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdcc(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [24 x i8] c"Cannot get current time\00"
@.str.1 = [47 x i8] c"%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%02d:%02d\09\00"
define void @timestamp() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timespec
%8 = alloca %struct.tm*
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%9 = bitcast %struct.timespec* %7 to i8*
store %struct.tm* null, %struct.tm** %8
%10 = load i32, i32* @CLOCK_REALTIME
%11 = call i32 @clock_gettime(i32 %10, %struct.timespec* %7)
store i32 %11, i32* %1
%12 = load i32, i32* %1
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%19 = load i32, i32* %18
store i32 %19, i32* %6
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 500
%23 = sdiv i32 %22, 1000
store i32 %23, i32* %2
br label %24
24: ; preds = %27, %17
%25 = load i32, i32* %2
%26 = icmp sgt i32 %25, 1000000
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* %2
%29 = sub nsw i32 %28, 1000000
store i32 %29, i32* %2
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %24
32: ; preds = %24
%33 = call %struct.tm* @gmtime(i32* %6)
store %struct.tm* %33, %struct.tm** %8
%34 = load %struct.tm*, %struct.tm** %8
%35 = icmp eq %struct.tm* %34, null
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %32
%39 = load %struct.tm*, %struct.tm** %8
%40 = getelementptr inbounds %struct.tm, %struct.tm* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = add nsw i32 %41, 1900
%43 = load %struct.tm*, %struct.tm** %8
%44 = getelementptr inbounds %struct.tm, %struct.tm* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = add nsw i32 %45, 1
%47 = load %struct.tm*, %struct.tm** %8
%48 = getelementptr inbounds %struct.tm, %struct.tm* %47, i32 0, i32 2
%49 = load i32, i32* %48
%50 = load %struct.tm*, %struct.tm** %8
%51 = getelementptr inbounds %struct.tm, %struct.tm* %50, i32 0, i32 3
%52 = load i32, i32* %51
%53 = load %struct.tm*, %struct.tm** %8
%54 = getelementptr inbounds %struct.tm, %struct.tm* %53, i32 0, i32 4
%55 = load i32, i32* %54
%56 = load %struct.tm*, %struct.tm** %8
%57 = getelementptr inbounds %struct.tm, %struct.tm* %56, i32 0, i32 5
%58 = load i32, i32* %57
%59 = load i32, i32* %2
%60 = load i32, i32* %5
%61 = icmp slt i32 %60, 0
%62 = zext i1 %61 to i64
%63 = select i1 %61, i32 45, i32 43
%64 = trunc i32 %63 to i8
%65 = load i32, i32* %3
%66 = load i32, i32* %4
%67 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %42, i32 %46, i32 %49, i32 %52, i32 %55, i32 %58, i32 %59, i8 signext %64, i32 %65, i32 %66)
store i32 %67, i32* %1
%68 = load i32, i32* %1
%69 = icmp ne i32 %68, 33
br i1 %69, label %70, label %72
70: ; preds = %38
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %38
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [24 x i8] c"Cannot get current time\00"
@.str.1 = [47 x i8] c"%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%02d:%02d\09\00"
define void @timestamp() {
%1 = alloca i32
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.timespec*
%4 = bitcast i32* %1 to i8*
%5 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%6 = load i32, i32* @CLOCK_REALTIME
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %3)
%8 = icmp eq i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = bitcast i64* %2 to i32*
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%16 = load i32, i32* %15
%17 = add nsw i32 %16, 500
%18 = sdiv i32 %17, 1000
%19 = icmp sgt i32 %16, 1000000499
br i1 %19, label %20, label %30
20: ; preds = %12
%21 = add nsw i32 %18, 999999
%22 = call i32 @llvm.smin.i32(i32 %18, i32 2000000)
%23 = sub nsw i32 %21, %22
%24 = udiv i32 %23, 1000000
%25 = mul i32 %24, -1000000
%26 = add i32 %14, %24
%27 = add nsw i32 %18, -1000000
%28 = add i32 %25, %27
%29 = add i32 %26, 1
br label %30
30: ; preds = %20, %12
%31 = phi i32 [ %14, %12 ], [ %29, %20 ]
%32 = phi i32 [ %18, %12 ], [ %28, %20 ]
store i32 %31, i32* %1
%33 = call %struct.tm* @gmtime(i32* %1)
%34 = icmp eq %struct.tm* %33, null
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
%38 = getelementptr inbounds %struct.tm, %struct.tm* %33, i64 0, i32 0
%39 = load i32, i32* %38
%40 = add nsw i32 %39, 1900
%41 = getelementptr inbounds %struct.tm, %struct.tm* %33, i64 0, i32 1
%42 = load i32, i32* %41
%43 = add nsw i32 %42, 1
%44 = getelementptr inbounds %struct.tm, %struct.tm* %33, i64 0, i32 2
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.tm, %struct.tm* %33, i64 0, i32 3
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.tm, %struct.tm* %33, i64 0, i32 4
%49 = load i32, i32* %48
%50 = getelementptr inbounds %struct.tm, %struct.tm* %33, i64 0, i32 5
%51 = load i32, i32* %50
%52 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %43, i32 %45, i32 %47, i32 %49, i32 %51, i32 %32, i8 signext 43, i32 0, i32 0)
%53 = icmp eq i32 %52, 33
br i1 %53, label %56, label %54
54: ; preds = %37
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %37
ret void
}