#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int file1, file2;
  file1 = open(argv[1], O_RDONLY);
  file2 = open(argv[2], O_WRONLY);
  if (!file1) {
    printf("open error\n");
    return 0;
  }
  char c;
  int index = 0;
  lseek(file1, 0, SEEK_END);
  int length = lseek(file1, 0, SEEK_CUR);
  if (!length)
    return 0;
  while (!lseek(file1, index, SEEK_SET)) {
    read(file1, &c, 1);
    write(file2, &c, 1);
    if (index == length - 1) {
      break;
    }
    index++;
  }
  close(file1);
  close(file2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11ca <main+0x6a>
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    129b <main+0x13b>
movl   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x14(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1210 <main+0xb0>
movl   $0x0,-0x4(%rbp)
jmp    129b <main+0x13b>
jmp    1215 <main+0xb5>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    123e <main+0xde>
jmp    1284 <main+0x124>
mov    -0x14(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0x18(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%eax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1276 <main+0x116>
jmp    1284 <main+0x124>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1215 <main+0xb5>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %ebx,%ebx
je     1239 <main+0xd9>
mov    %eax,%r14d
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
test   %eax,%eax
je     1245 <main+0xe5>
mov    %eax,%r15d
mov    0x2df3(%rip),%r13
mov    0x0(%r13),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
test   %eax,%eax
jne    1228 <main+0xc8>
mov    $0x1,%ebp
lea    0x7(%rsp),%r12
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    %ebp,%r15d
je     1228 <main+0xc8>
mov    0x0(%r13),%edx
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <lseek@plt>
add    $0x1,%ebp
test   %eax,%eax
je     11f0 <main+0x90>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
jmp    1245 <main+0xe5>
lea    0xdc0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str = [12 x i8] c"open error\0A\00"
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_WRONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %2
%24 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %61
25: ; preds = %2
store i32 0, i32* %9
%26 = load i32, i32* %6
%27 = load i32, i32* @SEEK_END
%28 = call i32 @lseek(i32 %26, i32 0, i32 %27)
%29 = load i32, i32* %6
%30 = load i32, i32* @SEEK_CUR
%31 = call i32 @lseek(i32 %29, i32 0, i32 %30)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %25
store i32 0, i32* %3
br label %61
35: ; preds = %25
br label %36
36: ; preds = %53, %35
%37 = load i32, i32* %6
%38 = load i32, i32* %9
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @lseek(i32 %37, i32 %38, i32 %39)
%41 = icmp ne i32 %40, 0
%42 = xor i1 %41, true
br i1 %42, label %43, label %56
43: ; preds = %36
%44 = load i32, i32* %6
%45 = call i32 @read(i32 %44, i8* %8, i32 1)
%46 = load i32, i32* %7
%47 = call i32 @write(i32 %46, i8* %8, i32 1)
%48 = load i32, i32* %9
%49 = load i32, i32* %10
%50 = sub nsw i32 %49, 1
%51 = icmp eq i32 %48, %50
br i1 %51, label %52, label %53
52: ; preds = %43
br label %56
53: ; preds = %43
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %36
56: ; preds = %52, %36
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* %7
%60 = call i32 @close(i32 %59)
store i32 0, i32* %3
br label %61
61: ; preds = %56, %34, %23
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
@str = [11 x i8] c"open error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %5, i32 %6)
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_WRONLY
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = icmp eq i32 %7, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %41
15: ; preds = %2
%16 = load i32, i32* @SEEK_END
%17 = tail call i32 @lseek(i32 %7, i32 0, i32 %16)
%18 = load i32, i32* @SEEK_CUR
%19 = tail call i32 @lseek(i32 %7, i32 0, i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %40, label %21
21: ; preds = %15
%22 = load i32, i32* @SEEK_SET
%23 = tail call i32 @lseek(i32 %7, i32 0, i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %37
25: ; preds = %21
%26 = add nsw i32 %19, -1
br label %31
27: ; preds = %31
%28 = load i32, i32* @SEEK_SET
%29 = tail call i32 @lseek(i32 %7, i32 %36, i32 %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %25, %27
%32 = phi i32 [ 0, %25 ], [ %36, %27 ]
%33 = call i32 @read(i32 %7, i8* %3, i32 1)
%34 = call i32 @write(i32 %11, i8* %3, i32 1)
%35 = icmp eq i32 %32, %26
%36 = add nuw nsw i32 %32, 1
br i1 %35, label %37, label %27
37: ; preds = %27, %31, %21
%38 = tail call i32 @close(i32 %7)
%39 = tail call i32 @close(i32 %11)
br label %40
40: ; preds = %15, %37
br label %41
41: ; preds = %40, %13
ret i32 0
}