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

/* Variables and functions */
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, char const *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int log_msg(FILE *fp, const char *location, int new_line_flag,
            const char *format, va_list ap) {
  char buffer[512], *bptr = buffer;
  int bsize = sizeof(buffer);
  if (vsnprintf(bptr, bsize, format, ap) < 0)
    return -errno;
  if (location != NULL) {
    if (fprintf(fp, "{%s} ", location) < 0)
      return -errno;
  }
  if (new_line_flag)
    strcat(buffer, "\n");
  if (fprintf(fp, "%s", buffer) < 0)
    return -errno;
  fflush(fp);
  return 0;
}


x86 assembly (clang, -O0):
<log_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x238(%rbp)
movl   $0x200,-0x23c(%rbp)
mov    -0x238(%rbp),%rdi
mov    -0x23c(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1030 <vsnprintf@plt>
cmp    $0x0,%rax
jge    11ac <log_msg+0x6c>
xor    %eax,%eax
mov    0x2e3e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124e <log_msg+0x10e>
cmpq   $0x0,-0x18(%rbp)
je     11ed <log_msg+0xad>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe3a(%rip),%rsi
call   1040 <fprintf@plt>
cmp    $0x0,%rax
jge    11e8 <log_msg+0xa8>
xor    %eax,%eax
mov    0x2e02(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124e <log_msg+0x10e>
jmp    11ed <log_msg+0xad>
cmpl   $0x0,-0x1c(%rbp)
je     120a <log_msg+0xca>
lea    -0x230(%rbp),%rdi
lea    0xe01(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x230(%rbp),%rdx
lea    0xdec(%rip),%rsi
call   1040 <fprintf@plt>
cmp    $0x0,%rax
jge    123e <log_msg+0xfe>
xor    %eax,%eax
mov    0x2dac(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124e <log_msg+0x10e>
mov    -0x10(%rbp),%rdi
call   1050 <fflush@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_msg>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %rcx,%rdx
mov    %r8d,%ecx
call   1030 <vsnprintf@plt>
test   %rax,%rax
js     11bf <log_msg+0x7f>
test   %rbp,%rbp
je     1188 <log_msg+0x48>
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1040 <fprintf@plt>
test   %rax,%rax
js     11bf <log_msg+0x7f>
test   %r14d,%r14d
je     119c <log_msg+0x5c>
lea    0xe72(%rip),%rsi
mov    %rsp,%rdi
call   1060 <strcat@plt>
lea    0xe65(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
call   1040 <fprintf@plt>
test   %rax,%rax
js     11bf <log_msg+0x7f>
mov    %rbx,%rdi
call   1050 <fflush@plt>
xor    %eax,%eax
jmp    11ca <log_msg+0x8a>
mov    0x2e1a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [6 x i8] c"{%s} \00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @log_msg(i32* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca [512 x i8]
%13 = alloca i8*
%14 = alloca i32
store i32* %0, i32** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
store i8* %15, i8** %13
store i32 512, i32* %14
%16 = load i8*, i8** %13
%17 = load i32, i32* %14
%18 = load i8*, i8** %10
%19 = load i32, i32* %11
%20 = call i64 @vsnprintf(i8* %16, i32 %17, i8* %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %5
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
store i32 %24, i32* %6
br label %54
25: ; preds = %5
%26 = load i8*, i8** %8
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %37
28: ; preds = %25
%29 = load i32*, i32** %7
%30 = load i8*, i8** %8
%31 = call i64 @fprintf(i32* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
store i32 %35, i32* %6
br label %54
36: ; preds = %28
br label %37
37: ; preds = %36, %25
%38 = load i32, i32* %9
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%42 = call i32 @strcat(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %40, %37
%44 = load i32*, i32** %7
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%46 = call i64 @fprintf(i32* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* @errno
%50 = sub nsw i32 0, %49
store i32 %50, i32* %6
br label %54
51: ; preds = %43
%52 = load i32*, i32** %7
%53 = call i32 @fflush(i32* %52)
store i32 0, i32* %6
br label %54
54: ; preds = %51, %48, %33, %22
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [6 x i8] c"{%s} \00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @log_msg(i32* nocapture %0, i8* %1, i32 %2, i8* nocapture %3, i32 %4) {
%6 = alloca [512 x i8]
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%8 = call i64 @vsnprintf(i8* %7, i32 512, i8* %3, i32 %4)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %33
13: ; preds = %5
%14 = icmp eq i8* %1, null
br i1 %14, label %21, label %15
15: ; preds = %13
%16 = tail call i64 @fprintf(i32* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
br label %33
21: ; preds = %15, %13
%22 = icmp eq i32 %2, 0
br i1 %22, label %25, label %23
23: ; preds = %21
%24 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %21
%26 = call i64 @fprintf(i32* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @errno
%30 = sub nsw i32 0, %29
br label %33
31: ; preds = %25
%32 = call i32 @fflush(i32* %0)
br label %33
33: ; preds = %31, %28, %18, %10
%34 = phi i32 [ %12, %10 ], [ %20, %18 ], [ %30, %28 ], [ 0, %31 ]
ret i32 %34
}