#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 O_CREAT;
int O_RDONLY;
int O_RDWR;
int STDOUT_FILENO;
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int strlen(char *);
int write(int, char *, int);
int main() {
  char buf[10];
  int fd;
  fd = open("header.c", O_RDONLY | O_CREAT);
  if (fd < 0) {
    perror("something must be wrong\n");
    exit(1);
  }
  int fdw = open("tmpout", O_RDWR | O_CREAT);
  if (fdw < 0) {
    perror("tmpout write error\n");
    exit(1);
  }
  int n, wn;
  while (1) {
    n = read(fd, buf, 10);
    wn = write(fdw, buf, strlen(buf));
    write(STDOUT_FILENO, "test block", 10);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
lea    0xe78(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b0 <main+0x50>
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
lea    0xe59(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f1 <main+0x91>
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11f6 <main+0x96>
mov    -0x14(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0xa,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0xe(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0xe(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
mov    $0xa,%edx
call   1030 <write@plt>
jmp    11f6 <main+0x96>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e5d(%rip),%rax
mov    0x2e7e(%rip),%rbx
mov    (%rbx),%esi
or     (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1207 <main+0xa7>
mov    %eax,%r14d
mov    0x2e40(%rip),%rax
mov    (%rbx),%esi
or     (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1210 <main+0xb0>
mov    %eax,%ebp
lea    0x6(%rsp),%rbx
mov    0x2e2e(%rip),%r12
lea    0xe7c(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1050 <read@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    (%r12),%edi
mov    %r15,%rsi
mov    $0xa,%edx
call   1030 <write@plt>
jmp    11d0 <main+0x70>
lea    0xdfb(%rip),%rdi
jmp    1217 <main+0xb7>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"header.c\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [25 x i8] c"something must be wrong\0A\00"
@.str.2 = [7 x i8] c"tmpout\00"
@O_RDWR = global i32 0
@.str.3 = [20 x i8] c"tmpout write error\0A\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [11 x i8] c"test block\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %16
br label %27
27: ; preds = %26, %27
%28 = load i32, i32* %3
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 10)
store i32 %30, i32* %5
%31 = load i32, i32* %4
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @write(i32 %31, i8* %32, i32 %34)
store i32 %35, i32* %6
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @write(i32 %36, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 10)
br label %27
}

LLVM IR (-O2):
@.str = [9 x i8] c"header.c\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [25 x i8] c"something must be wrong\0A\00"
@.str.2 = [7 x i8] c"tmpout\00"
@O_RDWR = global i32 0
@.str.3 = [20 x i8] c"tmpout write error\0A\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [11 x i8] c"test block\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11, %20
%21 = call i32 @read(i32 %6, i8* %2, i32 10)
%22 = call i32 @strlen(i8* %2)
%23 = call i32 @write(i32 %15, i8* %2, i32 %22)
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @write(i32 %24, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 10)
br label %20
}