#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_gmtoff;
  int tm_year;
  int tm_mon;
  int tm_mday;
  int tm_hour;
  int tm_min;
  int tm_sec;
};
struct timezone {
  int dummy;
};
struct timeval {
  int tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ abs(int);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int, int, int, int, int, int, int,
                           char, int /*<<< orphan*/, int /*<<< orphan*/);
void rtl_gettimeofday_to_iso8601date(struct timeval *tv, struct timezone *tz,
                                     char *buf) {
  struct tm *tm = localtime(&tv->tv_sec);
  int h, m;
  h = tm->tm_gmtoff / 3600;
  m = (tm->tm_gmtoff - h * 3600) / 60;
  char sign = (h < 0) ? '-' : '+';
  sprintf(buf, "%04d-%02d-%02dT%02d:%02d:%02d.%ld%c%02d:%02d",
          tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
          tm->tm_min, tm->tm_sec, tv->tv_usec / 1000, sign, abs(h), abs(m));
}


x86 assembly (clang, -O0):
<rtl_gettimeofday_to_iso8601date>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdi,-0x20(%rbp)
mov    %rsi,-0x28(%rbp)
mov    %rdx,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
call   1030 <localtime@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    $0xe10,%ecx
cltd
idiv   %ecx
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
imul   $0xe10,-0x3c(%rbp),%ecx
sub    %ecx,%eax
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%edx
mov    $0x2b,%eax
mov    $0x2d,%ecx
cmp    $0x0,%edx
cmovl  %ecx,%eax
mov    %al,-0x41(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%eax
add    $0x76c,%eax
mov    %eax,-0x5c(%rbp)
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x38(%rbp),%rax
mov    0x14(%rax),%r15d
mov    -0x38(%rbp),%rax
mov    0x18(%rax),%r14d
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ebx
mov    -0x41(%rbp),%al
mov    %al,-0x49(%rbp)
mov    -0x3c(%rbp),%edi
call   1040 <abs@plt>
mov    %eax,-0x48(%rbp)
mov    -0x40(%rbp),%edi
call   1040 <abs@plt>
mov    -0x68(%rbp),%rdi
mov    -0x5c(%rbp),%edx
mov    -0x58(%rbp),%ecx
mov    -0x54(%rbp),%r8d
mov    -0x50(%rbp),%r9d
mov    -0x49(%rbp),%r11b
mov    -0x48(%rbp),%r10d
lea    0xdd0(%rip),%rsi
mov    %r15d,(%rsp)
mov    %r14d,0x8(%rsp)
mov    %ebx,0x10(%rsp)
movsbl %r11b,%r11d
mov    %r11d,0x18(%rsp)
mov    %r10d,0x20(%rsp)
mov    %eax,0x28(%rsp)
call   1050 <sprintf@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtl_gettimeofday_to_iso8601date>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rdi,%r15
add    $0x4,%rdi
call   1030 <localtime@plt>
movslq (%rax),%rcx
xor    %r12d,%r12d
cmp    $0xfffffffffffff1f1,%rcx
setl   %r12b
imul   $0xffffffff91a2b3c5,%rcx,%rdi
shr    $0x20,%rdi
add    %ecx,%edi
mov    %edi,%edx
shr    $0x1f,%edx
sar    $0xb,%edi
add    %edx,%edi
imul   $0xfffff1f0,%edi,%edx
add    %ecx,%edx
movslq %edx,%rbx
imul   $0xffffffff88888889,%rbx,%rcx
shr    $0x20,%rcx
add    %ecx,%ebx
mov    %ebx,%ecx
shr    $0x1f,%ecx
sar    $0x5,%ebx
add    %ecx,%ebx
mov    $0x76c,%edx
add    0x4(%rax),%edx
mov    0x8(%rax),%ecx
mov    0xc(%rax),%r8d
add    $0x1,%ecx
mov    0x10(%rax),%r9d
mov    0x14(%rax),%r10d
movslq (%r15),%rsi
imul   $0x10624dd3,%rsi,%r11
mov    %r11,%rsi
shr    $0x3f,%rsi
shr    $0x20,%r11
sar    $0x6,%r11d
add    %esi,%r11d
mov    %edi,%r15d
neg    %r15d
cmovs  %edi,%r15d
mov    0x18(%rax),%r13d
mov    %ebx,%eax
neg    %eax
cmovs  %ebx,%eax
lea    (%r12,%r12,1),%ebx
add    $0x2b,%ebx
lea    0xe25(%rip),%rsi
mov    %r14,%rdi
push   %rax
push   %r15
push   %rbx
push   %r11
push   %r13
push   %r10
call   1040 <sprintf@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.timezone = type { i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32, i32 }
@.str = [45 x i8] c"%04d-%02d-%02dT%02d:%02d:%02d.%ld%c%02d:%02d\00"
define void @rtl_gettimeofday_to_iso8601date(%struct.timeval* %0, %struct.timezone* %1, i8* %2) {
%4 = alloca %struct.timeval*
%5 = alloca %struct.timezone*
%6 = alloca i8*
%7 = alloca %struct.tm*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store %struct.timeval* %0, %struct.timeval** %4
store %struct.timezone* %1, %struct.timezone** %5
store i8* %2, i8** %6
%11 = load %struct.timeval*, %struct.timeval** %4
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%13 = call %struct.tm* @localtime(i32* %12)
store %struct.tm* %13, %struct.tm** %7
%14 = load %struct.tm*, %struct.tm** %7
%15 = getelementptr inbounds %struct.tm, %struct.tm* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = sdiv i32 %16, 3600
store i32 %17, i32* %8
%18 = load %struct.tm*, %struct.tm** %7
%19 = getelementptr inbounds %struct.tm, %struct.tm* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* %8
%22 = mul nsw i32 %21, 3600
%23 = sub nsw i32 %20, %22
%24 = sdiv i32 %23, 60
store i32 %24, i32* %9
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
%27 = zext i1 %26 to i64
%28 = select i1 %26, i32 45, i32 43
%29 = trunc i32 %28 to i8
store i8 %29, i8* %10
%30 = load i8*, i8** %6
%31 = load %struct.tm*, %struct.tm** %7
%32 = getelementptr inbounds %struct.tm, %struct.tm* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = add nsw i32 %33, 1900
%35 = load %struct.tm*, %struct.tm** %7
%36 = getelementptr inbounds %struct.tm, %struct.tm* %35, i32 0, i32 2
%37 = load i32, i32* %36
%38 = add nsw i32 %37, 1
%39 = load %struct.tm*, %struct.tm** %7
%40 = getelementptr inbounds %struct.tm, %struct.tm* %39, i32 0, i32 3
%41 = load i32, i32* %40
%42 = load %struct.tm*, %struct.tm** %7
%43 = getelementptr inbounds %struct.tm, %struct.tm* %42, i32 0, i32 4
%44 = load i32, i32* %43
%45 = load %struct.tm*, %struct.tm** %7
%46 = getelementptr inbounds %struct.tm, %struct.tm* %45, i32 0, i32 5
%47 = load i32, i32* %46
%48 = load %struct.tm*, %struct.tm** %7
%49 = getelementptr inbounds %struct.tm, %struct.tm* %48, i32 0, i32 6
%50 = load i32, i32* %49
%51 = load %struct.timeval*, %struct.timeval** %4
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = sdiv i32 %53, 1000
%55 = load i8, i8* %10
%56 = load i32, i32* %8
%57 = call i32 @abs(i32 %56)
%58 = load i32, i32* %9
%59 = call i32 @abs(i32 %58)
%60 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %34, i32 %38, i32 %41, i32 %44, i32 %47, i32 %50, i32 %54, i8 signext %55, i32 %57, i32 %59)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.timezone = type { i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32, i32 }
@.str = [45 x i8] c"%04d-%02d-%02dT%02d:%02d:%02d.%ld%c%02d:%02d\00"
define void @rtl_gettimeofday_to_iso8601date(%struct.timeval* %0, %struct.timezone* nocapture readnone %1, i8* nocapture writeonly %2) {
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%5 = tail call %struct.tm* @localtime(i32* %4)
%6 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 0
%7 = load i32, i32* %6
%8 = sdiv i32 %7, 3600
%9 = mul nsw i32 %8, -3600
%10 = add i32 %9, %7
%11 = sdiv i32 %10, 60
%12 = icmp slt i32 %7, -3599
%13 = select i1 %12, i8 45, i8 43
%14 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 1
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1900
%17 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 2
%18 = load i32, i32* %17
%19 = add nsw i32 %18, 1
%20 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 3
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 4
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 5
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 6
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%29 = load i32, i32* %28
%30 = sdiv i32 %29, 1000
%31 = tail call i32 @llvm.abs.i32(i32 %8, i1 true)
%32 = tail call i32 @llvm.abs.i32(i32 %11, i1 true)
%33 = tail call i32 @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %19, i32 %21, i32 %23, i32 %25, i32 %27, i32 %30, i8 signext %13, i32 %31, i32 %32)
ret void
}