#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 /*<<< orphan*/ O_CREAT;
int O_RDWR;
int S_IREAD;
int S_IWRITE;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char const *argv[]) {
  int file_pointer_input, file_pointer_output;
  char *input_filename = "input.io";
  char *output_filename = "output.o";
  char buffer[64];
  file_pointer_input = open(input_filename, O_RDWR);
  file_pointer_output = open(output_filename, O_RDWR | O_APPEND);
  if (file_pointer_output == -1) {
    file_pointer_output = open(output_filename, O_CREAT, S_IWRITE | S_IREAD);
  }
  if (errno == 13) {
    printf("Permission denied\n");
    return 5;
  }
  char current_char[1];
  int readed, written;
  while (1) {
    readed = read(file_pointer_input, current_char, 1);
    if (readed == 0) {
      break;
    }
    if (current_char[0] >= 48 && current_char[0] <= 52) {
      written = write(file_pointer_output, current_char, 1);
      printf("Write %d %d\n", file_pointer_output, written);
    }
  };
  printf("End if file\n");
  close(file_pointer_input);
  close(file_pointer_output);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe90(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe8e(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2e06(%rip),%rax
or     (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11e9 <main+0x99>
mov    -0x28(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
mov    0x2de3(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dd8(%rip),%rax
cmpl   $0xd,(%rax)
jne    1213 <main+0xc3>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    12a7 <main+0x157>
jmp    1218 <main+0xc8>
mov    -0x14(%rbp),%edi
lea    -0x71(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
jne    123b <main+0xeb>
jmp    1282 <main+0x132>
movsbl -0x71(%rbp),%eax
cmp    $0x30,%eax
jl     127d <main+0x12d>
movsbl -0x71(%rbp),%eax
cmp    $0x34,%eax
jg     127d <main+0x12d>
mov    -0x18(%rbp),%edi
lea    -0x71(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x7c(%rbp),%edx
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1218 <main+0xc8>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e72(%rip),%rbx
mov    (%rbx),%esi
lea    0xe89(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r14d
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
or     (%rbx),%esi
lea    0xe76(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11cc <main+0x6c>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
mov    0x2e08(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe46(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
mov    0x2df5(%rip),%rax
cmpl   $0xd,(%rax)
jne    11ee <main+0x8e>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%eax
jmp    127b <main+0x11b>
lea    0x7(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
je     125e <main+0xfe>
lea    0x7(%rsp),%rbx
lea    0xe02(%rip),%r15
jmp    1234 <main+0xd4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
je     125e <main+0xfe>
movzbl 0x7(%rsp),%eax
add    $0xd0,%al
cmp    $0x4,%al
ja     1220 <main+0xc0>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1220 <main+0xc0>
lea    0xdba(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [9 x i8] c"input.io\00"
@.str.1 = [9 x i8] c"output.o\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@S_IWRITE = global i32 0
@S_IREAD = global i32 0
@errno = global i32 0
@.str.3 = [13 x i8] c"Write %d %d\0A\00"
@str = [12 x i8] c"End if file\00"
@str.5 = [18 x i8] c"Permission denied\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1 x i8]
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_APPEND
%8 = or i32 %7, %6
%9 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @S_IWRITE
%14 = load i32, i32* @S_IREAD
%15 = or i32 %14, %13
%16 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %15)
br label %17
17: ; preds = %11, %2
%18 = phi i32 [ %16, %11 ], [ %9, %2 ]
%19 = load i32, i32* @errno
%20 = icmp eq i32 %19, 13
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.5, i64 0, i64 0))
br label %41
23: ; preds = %17
%24 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%25 = call i32 @read(i32 %5, i8* %24, i32 1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %23, %34
%28 = load i8, i8* %24
%29 = add i8 %28, -48
%30 = icmp ult i8 %29, 5
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @write(i32 %18, i8* %24, i32 1)
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %18, i32 %32)
br label %34
34: ; preds = %31, %27
%35 = call i32 @read(i32 %5, i8* %24, i32 1)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %27
37: ; preds = %34, %23
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%39 = tail call i32 @close(i32 %5)
%40 = tail call i32 @close(i32 %18)
br label %41
41: ; preds = %37, %21
%42 = phi i32 [ 5, %21 ], [ 0, %37 ]
ret i32 %42
}