#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 scalar_t__ time_t;
struct tm {
  int tm_year;
  int tm_mon;
  int tm_mday;
  int /*<<< orphan*/ tm_sec;
  int /*<<< orphan*/ tm_min;
  int /*<<< orphan*/ tm_hour;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, ...);
struct tm *localtime(scalar_t__ *);
int _glh_write_timestamp(FILE *fp, time_t timestamp) {
  struct tm *t;
  if (timestamp < 0 || (t = localtime(&timestamp)) == NULL) {
    if (fprintf(fp, "?") < 0)
      return 1;
    return 0;
  };
  if (fprintf(fp, "%04d%02d%02d%02d%02d%02d", t->tm_year + 1900, t->tm_mon + 1,
              t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec) < 0)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<_glh_write_timestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jl     1152 <_glh_write_timestamp+0x32>
lea    -0x18(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1186 <_glh_write_timestamp+0x66>
mov    -0x10(%rbp),%rdi
lea    0xea3(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmp    $0x0,%rax
jge    117a <_glh_write_timestamp+0x5a>
movl   $0x1,-0x4(%rbp)
jmp    11f2 <_glh_write_timestamp+0xd2>
movl   $0x0,-0x4(%rbp)
jmp    11f2 <_glh_write_timestamp+0xd2>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
add    $0x76c,%edx
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0x1,%ecx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%r8d
mov    -0x20(%rbp),%rax
mov    0x14(%rax),%r9d
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%r10d
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%eax
lea    0xe3c(%rip),%rsi
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
mov    $0x0,%al
call   1040 <fprintf@plt>
cmp    $0x0,%rax
jge    11eb <_glh_write_timestamp+0xcb>
movl   $0x1,-0x4(%rbp)
jmp    11f2 <_glh_write_timestamp+0xd2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_glh_write_timestamp>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    %rsi,0x8(%rsp)
test   %rsi,%rsi
js     1179 <_glh_write_timestamp+0x59>
lea    0x8(%rsp),%rdi
call   1030 <localtime@plt>
test   %rax,%rax
je     1179 <_glh_write_timestamp+0x59>
mov    $0x76c,%edx
add    (%rax),%edx
mov    0x4(%rax),%ecx
mov    0x8(%rax),%r8d
add    $0x1,%ecx
mov    0x14(%rax),%r9d
mov    0x10(%rax),%r10d
mov    0xc(%rax),%r11d
lea    0xe9d(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
push   %r11
push   %r10
call   1040 <fprintf@plt>
add    $0x10,%rsp
jmp    118a <_glh_write_timestamp+0x6a>
lea    0xe80(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <fprintf@plt>
shr    $0x3f,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [2 x i8] c"?\00"
@.str.1 = [25 x i8] c"%04d%02d%02d%02d%02d%02d\00"
define i32 @_glh_write_timestamp(i32* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca %struct.tm*
store i32* %0, i32** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = icmp slt i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = call %struct.tm* @localtime(i64* %5)
store %struct.tm* %10, %struct.tm** %6
%11 = icmp eq %struct.tm* %10, null
br i1 %11, label %12, label %18
12: ; preds = %9, %2
%13 = load i32*, i32** %4
%14 = call i64 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 1, i32* %3
br label %44
17: ; preds = %12
store i32 0, i32* %3
br label %44
18: ; preds = %9
%19 = load i32*, i32** %4
%20 = load %struct.tm*, %struct.tm** %6
%21 = getelementptr inbounds %struct.tm, %struct.tm* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = add nsw i32 %22, 1900
%24 = load %struct.tm*, %struct.tm** %6
%25 = getelementptr inbounds %struct.tm, %struct.tm* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = add nsw i32 %26, 1
%28 = load %struct.tm*, %struct.tm** %6
%29 = getelementptr inbounds %struct.tm, %struct.tm* %28, i32 0, i32 2
%30 = load i32, i32* %29
%31 = load %struct.tm*, %struct.tm** %6
%32 = getelementptr inbounds %struct.tm, %struct.tm* %31, i32 0, i32 5
%33 = load i32, i32* %32
%34 = load %struct.tm*, %struct.tm** %6
%35 = getelementptr inbounds %struct.tm, %struct.tm* %34, i32 0, i32 4
%36 = load i32, i32* %35
%37 = load %struct.tm*, %struct.tm** %6
%38 = getelementptr inbounds %struct.tm, %struct.tm* %37, i32 0, i32 3
%39 = load i32, i32* %38
%40 = call i64 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %27, i32 %30, i32 %33, i32 %36, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %18
store i32 1, i32* %3
br label %44
43: ; preds = %18
store i32 0, i32* %3
br label %44
44: ; preds = %43, %42, %17, %16
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [2 x i8] c"?\00"
@.str.1 = [25 x i8] c"%04d%02d%02d%02d%02d%02d\00"
define i32 @_glh_write_timestamp(i32* nocapture %0, i64 %1) {
%3 = alloca i64
store i64 %1, i64* %3
%4 = icmp slt i64 %1, 0
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = call %struct.tm* @localtime(i64* %3)
%7 = icmp eq %struct.tm* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5, %2
%9 = call i64 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %26
10: ; preds = %5
%11 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 0
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1900
%14 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 1
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1
%17 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 2
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 5
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 4
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 3
%24 = load i32, i32* %23
%25 = call i64 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %16, i32 %18, i32 %20, i32 %22, i32 %24)
br label %26
26: ; preds = %10, %8
%27 = phi i64 [ %9, %8 ], [ %25, %10 ]
%28 = lshr i64 %27, 63
%29 = trunc i64 %28 to i32
ret i32 %29
}