#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_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *, ...);
int /*<<< orphan*/ lseek(int, long, int);
int open(char *, int /*<<< orphan*/, int);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  int size = 128;
  char buf[128];
  int n;
  if (argc != 2) {
    error("no input");
  }
  if ((fd = open(*++argv, O_RDWR, 0644)) == -1) {
    error("error opening file %s", *argv);
  }
  lseek(fd, 0L, 0);
  if ((n = read(fd, buf, size)) > 0) {
    lseek(fd, 0l, 2);
    if (n != write(fd, buf, n)) {
      error("writing file %s", *argv);
    }
  }
  lseek(fd, 0L, 0);
  while ((n = read(fd, buf, size)) > 0) {
    if (n != write(1, buf, n)) {
      error("writing file %s to stdout", *argv);
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x80,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1198 <main+0x38>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11df <main+0x7f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0xa4(%rbp)
cmp    $0x0,%eax
jle    126a <main+0x10a>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x2,%edx
call   1040 <lseek@plt>
mov    -0xa4(%rbp),%eax
mov    %eax,-0xa8(%rbp)
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0xa4(%rbp),%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    -0xa8(%rbp),%eax
cmp    %ecx,%eax
je     1265 <main+0x105>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1070 <error@plt>
jmp    126a <main+0x10a>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0xa4(%rbp)
cmp    $0x0,%eax
jle    12e6 <main+0x186>
mov    -0xa4(%rbp),%eax
mov    %eax,-0xac(%rbp)
lea    -0xa0(%rbp),%rsi
mov    -0xa4(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,%ecx
mov    -0xac(%rbp),%eax
cmp    %ecx,%eax
je     12e1 <main+0x181>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1070 <error@plt>
jmp    1278 <main+0x118>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
je     1185 <main+0x25>
lea    0xe82(%rip),%rdi
xor    %eax,%eax
call   1070 <error@plt>
mov    0x8(%r14),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11b6 <main+0x56>
mov    0x8(%r14),%rsi
lea    0xe5a(%rip),%rdi
xor    %eax,%eax
call   1070 <error@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <lseek@plt>
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0x80,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    120a <main+0xaa>
mov    %eax,%ebx
mov    %r12d,%edi
xor    %esi,%esi
mov    $0x2,%edx
call   1040 <lseek@plt>
mov    %rsp,%rsi
mov    %r12d,%edi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %eax,%ebx
je     120a <main+0xaa>
mov    0x8(%r14),%rsi
lea    0xe1c(%rip),%rdi
xor    %eax,%eax
call   1070 <error@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <lseek@plt>
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0x80,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1279 <main+0x119>
mov    %eax,%ebp
mov    %rsp,%rbx
lea    0xdf9(%rip),%r15
jmp    1256 <main+0xf6>
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1279 <main+0x119>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %eax,%ebp
je     1240 <main+0xe0>
mov    0x8(%r14),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <error@plt>
jmp    1240 <main+0xe0>
mov    %r12d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"no input\00"
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"error opening file %s\00"
@.str.2 = [16 x i8] c"writing file %s\00"
@.str.3 = [26 x i8] c"writing file %s to stdout\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 [128 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 128, i32* %7
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i32 1
store i8** %16, i8*** %5
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18, i32 420)
store i32 %19, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %21, %14
%26 = load i32, i32* %6
%27 = call i32 @lseek(i32 %26, i64 0, i32 0)
%28 = load i32, i32* %6
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%30 = load i32, i32* %7
%31 = call i32 @read(i32 %28, i8* %29, i32 %30)
store i32 %31, i32* %9
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %25
%34 = load i32, i32* %6
%35 = call i32 @lseek(i32 %34, i64 0, i32 2)
%36 = load i32, i32* %9
%37 = load i32, i32* %6
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%39 = load i32, i32* %9
%40 = call i32 @write(i32 %37, i8* %38, i32 %39)
%41 = icmp ne i32 %36, %40
br i1 %41, label %42, label %46
42: ; preds = %33
%43 = load i8**, i8*** %5
%44 = load i8*, i8** %43
%45 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %42, %33
br label %47
47: ; preds = %46, %25
%48 = load i32, i32* %6
%49 = call i32 @lseek(i32 %48, i64 0, i32 0)
br label %50
50: ; preds = %66, %47
%51 = load i32, i32* %6
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%53 = load i32, i32* %7
%54 = call i32 @read(i32 %51, i8* %52, i32 %53)
store i32 %54, i32* %9
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %67
56: ; preds = %50
%57 = load i32, i32* %9
%58 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%59 = load i32, i32* %9
%60 = call i32 @write(i32 1, i8* %58, i32 %59)
%61 = icmp ne i32 %57, %60
br i1 %61, label %62, label %66
62: ; preds = %56
%63 = load i8**, i8*** %5
%64 = load i8*, i8** %63
%65 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %64)
br label %66
66: ; preds = %62, %56
br label %50
67: ; preds = %50
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"no input\00"
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"error opening file %s\00"
@.str.2 = [16 x i8] c"writing file %s\00"
@.str.3 = [26 x i8] c"writing file %s to stdout\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([9 x i8], [9 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_RDWR
%12 = tail call i32 @open(i8* %10, i32 %11, i32 420)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = load i8*, i8** %9
%16 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %8
%18 = tail call i32 @lseek(i32 %12, i64 0, i32 0)
%19 = call i32 @read(i32 %12, i8* %4, i32 128)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %17
%22 = tail call i32 @lseek(i32 %12, i64 0, i32 2)
%23 = call i32 @write(i32 %12, i8* %4, i32 %19)
%24 = icmp eq i32 %19, %23
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = load i8*, i8** %9
%27 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %21, %25, %17
%29 = tail call i32 @lseek(i32 %12, i64 0, i32 0)
%30 = call i32 @read(i32 %12, i8* %4, i32 128)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %28, %39
%33 = phi i32 [ %40, %39 ], [ %30, %28 ]
%34 = call i32 @write(i32 1, i8* %4, i32 %33)
%35 = icmp eq i32 %33, %34
br i1 %35, label %39, label %36
36: ; preds = %32
%37 = load i8*, i8** %9
%38 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %36, %32
%40 = call i32 @read(i32 %12, i8* %4, i32 128)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %32, label %42
42: ; preds = %39, %28
%43 = tail call i32 @close(i32 %12)
ret i32 0
}