#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int atoi(char *);
int /*<<< orphan*/ err_quit(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *malloc(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fdin, fdout, bufsz;
  char *src;
  struct stat statbuf;
  if (argc != 4)
    err_quit("usage: read_write <fromfile> <tofile> <buf_size>");
  if ((fdin = open(argv[1], O_RDONLY)) < 0) {
    char buf[256];
    sprintf(buf, "can't open %s for reading", argv[1]);
    perror(buf);
    exit(errno);
  }
  if ((fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0644)) < 0) {
    char buf[256];
    sprintf(buf, "can't create %s for writing", argv[2]);
    perror(buf);
    exit(errno);
  }
  bufsz = atoi(argv[3]);
  src = malloc(bufsz);
  while ((read(fdin, src, bufsz)) > 0) {
    write(fdout, src, bufsz);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11bf <main+0x2f>
lea    0xe46(%rip),%rdi
call   1040 <err_quit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1218 <main+0x88>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe38(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x130(%rbp),%rdi
call   1080 <perror@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    0x2dc8(%rip),%rax
or     (%rax),%esi
mov    0x2db7(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1288 <main+0xf8>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xde2(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x230(%rbp),%rdi
call   1080 <perror@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jle    12d1 <main+0x141>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
jmp    12a4 <main+0x114>
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%r14
cmp    $0x4,%edi
je     11b3 <main+0x23>
lea    0xe52(%rip),%rdi
call   1040 <err_quit@plt>
mov    0x8(%r14),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     1253 <main+0xc3>
mov    %eax,%r15d
mov    0x2dff(%rip),%rax
mov    0x2e18(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%r14),%rdi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     1260 <main+0xd0>
mov    %eax,%r12d
mov    0x18(%r14),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rax,%rsi
nop
mov    %ebx,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    1241 <main+0xb1>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
jmp    1220 <main+0x90>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%r14),%rdx
lea    0xdd3(%rip),%rsi
jmp    126b <main+0xdb>
mov    0x10(%r14),%rdx
lea    0xde0(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
call   1080 <perror@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [49 x i8] c"usage: read_write <fromfile> <tofile> <buf_size>\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"can't open %s for reading\00"
@errno = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [28 x i8] c"can't create %s for writing\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 i32
%9 = alloca i8*
%10 = alloca %struct.stat
%11 = alloca [256 x i8]
%12 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 4
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @err_quit(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 (i8*, i32, ...) @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %17
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%31 = call i32 @perror(i8* %30)
%32 = load i32, i32* @errno
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %17
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = load i32, i32* @O_RDWR
%39 = load i32, i32* @O_CREAT
%40 = or i32 %38, %39
%41 = load i32, i32* @O_TRUNC
%42 = or i32 %40, %41
%43 = call i32 (i8*, i32, ...) @open(i8* %37, i32 %42, i32 420)
store i32 %43, i32* %7
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %34
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 2
%49 = load i8*, i8** %48
%50 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %49)
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%52 = call i32 @perror(i8* %51)
%53 = load i32, i32* @errno
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %34
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 3
%58 = load i8*, i8** %57
%59 = call i32 @atoi(i8* %58)
store i32 %59, i32* %8
%60 = load i32, i32* %8
%61 = call i8* @malloc(i32 %60)
store i8* %61, i8** %9
br label %62
62: ; preds = %68, %55
%63 = load i32, i32* %6
%64 = load i8*, i8** %9
%65 = load i32, i32* %8
%66 = call i64 @read(i32 %63, i8* %64, i32 %65)
%67 = icmp sgt i64 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %62
%69 = load i32, i32* %7
%70 = load i8*, i8** %9
%71 = load i32, i32* %8
%72 = call i32 @write(i32 %69, i8* %70, i32 %71)
br label %62
73: ; preds = %62
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str = [49 x i8] c"usage: read_write <fromfile> <tofile> <buf_size>\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"can't open %s for reading\00"
@errno = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [28 x i8] c"can't create %s for writing\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = icmp eq i32 %0, 4
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @err_quit(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 (i8*, i32, ...) @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %8
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%16 = load i8*, i8** %9
%17 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = call i32 @perror(i8* %15)
%19 = load i32, i32* @errno
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %8
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_CREAT
%26 = or i32 %25, %24
%27 = load i32, i32* @O_TRUNC
%28 = or i32 %26, %27
%29 = tail call i32 (i8*, i32, ...) @open(i8* %23, i32 %28, i32 420)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %21
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%33 = load i8*, i8** %22
%34 = call i32 @sprintf(i8* %32, i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = call i32 @perror(i8* %32)
%36 = load i32, i32* @errno
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %21
%39 = getelementptr inbounds i8*, i8** %1, i64 3
%40 = load i8*, i8** %39
%41 = tail call i32 @atoi(i8* %40)
%42 = tail call i8* @malloc(i32 %41)
%43 = tail call i64 @read(i32 %12, i8* %42, i32 %41)
%44 = icmp sgt i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %38, %45
%46 = tail call i32 @write(i32 %29, i8* %42, i32 %41)
%47 = tail call i64 @read(i32 %12, i8* %42, i32 %41)
%48 = icmp sgt i64 %47, 0
br i1 %48, label %45, label %49
49: ; preds = %45, %38
ret i32 0
}