#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 dummy;
};
struct timeval {
  scalar_t__ tv_usec;
  int /*<<< orphan*/ tv_sec;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
struct tm *gmtime(int /*<<< orphan*/ *);
int snprintf(char *, size_t, char *, int);
scalar_t__ strftime(char *, size_t, char *, struct tm *);
ssize_t format_timeval(struct timeval *tv, char *buf, size_t sz) {
  ssize_t written = -1;
  struct tm *gm = gmtime(&tv->tv_sec);
  if (gm) {
    written = (ssize_t)strftime(buf, sz, "%Y-%m-%d %H:%M:%S", gm);
    if ((written > 0) && ((size_t)written < sz)) {
      int w = snprintf(buf + written, sz - (size_t)written, ".%06d",
                       ((int)tv->tv_usec) / 1000);
      written = (w > 0) ? written + w : -1;
    }
  }
  return written;
}


x86 assembly (clang, -O0):
<format_timeval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
mov    -0x8(%rbp),%rdi
add    $0x8,%rdi
call   1050 <gmtime@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1208 <format_timeval+0xd8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rcx
lea    0xe85(%rip),%rdx
call   1040 <strftime@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    1203 <format_timeval+0xd3>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1203 <format_timeval+0xd3>
mov    -0x10(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
sub    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xe4d(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11eb <format_timeval+0xbb>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    11fb <format_timeval+0xcb>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
jmp    11fb <format_timeval+0xcb>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1208 <format_timeval+0xd8>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_timeval>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
add    $0x8,%rdi
call   1050 <gmtime@plt>
test   %rax,%rax
je     11b5 <format_timeval+0x85>
lea    0xeaa(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rcx
call   1040 <strftime@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    11bc <format_timeval+0x8c>
cmp    %r14,%rbx
jae    11bc <format_timeval+0x8c>
add    %rbx,%r15
sub    %rbx,%r14
movslq (%r12),%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rax
shr    $0x3f,%rax
sar    $0x26,%rcx
add    %eax,%ecx
lea    0xe7c(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <snprintf@plt>
cltq
add    %rax,%rbx
test   %eax,%eax
mov    $0xffffffffffffffff,%rax
cmovle %rax,%rbx
jmp    11bc <format_timeval+0x8c>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [6 x i8] c".%06d\00"
define i64 @format_timeval(%struct.timeval* %0, i8* %1, i64 %2) {
%4 = alloca %struct.timeval*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca %struct.tm*
%9 = alloca i32
store %struct.timeval* %0, %struct.timeval** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 -1, i64* %7
%10 = load %struct.timeval*, %struct.timeval** %4
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%12 = call %struct.tm* @gmtime(i32* %11)
store %struct.tm* %12, %struct.tm** %8
%13 = load %struct.tm*, %struct.tm** %8
%14 = icmp ne %struct.tm* %13, null
br i1 %14, label %15, label %50
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i64, i64* %6
%18 = load %struct.tm*, %struct.tm** %8
%19 = call i64 @strftime(i8* %16, i64 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %18)
store i64 %19, i64* %7
%20 = load i64, i64* %7
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %49
22: ; preds = %15
%23 = load i64, i64* %7
%24 = load i64, i64* %6
%25 = icmp ult i64 %23, %24
br i1 %25, label %26, label %49
26: ; preds = %22
%27 = load i8*, i8** %5
%28 = load i64, i64* %7
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = load i64, i64* %6
%31 = load i64, i64* %7
%32 = sub i64 %30, %31
%33 = load %struct.timeval*, %struct.timeval** %4
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = trunc i64 %35 to i32
%37 = sdiv i32 %36, 1000
%38 = call i32 @snprintf(i8* %29, i64 %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %26
%42 = load i64, i64* %7
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = add nsw i64 %42, %44
br label %47
46: ; preds = %26
br label %47
47: ; preds = %46, %41
%48 = phi i64 [ %45, %41 ], [ -1, %46 ]
store i64 %48, i64* %7
br label %49
49: ; preds = %47, %22, %15
br label %50
50: ; preds = %49, %3
%51 = load i64, i64* %7
ret i64 %51
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [6 x i8] c".%06d\00"
define i64 @format_timeval(%struct.timeval* %0, i8* %1, i64 %2) {
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%5 = tail call %struct.tm* @gmtime(i32* %4)
%6 = icmp eq %struct.tm* %5, null
br i1 %6, label %24, label %7
7: ; preds = %3
%8 = tail call i64 @strftime(i8* %1, i64 %2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %5)
%9 = icmp sgt i64 %8, 0
%10 = icmp ult i64 %8, %2
%11 = and i1 %9, %10
br i1 %11, label %12, label %24
12: ; preds = %7
%13 = getelementptr inbounds i8, i8* %1, i64 %8
%14 = sub i64 %2, %8
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%16 = load i64, i64* %15
%17 = trunc i64 %16 to i32
%18 = sdiv i32 %17, 1000
%19 = tail call i32 @snprintf(i8* %13, i64 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = icmp sgt i32 %19, 0
%21 = sext i32 %19 to i64
%22 = add nsw i64 %8, %21
%23 = select i1 %20, i64 %22, i64 -1
br label %24
24: ; preds = %7, %12, %3
%25 = phi i64 [ %23, %12 ], [ %8, %7 ], [ -1, %3 ]
ret i64 %25
}