#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_hour;
  int tm_min;
  int tm_sec;
};
struct timezone {
  int dummy;
};
struct timeval {
  scalar_t__ tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int gettimeofday(struct timeval *, struct timezone *);
int /*<<< orphan*/ localtime_r(int /*<<< orphan*/ *, struct tm *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, ...);
char *NaClTimeStampString(char *buffer, size_t buffer_size) {
  struct timeval tv;
  struct tm bdt;
  if (-1 == gettimeofday(&tv, (struct timezone *)NULL)) {
    snprintf(buffer, buffer_size, "-NA-");
    return buffer;
  }
  (void)localtime_r(&tv.tv_sec, &bdt);
  snprintf(buffer, buffer_size, "%02d:%02d:%02d.%06d", bdt.tm_hour, bdt.tm_min,
           bdt.tm_sec, (int)tv.tv_usec);
  return buffer;
}


x86 assembly (clang, -O0):
<NaClTimeStampString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    117f <NaClTimeStampString+0x4f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    0xe95(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c0 <NaClTimeStampString+0x90>
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
lea    -0x38(%rbp),%rsi
call   1030 <localtime_r@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%r8d
mov    -0x30(%rbp),%r9d
mov    -0x28(%rbp),%rax
lea    0xe57(%rip),%rdx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NaClTimeStampString>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
cmp    $0xffffffff,%eax
je     1188 <NaClTimeStampString+0x58>
lea    0x20(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1030 <localtime_r@plt>
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
mov    0x10(%rsp),%r9d
mov    0x18(%rsp),%eax
mov    %eax,(%rsp)
lea    0xe8c(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <snprintf@plt>
jmp    119c <NaClTimeStampString+0x6c>
lea    0xe71(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32, i32, i32 }
%struct.timezone = type { i32 }
@.str = [5 x i8] c"-NA-\00"
@.str.1 = [20 x i8] c"%02d:%02d:%02d.%06d\00"
define i8* @NaClTimeStampString(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.timeval
%7 = alloca %struct.tm
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = call i32 @gettimeofday(%struct.timeval* %6, %struct.timezone* null)
%9 = icmp eq i32 -1, %8
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = load i64, i64* %5
%13 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %11, i64 %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = load i8*, i8** %4
store i8* %14, i8** %3
br label %31
15: ; preds = %2
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%17 = call i32 @localtime_r(i32* %16, %struct.tm* %7)
%18 = load i8*, i8** %4
%19 = load i64, i64* %5
%20 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 2
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%27 = load i64, i64* %26
%28 = trunc i64 %27 to i32
%29 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %18, i64 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %23, i32 %25, i32 %28)
%30 = load i8*, i8** %4
store i8* %30, i8** %3
br label %31
31: ; preds = %15, %10
%32 = load i8*, i8** %3
ret i8* %32
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32, i32, i32 }
%struct.timezone = type { i32 }
@.str = [5 x i8] c"-NA-\00"
@.str.1 = [20 x i8] c"%02d:%02d:%02d.%06d\00"
define i8* @NaClTimeStampString(i8* returned writeonly %0, i64 %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.tm
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast %struct.tm* %4 to i8*
%7 = call i32 @gettimeofday(%struct.timeval* %3, %struct.timezone* null)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %24
11: ; preds = %2
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%13 = call i32 @localtime_r(i32* %12, %struct.tm* %4)
%14 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 2
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%21 = load i64, i64* %20
%22 = trunc i64 %21 to i32
%23 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %17, i32 %19, i32 %22)
br label %24
24: ; preds = %11, %9
ret i8* %0
}