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

/* Variables and functions */
int add_unsafe_parse_katcl(struct katcl_parse *, int, char *);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int /*<<< orphan*/,
                            unsigned int);
int add_timestamp_parse_katcl(struct katcl_parse *p, int flags,
                              struct timeval *tv) {
  struct timeval *tvp, tva;
  unsigned int milli;
  char buffer[16];
  if (tv == NULL) {
    tvp = &tva;
    if (gettimeofday(tvp, NULL) < 0) {
      return -1;
    }
  } else {
    tvp = tv;
  }
  milli = tvp->tv_usec / 1000;
  snprintf(buffer, 16, "%lu%03u", tvp->tv_sec, milli);
  buffer[16 - 1] = '\0';
  return add_unsafe_parse_katcl(p, flags | 0x10, buffer);
}


x86 assembly (clang, -O0):
<add_timestamp_parse_katcl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117e <add_timestamp_parse_katcl+0x4e>
lea    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
jge    1179 <add_timestamp_parse_katcl+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <add_timestamp_parse_katcl+0xa1>
jmp    1186 <add_timestamp_parse_katcl+0x56>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x34(%rbp),%r8d
mov    $0x10,%esi
lea    0xe4e(%rip),%rdx
call   1040 <snprintf@plt>
movb   $0x0,-0x41(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
or     $0x10,%esi
lea    -0x50(%rbp),%rdx
call   1030 <add_unsafe_parse_katcl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_timestamp_parse_katcl>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
test   %rdx,%rdx
jne    1159 <add_timestamp_parse_katcl+0x29>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
js     11a9 <add_timestamp_parse_katcl+0x79>
movslq (%rbx),%rax
imul   $0x10624dd3,%rax,%r8
mov    %r8,%rax
shr    $0x3f,%rax
sar    $0x26,%r8
add    %eax,%r8d
mov    0x4(%rbx),%ecx
lea    0xe85(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x10,%esi
call   1040 <snprintf@plt>
movb   $0x0,0xf(%rsp)
or     $0x10,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1030 <add_unsafe_parse_katcl@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    11a0 <add_timestamp_parse_katcl+0x70>

LLVM IR (-O0):
%struct.katcl_parse = type { i32 }
%struct.timeval = type { i32, i32 }
@.str = [8 x i8] c"%lu%03u\00"
define i32 @add_timestamp_parse_katcl(%struct.katcl_parse* %0, i32 %1, %struct.timeval* %2) {
%4 = alloca i32
%5 = alloca %struct.katcl_parse*
%6 = alloca i32
%7 = alloca %struct.timeval*
%8 = alloca %struct.timeval*
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca [16 x i8]
store %struct.katcl_parse* %0, %struct.katcl_parse** %5
store i32 %1, i32* %6
store %struct.timeval* %2, %struct.timeval** %7
%12 = load %struct.timeval*, %struct.timeval** %7
%13 = icmp eq %struct.timeval* %12, null
br i1 %13, label %14, label %20
14: ; preds = %3
store %struct.timeval* %9, %struct.timeval** %8
%15 = load %struct.timeval*, %struct.timeval** %8
%16 = call i64 @gettimeofday(%struct.timeval* %15, i32* null)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 -1, i32* %4
br label %39
19: ; preds = %14
br label %22
20: ; preds = %3
%21 = load %struct.timeval*, %struct.timeval** %7
store %struct.timeval* %21, %struct.timeval** %8
br label %22
22: ; preds = %20, %19
%23 = load %struct.timeval*, %struct.timeval** %8
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = sdiv i32 %25, 1000
store i32 %26, i32* %10
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%28 = load %struct.timeval*, %struct.timeval** %8
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load i32, i32* %10
%32 = call i32 @snprintf(i8* %27, i32 16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %31)
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 15
store i8 0, i8* %33
%34 = load %struct.katcl_parse*, %struct.katcl_parse** %5
%35 = load i32, i32* %6
%36 = or i32 %35, 16
%37 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%38 = call i32 @add_unsafe_parse_katcl(%struct.katcl_parse* %34, i32 %36, i8* %37)
store i32 %38, i32* %4
br label %39
39: ; preds = %22, %18
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
%struct.katcl_parse = type { i32 }
%struct.timeval = type { i32, i32 }
@.str = [8 x i8] c"%lu%03u\00"
define i32 @add_timestamp_parse_katcl(%struct.katcl_parse* %0, i32 %1, %struct.timeval* %2) {
%4 = alloca %struct.timeval
%5 = alloca [16 x i8]
%6 = bitcast %struct.timeval* %4 to i8*
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%8 = icmp eq %struct.timeval* %2, null
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = call i64 @gettimeofday(%struct.timeval* %4, i32* null)
%11 = icmp slt i64 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %3, %9
%13 = phi %struct.timeval* [ %4, %9 ], [ %2, %3 ]
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i64 0, i32 0
%15 = load i32, i32* %14
%16 = sdiv i32 %15, 1000
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 @snprintf(i8* %7, i32 16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %16)
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 15
store i8 0, i8* %20
%21 = or i32 %1, 16
%22 = call i32 @add_unsafe_parse_katcl(%struct.katcl_parse* %0, i32 %21, i8* %7)
br label %23
23: ; preds = %9, %12
%24 = phi i32 [ %22, %12 ], [ -1, %9 ]
ret i32 %24
}