#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_year;
  int tm_mon;
  int tm_mday;
  int tm_hour;
  int tm_min;
  int tm_sec;
};
struct timeval {
  scalar_t__ tv_usec;
  int /*<<< orphan*/ tv_sec;
};
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
struct tm *gmtime_r(int /*<<< orphan*/ *, struct tm *);
int snprintf(char *, int, char *, int, int, int, int, int, int, int);
int isotime(char *buffer, int length, const struct timeval *t) {
  struct tm timestamp;
  struct tm *r = gmtime_r(&t->tv_sec, &timestamp);
  assert(r == &timestamp);
  int len = snprintf(buffer, length, "%04d-%02d-%02dT%02d:%02d:%02d.%06dZ",
                     timestamp.tm_year + 1900, timestamp.tm_mon + 1,
                     timestamp.tm_mday, timestamp.tm_hour, timestamp.tm_min,
                     timestamp.tm_sec, (int32_t)t->tv_usec);
  return len;
}


x86 assembly (clang, -O0):
<isotime>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x68,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x8,%rdi
lea    -0x38(%rbp),%rsi
call   1040 <gmtime_r@plt>
mov    %rax,-0x40(%rbp)
lea    -0x38(%rbp),%rax
cmp    %rax,-0x40(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x38(%rbp),%ecx
add    $0x76c,%ecx
mov    -0x34(%rbp),%r8d
add    $0x1,%r8d
mov    -0x30(%rbp),%r9d
mov    -0x2c(%rbp),%ebx
mov    -0x28(%rbp),%r11d
mov    -0x24(%rbp),%r10d
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
lea    0xe5d(%rip),%rdx
mov    %ebx,(%rsp)
mov    %r11d,0x8(%rsp)
mov    %r10d,0x10(%rsp)
mov    %eax,0x18(%rsp)
call   1030 <snprintf@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<isotime>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x8(%rdx),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1040 <gmtime_r@plt>
xor    %edi,%edi
cmp    %rbx,%rax
sete   %dil
call   1050 <assert@plt>
mov    $0x76c,%ecx
add    (%rsp),%ecx
mov    0x4(%rsp),%r8d
mov    0x8(%rsp),%r9d
add    $0x1,%r8d
mov    0xc(%rsp),%r10d
mov    0x10(%rsp),%ebx
mov    0x14(%rsp),%eax
mov    0x0(%rbp),%ebp
lea    0xe73(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
push   %rbp
push   %rax
push   %rbx
push   %r10
call   1030 <snprintf@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [36 x i8] c"%04d-%02d-%02dT%02d:%02d:%02d.%06dZ\00"
define i32 @isotime(i8* %0, i32 %1, %struct.timeval* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.timeval*
%7 = alloca %struct.tm
%8 = alloca %struct.tm*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store %struct.timeval* %2, %struct.timeval** %6
%10 = load %struct.timeval*, %struct.timeval** %6
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%12 = call %struct.tm* @gmtime_r(i32* %11, %struct.tm* %7)
store %struct.tm* %12, %struct.tm** %8
%13 = load %struct.tm*, %struct.tm** %8
%14 = icmp eq %struct.tm* %13, %7
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8*, i8** %4
%18 = load i32, i32* %5
%19 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 0
%20 = load i32, i32* %19
%21 = add nsw i32 %20, 1900
%22 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 1
%23 = load i32, i32* %22
%24 = add nsw i32 %23, 1
%25 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 2
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 3
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 5
%32 = load i32, i32* %31
%33 = load %struct.timeval*, %struct.timeval** %6
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = trunc i64 %35 to i32
%37 = call i32 @snprintf(i8* %17, i32 %18, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %24, i32 %26, i32 %28, i32 %30, i32 %32, i32 %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
ret i32 %38
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [36 x i8] c"%04d-%02d-%02dT%02d:%02d:%02d.%06dZ\00"
define i32 @isotime(i8* %0, i32 %1, %struct.timeval* %2) {
%4 = alloca %struct.tm
%5 = bitcast %struct.tm* %4 to i8*
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%7 = call %struct.tm* @gmtime_r(i32* %6, %struct.tm* %4)
%8 = icmp eq %struct.tm* %7, %4
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1900
%14 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 1
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1
%17 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 2
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 3
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 4
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 5
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%26 = load i64, i64* %25
%27 = trunc i64 %26 to i32
%28 = call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %16, i32 %18, i32 %20, i32 %22, i32 %24, i32 %27)
ret i32 %28
}