#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 MAP_LOCKED;
int MAP_SHARED;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int fread(char *, int, unsigned long, int /*<<< orphan*/ *);
int fwrite(char *, unsigned long, int, int /*<<< orphan*/ *);
void *mmap(int /*<<< orphan*/, unsigned long, int /*<<< orphan*/, int,
           int /*<<< orphan*/, unsigned long);
int /*<<< orphan*/ munmap(void *, unsigned long);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void log2file(const char *fname, const char *fname_log) {
  FILE *f;
  FILE *f_log;
  unsigned long offset = 0, len = 4096;
  void *address = (void *)-1;
  char *str;
  int i;
  int result;
  char buf[4096];
  if (!(f = fopen(fname, "r"))) {
    fprintf(stderr, "Fail to open %s: %s\n", fname, strerror(errno));
    exit(1);
  }
  if (!(f_log = fopen(fname_log, "w"))) {
    fprintf(stderr, "Fail to open %s to write into: %s\n", fname_log,
            strerror(errno));
    exit(1);
  }
  printf("Logging...\n"
         "Use flush command to stop logging\n");
  while (1) {
    result = fread(buf, 1, len, f);
    if (result != len) {
      if (feof(f)) {
        if (fwrite(buf, 1, result, f_log) != result) {
          fprintf(stderr, "fwrite() %s fail:%s\n", fname_log, strerror(errno));
          fclose(f);
          fclose(f_log);
          exit(1);
        }
        break;
      } else {
        fprintf(stderr, "fread file %s error:%s\n", fname, strerror(errno));
        fclose(f);
        fclose(f_log);
        exit(1);
      }
    }
    address =
        mmap(0, len, PROT_READ, MAP_LOCKED | MAP_SHARED, fileno(f), offset);
    if (address == (void *)-1) {
      fprintf(stderr, "mmap() file%s error:%s\n", fname, strerror(errno));
      fclose(f);
      fclose(f_log);
      exit(1);
    }
    str = (char *)address;
    if (fwrite(str, len, 1, f_log) != 1) {
      fprintf(stderr, "fwrite() %s fail:%s\n", fname_log, strerror(errno));
      fclose(f);
      fclose(f_log);
      exit(1);
    }
    munmap(address, len);
    address = (void *)-1;
  }
  printf("Logs has been written into file %s\n", fname_log);
  fclose(f);
  fclose(f_log);
}


