#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];
  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    -0x138(%rbp),%rsi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x140(%rbp),%rsi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x138(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x140(%rbp),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    0x2df8(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x124(%rbp)
cmpl   $0x100,-0x124(%rbp)
je     126e <main+0x11e>
movslq -0x124(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    123b <main+0xeb>
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    0xd89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x120(%rbp),%rsi
lea    0xd8a(%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 i8*, i8** %12
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %13
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %12
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25, i32 0)
store i32 %26, i32* %6
%27 = load i8*, i8** %13
%28 = load i32, i32* @O_WRONLY
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %27, i32 %30, i32 438)
store i32 %31, i32* %7
%32 = load i32, i32* %6
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %34)
store i32 0, i32* %9
br label %36
36: ; preds = %43, %2
%37 = load i32, i32* %9
%38 = icmp ne i32 %37, 256
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 %41
store i8 0, i8* %42
br label %43
43: ; preds = %39
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
store i32 %45, i32* %9
br label %36
46: ; preds = %36
%47 = load i32, i32* %6
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 256)
store i32 %49, i32* %10
%50 = load i32, i32* %7
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%52 = load i32, i32* %10
%53 = call i32 @write(i32 %50, i8* %51, i32 %52)
store i32 %53, i32* %11
%54 = load i32, i32* %10
%55 = load i32, i32* %11
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %54, i32 %55)
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %57)
%59 = 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
}