#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_WRONLY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int rfd;
  int wfd;
  char buf[256];
  int idx = 0;
  int readcnt = 0;
  int writecnt = 0;
  char *testfilein = argv[1];
  char *testfileout = argv[2];
  argc = argc;
  printf("Test file in is %s\n", testfilein);
  printf("Test file out is %s\n", testfileout);
  rfd = open(testfilein, O_RDONLY, 0);
  wfd = open(testfileout, O_WRONLY | O_CREAT, 0666);
  printf("rfd is %d\n", rfd);
  printf("wfd is %d\n", wfd);
  for (idx = 0; idx != 256; idx++) {
    buf[idx] = '\0';
  }
  readcnt = read(rfd, buf, 256);
  writecnt = write(wfd, buf, readcnt);
  printf("Read %d and wrote %d\n", readcnt, writecnt);
  printf("%s", buf);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x124(%rbp)
movl   $0x0,-0x128(%rbp)
movl   $0x0,-0x12c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x138(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x140(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x138(%rbp),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x140(%rbp),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x138(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x140(%rbp),%rdi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2df2(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x124(%rbp)
cmpl   $0x100,-0x124(%rbp)
je     1274 <main+0x124>
movslq -0x124(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    1241 <main+0xf1>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x128(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x128(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x12c(%rbp)
mov    -0x128(%rbp),%esi
mov    -0x12c(%rbp),%edx
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x120(%rbp),%rsi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%rbp
lea    0xe96(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe99(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2e27(%rip),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,%ebp
lea    0xe67(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe62(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xf0(%rsp)
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xdd3(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdd7(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Test file in is %s\0A\00"
@.str.1 = [21 x i8] c"Test file out is %s\0A\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [11 x i8] c"rfd is %d\0A\00"
@.str.3 = [11 x i8] c"wfd is %d\0A\00"
@.str.4 = [22 x i8] c"Read %d and wrote %d\0A\00"
@.str.5 = [3 x i8] c"%s\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 [256 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %12
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
store i8* %19, i8** %13
%20 = load i32, i32* %4
store i32 %20, i32* %4
%21 = load i8*, i8** %12
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %13
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %12
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26, i32 0)
store i32 %27, i32* %6
%28 = load i8*, i8** %13
%29 = load i32, i32* @O_WRONLY
%30 = load i32, i32* @O_CREAT
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %28, i32 %31, i32 438)
store i32 %32, i32* %7
%33 = load i32, i32* %6
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %35)
store i32 0, i32* %9
br label %37
37: ; preds = %44, %2
%38 = load i32, i32* %9
%39 = icmp ne i32 %38, 256
br i1 %39, label %40, label %47
40: ; preds = %37
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %40
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
store i32 %46, i32* %9
br label %37
47: ; preds = %37
%48 = load i32, i32* %6
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%50 = call i32 @read(i32 %48, i8* %49, i32 256)
store i32 %50, i32* %10
%51 = load i32, i32* %7
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%53 = load i32, i32* %10
%54 = call i32 @write(i32 %51, i8* %52, i32 %53)
store i32 %54, i32* %11
%55 = load i32, i32* %10
%56 = load i32, i32* %11
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %55, i32 %56)
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %58)
%60 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [20 x i8] c"Test file in is %s\0A\00"
@.str.1 = [21 x i8] c"Test file out is %s\0A\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [11 x i8] c"rfd is %d\0A\00"
@.str.3 = [11 x i8] c"wfd is %d\0A\00"
@.str.4 = [22 x i8] c"Read %d and wrote %d\0A\00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %7, i32 %12, i32 0)
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%17 = tail call i32 @open(i8* %9, i32 %16, i32 438)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%20 = call i32 @read(i32 %13, i8* %5, i32 256)
%21 = call i32 @write(i32 %17, i8* %5, i32 %20)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %20, i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %5)
%24 = call i32 @exit(i32 0)
unreachable
}