#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;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ safe_concatenate(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ vfprintf(int /*<<< orphan*/, char *, int /*<<< orphan*/);
void log_formatted(char *message, int type, va_list args) {
  char buffer[1024] = {0};
  switch (type) {
  case 1:
    safe_concatenate(buffer, 1024, "[ERROR]: ", message);
    vfprintf(stderr, buffer, args);
    fprintf(stderr, "\n");
    break;
  case 2:
    safe_concatenate(buffer, 1024, "[DEBUG]: ", message);
    vfprintf(stdout, buffer, args);
    fprintf(stdout, "\n");
    break;
  case 3:
    safe_concatenate(buffer, 1024, "[INFO]: ", message);
    vfprintf(stdout, buffer, args);
    fprintf(stdout, "\n");
    break;
  }
}


x86 assembly (clang, -O0):
<log_formatted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1030 <memset@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x414(%rbp)
sub    $0x1,%eax
je     11a7 <log_formatted+0x67>
jmp    117f <log_formatted+0x3f>
mov    -0x414(%rbp),%eax
sub    $0x2,%eax
je     11f5 <log_formatted+0xb5>
jmp    1193 <log_formatted+0x53>
mov    -0x414(%rbp),%eax
sub    $0x3,%eax
je     1243 <log_formatted+0x103>
jmp    128c <log_formatted+0x14c>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xe42(%rip),%rdx
call   1060 <safe_concatenate@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1050 <vfprintf@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1040 <fprintf@plt>
jmp    128c <log_formatted+0x14c>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xe00(%rip),%rdx
call   1060 <safe_concatenate@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1050 <vfprintf@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
call   1040 <fprintf@plt>
jmp    128c <log_formatted+0x14c>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xdbc(%rip),%rdx
call   1060 <safe_concatenate@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1050 <vfprintf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_formatted>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1030 <memset@plt>
add    $0xffffffff,%ebp
cmp    $0x2,%ebp
ja     11b8 <log_formatted+0x78>
movslq %ebp,%rax
lea    0xeaa(%rip),%rcx
movslq (%rcx,%rax,4),%rdx
add    %rcx,%rdx
lea    0x2c84(%rip),%rcx
mov    (%rcx,%rax,8),%rbp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x400,%esi
mov    %rbx,%rcx
call   1060 <safe_concatenate@plt>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <vfprintf@plt>
mov    0x0(%rbp),%edi
lea    0xe57(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"[ERROR]: \00"
@stderr = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [10 x i8] c"[DEBUG]: \00"
@stdout = global i32 0
@.str.3 = [9 x i8] c"[INFO]: \00"
define void @log_formatted(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1024 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = bitcast [1024 x i8]* %7 to i8*
%9 = load i32, i32* %5
switch i32 %9, label %40 [
i32 1, label %10
i32 2, label %20
i32 3, label %30
]
10: ; preds = %3
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%12 = load i8*, i8** %4
%13 = call i32 @safe_concatenate(i8* %11, i32 1024, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @stderr
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%16 = load i32, i32* %6
%17 = call i32 @vfprintf(i32 %14, i8* %15, i32 %16)
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %40
20: ; preds = %3
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%22 = load i8*, i8** %4
%23 = call i32 @safe_concatenate(i8* %21, i32 1024, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @stdout
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%26 = load i32, i32* %6
%27 = call i32 @vfprintf(i32 %24, i8* %25, i32 %26)
%28 = load i32, i32* @stdout
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %40
30: ; preds = %3
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%32 = load i8*, i8** %4
%33 = call i32 @safe_concatenate(i8* %31, i32 1024, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @stdout
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%36 = load i32, i32* %6
%37 = call i32 @vfprintf(i32 %34, i8* %35, i32 %36)
%38 = load i32, i32* @stdout
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %3, %30, %20, %10
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"[ERROR]: \00"
@stderr = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [10 x i8] c"[DEBUG]: \00"
@stdout = global i32 0
@.str.3 = [9 x i8] c"[INFO]: \00"
@reltable.log_formatted = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([10 x i8]* @.str to i64), i64 ptrtoint ([3 x i32]* @reltable.log_formatted to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([10 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.log_formatted to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.3 to i64), i64 ptrtoint ([3 x i32]* @reltable.log_formatted to i64)) to i32)]
@switch.table.log_formatted.4 = [3 x i32*] [i32* @stderr, i32* @stdout, i32* @stdout]
define void @log_formatted(i8* %0, i32 %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = add i32 %1, -1
%7 = icmp ult i32 %6, 3
br i1 %7, label %8, label %20
8: ; preds = %3
%9 = sext i32 %6 to i64
%10 = shl i64 %9, 2
%11 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.log_formatted to i8*), i64 %10)
%12 = sext i32 %6 to i64
%13 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.log_formatted.4, i64 0, i64 %12
%14 = load i32*, i32** %13
%15 = call i32 @safe_concatenate(i8* %5, i32 1024, i8* %11, i8* %0)
%16 = load i32, i32* %14
%17 = call i32 @vfprintf(i32 %16, i8* %5, i32 %2)
%18 = load i32, i32* %14
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %3, %8
ret void
}