#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 {
  int tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ localtime_r(int /*<<< orphan*/ *, struct tm *);
int /*<<< orphan*/ sprintf(char *const, char *, int, int, int, int, int, int,
                           int);
void timeval_to_str(struct timeval *clock, char *const buffer) {
  struct tm result;
  localtime_r(&clock->tv_sec, &result);
  sprintf(buffer, "%04d/%02d/%02d %02d:%02d:%02d.%06d", result.tm_year + 1900,
          result.tm_mon + 1, result.tm_mday, result.tm_hour, result.tm_min,
          result.tm_sec, (int)(clock->tv_usec / 100000));
}


x86 assembly (clang, -O0):
<timeval_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x4,%rdi
lea    -0x28(%rbp),%rsi
call   1030 <localtime_r@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%eax
add    $0x76c,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    -0x20(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
mov    -0x18(%rbp),%r11d
mov    -0x14(%rbp),%r10d
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    $0x186a0,%esi
cltd
idiv   %esi
mov    -0x2c(%rbp),%edx
lea    0xe82(%rip),%rsi
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
call   1040 <sprintf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_to_str>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
add    $0x4,%rdi
mov    %rsp,%rsi
call   1030 <localtime_r@plt>
mov    $0x76c,%edx
add    (%rsp),%edx
mov    0x4(%rsp),%ecx
mov    0x8(%rsp),%r8d
add    $0x1,%ecx
mov    0xc(%rsp),%r9d
mov    0x10(%rsp),%r10d
mov    0x14(%rsp),%eax
movslq (%rbx),%rsi
imul   $0x14f8b589,%rsi,%rbx
mov    %rbx,%rsi
shr    $0x3f,%rsi
sar    $0x2d,%rbx
add    %esi,%ebx
sub    $0x8,%rsp
lea    0xe83(%rip),%rsi
mov    %r14,%rdi
push   %rbx
push   %rax
push   %r10
call   1040 <sprintf@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [35 x i8] c"%04d/%02d/%02d %02d:%02d:%02d.%06d\00"
define void @timeval_to_str(%struct.timeval* %0, i8* %1) {
%3 = alloca %struct.timeval*
%4 = alloca i8*
%5 = alloca %struct.tm
store %struct.timeval* %0, %struct.timeval** %3
store i8* %1, i8** %4
%6 = load %struct.timeval*, %struct.timeval** %3
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%8 = call i32 @localtime_r(i32* %7, %struct.tm* %5)
%9 = load i8*, i8** %4
%10 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 0
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 1900
%13 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 1
%14 = load i32, i32* %13
%15 = add nsw i32 %14, 1
%16 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 2
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 3
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 4
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 5
%23 = load i32, i32* %22
%24 = load %struct.timeval*, %struct.timeval** %3
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = sdiv i32 %26, 100000
%28 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %15, i32 %17, i32 %19, i32 %21, i32 %23, i32 %27)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [35 x i8] c"%04d/%02d/%02d %02d:%02d:%02d.%06d\00"
define void @timeval_to_str(%struct.timeval* %0, i8* nocapture writeonly %1) {
%3 = alloca %struct.tm
%4 = bitcast %struct.tm* %3 to i8*
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%6 = call i32 @localtime_r(i32* %5, %struct.tm* %3)
%7 = getelementptr inbounds %struct.tm, %struct.tm* %3, i64 0, i32 0
%8 = load i32, i32* %7
%9 = add nsw i32 %8, 1900
%10 = getelementptr inbounds %struct.tm, %struct.tm* %3, i64 0, i32 1
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 1
%13 = getelementptr inbounds %struct.tm, %struct.tm* %3, i64 0, i32 2
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.tm, %struct.tm* %3, i64 0, i32 3
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.tm, %struct.tm* %3, i64 0, i32 4
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.tm, %struct.tm* %3, i64 0, i32 5
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%22 = load i32, i32* %21
%23 = sdiv i32 %22, 100000
%24 = call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %12, i32 %14, i32 %16, i32 %18, i32 %20, i32 %23)
ret void
}