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

/* Variables and functions */
int append_args_katcl(struct katcl_line *, int /*<<< orphan*/, char *,
                      int /*<<< orphan*/, unsigned int);
int append_string_katcl(struct katcl_line *, int, char *);
int append_vargs_katcl(struct katcl_line *, int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
char *log_to_string_katcl(int);
int vector_sum(int *, int);
int vlog_message_katcl(struct katcl_line *cl, int level, char *name, char *fmt,
                       va_list args) {
  int result[5];
  struct timeval now;
  unsigned int milli;
  char *subsystem, *logstring;
  logstring = log_to_string_katcl(level);
  if (logstring == NULL) {
    return -1;
  }
  subsystem = name ? name : "unknown";
  gettimeofday(&now, NULL);
  milli = now.tv_usec / 1000;
  result[0] = append_string_katcl(cl, 0x01, "#log");
  result[1] = append_string_katcl(cl, 0, logstring);
  result[2] = append_args_katcl(cl, 0, "%lu%03d", now.tv_sec, milli);
  result[3] = append_string_katcl(cl, 0, subsystem);
  result[4] = append_vargs_katcl(cl, 0x02, fmt, args);
  return vector_sum(result, 5);
}


x86 assembly (clang, -O0):
<vlog_message_katcl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <log_to_string_katcl@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    119e <vlog_message_katcl+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <vlog_message_katcl+0x10a>
cmpq   $0x0,-0x20(%rbp)
je     11b6 <vlog_message_katcl+0x56>
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
jmp    11c6 <vlog_message_katcl+0x66>
lea    0xe43(%rip),%rax
mov    %rax,-0x68(%rbp)
jmp    11c6 <vlog_message_katcl+0x66>
mov    -0x68(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x48(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
lea    0xe0f(%rip),%rdx
call   1030 <append_string_katcl@plt>
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x60(%rbp),%rdx
xor    %esi,%esi
call   1030 <append_string_katcl@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x44(%rbp),%ecx
mov    -0x4c(%rbp),%r8d
xor    %esi,%esi
lea    0xde6(%rip),%rdx
call   1060 <append_args_katcl@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x58(%rbp),%rdx
xor    %esi,%esi
call   1030 <append_string_katcl@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x2,%esi
call   1050 <append_vargs_katcl@plt>
mov    %eax,-0x30(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x5,%esi
call   1080 <vector_sum@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vlog_message_katcl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rdi,%rbp
mov    %esi,%edi
call   1070 <log_to_string_katcl@plt>
test   %rax,%rax
je     123e <vlog_message_katcl+0xde>
mov    %rax,%r13
test   %rbx,%rbx
lea    0xe69(%rip),%r12
cmovne %rbx,%r12
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
movslq 0x8(%rsp),%rax
imul   $0x10624dd3,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x26,%rbx
add    %eax,%ebx
lea    0xe41(%rip),%rdx
mov    %rbp,%rdi
mov    $0x1,%esi
call   1030 <append_string_katcl@plt>
mov    %eax,0x10(%rsp)
mov    %rbp,%rdi
xor    %esi,%esi
mov    %r13,%rdx
call   1030 <append_string_katcl@plt>
mov    %eax,0x14(%rsp)
mov    0xc(%rsp),%ecx
lea    0xe19(%rip),%rdx
mov    %rbp,%rdi
xor    %esi,%esi
mov    %ebx,%r8d
call   1060 <append_args_katcl@plt>
mov    %eax,0x18(%rsp)
mov    %rbp,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1030 <append_string_katcl@plt>
mov    %eax,0x1c(%rsp)
mov    %rbp,%rdi
mov    $0x2,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1050 <append_vargs_katcl@plt>
mov    %eax,0x20(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x5,%esi
call   1080 <vector_sum@plt>
jmp    1243 <vlog_message_katcl+0xe3>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.katcl_line = type { i32 }
%struct.timeval = type { i32, i32 }
@.str = [8 x i8] c"unknown\00"
@.str.1 = [5 x i8] c"#log\00"
@.str.2 = [8 x i8] c"%lu%03d\00"
define i32 @vlog_message_katcl(%struct.katcl_line* %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.katcl_line*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca [5 x i32]
%13 = alloca %struct.timeval
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i8*
store %struct.katcl_line* %0, %struct.katcl_line** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%17 = load i32, i32* %8
%18 = call i8* @log_to_string_katcl(i32 %17)
store i8* %18, i8** %16
%19 = load i8*, i8** %16
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %5
store i32 -1, i32* %6
br label %58
22: ; preds = %5
%23 = load i8*, i8** %9
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = load i8*, i8** %9
br label %28
27: ; preds = %22
br label %28
28: ; preds = %27, %25
%29 = phi i8* [ %26, %25 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %27 ]
store i8* %29, i8** %15
%30 = call i32 @gettimeofday(%struct.timeval* %13, i32* null)
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%32 = load i32, i32* %31
%33 = sdiv i32 %32, 1000
store i32 %33, i32* %14
%34 = load %struct.katcl_line*, %struct.katcl_line** %7
%35 = call i32 @append_string_katcl(%struct.katcl_line* %34, i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%36 = getelementptr inbounds [5 x i32], [5 x i32]* %12, i64 0, i64 0
store i32 %35, i32* %36
%37 = load %struct.katcl_line*, %struct.katcl_line** %7
%38 = load i8*, i8** %16
%39 = call i32 @append_string_katcl(%struct.katcl_line* %37, i32 0, i8* %38)
%40 = getelementptr inbounds [5 x i32], [5 x i32]* %12, i64 0, i64 1
store i32 %39, i32* %40
%41 = load %struct.katcl_line*, %struct.katcl_line** %7
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
%43 = load i32, i32* %42
%44 = load i32, i32* %14
%45 = call i32 @append_args_katcl(%struct.katcl_line* %41, i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %43, i32 %44)
%46 = getelementptr inbounds [5 x i32], [5 x i32]* %12, i64 0, i64 2
store i32 %45, i32* %46
%47 = load %struct.katcl_line*, %struct.katcl_line** %7
%48 = load i8*, i8** %15
%49 = call i32 @append_string_katcl(%struct.katcl_line* %47, i32 0, i8* %48)
%50 = getelementptr inbounds [5 x i32], [5 x i32]* %12, i64 0, i64 3
store i32 %49, i32* %50
%51 = load %struct.katcl_line*, %struct.katcl_line** %7
%52 = load i8*, i8** %10
%53 = load i32, i32* %11
%54 = call i32 @append_vargs_katcl(%struct.katcl_line* %51, i32 2, i8* %52, i32 %53)
%55 = getelementptr inbounds [5 x i32], [5 x i32]* %12, i64 0, i64 4
store i32 %54, i32* %55
%56 = getelementptr inbounds [5 x i32], [5 x i32]* %12, i64 0, i64 0
%57 = call i32 @vector_sum(i32* %56, i32 5)
store i32 %57, i32* %6
br label %58
58: ; preds = %28, %21
%59 = load i32, i32* %6
ret i32 %59
}

LLVM IR (-O2):
%struct.katcl_line = type { i32 }
%struct.timeval = type { i32, i32 }
@.str = [8 x i8] c"unknown\00"
@.str.1 = [5 x i8] c"#log\00"
@.str.2 = [8 x i8] c"%lu%03d\00"
define i32 @vlog_message_katcl(%struct.katcl_line* %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca [5 x i32]
%7 = alloca %struct.timeval
%8 = bitcast [5 x i32]* %6 to i8*
%9 = bitcast %struct.timeval* %7 to i8*
%10 = tail call i8* @log_to_string_katcl(i32 %1)
%11 = icmp eq i8* %10, null
br i1 %11, label %32, label %12
12: ; preds = %5
%13 = icmp eq i8* %2, null
%14 = select i1 %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %2
%15 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
%17 = load i32, i32* %16
%18 = sdiv i32 %17, 1000
%19 = tail call i32 @append_string_katcl(%struct.katcl_line* %0, i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = getelementptr inbounds [5 x i32], [5 x i32]* %6, i64 0, i64 0
store i32 %19, i32* %20
%21 = tail call i32 @append_string_katcl(%struct.katcl_line* %0, i32 0, i8* %10)
%22 = getelementptr inbounds [5 x i32], [5 x i32]* %6, i64 0, i64 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
%24 = load i32, i32* %23
%25 = tail call i32 @append_args_katcl(%struct.katcl_line* %0, i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %18)
%26 = getelementptr inbounds [5 x i32], [5 x i32]* %6, i64 0, i64 2
store i32 %25, i32* %26
%27 = tail call i32 @append_string_katcl(%struct.katcl_line* %0, i32 0, i8* %14)
%28 = getelementptr inbounds [5 x i32], [5 x i32]* %6, i64 0, i64 3
store i32 %27, i32* %28
%29 = tail call i32 @append_vargs_katcl(%struct.katcl_line* %0, i32 2, i8* %3, i32 %4)
%30 = getelementptr inbounds [5 x i32], [5 x i32]* %6, i64 0, i64 4
store i32 %29, i32* %30
%31 = call i32 @vector_sum(i32* %20, i32 5)
br label %32
32: ; preds = %5, %12
%33 = phi i32 [ %31, %12 ], [ -1, %5 ]
ret i32 %33
}