#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_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd, fd1;
  if (argc != 3) {
    printf("para error\n");
    return -1;
  }
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  int ret, ret1, ret2;
  char buff[1024] = {0};
  ret = read(fd, buff, 1023);
  if (ret < 0) {
    perror("read");
    close(fd);
    return -1;
  }
  printf("read-ret:%d\n", ret);
  printf("read-argv[1]:\n%s\n", buff);
  fd1 = open(argv[2], O_RDWR | O_TRUNC | O_CREAT, 0640);
  if (fd1 < 0) {
    perror("open");
    return -1;
  }
  ret1 = write(fd1, buff, ret);
  if (ret1 < 0) {
    perror("write");
    close(fd1);
    return -1;
  }
  printf("write-argv[2]:\n%s\n", buff);
  close(fd);
  close(fd1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ad <main+0x3d>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <main+0x1b3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ea <main+0x7a>
lea    0xe33(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <main+0x1b3>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    123e <main+0xce>
lea    0xdec(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <main+0x1b3>
mov    -0x1c(%rbp),%esi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x430(%rbp),%rsi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    0x2d74(%rip),%rax
or     (%rax),%esi
mov    0x2d73(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12b8 <main+0x148>
lea    0xd65(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <main+0x1b3>
mov    -0x18(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12f7 <main+0x187>
lea    0xd57(%rip),%rdi
call   1090 <perror@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <main+0x1b3>
lea    -0x430(%rbp),%rsi
lea    0xd36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
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    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
cmp    $0x3,%edi
jne    1268 <main+0xe8>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     1287 <main+0x107>
mov    %eax,%r14d
mov    %rsp,%rbp
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x3ff,%edx
call   1080 <read@plt>
test   %eax,%eax
js     1295 <main+0x115>
mov    %eax,%ebp
lea    0xe21(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe1e(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dce(%rip),%rax
mov    0x2ddf(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbx),%rdi
mov    0x2dd8(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     1287 <main+0x107>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    %ebp,%edx
call   1040 <write@plt>
test   %eax,%eax
js     12a6 <main+0x126>
lea    0xde4(%rip),%rdi
xor    %ebp,%ebp
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1279 <main+0xf9>
lea    0xdd3(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd72(%rip),%rdi
call   10a0 <perror@plt>
jmp    1274 <main+0xf4>
lea    0xd69(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
jmp    12b4 <main+0x134>
lea    0xd7c(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1274 <main+0xf4>

LLVM IR (-O0):
@.str = [12 x i8] c"para error\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [13 x i8] c"read-ret:%d\0A\00"
@.str.4 = [18 x i8] c"read-argv[1]:\0A%s\0A\00"
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [19 x i8] c"write-argv[2]:\0A%s\0A\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 i32
%11 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %73
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 i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %73
26: ; preds = %16
%27 = bitcast [1024 x i8]* %11 to i8*
%28 = load i32, i32* %6
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 1023)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
store i32 -1, i32* %3
br label %73
37: ; preds = %26
%38 = load i32, i32* %8
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %40)
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = load i32, i32* @O_RDWR
%46 = load i32, i32* @O_TRUNC
%47 = or i32 %45, %46
%48 = load i32, i32* @O_CREAT
%49 = or i32 %47, %48
%50 = call i32 (i8*, i32, ...) @open(i8* %44, i32 %49, i32 416)
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %37
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %73
55: ; preds = %37
%56 = load i32, i32* %7
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%58 = load i32, i32* %8
%59 = call i32 @write(i32 %56, i8* %57, i32 %58)
store i32 %59, i32* %9
%60 = load i32, i32* %9
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %55
%63 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%64 = load i32, i32* %7
%65 = call i32 @close(i32 %64)
store i32 -1, i32* %3
br label %73
66: ; preds = %55
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %67)
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
%71 = load i32, i32* %7
%72 = call i32 @close(i32 %71)
store i32 0, i32* %3
br label %73
73: ; preds = %66, %62, %53, %33, %24, %14
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [13 x i8] c"read-ret:%d\0A\00"
@.str.4 = [18 x i8] c"read-argv[1]:\0A%s\0A\00"
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [19 x i8] c"write-argv[2]:\0A%s\0A\00"
@str = [11 x i8] c"para error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = icmp eq i32 %0, 3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %48
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 (i8*, i32, ...) @open(i8* %9, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %48
15: ; preds = %7
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = call i32 @read(i32 %11, i8* %16, i32 1023)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @close(i32 %11)
br label %46
22: ; preds = %15
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %16)
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDWR
%28 = load i32, i32* @O_TRUNC
%29 = or i32 %28, %27
%30 = load i32, i32* @O_CREAT
%31 = or i32 %29, %30
%32 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %31, i32 416)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %22
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %46
36: ; preds = %22
%37 = call i32 @write(i32 %32, i8* %16, i32 %17)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @close(i32 %32)
br label %46
42: ; preds = %36
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %16)
%44 = call i32 @close(i32 %11)
%45 = call i32 @close(i32 %32)
br label %46
46: ; preds = %42, %39, %34, %19
%47 = phi i32 [ -1, %19 ], [ -1, %34 ], [ -1, %39 ], [ 0, %42 ]
br label %48
48: ; preds = %46, %13, %5
%49 = phi i32 [ -1, %5 ], [ -1, %13 ], [ %47, %46 ]
ret i32 %49
}