#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_APPEND;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int dup(int);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc > 1) {
    int stdoutCopy = dup(1);
    close(1);
    int fdWrite = open(argv[1], O_WRONLY | O_CREAT | O_APPEND, 0666);
    if (fdWrite == -1) {
      write(2, "Cannot open file", 16);
      return 1;
    }
    close(0);
    int fdRead = open(argv[1], O_RDONLY);
    if (fdRead == -1) {
      write(2, "Cannot open file", 16);
      return 1;
    }
    char c;
    while (read(0, &c, 1) && !(c >= '0' && c <= '9')) {
      write(1, &c, 1);
    }
    char buff[100];
    int readCount;
    close(1);
    dup(stdoutCopy);
    while (readCount = read(0, buff, 100))
      write(1, buff, readCount);
  } else {
    printf("No file name provided");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    131e <main+0x1be>
mov    $0x1,%edi
call   1050 <dup@plt>
mov    %eax,-0x14(%rbp)
mov    $0x1,%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
mov    0x2e05(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11f8 <main+0x98>
mov    $0x2,%edi
lea    0xe1e(%rip),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
movl   $0x1,-0x4(%rbp)
jmp    1331 <main+0x1d1>
xor    %edi,%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1246 <main+0xe6>
mov    $0x2,%edi
lea    0xdd0(%rip),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
movl   $0x1,-0x4(%rbp)
jmp    1331 <main+0x1d1>
jmp    124b <main+0xeb>
xor    %edi,%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x95(%rbp)
je     12a1 <main+0x141>
movsbl -0x1d(%rbp),%ecx
xor    %eax,%eax
cmp    $0x30,%ecx
mov    %al,-0x96(%rbp)
jl     1293 <main+0x133>
movsbl -0x1d(%rbp),%eax
cmp    $0x39,%eax
setle  %al
mov    %al,-0x96(%rbp)
mov    -0x96(%rbp),%al
xor    $0xff,%al
mov    %al,-0x95(%rbp)
mov    -0x95(%rbp),%al
test   $0x1,%al
jne    12b4 <main+0x154>
jmp    12c9 <main+0x169>
mov    $0x1,%edx
lea    -0x1d(%rbp),%rsi
mov    %edx,%edi
call   1030 <write@plt>
jmp    124b <main+0xeb>
mov    $0x1,%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <dup@plt>
lea    -0x90(%rbp),%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1070 <read@plt>
mov    %eax,-0x94(%rbp)
cmp    $0x0,%eax
je     1319 <main+0x1b9>
lea    -0x90(%rbp),%rsi
mov    -0x94(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    12db <main+0x17b>
jmp    132a <main+0x1ca>
lea    0xcec(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
cmp    $0x2,%edi
jl     1294 <main+0x134>
mov    %rsi,%rbx
mov    $0x1,%ebp
mov    $0x1,%edi
call   1050 <dup@plt>
mov    %eax,%r14d
mov    $0x1,%edi
call   1060 <close@plt>
mov    0x2e2e(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbx),%rdi
mov    0x2e10(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0x144>
xor    %edi,%edi
call   1060 <close@plt>
mov    0x8(%rbx),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12bc <main+0x15c>
lea    0xf(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
je     1240 <main+0xe0>
lea    0xf(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
movzbl 0xf(%rsp),%eax
add    $0xc6,%al
cmp    $0xf5,%al
ja     1240 <main+0xe0>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
jne    1210 <main+0xb0>
mov    $0x1,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1050 <dup@plt>
xor    %ebp,%ebp
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1070 <read@plt>
test   %eax,%eax
je     12d7 <main+0x177>
lea    0x10(%rsp),%rbx
xchg   %ax,%ax
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
test   %eax,%eax
jne    1270 <main+0x110>
jmp    12d7 <main+0x177>
lea    0xd76(%rip),%rdi
call   1040 <printf@plt>
xor    %ebp,%ebp
jmp    12d7 <main+0x177>
lea    0xd55(%rip),%rsi
mov    $0x2,%edi
mov    $0x10,%edx
call   1030 <write@plt>
jmp    12d7 <main+0x177>
lea    0xd3d(%rip),%rsi
mov    $0x2,%edi
mov    $0x10,%edx
call   1030 <write@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str = [17 x i8] c"Cannot open file\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"No file name provided\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 i8
%10 = alloca [100 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %73
14: ; preds = %2
%15 = call i32 @dup(i32 1)
store i32 %15, i32* %6
%16 = call i32 @close(i32 1)
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = load i32, i32* @O_APPEND
%24 = or i32 %22, %23
%25 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %24, i32 438)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %14
%29 = call i32 @write(i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 16)
store i32 1, i32* %3
br label %76
30: ; preds = %14
%31 = call i32 @close(i32 0)
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i32, i32* @O_RDONLY
%36 = call i32 (i8*, i32, ...) @open(i8* %34, i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = call i32 @write(i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 16)
store i32 1, i32* %3
br label %76
41: ; preds = %30
br label %42
42: ; preds = %58, %41
%43 = call i32 @read(i32 0, i8* %9, i32 1)
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %56
45: ; preds = %42
%46 = load i8, i8* %9
%47 = sext i8 %46 to i32
%48 = icmp sge i32 %47, 48
br i1 %48, label %49, label %53
49: ; preds = %45
%50 = load i8, i8* %9
%51 = sext i8 %50 to i32
%52 = icmp sle i32 %51, 57
br label %53
53: ; preds = %49, %45
%54 = phi i1 [ false, %45 ], [ %52, %49 ]
%55 = xor i1 %54, true
br label %56
56: ; preds = %53, %42
%57 = phi i1 [ false, %42 ], [ %55, %53 ]
br i1 %57, label %58, label %60
58: ; preds = %56
%59 = call i32 @write(i32 1, i8* %9, i32 1)
br label %42
60: ; preds = %56
%61 = call i32 @close(i32 1)
%62 = load i32, i32* %6
%63 = call i32 @dup(i32 %62)
br label %64
64: ; preds = %68, %60
%65 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%66 = call i32 @read(i32 0, i8* %65, i32 100)
store i32 %66, i32* %11
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %64
%69 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%70 = load i32, i32* %11
%71 = call i32 @write(i32 1, i8* %69, i32 %70)
br label %64
72: ; preds = %64
br label %75
73: ; preds = %2
%74 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %75
75: ; preds = %73, %72
store i32 0, i32* %3
br label %76
76: ; preds = %75, %39, %28
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str = [17 x i8] c"Cannot open file\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"No file name provided\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca [100 x i8]
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %51
6: ; preds = %2
%7 = tail call i32 @dup(i32 1)
%8 = tail call i32 @close(i32 1)
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @O_APPEND
%15 = or i32 %13, %14
%16 = tail call i32 (i8*, i32, ...) @open(i8* %10, i32 %15, i32 438)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %6
%19 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 16)
br label %53
20: ; preds = %6
%21 = tail call i32 @close(i32 0)
%22 = load i8*, i8** %9
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 (i8*, i32, ...) @open(i8* %22, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 16)
br label %53
28: ; preds = %20
%29 = call i32 @read(i32 0, i8* %3, i32 1)
%30 = icmp eq i32 %29, 0
br i1 %30, label %39, label %31
31: ; preds = %28, %35
%32 = load i8, i8* %3
%33 = add i8 %32, -58
%34 = icmp ult i8 %33, -10
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = call i32 @write(i32 1, i8* %3, i32 1)
%37 = call i32 @read(i32 0, i8* %3, i32 1)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %31
39: ; preds = %31, %35, %28
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%41 = tail call i32 @close(i32 1)
%42 = tail call i32 @dup(i32 %7)
%43 = call i32 @read(i32 0, i8* %40, i32 100)
%44 = icmp eq i32 %43, 0
br i1 %44, label %50, label %45
45: ; preds = %39, %45
%46 = phi i32 [ %48, %45 ], [ %43, %39 ]
%47 = call i32 @write(i32 1, i8* %40, i32 %46)
%48 = call i32 @read(i32 0, i8* %40, i32 100)
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %45
50: ; preds = %45, %39
br label %53
51: ; preds = %2
%52 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %53
53: ; preds = %26, %18, %51, %50
%54 = phi i32 [ 0, %50 ], [ 0, %51 ], [ 1, %18 ], [ 1, %26 ]
ret i32 %54
}