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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, char *, ...);
int /*<<< orphan*/ free(char *);
char *merge_tags(char **, size_t);
int /*<<< orphan*/ printf(char *, ...);
int log_message(short level, char *message, char **tags, size_t tag_count) {
  FILE *log_file = fopen("log.txt", "a");
  if (log_file == NULL) {
    printf("Could not open the log file!\n");
  } else {
    char *level_string = NULL;
    if (level == 1) {
      level_string = "INFO";
    } else if (level == 2) {
      level_string = "WARN";
    }
    if (level_string != NULL) {
      if (tag_count > 0) {
        char *tag_string = merge_tags(tags, tag_count);
        fprintf(log_file, "%s [%s]: %s\n", level_string, tag_string, message);
        free(tag_string);
      } else {
        fprintf(log_file, "%s: %s\n", level_string, message);
      }
      fclose(log_file);
      return 0;
    } else {
      printf("Got unkown log level: %d!\n", level);
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<log_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    0xe7e(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b0 <log_message+0x50>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    128b <log_message+0x12b>
movq   $0x0,-0x30(%rbp)
movswl -0x6(%rbp),%eax
cmp    $0x1,%eax
jne    11d5 <log_message+0x75>
lea    0xe5c(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11f2 <log_message+0x92>
movswl -0x6(%rbp),%eax
cmp    $0x2,%eax
jne    11ed <log_message+0x8d>
lea    0xe44(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11f2 <log_message+0x92>
cmpq   $0x0,-0x30(%rbp)
je     1274 <log_message+0x114>
cmpq   $0x0,-0x20(%rbp)
jbe    1245 <log_message+0xe5>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <merge_tags@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    125f <log_message+0xff>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1292 <log_message+0x132>
movswl -0x6(%rbp),%esi
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    128b <log_message+0x12b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_message>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe73(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11eb <log_message+0x7b>
mov    %rax,%rbx
cmp    $0x1,%bp
je     11f9 <log_message+0x89>
cmp    $0x2,%bp
jne    1228 <log_message+0xb8>
lea    0xe5b(%rip),%r13
test   %r15,%r15
je     1205 <log_message+0x95>
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <merge_tags@plt>
mov    %rax,%rbp
lea    0xe46(%rip),%rsi
mov    %rbx,%rdi
mov    %r13,%rdx
mov    %rax,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    121c <log_message+0xac>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
jmp    1239 <log_message+0xc9>
lea    0xe0a(%rip),%r13
test   %r15,%r15
jne    11b9 <log_message+0x49>
lea    0xe15(%rip),%rsi
mov    %rbx,%rdi
mov    %r13,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    123e <log_message+0xce>
movswl %bp,%esi
lea    0xdf7(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"log.txt\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [30 x i8] c"Could not open the log file!\0A\00"
@.str.3 = [5 x i8] c"INFO\00"
@.str.4 = [5 x i8] c"WARN\00"
@.str.5 = [13 x i8] c"%s [%s]: %s\0A\00"
@.str.6 = [8 x i8] c"%s: %s\0A\00"
@.str.7 = [27 x i8] c"Got unkown log level: %d!\0A\00"
define i32 @log_message(i16 signext %0, i8* %1, i8** %2, i64 %3) {
%5 = alloca i32
%6 = alloca i16
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i8*
store i16 %0, i16* %6
store i8* %1, i8** %7
store i8** %2, i8*** %8
store i64 %3, i64* %9
%13 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %10
%14 = load i32*, i32** %10
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %59
18: ; preds = %4
store i8* null, i8** %11
%19 = load i16, i16* %6
%20 = sext i16 %19 to i32
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %23
22: ; preds = %18
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %11
br label %29
23: ; preds = %18
%24 = load i16, i16* %6
%25 = sext i16 %24 to i32
%26 = icmp eq i32 %25, 2
br i1 %26, label %27, label %28
27: ; preds = %23
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8** %11
br label %28
28: ; preds = %27, %23
br label %29
29: ; preds = %28, %22
%30 = load i8*, i8** %11
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %54
32: ; preds = %29
%33 = load i64, i64* %9
%34 = icmp ugt i64 %33, 0
br i1 %34, label %35, label %46
35: ; preds = %32
%36 = load i8**, i8*** %8
%37 = load i64, i64* %9
%38 = call i8* @merge_tags(i8** %36, i64 %37)
store i8* %38, i8** %12
%39 = load i32*, i32** %10
%40 = load i8*, i8** %11
%41 = load i8*, i8** %12
%42 = load i8*, i8** %7
%43 = call i32 (i32*, i8*, i8*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %40, i8* %41, i8* %42)
%44 = load i8*, i8** %12
%45 = call i32 @free(i8* %44)
br label %51
46: ; preds = %32
%47 = load i32*, i32** %10
%48 = load i8*, i8** %11
%49 = load i8*, i8** %7
%50 = call i32 (i32*, i8*, i8*, i8*, ...) @fprintf(i32* %47, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* %48, i8* %49)
br label %51
51: ; preds = %46, %35
%52 = load i32*, i32** %10
%53 = call i32 @fclose(i32* %52)
store i32 0, i32* %5
br label %60
54: ; preds = %29
%55 = load i16, i16* %6
%56 = sext i16 %55 to i32
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %54
br label %59
59: ; preds = %58, %16
store i32 1, i32* %5
br label %60
60: ; preds = %59, %51
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
@.str = [8 x i8] c"log.txt\00"
@.str.1 = [2 x i8] c"a\00"
@.str.3 = [5 x i8] c"INFO\00"
@.str.4 = [5 x i8] c"WARN\00"
@.str.5 = [13 x i8] c"%s [%s]: %s\0A\00"
@.str.6 = [8 x i8] c"%s: %s\0A\00"
@.str.7 = [27 x i8] c"Got unkown log level: %d!\0A\00"
@str = [29 x i8] c"Could not open the log file!\00"
define i32 @log_message(i16 signext %0, i8* %1, i8** %2, i64 %3) {
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %25
9: ; preds = %4
switch i16 %0, label %20 [
i16 1, label %11
i16 2, label %10
]
10: ; preds = %9
br label %11
11: ; preds = %10, %9
%12 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %9 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %10 ]
%13 = icmp eq i64 %3, 0
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = tail call i8* @merge_tags(i8** %2, i64 %3)
%16 = tail call i32 (i32*, i8*, i8*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %12, i8* %15, i8* %1)
%17 = tail call i32 @free(i8* %15)
br label %23
18: ; preds = %11
%19 = tail call i32 (i32*, i8*, i8*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* %12, i8* %1)
br label %23
20: ; preds = %9
%21 = sext i16 %0 to i32
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %21)
br label %25
23: ; preds = %14, %18
%24 = tail call i32 @fclose(i32* %5)
br label %25
25: ; preds = %7, %20, %23
%26 = phi i32 [ 0, %23 ], [ 1, %20 ], [ 1, %7 ]
ret i32 %26
}