#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close_log();
char fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ *open_log();
int strlen(char *);
int vsnprintf(char *, int, char *, int /*<<< orphan*/);
int write_log_comment_valist(int length, char *fmt, va_list ap) {
  int i, ret = 0;
  char *str;
  FILE *fp = open_log();
  if (fp == NULL)
    return 1;
  length++;
  str = malloc(length * sizeof(char));
  i = vsnprintf(str, length, fmt, ap);
  if ((length <= i) || (i < 0))
    ret = 1;
  length = strlen(str);
  fputc('#', fp);
  fputc(' ', fp);
  for (i = 0; i < length - 1; i++) {
    if (fputc(str[i], fp) == '\n') {
      fputc('#', fp);
      fputc(' ', fp);
    }
  }
  fputc(str[length - 1], fp);
  close_log();
  free(str);
  return ret;
}


x86 assembly (clang, -O0):
<write_log_comment_valist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <open_log@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ab <write_log_comment_valist+0x3b>
movl   $0x1,-0x4(%rbp)
jmp    12be <write_log_comment_valist+0x14e>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
movslq -0x8(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    11f3 <write_log_comment_valist+0x83>
cmpl   $0x0,-0x18(%rbp)
jge    11fa <write_log_comment_valist+0x8a>
movl   $0x1,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x8(%rbp)
mov    -0x30(%rbp),%rsi
mov    $0x23,%edi
call   1080 <fputc@plt>
mov    -0x30(%rbp),%rsi
mov    $0x20,%edi
call   1080 <fputc@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    128c <write_log_comment_valist+0x11c>
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x30(%rbp),%rsi
movsbl %al,%edi
call   1080 <fputc@plt>
movsbl %al,%eax
cmp    $0xa,%eax
jne    1279 <write_log_comment_valist+0x109>
mov    -0x30(%rbp),%rsi
mov    $0x23,%edi
call   1080 <fputc@plt>
mov    -0x30(%rbp),%rsi
mov    $0x20,%edi
call   1080 <fputc@plt>
jmp    127e <write_log_comment_valist+0x10e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1229 <write_log_comment_valist+0xb9>
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    -0x30(%rbp),%rsi
movsbl %al,%edi
call   1080 <fputc@plt>
mov    $0x0,%al
call   1070 <close_log@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_log_comment_valist>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r13
mov    %edi,%r15d
xor    %eax,%eax
call   1060 <open_log@plt>
test   %rax,%rax
je     125b <write_log_comment_valist+0xeb>
mov    %rax,%rbx
lea    0x1(%r15),%ebp
mov    %ebp,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r13,%rdx
mov    %r12d,%ecx
call   1040 <vsnprintf@plt>
cmp    %r15d,%eax
setg   %cl
test   %eax,%eax
sets   %r15b
or     %cl,%r15b
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    $0x23,%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
mov    $0x20,%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
lea    -0x1(%rbp),%r12d
cmp    $0x2,%ebp
jl     1236 <write_log_comment_valist+0xc6>
mov    %r12d,%r13d
xor    %ebp,%ebp
jmp    1209 <write_log_comment_valist+0x99>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    %rbp,%r13
je     1236 <write_log_comment_valist+0xc6>
movsbl (%r14,%rbp,1),%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
cmp    $0xa,%al
jne    1200 <write_log_comment_valist+0x90>
mov    $0x23,%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
mov    $0x20,%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
jmp    1200 <write_log_comment_valist+0x90>
movzbl %r15b,%ebp
movslq %r12d,%rax
movsbl (%r14,%rax,1),%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
xor    %eax,%eax
call   1070 <close_log@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1260 <write_log_comment_valist+0xf0>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_log_comment_valist(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%12 = call i32* (...) @open_log()
store i32* %12, i32** %11
%13 = load i32*, i32** %11
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 1, i32* %4
br label %80
16: ; preds = %3
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 1
%22 = trunc i64 %21 to i32
%23 = call i8* @malloc(i32 %22)
store i8* %23, i8** %10
%24 = load i8*, i8** %10
%25 = load i32, i32* %5
%26 = load i8*, i8** %6
%27 = load i32, i32* %7
%28 = call i32 @vsnprintf(i8* %24, i32 %25, i8* %26, i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %5
%30 = load i32, i32* %8
%31 = icmp sle i32 %29, %30
br i1 %31, label %35, label %32
32: ; preds = %16
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %32, %16
store i32 1, i32* %9
br label %36
36: ; preds = %35, %32
%37 = load i8*, i8** %10
%38 = call i32 @strlen(i8* %37)
store i32 %38, i32* %5
%39 = load i32*, i32** %11
%40 = call signext i8 @fputc(i8 signext 35, i32* %39)
%41 = load i32*, i32** %11
%42 = call signext i8 @fputc(i8 signext 32, i32* %41)
store i32 0, i32* %8
br label %43
43: ; preds = %64, %36
%44 = load i32, i32* %8
%45 = load i32, i32* %5
%46 = sub nsw i32 %45, 1
%47 = icmp slt i32 %44, %46
br i1 %47, label %48, label %67
48: ; preds = %43
%49 = load i8*, i8** %10
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i8, i8* %52
%54 = load i32*, i32** %11
%55 = call signext i8 @fputc(i8 signext %53, i32* %54)
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 10
br i1 %57, label %58, label %63
58: ; preds = %48
%59 = load i32*, i32** %11
%60 = call signext i8 @fputc(i8 signext 35, i32* %59)
%61 = load i32*, i32** %11
%62 = call signext i8 @fputc(i8 signext 32, i32* %61)
br label %63
63: ; preds = %58, %48
br label %64
64: ; preds = %63
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
br label %43
67: ; preds = %43
%68 = load i8*, i8** %10
%69 = load i32, i32* %5
%70 = sub nsw i32 %69, 1
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %68, i64 %71
%73 = load i8, i8* %72
%74 = load i32*, i32** %11
%75 = call signext i8 @fputc(i8 signext %73, i32* %74)
%76 = call i32 (...) @close_log()
%77 = load i8*, i8** %10
%78 = call i32 @free(i8* %77)
%79 = load i32, i32* %9
store i32 %79, i32* %4
br label %80
80: ; preds = %67, %15
%81 = load i32, i32* %4
ret i32 %81
}

LLVM IR (-O2):
define i32 @write_log_comment_valist(i32 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* (...) @open_log()
%5 = icmp eq i32* %4, null
br i1 %5, label %40, label %6
6: ; preds = %3
%7 = add nsw i32 %0, 1
%8 = tail call i8* @malloc(i32 %7)
%9 = tail call i32 @vsnprintf(i8* %8, i32 %7, i8* %1, i32 %2)
%10 = icmp sgt i32 %9, %0
%11 = icmp slt i32 %9, 0
%12 = or i1 %10, %11
%13 = zext i1 %12 to i32
%14 = tail call i32 @strlen(i8* %8)
%15 = tail call signext i8 @fputc(i8 signext 35, i32* %4)
%16 = tail call signext i8 @fputc(i8 signext 32, i32* %4)
%17 = add i32 %14, -1
%18 = icmp sgt i32 %14, 1
br i1 %18, label %19, label %33
19: ; preds = %6
%20 = zext i32 %17 to i64
br label %21
21: ; preds = %19, %30
%22 = phi i64 [ 0, %19 ], [ %31, %30 ]
%23 = getelementptr inbounds i8, i8* %8, i64 %22
%24 = load i8, i8* %23
%25 = tail call signext i8 @fputc(i8 signext %24, i32* %4)
%26 = icmp eq i8 %25, 10
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = tail call signext i8 @fputc(i8 signext 35, i32* %4)
%29 = tail call signext i8 @fputc(i8 signext 32, i32* %4)
br label %30
30: ; preds = %21, %27
%31 = add nuw nsw i64 %22, 1
%32 = icmp eq i64 %31, %20
br i1 %32, label %33, label %21
33: ; preds = %30, %6
%34 = sext i32 %17 to i64
%35 = getelementptr inbounds i8, i8* %8, i64 %34
%36 = load i8, i8* %35
%37 = tail call signext i8 @fputc(i8 signext %36, i32* %4)
%38 = tail call i32 (...) @close_log()
%39 = tail call i32 @free(i8* %8)
br label %40
40: ; preds = %3, %33
%41 = phi i32 [ %13, %33 ], [ 1, %3 ]
ret i32 %41
}