#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_IRUSR;
int S_IWUSR;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fin, fout;
  int nr, nw;
  char buf[1024];
  if (argc != 3) {
    printf("Usage: ex4p4 <FILE_IN> <FILE_OUT>\n");
    exit(1);
  }
  fin = open(argv[1], O_RDONLY);
  fout = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if (fin == -1 || fout == -1) {
    fprintf(stdout, "Error while opening the files\n");
    exit(1);
  }
  do {
    nr = read(fin, buf, 1024);
    nw = write(fout, buf, nr);
  } while (nr == 1024);
  if (close(fin) || close(fout)) {
    fprintf(stderr, "Error while closing files");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11a9 <main+0x39>
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
mov    0x2e0b(%rip),%rax
or     (%rax),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
mov    0x2dd9(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1217 <main+0xa7>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1236 <main+0xc6>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    123b <main+0xcb>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x400,-0x1c(%rbp)
je     123b <main+0xcb>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jne    1298 <main+0x128>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     12b7 <main+0x147>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    0xd9a(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
cmp    $0x3,%edi
jne    1263 <main+0xf3>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
mov    0x2df5(%rip),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
mov    0x2df2(%rip),%rax
mov    0x2e03(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%r14d
je     1279 <main+0x109>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     1279 <main+0x109>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0x400,%ebp
je     1200 <main+0x90>
mov    %r14d,%edi
call   1050 <close@plt>
test   %rax,%rax
jne    1251 <main+0xe1>
mov    %r15d,%edi
call   1050 <close@plt>
test   %rax,%rax
jne    1251 <main+0xe1>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
jmp    1289 <main+0x119>
lea    0xdcf(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd77(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"Usage: ex4p4 <FILE_IN> <FILE_OUT>\0A\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stdout = global i32 0
@.str.1 = [31 x i8] c"Error while opening the files\0A\00"
@stderr = global i32 0
@.str.2 = [26 x i8] c"Error while closing files\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 i32
%10 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_CREAT
%27 = or i32 %25, %26
%28 = load i32, i32* @O_TRUNC
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IRUSR
%31 = load i32, i32* @S_IWUSR
%32 = or i32 %30, %31
%33 = call i32 (i8*, i32, ...) @open(i8* %24, i32 %29, i32 %32)
store i32 %33, i32* %7
%34 = load i32, i32* %6
%35 = icmp eq i32 %34, -1
br i1 %35, label %39, label %36
36: ; preds = %16
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %36, %16
%40 = load i32, i32* @stdout
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %36
br label %44
44: ; preds = %52, %43
%45 = load i32, i32* %6
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%47 = call i32 @read(i32 %45, i8* %46, i32 1024)
store i32 %47, i32* %8
%48 = load i32, i32* %7
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%50 = load i32, i32* %8
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
store i32 %51, i32* %9
br label %52
52: ; preds = %44
%53 = load i32, i32* %8
%54 = icmp eq i32 %53, 1024
br i1 %54, label %44, label %55
55: ; preds = %52
%56 = load i32, i32* %6
%57 = call i64 @close(i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %63, label %59
59: ; preds = %55
%60 = load i32, i32* %7
%61 = call i64 @close(i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %59, %55
%64 = load i32, i32* @stderr
%65 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %59
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stdout = global i32 0
@.str.1 = [31 x i8] c"Error while opening the files\0A\00"
@stderr = global i32 0
@.str.2 = [26 x i8] c"Error while closing files\00"
@str = [34 x i8] c"Usage: ex4p4 <FILE_IN> <FILE_OUT>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 (i8*, i32, ...) @open(i8* %11, i32 %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %17, %16
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %22, %21
%24 = tail call i32 (i8*, i32, ...) @open(i8* %15, i32 %20, i32 %23)
%25 = icmp eq i32 %13, -1
%26 = icmp eq i32 %24, -1
%27 = or i1 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %9
%29 = load i32, i32* @stdout
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %9, %32
%33 = call i32 @read(i32 %13, i8* %4, i32 1024)
%34 = call i32 @write(i32 %24, i8* %4, i32 %33)
%35 = icmp eq i32 %33, 1024
br i1 %35, label %32, label %36
36: ; preds = %32
%37 = tail call i64 @close(i32 %13)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = tail call i64 @close(i32 %24)
%41 = icmp eq i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %39, %36
%43 = load i32, i32* @stderr
%44 = tail call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%45 = tail call i32 @exit(i32 1)
unreachable
46: ; preds = %39
ret i32 0
}