#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 getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
char read_proc();
char *tracecmd_get_tracing_file(char *);
int /*<<< orphan*/ tracecmd_put_tracing_file(char *);
void read_trace(void) {
  FILE *fp;
  char *path;
  char *buf = NULL;
  size_t n;
  int r;
  if (read_proc() == '1')
    printf("(stack tracer running)\n");
  else
    printf("(stack tracer not running)\n");
  path = tracecmd_get_tracing_file("stack_trace");
  fp = fopen(path, "r");
  if (!fp)
    die("reading to '%s'", path);
  tracecmd_put_tracing_file(path);
  while ((r = getline(&buf, &n, fp)) >= 0) {
    if (buf[0] != '#')
      printf("%s", buf);
    free(buf);
    buf = NULL;
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<read_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   10b0 <read_proc@plt>
movsbl %al,%eax
cmp    $0x31,%eax
jne    11c6 <read_trace+0x36>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    11d4 <read_trace+0x44>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe59(%rip),%rdi
call   1070 <tracecmd_get_tracing_file@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1213 <read_trace+0x83>
mov    -0x10(%rbp),%rsi
lea    0xe34(%rip),%rdi
call   1090 <die@plt>
mov    -0x10(%rbp),%rdi
call   1030 <tracecmd_put_tracing_file@plt>
mov    -0x8(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10a0 <getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jl     1271 <read_trace+0xe1>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x23,%eax
je     125b <read_trace+0xcb>
mov    -0x18(%rbp),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x18(%rbp)
jmp    121c <read_trace+0x8c>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_trace>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   10c0 <read_proc@plt>
cmp    $0x31,%al
lea    0xe78(%rip),%rax
lea    0xe56(%rip),%rdi
cmove  %rax,%rdi
call   1050 <puts@plt>
lea    0xe25(%rip),%rdi
call   1080 <tracecmd_get_tracing_file@plt>
mov    %rax,%r14
lea    0xe22(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1209 <read_trace+0x69>
lea    0xe0d(%rip),%rdi
mov    %r14,%rsi
call   10a0 <die@plt>
mov    %r14,%rdi
call   1030 <tracecmd_put_tracing_file@plt>
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
call   10b0 <getline@plt>
test   %eax,%eax
js     127e <read_trace+0xde>
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%r15
lea    0xde6(%rip),%r12
jmp    1263 <read_trace+0xc3>
nopw   0x0(%rax,%rax,1)
mov    %rsi,%rdi
call   1040 <free@plt>
movq   $0x0,0x8(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10b0 <getline@plt>
test   %eax,%eax
js     127e <read_trace+0xde>
mov    0x8(%rsp),%rsi
cmpb   $0x23,(%rsi)
je     1240 <read_trace+0xa0>
mov    %r12,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%rsp),%rsi
jmp    1240 <read_trace+0xa0>
mov    %rbx,%rdi
call   1060 <fclose@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c"(stack tracer running)\0A\00"
@.str.1 = [28 x i8] c"(stack tracer not running)\0A\00"
@.str.2 = [12 x i8] c"stack_trace\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [16 x i8] c"reading to '%s'\00"
@.str.5 = [3 x i8] c"%s\00"
define void @read_trace() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i8* null, i8** %3
%6 = call signext i8 (...) @read_proc()
%7 = sext i8 %6 to i32
%8 = icmp eq i32 %7, 49
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %9
%14 = call i8* @tracecmd_get_tracing_file(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i8* %14, i8** %2
%15 = load i8*, i8** %2
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %16, i32** %1
%17 = load i32*, i32** %1
%18 = icmp ne i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %13
%20 = load i8*, i8** %2
%21 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %19, %13
%23 = load i8*, i8** %2
%24 = call i32 @tracecmd_put_tracing_file(i8* %23)
br label %25
25: ; preds = %38, %22
%26 = load i32*, i32** %1
%27 = call i32 @getline(i8** %3, i64* %4, i32* %26)
store i32 %27, i32* %5
%28 = icmp sge i32 %27, 0
br i1 %28, label %29, label %41
29: ; preds = %25
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 35
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i8*, i8** %3
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %35, %29
%39 = load i8*, i8** %3
%40 = call i32 @free(i8* %39)
store i8* null, i8** %3
br label %25
41: ; preds = %25
%42 = load i32*, i32** %1
%43 = call i32 @fclose(i32* %42)
ret void
}

LLVM IR (-O2):
@.str.2 = [12 x i8] c"stack_trace\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [16 x i8] c"reading to '%s'\00"
@.str.5 = [3 x i8] c"%s\00"
@str = [27 x i8] c"(stack tracer not running)\00"
@str.6 = [23 x i8] c"(stack tracer running)\00"
define void @read_trace() {
%1 = alloca i8*
%2 = alloca i64
%3 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%4 = bitcast i64* %2 to i8*
%5 = tail call signext i8 (...) @read_proc()
%6 = icmp eq i8 %5, 49
%7 = select i1 %6, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @str.6, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0)
%8 = tail call i32 @puts(i8* dereferenceable(1) %7)
%9 = tail call i8* @tracecmd_get_tracing_file(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = tail call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %9)
br label %14
14: ; preds = %12, %0
%15 = tail call i32 @tracecmd_put_tracing_file(i8* %9)
%16 = call i32 @getline(i8** %1, i64* %2, i32* %10)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %30
18: ; preds = %14, %25
%19 = load i8*, i8** %1
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 35
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %19)
%24 = load i8*, i8** %1
br label %25
25: ; preds = %22, %18
%26 = phi i8* [ %24, %22 ], [ %19, %18 ]
%27 = call i32 @free(i8* %26)
store i8* null, i8** %1
%28 = call i32 @getline(i8** %1, i64* %2, i32* %10)
%29 = icmp sgt i32 %28, -1
br i1 %29, label %18, label %30
30: ; preds = %25, %14
%31 = call i32 @fclose(i32* %10)
ret void
}