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

/* Variables and functions */
int ERANGE;
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
struct tm *localtime(int /*<<< orphan*/ *);
int snprintf(char *, int, char *, int, int, int, int, int, int, int);
int time_string_now(char *buf) {
  struct tm *tm;
  struct timeval tv;
  if (gettimeofday(&tv, NULL) < 0)
    return -1;
  tm = localtime(&tv.tv_sec);
  if (tm == NULL)
    return -1;
  tm->tm_year += 1900;
  tm->tm_mon += 1;
  if (snprintf(buf, (4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 3 + 1),
               "%4d-%02d-%02d %02d:%02d:%02d.%03d", tm->tm_year, tm->tm_mon,
               tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
               (int)(tv.tv_usec / 1000ull % 1000)) >=
      (4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 3 + 1))
    return ERANGE;
  return 0;
}


x86 assembly (clang, -O0):
<time_string_now>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x48,%rsp
mov    %rdi,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
jge    1160 <time_string_now+0x30>
movl   $0xffffffff,-0xc(%rbp)
jmp    1231 <time_string_now+0x101>
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
call   1030 <localtime@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1188 <time_string_now+0x58>
movl   $0xffffffff,-0xc(%rbp)
jmp    1231 <time_string_now+0x101>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x76c,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%r9d
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%ebx
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%r11d
mov    -0x20(%rbp),%rax
mov    0x14(%rax),%r10d
mov    -0x30(%rbp),%rax
mov    $0x3e8,%esi
xor    %edx,%edx
div    %rsi
mov    $0x3e8,%esi
xor    %edx,%edx
div    %rsi
mov    %edx,%eax
mov    $0x18,%esi
lea    0xe06(%rip),%rdx
mov    %ebx,(%rsp)
mov    %r11d,0x8(%rsp)
mov    %r10d,0x10(%rsp)
mov    %eax,0x18(%rsp)
call   1040 <snprintf@plt>
cmp    $0x18,%eax
jl     122a <time_string_now+0xfa>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    1231 <time_string_now+0x101>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_string_now>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     11ef <time_string_now+0xbf>
lea    0x8(%rsp),%rdi
call   1030 <localtime@plt>
test   %rax,%rax
je     11ef <time_string_now+0xbf>
mov    %rax,%rsi
mov    $0x76c,%ecx
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    0x4(%rax),%r8d
mov    0x8(%rax),%r9d
add    $0x1,%r8d
mov    %r8d,0x4(%rax)
mov    0xc(%rax),%r10d
mov    0x10(%rax),%r11d
mov    (%rsp),%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rdi
9b c4 20
mul    %rdi
mov    %rdx,%rax
mov    0x14(%rsi),%r14d
mov    %rdx,%rbp
shr    $0x7,%rax
mul    %rdi
shr    $0x4,%rbp
shr    $0x4,%rdx
imul   $0x3e8,%edx,%eax
sub    %eax,%ebp
lea    0xe39(%rip),%rdx
mov    %rbx,%rdi
mov    $0x18,%esi
push   %rbp
push   %r14
push   %r11
push   %r10
call   1040 <snprintf@plt>
add    $0x20,%rsp
xor    %ebp,%ebp
cmp    $0x18,%eax
jl     11ef <time_string_now+0xbf>
mov    0x2dfb(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
%struct.timeval = type { i64, i32 }
@.str = [34 x i8] c"%4d-%02d-%02d %02d:%02d:%02d.%03d\00"
@ERANGE = global i32 0
define i32 @time_string_now(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.tm*
%5 = alloca %struct.timeval
store i8* %0, i8** %3
%6 = call i64 @gettimeofday(%struct.timeval* %5, i32* null)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %53
9: ; preds = %1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%11 = call %struct.tm* @localtime(i32* %10)
store %struct.tm* %11, %struct.tm** %4
%12 = load %struct.tm*, %struct.tm** %4
%13 = icmp eq %struct.tm* %12, null
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 -1, i32* %2
br label %53
15: ; preds = %9
%16 = load %struct.tm*, %struct.tm** %4
%17 = getelementptr inbounds %struct.tm, %struct.tm* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = add nsw i32 %18, 1900
store i32 %19, i32* %17
%20 = load %struct.tm*, %struct.tm** %4
%21 = getelementptr inbounds %struct.tm, %struct.tm* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = add nsw i32 %22, 1
store i32 %23, i32* %21
%24 = load i8*, i8** %3
%25 = load %struct.tm*, %struct.tm** %4
%26 = getelementptr inbounds %struct.tm, %struct.tm* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load %struct.tm*, %struct.tm** %4
%29 = getelementptr inbounds %struct.tm, %struct.tm* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load %struct.tm*, %struct.tm** %4
%32 = getelementptr inbounds %struct.tm, %struct.tm* %31, i32 0, i32 2
%33 = load i32, i32* %32
%34 = load %struct.tm*, %struct.tm** %4
%35 = getelementptr inbounds %struct.tm, %struct.tm* %34, i32 0, i32 3
%36 = load i32, i32* %35
%37 = load %struct.tm*, %struct.tm** %4
%38 = getelementptr inbounds %struct.tm, %struct.tm* %37, i32 0, i32 4
%39 = load i32, i32* %38
%40 = load %struct.tm*, %struct.tm** %4
%41 = getelementptr inbounds %struct.tm, %struct.tm* %40, i32 0, i32 5
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%44 = load i64, i64* %43
%45 = udiv i64 %44, 1000
%46 = urem i64 %45, 1000
%47 = trunc i64 %46 to i32
%48 = call i32 @snprintf(i8* %24, i32 24, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %30, i32 %33, i32 %36, i32 %39, i32 %42, i32 %47)
%49 = icmp sge i32 %48, 24
br i1 %49, label %50, label %52
50: ; preds = %15
%51 = load i32, i32* @ERANGE
store i32 %51, i32* %2
br label %53
52: ; preds = %15
store i32 0, i32* %2
br label %53
53: ; preds = %52, %50, %14, %8
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [34 x i8] c"%4d-%02d-%02d %02d:%02d:%02d.%03d\00"
@ERANGE = global i32 0
define i32 @time_string_now(i8* %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = call i64 @gettimeofday(%struct.timeval* %2, i32* null)
%5 = icmp slt i64 %4, 0
br i1 %5, label %34, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%8 = call %struct.tm* @localtime(i32* %7)
%9 = icmp eq %struct.tm* %8, null
br i1 %9, label %34, label %10
10: ; preds = %6
%11 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 0
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1900
store i32 %13, i32* %11
%14 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 1
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1
store i32 %16, i32* %14
%17 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 2
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 3
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 4
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.tm, %struct.tm* %8, 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 = udiv i64 %26, 1000
%28 = urem i64 %27, 1000
%29 = trunc i64 %28 to i32
%30 = call i32 @snprintf(i8* %0, i32 24, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %16, i32 %18, i32 %20, i32 %22, i32 %24, i32 %29)
%31 = icmp sgt i32 %30, 23
%32 = load i32, i32* @ERANGE
%33 = select i1 %31, i32 %32, i32 0
br label %34
34: ; preds = %10, %6, %1
%35 = phi i32 [ -1, %1 ], [ -1, %6 ], [ %33, %10 ]
ret i32 %35
}