#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*/ die(char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
char *get_event_file(char *, char *, int);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdout;
scalar_t__ strncmp(char *, char *, int);
int event_format_write(char *fbuf, int len) {
  char *file = get_event_file("format", fbuf, len);
  char *buf = NULL;
  size_t l;
  FILE *fp;
  int n;
  printf("system: %s\n", fbuf);
  fp = fopen(file, "r");
  if (!fp)
    die("reading %s", file);
  do {
    n = getline(&buf, &l, fp);
    if (n > 0) {
      if (strncmp(buf, "print fmt", 9) == 0)
        break;
      fwrite(buf, 1, n, stdout);
    }
  } while (n > 0);
  fclose(fp);
  free(buf);
  free(file);
  return 0;
}


x86 assembly (clang, -O0):
<event_format_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe53(%rip),%rdi
call   1050 <get_event_file@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe3e(%rip),%rdi
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11fd <event_format_write+0x6d>
mov    -0x18(%rbp),%rsi
lea    0xe1d(%rip),%rdi
call   1090 <die@plt>
jmp    1202 <event_format_write+0x72>
mov    -0x30(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <getline@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    125e <event_format_write+0xce>
mov    -0x20(%rbp),%rdi
lea    0xdf5(%rip),%rsi
mov    $0x9,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1244 <event_format_write+0xb4>
jmp    126d <event_format_write+0xdd>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    0x2d8e(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
jmp    1263 <event_format_write+0xd3>
cmpl   $0x0,-0x34(%rbp)
jg     1202 <event_format_write+0x72>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<event_format_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%edx
mov    %rdi,%rbx
lea    0xe56(%rip),%rdi
mov    %rbx,%rsi
call   1050 <get_event_file@plt>
mov    %rax,%rbp
movq   $0x0,(%rsp)
lea    0xe43(%rip),%rdi
mov    %rbx,%rsi
call   1070 <printf@plt>
lea    0xe40(%rip),%rsi
mov    %rbp,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f2 <event_format_write+0x62>
lea    0xe2b(%rip),%rdi
mov    %rbp,%rsi
call   1090 <die@plt>
mov    %rbp,0x8(%rsp)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
call   10a0 <getline@plt>
test   %eax,%eax
jle    126d <event_format_write+0xdd>
mov    %eax,%ebp
lea    0xe0c(%rip),%r15
mov    0x2dc5(%rip),%r14
mov    %rsp,%r12
lea    0x10(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%rsp),%rdi
mov    %r15,%rsi
mov    $0x9,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     126d <event_format_write+0xdd>
mov    (%rsp),%rdi
mov    (%r14),%ecx
mov    $0x1,%esi
mov    %ebp,%edx
call   10b0 <fwrite@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10a0 <getline@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1230 <event_format_write+0xa0>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"format\00"
@.str.1 = [12 x i8] c"system: %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"reading %s\00"
@.str.4 = [10 x i8] c"print fmt\00"
@stdout = global i32 0
define i32 @event_format_write(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i8*, i8** %3
%11 = load i32, i32* %4
%12 = call i8* @get_event_file(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %11)
store i8* %12, i8** %5
store i8* null, i8** %6
%13 = load i8*, i8** %3
%14 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = icmp ne i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %2
%20 = load i8*, i8** %5
%21 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %19, %2
br label %23
23: ; preds = %39, %22
%24 = load i32*, i32** %8
%25 = call i32 @getline(i8** %6, i64* %7, i32* %24)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %23
%29 = load i8*, i8** %6
%30 = call i64 @strncmp(i8* %29, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 9)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
br label %42
33: ; preds = %28
%34 = load i8*, i8** %6
%35 = load i32, i32* %9
%36 = load i32, i32* @stdout
%37 = call i32 @fwrite(i8* %34, i32 1, i32 %35, i32 %36)
br label %38
38: ; preds = %33, %23
br label %39
39: ; preds = %38
%40 = load i32, i32* %9
%41 = icmp sgt i32 %40, 0
br i1 %41, label %23, label %42
42: ; preds = %39, %32
%43 = load i32*, i32** %8
%44 = call i32 @fclose(i32* %43)
%45 = load i8*, i8** %6
%46 = call i32 @free(i8* %45)
%47 = load i8*, i8** %5
%48 = call i32 @free(i8* %47)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"format\00"
@.str.1 = [12 x i8] c"system: %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"reading %s\00"
@.str.4 = [10 x i8] c"print fmt\00"
@stdout = global i32 0
define i32 @event_format_write(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = tail call i8* @get_event_file(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%6 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%7 = bitcast i64* %4 to i8*
%8 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%9 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %5)
br label %13
13: ; preds = %11, %2
%14 = call i32 @getline(i8** %3, i64* %4, i32* %9)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %13, %21
%17 = phi i32 [ %25, %21 ], [ %14, %13 ]
%18 = load i8*, i8** %3
%19 = call i64 @strncmp(i8* %18, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 9)
%20 = icmp eq i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %16
%22 = load i8*, i8** %3
%23 = load i32, i32* @stdout
%24 = call i32 @fwrite(i8* %22, i32 1, i32 %17, i32 %23)
%25 = call i32 @getline(i8** %3, i64* %4, i32* %9)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %16, label %27
27: ; preds = %16, %21, %13
%28 = call i32 @fclose(i32* %9)
%29 = load i8*, i8** %3
%30 = call i32 @free(i8* %29)
%31 = call i32 @free(i8* %5)
ret i32 0
}