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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ block_sigint();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int getpid();
int open(char const *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char *, int);
void *reader_thread(void *data) {
  char buf[131072];
  char filename[128];
  const char *output = data;
  int failed = 0, fd, out_fd, len;
  block_sigint();
  if (output) {
    out_fd = open(output, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
    if (out_fd < 0) {
      fprintf(stderr, "Cannot open output file %s\n", output);
      return NULL;
    }
  } else
    out_fd = 2;
  sprintf(filename, "/sys/kernel/debug/ktap/trace_pipe_%d", getpid());
open_again:
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    usleep(10000);
    if (failed++ == 10) {
      fprintf(stderr, "Cannot open file %s\n", filename);
      return NULL;
    }
    goto open_again;
  }
  while ((len = read(fd, buf, sizeof(buf))) > 0)
    write(out_fd, buf, len);
  close(fd);
  close(out_fd);
  return NULL;
}


x86 assembly (clang, -O0):
<reader_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x200b0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20098(%rbp)
movl   $0x0,-0x2009c(%rbp)
mov    $0x0,%al
call   10a0 <block_sigint@plt>
cmpq   $0x0,-0x20098(%rbp)
je     1245 <reader_thread+0xb5>
mov    -0x20098(%rbp),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2dc8(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
mov    0x2dd5(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x200a4(%rbp)
cmpl   $0x0,-0x200a4(%rbp)
jge    1240 <reader_thread+0xb0>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    -0x20098(%rbp),%rdx
lea    0xdd2(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    135b <reader_thread+0x1cb>
jmp    124f <reader_thread+0xbf>
movl   $0x2,-0x200a4(%rbp)
lea    -0x20090(%rbp),%rax
mov    %rax,-0x200b0(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x200b0(%rbp),%rdi
mov    %eax,%edx
lea    0xda8(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x20090(%rbp),%rdi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x200a0(%rbp)
cmpl   $0x0,-0x200a0(%rbp)
jge    12f5 <reader_thread+0x165>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    -0x2009c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x2009c(%rbp)
cmp    $0xa,%eax
jne    12f0 <reader_thread+0x160>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
lea    -0x20090(%rbp),%rdx
lea    0xd63(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    135b <reader_thread+0x1cb>
jmp    1279 <reader_thread+0xe9>
jmp    12fa <reader_thread+0x16a>
mov    -0x200a0(%rbp),%edi
lea    -0x20010(%rbp),%rsi
mov    $0x20000,%edx
call   1060 <read@plt>
mov    %eax,-0x200a8(%rbp)
cmp    $0x0,%eax
jle    133d <reader_thread+0x1ad>
mov    -0x200a4(%rbp),%edi
lea    -0x20010(%rbp),%rsi
mov    -0x200a8(%rbp),%edx
call   1030 <write@plt>
jmp    12fa <reader_thread+0x16a>
mov    -0x200a0(%rbp),%edi
call   1050 <close@plt>
mov    -0x200a4(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x200b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reader_thread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x20088,%rsp
mov    %rdi,%rbp
xor    %eax,%eax
call   10a0 <block_sigint@plt>
test   %rbp,%rbp
je     1207 <reader_thread+0x77>
mov    0x2e45(%rip),%rax
mov    0x2dee(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2b(%rip),%rax
or     (%rax),%esi
mov    0x2dea(%rip),%rax
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    120d <reader_thread+0x7d>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
mov    %rbp,%rdx
call   1070 <fprintf@plt>
jmp    13cf <reader_thread+0x23f>
mov    $0x2,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe01(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    0x2da9(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    137c <reader_thread+0x1ec>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    (%r15),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    137c <reader_thread+0x1ec>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    (%r15),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    137c <reader_thread+0x1ec>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    (%r15),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    137c <reader_thread+0x1ec>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    (%r15),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    137c <reader_thread+0x1ec>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    (%r15),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    137c <reader_thread+0x1ec>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    (%r15),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    137c <reader_thread+0x1ec>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    (%r15),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    137c <reader_thread+0x1ec>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    (%r15),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    137c <reader_thread+0x1ec>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    (%r15),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    137c <reader_thread+0x1ec>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    (%r15),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     13df <reader_thread+0x24f>
lea    0x80(%rsp),%rsi
mov    %ebp,%edi
mov    $0x20000,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    13c0 <reader_thread+0x230>
lea    0x80(%rsp),%rbx
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x20000,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     13a0 <reader_thread+0x210>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20088,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    0x2bd0(%rip),%rax
mov    (%rax),%edi
lea    0xc48(%rip),%rsi
mov    %rsp,%rdx
jmp    11fd <reader_thread+0x6d>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Cannot open output file %s\0A\00"
@.str.1 = [37 x i8] c"/sys/kernel/debug/ktap/trace_pipe_%d\00"
@O_RDONLY = global i32 0
@.str.2 = [21 x i8] c"Cannot open file %s\0A\00"
define i8* @reader_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [131072 x i8]
%5 = alloca [128 x i8]
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
store i8* %11, i8** %6
store i32 0, i32* %7
%12 = call i32 (...) @block_sigint()
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %33
15: ; preds = %1
%16 = load i8*, i8** %6
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* @O_WRONLY
%19 = or i32 %17, %18
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %22, %23
%25 = call i32 (i8*, i32, ...) @open(i8* %16, i32 %21, i32 %24)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %15
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %6
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %30)
store i8* null, i8** %2
br label %70
32: ; preds = %15
br label %34
33: ; preds = %1
store i32 2, i32* %9
br label %34
34: ; preds = %33, %32
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%36 = call i32 (...) @getpid()
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %53, %34
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%40 = load i32, i32* @O_RDONLY
%41 = call i32 (i8*, i32, ...) @open(i8* %39, i32 %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %54
44: ; preds = %38
%45 = call i32 @usleep(i32 10000)
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
%48 = icmp eq i32 %46, 10
br i1 %48, label %49, label %53
49: ; preds = %44
%50 = load i32, i32* @stderr
%51 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%52 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %51)
store i8* null, i8** %2
br label %70
53: ; preds = %44
br label %38
54: ; preds = %38
br label %55
55: ; preds = %60, %54
%56 = load i32, i32* %8
%57 = getelementptr inbounds [131072 x i8], [131072 x i8]* %4, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 131072)
store i32 %58, i32* %10
%59 = icmp sgt i32 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %55
%61 = load i32, i32* %9
%62 = getelementptr inbounds [131072 x i8], [131072 x i8]* %4, i64 0, i64 0
%63 = load i32, i32* %10
%64 = call i32 @write(i32 %61, i8* %62, i32 %63)
br label %55
65: ; preds = %55
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* %9
%69 = call i32 @close(i32 %68)
store i8* null, i8** %2
br label %70
70: ; preds = %65, %49, %28
%71 = load i8*, i8** %2
ret i8* %71
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Cannot open output file %s\0A\00"
@.str.1 = [37 x i8] c"/sys/kernel/debug/ktap/trace_pipe_%d\00"
@O_RDONLY = global i32 0
@.str.2 = [21 x i8] c"Cannot open file %s\0A\00"
define noalias i8* @reader_thread(i8* %0) {
%2 = alloca [131072 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [131072 x i8], [131072 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = tail call i32 (...) @block_sigint()
%7 = icmp eq i8* %0, null
br i1 %7, label %22, label %8
8: ; preds = %1
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_WRONLY
%11 = or i32 %10, %9
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %13, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %8
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %95
22: ; preds = %1, %8
%23 = phi i32 [ %17, %8 ], [ 2, %1 ]
%24 = tail call i32 (...) @getpid()
%25 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @O_RDONLY
%27 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %78, %73, %68, %63, %58, %53, %48, %43, %38, %33, %22
%30 = phi i32 [ %27, %22 ], [ %36, %33 ], [ %41, %38 ], [ %46, %43 ], [ %51, %48 ], [ %56, %53 ], [ %61, %58 ], [ %66, %63 ], [ %71, %68 ], [ %76, %73 ], [ %81, %78 ]
%31 = call i32 @read(i32 %30, i8* %4, i32 131072)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %87, label %92
33: ; preds = %22
%34 = tail call i32 @usleep(i32 10000)
%35 = load i32, i32* @O_RDONLY
%36 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %29
38: ; preds = %33
%39 = tail call i32 @usleep(i32 10000)
%40 = load i32, i32* @O_RDONLY
%41 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %40)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %29
43: ; preds = %38
%44 = tail call i32 @usleep(i32 10000)
%45 = load i32, i32* @O_RDONLY
%46 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %45)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %29
48: ; preds = %43
%49 = tail call i32 @usleep(i32 10000)
%50 = load i32, i32* @O_RDONLY
%51 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %50)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %29
53: ; preds = %48
%54 = tail call i32 @usleep(i32 10000)
%55 = load i32, i32* @O_RDONLY
%56 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %55)
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %29
58: ; preds = %53
%59 = tail call i32 @usleep(i32 10000)
%60 = load i32, i32* @O_RDONLY
%61 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %60)
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %29
63: ; preds = %58
%64 = tail call i32 @usleep(i32 10000)
%65 = load i32, i32* @O_RDONLY
%66 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %65)
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %29
68: ; preds = %63
%69 = tail call i32 @usleep(i32 10000)
%70 = load i32, i32* @O_RDONLY
%71 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %70)
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %29
73: ; preds = %68
%74 = tail call i32 @usleep(i32 10000)
%75 = load i32, i32* @O_RDONLY
%76 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %75)
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %29
78: ; preds = %73
%79 = tail call i32 @usleep(i32 10000)
%80 = load i32, i32* @O_RDONLY
%81 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %80)
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %29
83: ; preds = %78
%84 = tail call i32 @usleep(i32 10000)
%85 = load i32, i32* @stderr
%86 = call i32 @fprintf(i32 %85, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %95
87: ; preds = %29, %87
%88 = phi i32 [ %90, %87 ], [ %31, %29 ]
%89 = call i32 @write(i32 %23, i8* %4, i32 %88)
%90 = call i32 @read(i32 %30, i8* %4, i32 131072)
%91 = icmp sgt i32 %90, 0
br i1 %91, label %87, label %92
92: ; preds = %87, %29
%93 = tail call i32 @close(i32 %30)
%94 = tail call i32 @close(i32 %23)
br label %95
95: ; preds = %92, %83, %19
ret i8* null
}