#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 */
typedef int ssize_t;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(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[]) {
  char buf[16];
  int rfd, wfd;
  ssize_t ret, wret;
  rfd = open(argv[1], O_RDONLY);
  do {
    if (rfd == -1) {
      perror("open");
      exit(1);
    }
  } while (0);
  wfd = open(argv[2], O_WRONLY | O_CREAT | O_APPEND, 0644);
  do {
    if (wfd == -1) {
      perror("create");
      exit(1);
    }
  } while (0);
  while ((ret = read(rfd, buf, 16)) > 0) {
    printf(".");
    wret = write(wfd, buf, ret);
    do {
      if (wret == -1) {
        perror("write");
        exit(1);
      }
    } while (0);
  }
  do {
    if (ret == -1) {
      perror("read");
      exit(1);
    }
  } while (0);
  printf(" finish\n");
  close(rfd);
  close(wfd);
  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    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11c1 <main+0x51>
lea    0xe4e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11c6 <main+0x56>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1218 <main+0xa8>
lea    0xdfc(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    121d <main+0xad>
jmp    1222 <main+0xb2>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1060 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    1287 <main+0x117>
lea    0xdc6(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    127d <main+0x10d>
lea    0xda0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1282 <main+0x112>
jmp    1222 <main+0xb2>
jmp    128c <main+0x11c>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12ac <main+0x13c>
lea    0xd77(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    12b1 <main+0x141>
lea    0xd61(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1266 <main+0xe6>
mov    %eax,%r14d
mov    0x2e14(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbx),%rdi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     126f <main+0xef>
mov    %eax,%r15d
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1238 <main+0xb8>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
jne    11f0 <main+0x70>
lea    0xde3(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmp    $0xffffffff,%ebp
je     1278 <main+0xf8>
lea    0xdd3(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd93(%rip),%rdi
jmp    1229 <main+0xa9>
lea    0xd8f(%rip),%rdi
jmp    1229 <main+0xa9>
lea    0xd93(%rip),%rdi
jmp    1229 <main+0xa9>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.1 = [7 x i8] c"create\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [9 x i8] c" finish\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [16 x i8]
%7 = alloca i32
%8 = alloca i32
%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 (i8*, i32, ...) @open(i8* %13, i32 %14)
store i32 %15, i32* %7
br label %16
16: ; preds = %2
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %16
br label %23
23: ; preds = %22
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @O_APPEND
%31 = or i32 %29, %30
%32 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %31, i32 420)
store i32 %32, i32* %8
br label %33
33: ; preds = %23
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %33
br label %40
40: ; preds = %39
br label %41
41: ; preds = %59, %40
%42 = load i32, i32* %7
%43 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 16)
store i32 %44, i32* %9
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %60
46: ; preds = %41
%47 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* %8
%49 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%50 = load i32, i32* %9
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
store i32 %51, i32* %10
br label %52
52: ; preds = %46
%53 = load i32, i32* %10
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %52
br label %59
59: ; preds = %58
br label %41
60: ; preds = %41
br label %61
61: ; preds = %60
%62 = load i32, i32* %9
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %61
br label %68
68: ; preds = %67
%69 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%70 = load i32, i32* %7
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* %8
%73 = call i32 @close(i32 %72)
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.1 = [7 x i8] c"create\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [5 x i8] c"read\00"
@str = [8 x i8] c" finish\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 (i8*, i32, ...) @open(i8* %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%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_APPEND
%20 = or i32 %18, %19
%21 = tail call i32 (i8*, i32, ...) @open(i8* %15, i32 %20, i32 420)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %13, %29
%27 = call i32 @read(i32 %8, i8* %4, i32 16)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = tail call i32 @putchar(i32 46)
%31 = call i32 @write(i32 %21, i8* %4, i32 %27)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %26
33: ; preds = %29
%34 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %26
%37 = icmp eq i32 %27, -1
br i1 %37, label %38, label %41
38: ; preds = %36
%39 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %36
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%43 = tail call i32 @close(i32 %8)
%44 = tail call i32 @close(i32 %21)
ret i32 0
}