#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 /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd_from;
  int fd_to;
  fd_from = open("test1_readfile", O_RDONLY);
  if (fd_from == -1) {
    fprintf(stderr, "failed to open %s\n", "test1_readfile");
    return -1;
  }
  fd_to = open("test1_writefile", O_WRONLY | O_CREAT | O_TRUNC,
               S_IRWXU | S_IRWXG | S_IRWXO);
  if (fd_to == -1) {
    fprintf(stderr, "failed to open output file:%s\n", "test1_writefile");
    return -1;
  }
  char buf[(100)];
  int n;
  int ret = 0;
  while (n = read(fd_from, buf, (100))) {
    if (n < 0) {
      fprintf(stderr, "failed read from %s\n", "test1_readfile");
      ret = -1;
      break;
    }
    n = write(fd_to, buf, n);
    if (n < 0) {
      fprintf(stderr, "failed to write to %s\n", "test1_writefile");
      ret = -1;
      break;
    }
  }
  close(fd_from);
  close(fd_to);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b5 <main+0x65>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
lea    0xe5c(%rip),%rdx
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fe <main+0x1ae>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    0x2de8(%rip),%rax
or     (%rax),%edx
mov    0x2df7(%rip),%rax
or     (%rax),%edx
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    122e <main+0xde>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
lea    0xe05(%rip),%rdx
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fe <main+0x1ae>
movl   $0x0,-0x88(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,-0x84(%rbp)
cmp    $0x0,%eax
je     12e5 <main+0x195>
cmpl   $0x0,-0x84(%rbp)
jge    1290 <main+0x140>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
lea    0xd84(%rip),%rdx
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x88(%rbp)
ff ff ff
jmp    12e5 <main+0x195>
mov    -0x18(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x84(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    12e0 <main+0x190>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
lea    0xd56(%rip),%rdx
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x88(%rbp)
ff ff ff
jmp    12e5 <main+0x195>
jmp    1238 <main+0xe8>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x88(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     120f <main+0xbf>
mov    %eax,%r14d
mov    0x2e1e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5c(%rip),%rax
or     (%rax),%esi
mov    0x2e33(%rip),%rax
mov    0x2e24(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e31(%rip),%rax
or     (%rax),%edx
lea    0xe6a(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1228 <main+0xd8>
mov    %eax,%ebp
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1050 <read@plt>
test   %eax,%eax
je     124b <main+0xfb>
js     124f <main+0xff>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11d0 <main+0x80>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
lea    0xe15(%rip),%rdx
jmp    1266 <main+0x116>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
lea    0xdda(%rip),%rdx
jmp    123f <main+0xef>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
lea    0xde3(%rip),%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    127f <main+0x12f>
xor    %ebx,%ebx
jmp    1270 <main+0x120>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
lea    0xd9a(%rip),%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"test1_readfile\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"failed to open %s\0A\00"
@.str.2 = [16 x i8] c"test1_writefile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.3 = [31 x i8] c"failed to open output file:%s\0A\00"
@.str.4 = [21 x i8] c"failed read from %s\0A\00"
@.str.5 = [23 x i8] c"failed to write to %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %64
18: ; preds = %2
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRWXU
%25 = load i32, i32* @S_IRWXG
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IRWXO
%28 = or i32 %26, %27
%29 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %64
35: ; preds = %18
store i32 0, i32* %10
br label %36
36: ; preds = %57, %35
%37 = load i32, i32* %6
%38 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 100)
store i32 %39, i32* %9
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %58
41: ; preds = %36
%42 = load i32, i32* %9
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %10
br label %58
47: ; preds = %41
%48 = load i32, i32* %7
%49 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%50 = load i32, i32* %9
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
store i32 %51, i32* %9
%52 = load i32, i32* %9
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %47
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %10
br label %58
57: ; preds = %47
br label %36
58: ; preds = %54, %44, %36
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %10
store i32 %63, i32* %3
br label %64
64: ; preds = %58, %32, %15
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@.str = [15 x i8] c"test1_readfile\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"failed to open %s\0A\00"
@.str.2 = [16 x i8] c"test1_writefile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.3 = [31 x i8] c"failed to open output file:%s\0A\00"
@.str.4 = [21 x i8] c"failed read from %s\0A\00"
@.str.5 = [23 x i8] c"failed to write to %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %46
10: ; preds = %2
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRWXU
%17 = load i32, i32* @S_IRWXG
%18 = or i32 %17, %16
%19 = load i32, i32* @S_IRWXO
%20 = or i32 %18, %19
%21 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %10
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %46
26: ; preds = %10
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
br label %28
28: ; preds = %36, %26
%29 = call i32 @read(i32 %5, i8* %27, i32 100)
%30 = icmp eq i32 %29, 0
br i1 %30, label %42, label %31
31: ; preds = %28
%32 = icmp slt i32 %29, 0
br i1 %32, label %33, label %36
33: ; preds = %31
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %42
36: ; preds = %31
%37 = call i32 @write(i32 %21, i8* %27, i32 %29)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %28
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = tail call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %28, %39, %33
%43 = phi i32 [ -1, %33 ], [ -1, %39 ], [ 0, %28 ]
%44 = tail call i32 @close(i32 %5)
%45 = tail call i32 @close(i32 %21)
br label %46
46: ; preds = %42, %23, %7
%47 = phi i32 [ -1, %7 ], [ -1, %23 ], [ %43, %42 ]
ret i32 %47
}