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

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char *, int);
int /*<<< orphan*/ strcpy(char *, ...);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int klog(char *msg) {
  char *buf = malloc(sizeof(char) * 256);
  int ret = -1;
  int outbuf = open("/dev/kmsg", O_WRONLY);
  if (outbuf > 0) {
    strcpy(buf, *msg);
  } else {
    if (access("/cache/lible.log", F_OK) == -1) {
      outbuf = open("/cache/lible.log", O_CREAT | O_WRONLY);
    } else {
      outbuf = open("/cache/lible.log", O_APPEND);
    }
    strcpy(buf, "COULD NOT OPEN KMSG PLEASE FIX");
  }
  ret = write(outbuf, buf, strlen(buf));
  close(outbuf);
  free(buf);
  return ret;
}


x86 assembly (clang, -O0):
<klog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11da <klog+0x5a>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
movsbl (%rax),%esi
xor    %eax,%eax
call   1040 <strcpy@plt>
jmp    1248 <klog+0xc8>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    0xe20(%rip),%rdi
call   10a0 <access@plt>
cmp    $0xffffffff,%eax
jne    121e <klog+0x9e>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    0x2dc8(%rip),%rax
or     (%rax),%esi
lea    0xdf9(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
jmp    1236 <klog+0xb6>
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
lea    0xddc(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdda(%rip),%rsi
xor    %eax,%eax
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<klog>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    0x2e32(%rip),%rbx
mov    (%rbx),%esi
lea    0xe59(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
jle    11c2 <klog+0x42>
mov    %eax,%ebp
movsbl (%r14),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <strcpy@plt>
jmp    1210 <klog+0x90>
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
lea    0xe38(%rip),%rbp
mov    %rbp,%rdi
call   10a0 <access@plt>
cmp    $0xffffffff,%eax
je     11ea <klog+0x6a>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
jmp    11f5 <klog+0x75>
mov    0x2e07(%rip),%rax
mov    (%rbx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
lea    0xe15(%rip),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/kmsg\00"
@O_WRONLY = global i32 0
@.str.1 = [17 x i8] c"/cache/lible.log\00"
@F_OK = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.2 = [31 x i8] c"COULD NOT OPEN KMSG PLEASE FIX\00"
define i32 @klog(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = call i8* @malloc(i32 256)
store i8* %6, i8** %3
store i32 -1, i32* %4
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i8*, i8** %2
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = call i32 (i8*, ...) @strcpy(i8* %12, i32 %15)
br label %32
17: ; preds = %1
%18 = load i32, i32* @F_OK
%19 = call i32 @access(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_WRONLY
%24 = or i32 %22, %23
%25 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 %25, i32* %5
br label %29
26: ; preds = %17
%27 = load i32, i32* @O_APPEND
%28 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %27)
store i32 %28, i32* %5
br label %29
29: ; preds = %26, %21
%30 = load i8*, i8** %3
%31 = call i32 (i8*, ...) @strcpy(i8* %30, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %29, %11
%33 = load i32, i32* %5
%34 = load i8*, i8** %3
%35 = load i8*, i8** %3
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 %33, i8* %34, i32 %36)
store i32 %37, i32* %4
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
%40 = load i8*, i8** %3
%41 = call i32 @free(i8* %40)
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/kmsg\00"
@O_WRONLY = global i32 0
@.str.1 = [17 x i8] c"/cache/lible.log\00"
@F_OK = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.2 = [31 x i8] c"COULD NOT OPEN KMSG PLEASE FIX\00"
define i32 @klog(i8* nocapture %0) {
%2 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i8, i8* %0
%8 = sext i8 %7 to i32
%9 = tail call i32 (i8*, ...) @strcpy(i8* %2, i32 %8)
br label %21
10: ; preds = %1
%11 = load i32, i32* @F_OK
%12 = tail call i32 @access(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, -1
%14 = load i32, i32* @O_APPEND
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_WRONLY
%17 = or i32 %16, %15
%18 = select i1 %13, i32 %17, i32 %14
%19 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = tail call i32 (i8*, ...) @strcpy(i8* %2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %10, %6
%22 = phi i32 [ %4, %6 ], [ %19, %10 ]
%23 = tail call i32 @strlen(i8* %2)
%24 = tail call i32 @write(i32 %22, i8* %2, i32 %23)
%25 = tail call i32 @close(i32 %22)
%26 = tail call i32 @free(i8* %2)
ret i32 %24
}