#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 dummy;
};
struct timeval {
  scalar_t__ tv_usec;
  int /*<<< orphan*/ tv_sec;
};
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, long);
size_t strftime(char *, int, char *, struct tm *);
void printTimeString(FILE *fp) {
  size_t charsWritten;
  char buffer[80];
  struct timeval tv;
  struct tm *tm;
  if (gettimeofday(&tv, NULL) == -1) {
    printf("gettimeofday error\n");
    abort();
  }
  tm = localtime(&tv.tv_sec);
  if (tm == NULL) {
    printf("localtime error\n");
    abort();
  }
  charsWritten = strftime(buffer, sizeof(buffer), "%Y-%b-%d %H:%M:%S", tm);
  if (charsWritten == 0) {
    printf("strftime error\n");
    abort();
  } else if (charsWritten > (sizeof(buffer) - 8)) {
    printf("strftime overflow\n");
    abort();
  }
  snprintf(buffer + charsWritten, 8, ".%06ld", (long)tv.tv_usec);
  fputs(buffer, fp);
}


x86 assembly (clang, -O0):
<printTimeString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x70(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
cmp    $0xffffffff,%eax
jne    11a8 <printTimeString+0x38>
lea    0xe64(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
lea    -0x70(%rbp),%rdi
add    $0x8,%rdi
call   1030 <localtime@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    11d7 <printTimeString+0x67>
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
lea    -0x60(%rbp),%rdi
mov    -0x78(%rbp),%rcx
mov    $0x50,%esi
lea    0xe3a(%rip),%rdx
call   1090 <strftime@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1212 <printTimeString+0xa2>
lea    0xe31(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
cmpq   $0x48,-0x10(%rbp)
jbe    1230 <printTimeString+0xc0>
lea    0xe23(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    1235 <printTimeString+0xc5>
lea    -0x60(%rbp),%rdi
add    -0x10(%rbp),%rdi
mov    -0x70(%rbp),%rcx
mov    $0x8,%esi
lea    0xe0d(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <fputs@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printTimeString>:
push   %rbx
sub    $0x60,%rsp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
cmp    $0xffffffff,%eax
je     11ea <printTimeString+0x7a>
lea    0x8(%rsp),%rdi
call   1030 <localtime@plt>
test   %rax,%rax
je     11f3 <printTimeString+0x83>
lea    0xe63(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x50,%esi
mov    %rax,%rcx
call   1090 <strftime@plt>
test   %rax,%rax
je     11fc <printTimeString+0x8c>
cmp    $0x49,%rax
jae    1205 <printTimeString+0x95>
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
mov    (%rsp),%rcx
lea    0xe45(%rip),%rdx
mov    $0x8,%esi
call   1060 <snprintf@plt>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   1080 <fputs@plt>
add    $0x60,%rsp
pop    %rbx
ret
lea    0xe59(%rip),%rdi
jmp    120c <printTimeString+0x9c>
lea    0xe40(%rip),%rdi
jmp    120c <printTimeString+0x9c>
lea    0xe28(%rip),%rdi
jmp    120c <printTimeString+0x9c>
lea    0xe0d(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32 }
@.str = [20 x i8] c"gettimeofday error\0A\00"
@.str.1 = [17 x i8] c"localtime error\0A\00"
@.str.2 = [18 x i8] c"%Y-%b-%d %H:%M:%S\00"
@.str.3 = [16 x i8] c"strftime error\0A\00"
@.str.4 = [19 x i8] c"strftime overflow\0A\00"
@.str.5 = [7 x i8] c".%06ld\00"
define void @printTimeString(i32* %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca [80 x i8]
%5 = alloca %struct.timeval
%6 = alloca %struct.tm*
store i32* %0, i32** %2
%7 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%11 = call i32 (...) @abort()
unreachable
12: ; preds = %1
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%14 = call %struct.tm* @localtime(i32* %13)
store %struct.tm* %14, %struct.tm** %6
%15 = load %struct.tm*, %struct.tm** %6
%16 = icmp eq %struct.tm* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 (...) @abort()
unreachable
20: ; preds = %12
%21 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%22 = load %struct.tm*, %struct.tm** %6
%23 = call i64 @strftime(i8* %21, i32 80, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), %struct.tm* %22)
store i64 %23, i64* %3
%24 = load i64, i64* %3
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 (...) @abort()
unreachable
29: ; preds = %20
%30 = load i64, i64* %3
%31 = icmp ugt i64 %30, 72
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 (...) @abort()
unreachable
35: ; preds = %29
br label %36
36: ; preds = %35
%37 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%38 = load i64, i64* %3
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%41 = load i64, i64* %40
%42 = call i32 @snprintf(i8* %39, i32 8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i64 %41)
%43 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%44 = load i32*, i32** %2
%45 = call i32 @fputs(i8* %43, i32* %44)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32 }
@.str.2 = [18 x i8] c"%Y-%b-%d %H:%M:%S\00"
@.str.5 = [7 x i8] c".%06ld\00"
@str = [18 x i8] c"strftime overflow\00"
@str.6 = [15 x i8] c"strftime error\00"
@str.7 = [16 x i8] c"localtime error\00"
@str.8 = [19 x i8] c"gettimeofday error\00"
define void @printTimeString(i32* nocapture %0) {
%2 = alloca [80 x i8]
%3 = alloca %struct.timeval
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.timeval* %3 to i8*
%6 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.8, i64 0, i64 0))
%10 = tail call i32 (...) @abort()
unreachable
11: ; preds = %1
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%13 = call %struct.tm* @localtime(i32* %12)
%14 = icmp eq %struct.tm* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0))
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %11
%19 = call i64 @strftime(i8* %4, i32 80, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), %struct.tm* %13)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.6, i64 0, i64 0))
%23 = call i32 (...) @abort()
unreachable
24: ; preds = %18
%25 = icmp ugt i64 %19, 72
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%28 = call i32 (...) @abort()
unreachable
29: ; preds = %24
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %19
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%32 = load i64, i64* %31
%33 = call i32 @snprintf(i8* %30, i32 8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i64 %32)
%34 = call i32 @fputs(i8* %4, i32* %0)
ret void
}