#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 /*<<< orphan*/ time_t;
struct tm {
  int dummy;
};
struct timespec {
  int tv_nsec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ EINVAL;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ gmtime_r(int /*<<< orphan*/ *, struct tm *);
size_t snprintf(char *, size_t, char *, int);
scalar_t__ strftime(char *, size_t, char *, struct tm *);
int strlen(char *);
int wallclock_get_zulu(char *buf, size_t len) {
  struct timespec ts;
  struct tm tm;
  time_t t;
  if (len < 33) {
    errno = EINVAL;
    return -1;
  }
  if (clock_gettime(CLOCK_REALTIME, &ts) < 0)
    return -1;
  t = ts.tv_sec;
  if (!gmtime_r(&t, &tm)) {
    errno = EINVAL;
    return -1;
  }
  if (strftime(buf, len, "%FT%T", &tm) == 0) {
    errno = EINVAL;
    return -1;
  }
  if (snprintf(buf + 19, len - 19, ".%.6luZ", ts.tv_nsec / 1000) >= len - 20) {
    errno = EINVAL;
    return -1;
  }
  return strlen(buf);
}


x86 assembly (clang, -O0):
<wallclock_get_zulu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x21,-0x18(%rbp)
jae    1189 <wallclock_get_zulu+0x39>
mov    0x2e66(%rip),%rax
mov    (%rax),%ecx
mov    0x2e65(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <wallclock_get_zulu+0x13f>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
jge    11b1 <wallclock_get_zulu+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <wallclock_get_zulu+0x13f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <gmtime_r@plt>
cmp    $0x0,%eax
jne    11eb <wallclock_get_zulu+0x9b>
mov    0x2e04(%rip),%rax
mov    (%rax),%ecx
mov    0x2e03(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <wallclock_get_zulu+0x13f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    0xe06(%rip),%rdx
lea    -0x28(%rbp),%rcx
call   1070 <strftime@plt>
cmp    $0x0,%rax
jne    122b <wallclock_get_zulu+0xdb>
mov    0x2dc4(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc3(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <wallclock_get_zulu+0x13f>
mov    -0x10(%rbp),%rdi
add    $0x13,%rdi
mov    -0x18(%rbp),%rsi
sub    $0x13,%rsi
mov    -0x20(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xdb7(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rcx
sub    $0x14,%rcx
cmp    %rcx,%rax
jb     1283 <wallclock_get_zulu+0x133>
mov    0x2d6c(%rip),%rax
mov    (%rax),%ecx
mov    0x2d6b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <wallclock_get_zulu+0x13f>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wallclock_get_zulu>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x20,%rsi
ja     117a <wallclock_get_zulu+0x2a>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    0x2e72(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
jmp    1228 <wallclock_get_zulu+0xd8>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     1228 <wallclock_get_zulu+0xd8>
mov    0xc(%rsp),%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
call   1060 <gmtime_r@plt>
test   %eax,%eax
je     1216 <wallclock_get_zulu+0xc6>
lea    0xe3f(%rip),%rdx
mov    %rsp,%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <strftime@plt>
test   %rax,%rax
je     1216 <wallclock_get_zulu+0xc6>
lea    0x13(%r14),%rdi
lea    -0x13(%rbx),%rsi
movslq 0x8(%rsp),%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rax
shr    $0x3f,%rax
sar    $0x26,%rcx
add    %eax,%ecx
lea    0xe0a(%rip),%rdx
call   1050 <snprintf@plt>
add    $0xffffffffffffffec,%rbx
cmp    %rbx,%rax
jae    1216 <wallclock_get_zulu+0xc6>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
jmp    1228 <wallclock_get_zulu+0xd8>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    0x2dba(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.tm = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@CLOCK_REALTIME = global i32 0
@.str = [6 x i8] c"%FT%T\00"
@.str.1 = [8 x i8] c".%.6luZ\00"
define i32 @wallclock_get_zulu(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.timespec
%7 = alloca %struct.tm
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i64, i64* %5
%10 = icmp ult i64 %9, 33
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
store i32 -1, i32* %3
br label %49
13: ; preds = %2
%14 = load i32, i32* @CLOCK_REALTIME
%15 = call i64 @clock_gettime(i32 %14, %struct.timespec* %6)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 -1, i32* %3
br label %49
18: ; preds = %13
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%20 = load i32, i32* %19
store i32 %20, i32* %8
%21 = call i32 @gmtime_r(i32* %8, %struct.tm* %7)
%22 = icmp ne i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = load i32, i32* @EINVAL
store i32 %24, i32* @errno
store i32 -1, i32* %3
br label %49
25: ; preds = %18
%26 = load i8*, i8** %4
%27 = load i64, i64* %5
%28 = call i64 @strftime(i8* %26, i64 %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.tm* %7)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i32, i32* @EINVAL
store i32 %31, i32* @errno
store i32 -1, i32* %3
br label %49
32: ; preds = %25
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 19
%35 = load i64, i64* %5
%36 = sub i64 %35, 19
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%38 = load i32, i32* %37
%39 = sdiv i32 %38, 1000
%40 = call i64 @snprintf(i8* %34, i64 %36, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = load i64, i64* %5
%42 = sub i64 %41, 20
%43 = icmp uge i64 %40, %42
br i1 %43, label %44, label %46
44: ; preds = %32
%45 = load i32, i32* @EINVAL
store i32 %45, i32* @errno
store i32 -1, i32* %3
br label %49
46: ; preds = %32
%47 = load i8*, i8** %4
%48 = call i32 @strlen(i8* %47)
store i32 %48, i32* %3
br label %49
49: ; preds = %46, %44, %30, %23, %17, %11
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.tm = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@CLOCK_REALTIME = global i32 0
@.str = [6 x i8] c"%FT%T\00"
@.str.1 = [8 x i8] c".%.6luZ\00"
define i32 @wallclock_get_zulu(i8* %0, i64 %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.tm
%5 = alloca i32
%6 = bitcast %struct.timespec* %3 to i8*
%7 = bitcast %struct.tm* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = icmp ult i64 %1, 33
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
br label %41
12: ; preds = %2
%13 = load i32, i32* @CLOCK_REALTIME
%14 = call i64 @clock_gettime(i32 %13, %struct.timespec* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %41, label %16
16: ; preds = %12
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%18 = load i32, i32* %17
store i32 %18, i32* %5
%19 = call i32 @gmtime_r(i32* %5, %struct.tm* %4)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = load i32, i32* @EINVAL
store i32 %22, i32* @errno
br label %41
23: ; preds = %16
%24 = call i64 @strftime(i8* %0, i64 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.tm* %4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = load i32, i32* @EINVAL
store i32 %27, i32* @errno
br label %41
28: ; preds = %23
%29 = getelementptr inbounds i8, i8* %0, i64 19
%30 = add i64 %1, -19
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%32 = load i32, i32* %31
%33 = sdiv i32 %32, 1000
%34 = call i64 @snprintf(i8* %29, i64 %30, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = add i64 %1, -20
%36 = icmp ult i64 %34, %35
br i1 %36, label %39, label %37
37: ; preds = %28
%38 = load i32, i32* @EINVAL
store i32 %38, i32* @errno
br label %41
39: ; preds = %28
%40 = call i32 @strlen(i8* %0)
br label %41
41: ; preds = %12, %39, %37, %26, %21, %10
%42 = phi i32 [ -1, %10 ], [ -1, %26 ], [ -1, %37 ], [ %40, %39 ], [ -1, %21 ], [ -1, %12 ]
ret i32 %42
}