x86 assembly (clang, -O0):
<log2file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10c0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x1000,-0x30(%rbp)
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    125a <log2file+0x9a>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x105c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x1058(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x105c(%rbp),%edi
mov    -0x1058(%rbp),%rdx
mov    %rax,%rcx
lea    0xdb7(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xdb2(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    12c6 <log2file+0x106>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x106c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1068(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x106c(%rbp),%edi
mov    -0x1068(%rbp),%rdx
mov    %rax,%rcx
lea    0xd62(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x1050(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x48(%rbp)
movslq -0x48(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     13f7 <log2file+0x237>
mov    -0x18(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     1397 <log2file+0x1d7>
lea    -0x1050(%rbp),%rdi
mov    -0x48(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
cmp    -0x48(%rbp),%eax
je     1392 <log2file+0x1d2>
mov    0x2c8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x107c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1078(%rbp)
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x107c(%rbp),%edi
mov    -0x1078(%rbp),%rdx
mov    %rax,%rcx
lea    0xcf9(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    156c <log2file+0x3ac>
mov    0x2c2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x108c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x1088(%rbp)
mov    0x2c08(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x108c(%rbp),%edi
mov    -0x1088(%rbp),%rdx
mov    %rax,%rcx
lea    0xca9(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x10a0(%rbp)
mov    0x2bef(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1094(%rbp)
mov    0x2bb8(%rip),%rax
mov    (%rax),%eax
mov    0x2bc7(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1090(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <fileno@plt>
mov    -0x10a0(%rbp),%rsi
mov    -0x1094(%rbp),%edx
mov    -0x1090(%rbp),%ecx
mov    %eax,%r8d
mov    -0x28(%rbp),%r9
xor    %edi,%edi
call   1050 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x38(%rbp)
jne    14c8 <log2file+0x308>
mov    0x2b59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10ac(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10a8(%rbp)
mov    0x2b37(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x10ac(%rbp),%edi
mov    -0x10a8(%rbp),%rdx
mov    %rax,%rcx
lea    0xbf0(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   10d0 <fwrite@plt>
cmp    $0x1,%eax
je     154f <log2file+0x38f>
mov    0x2ad2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10bc(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x10b8(%rbp)
mov    0x2ab0(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x10bc(%rbp),%edi
mov    -0x10b8(%rbp),%rdx
mov    %rax,%rcx
lea    0xb3c(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10a0 <munmap@plt>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
jmp    12d4 <log2file+0x114>
mov    -0x10(%rbp),%rsi
lea    0xb38(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x10c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log2file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe12(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     13c4 <log2file+0x1f4>
mov    %rax,%r14
lea    0xe11(%rip),%rsi
mov    %r15,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     13e9 <log2file+0x219>
mov    %rax,%rbx
lea    0xe84(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rdi
mov    $0x1000,%edx
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
cmp    $0x1000,%eax
jne    12f8 <log2file+0x128>
mov    %r12,(%rsp)
mov    %r15,0x8(%rsp)
mov    0x2d8e(%rip),%r15
lea    0x10(%rsp),%r13
nop
mov    0x2d91(%rip),%rax
mov    (%rax),%ebp
mov    (%r15),%r12d
mov    0x2d5d(%rip),%rax
or     (%rax),%r12d
mov    %r14,%rdi
call   10a0 <fileno@plt>
mov    $0x1000,%esi
xor    %edi,%edi
mov    %ebp,%edx
mov    %r12d,%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1357 <log2file+0x187>
mov    %rax,%rbp
mov    $0x1000,%esi
mov    %rax,%rdi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10e0 <fwrite@plt>
cmp    $0x1,%eax
jne    137d <log2file+0x1ad>
mov    $0x1000,%esi
mov    %rbp,%rdi
call   10b0 <munmap@plt>
mov    $0x1000,%edx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    $0x1000,%eax
je     1260 <log2file+0x90>
mov    %eax,%ebp
mov    0x8(%rsp),%r15
mov    (%rsp),%r12
mov    %r14,%rdi
call   1090 <feof@plt>
test   %rax,%rax
je     141e <log2file+0x24e>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   10e0 <fwrite@plt>
cmp    %ebp,%eax
jne    1446 <log2file+0x276>
lea    0xd55(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>
mov    0x2c6a(%rip),%rax
mov    (%rax),%ebp
mov    0x2c59(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xcf4(%rip),%rsi
mov    %ebp,%edi
mov    (%rsp),%rdx
jmp    13a2 <log2file+0x1d2>
mov    0x2c44(%rip),%rax
mov    (%rax),%ebp
mov    0x2c33(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xca1(%rip),%rsi
mov    %ebp,%edi
mov    0x8(%rsp),%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2bfd(%rip),%rax
mov    (%rax),%ebx
mov    0x2bec(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xc20(%rip),%rsi
mov    %ebx,%edi
mov    %r12,%rdx
jmp    140c <log2file+0x23c>
mov    0x2bd8(%rip),%rax
mov    (%rax),%ebx
mov    0x2bc7(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xc12(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2ba3(%rip),%rax
mov    (%rax),%ebp
mov    0x2b92(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xc15(%rip),%rsi
mov    %ebp,%edi
mov    %r12,%rdx
jmp    13a2 <log2file+0x1d2>
mov    0x2b7b(%rip),%rax
mov    (%rax),%ebp
mov    0x2b6a(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xbd8(%rip),%rsi
mov    %ebp,%edi
mov    %r15,%rdx
jmp    13a2 <log2file+0x1d2>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Fail to open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [35 x i8] c"Fail to open %s to write into: %s\0A\00"
@.str.4 = [46 x i8] c"Logging...\0AUse flush command to stop logging\0A\00"
@.str.5 = [21 x i8] c"fwrite() %s fail:%s\0A\00"
@.str.6 = [24 x i8] c"fread file %s error:%s\0A\00"
@PROT_READ = global i32 0
@MAP_LOCKED = global i32 0
@MAP_SHARED = global i32 0
@.str.7 = [24 x i8] c"mmap() file%s error:%s\0A\00"
@.str.8 = [36 x i8] c"Logs has been written into file %s\0A\00"
define void @log2file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca [4096 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
store i64 0, i64* %7
store i64 4096, i64* %8
store i8* inttoptr (i64 -1 to i8*), i8** %9
%14 = load i8*, i8** %3
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %5
%16 = icmp ne i32* %15, null
br i1 %16, label %24, label %17
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %3
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i8*, i8** %4
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %26, i32** %6
%27 = icmp ne i32* %26, null
br i1 %27, label %35, label %28
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %4
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %30, i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %24
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %35, %121
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%39 = load i64, i64* %8
%40 = load i32*, i32** %5
%41 = call i32 @fread(i8* %38, i32 1, i64 %39, i32* %40)
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = sext i32 %42 to i64
%44 = load i64, i64* %8
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %80
46: ; preds = %37
%47 = load i32*, i32** %5
%48 = call i64 @feof(i32* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %69
50: ; preds = %46
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%52 = load i32, i32* %12
%53 = load i32*, i32** %6
%54 = call i32 @fwrite(i8* %51, i64 1, i32 %52, i32* %53)
%55 = load i32, i32* %12
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %68
57: ; preds = %50
%58 = load i32, i32* @stderr
%59 = load i8*, i8** %4
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %59, i8* %61)
%63 = load i32*, i32** %5
%64 = call i32 @fclose(i32* %63)
%65 = load i32*, i32** %6
%66 = call i32 @fclose(i32* %65)
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %50
br label %125
69: ; preds = %46
%70 = load i32, i32* @stderr
%71 = load i8*, i8** %3
%72 = load i32, i32* @errno
%73 = call i8* @strerror(i32 %72)
%74 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %71, i8* %73)
%75 = load i32*, i32** %5
%76 = call i32 @fclose(i32* %75)
%77 = load i32*, i32** %6
%78 = call i32 @fclose(i32* %77)
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %37
%81 = load i64, i64* %8
%82 = load i32, i32* @PROT_READ
%83 = load i32, i32* @MAP_LOCKED
%84 = load i32, i32* @MAP_SHARED
%85 = or i32 %83, %84
%86 = load i32*, i32** %5
%87 = call i32 @fileno(i32* %86)
%88 = load i64, i64* %7
%89 = call i8* @mmap(i32 0, i64 %81, i32 %82, i32 %85, i32 %87, i64 %88)
store i8* %89, i8** %9
%90 = load i8*, i8** %9
%91 = icmp eq i8* %90, inttoptr (i64 -1 to i8*)
br i1 %91, label %92, label %103
92: ; preds = %80
%93 = load i32, i32* @stderr
%94 = load i8*, i8** %3
%95 = load i32, i32* @errno
%96 = call i8* @strerror(i32 %95)
%97 = call i32 @fprintf(i32 %93, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i8* %94, i8* %96)
%98 = load i32*, i32** %5
%99 = call i32 @fclose(i32* %98)
%100 = load i32*, i32** %6
%101 = call i32 @fclose(i32* %100)
%102 = call i32 @exit(i32 1)
unreachable
103: ; preds = %80
%104 = load i8*, i8** %9
store i8* %104, i8** %10
%105 = load i8*, i8** %10
%106 = load i64, i64* %8
%107 = load i32*, i32** %6
%108 = call i32 @fwrite(i8* %105, i64 %106, i32 1, i32* %107)
%109 = icmp ne i32 %108, 1
br i1 %109, label %110, label %121
110: ; preds = %103
%111 = load i32, i32* @stderr
%112 = load i8*, i8** %4
%113 = load i32, i32* @errno
%114 = call i8* @strerror(i32 %113)
%115 = call i32 @fprintf(i32 %111, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %112, i8* %114)
%116 = load i32*, i32** %5
%117 = call i32 @fclose(i32* %116)
%118 = load i32*, i32** %6
%119 = call i32 @fclose(i32* %118)
%120 = call i32 @exit(i32 1)
unreachable
121: ; preds = %103
%122 = load i8*, i8** %9
%123 = load i64, i64* %8
%124 = call i32 @munmap(i8* %122, i64 %123)
store i8* inttoptr (i64 -1 to i8*), i8** %9
br label %37
125: ; preds = %68
%126 = load i8*, i8** %4
%127 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0), i8* %126)
%128 = load i32*, i32** %5
%129 = call i32 @fclose(i32* %128)
%130 = load i32*, i32** %6
%131 = call i32 @fclose(i32* %130)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Fail to open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [35 x i8] c"Fail to open %s to write into: %s\0A\00"
@.str.5 = [21 x i8] c"fwrite() %s fail:%s\0A\00"
@.str.6 = [24 x i8] c"fread file %s error:%s\0A\00"
@PROT_READ = global i32 0
@MAP_LOCKED = global i32 0
@MAP_SHARED = global i32 0
@.str.7 = [24 x i8] c"mmap() file%s error:%s\0A\00"
@.str.8 = [36 x i8] c"Logs has been written into file %s\0A\00"
@str = [45 x i8] c"Logging...\0AUse flush command to stop logging\00"
define void @log2file(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%24 = call i32 @fread(i8* %4, i32 1, i64 4096, i32* %5)
%25 = icmp eq i32 %24, 4096
br i1 %25, label %49, label %26
26: ; preds = %76, %22
%27 = phi i32 [ %24, %22 ], [ %78, %76 ]
%28 = tail call i64 @feof(i32* %5)
%29 = icmp eq i64 %28, 0
br i1 %29, label %41, label %30
30: ; preds = %26
%31 = call i32 @fwrite(i8* %4, i64 1, i32 %27, i32* %14)
%32 = icmp eq i32 %31, %27
br i1 %32, label %80, label %33
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %1, i8* %36)
%38 = tail call i32 @fclose(i32* %5)
%39 = tail call i32 @fclose(i32* %14)
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %26
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = tail call i8* @strerror(i32 %43)
%45 = tail call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %0, i8* %44)
%46 = tail call i32 @fclose(i32* %5)
%47 = tail call i32 @fclose(i32* %14)
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %22, %76
%50 = load i32, i32* @PROT_READ
%51 = load i32, i32* @MAP_LOCKED
%52 = load i32, i32* @MAP_SHARED
%53 = or i32 %52, %51
%54 = tail call i32 @fileno(i32* %5)
%55 = tail call i8* @mmap(i32 0, i64 4096, i32 %50, i32 %53, i32 %54, i64 0)
%56 = icmp eq i8* %55, inttoptr (i64 -1 to i8*)
br i1 %56, label %57, label %65
57: ; preds = %49
%58 = load i32, i32* @stderr
%59 = load i32, i32* @errno
%60 = tail call i8* @strerror(i32 %59)
%61 = tail call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %60)
%62 = tail call i32 @fclose(i32* %5)
%63 = tail call i32 @fclose(i32* %14)
%64 = tail call i32 @exit(i32 1)
unreachable
65: ; preds = %49
%66 = tail call i32 @fwrite(i8* %55, i64 4096, i32 1, i32* %14)
%67 = icmp eq i32 %66, 1
br i1 %67, label %76, label %68
68: ; preds = %65
%69 = load i32, i32* @stderr
%70 = load i32, i32* @errno
%71 = tail call i8* @strerror(i32 %70)
%72 = tail call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %1, i8* %71)
%73 = tail call i32 @fclose(i32* %5)
%74 = tail call i32 @fclose(i32* %14)
%75 = tail call i32 @exit(i32 1)
unreachable
76: ; preds = %65
%77 = tail call i32 @munmap(i8* %55, i64 4096)
%78 = call i32 @fread(i8* %4, i32 1, i64 4096, i32* %5)
%79 = icmp eq i32 %78, 4096
br i1 %79, label %49, label %26
80: ; preds = %30
%81 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0), i8* %1)
%82 = tail call i32 @fclose(i32* %5)
%83 = tail call i32 @fclose(i32* %14)
ret void
}