#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_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int fp;
  int n = 0;
  char c = '0';
  printf("This is test of redirection (<) !\n");
  fp = open(argv[1], O_RDONLY, S_IRUSR | S_IWUSR);
  if (!fp)
    perror("failed to open file");
  else {
    while ((n = read(fp, &c, 1)) > 0)
      write(STDOUT_FILENO, &c, 1);
  }
  if (fp)
    close(fp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movb   $0x30,-0x19(%rbp)
lea    0xe78(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11d3 <main+0x73>
lea    0xe5a(%rip),%rdi
call   1080 <perror@plt>
jmp    1216 <main+0xb6>
jmp    11d8 <main+0x78>
mov    -0x14(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1211 <main+0xb1>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11d8 <main+0x78>
jmp    1216 <main+0xb6>
cmpl   $0x0,-0x14(%rbp)
je     1228 <main+0xc8>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movb   $0x30,0xf(%rsp)
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rbx),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
mov    0x2e41(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1070 <open@plt>
test   %eax,%eax
je     1205 <main+0xa5>
mov    %eax,%ebp
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11f3 <main+0x93>
mov    0x2e2e(%rip),%r14
lea    0xf(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     11d0 <main+0x70>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf4(%rip),%rdi
call   1080 <perror@plt>
jmp    11fa <main+0x9a>

LLVM IR (-O0):
@.str = [35 x i8] c"This is test of redirection (<) !\0A\00"
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [20 x i8] c"failed to open file\00"
@STDOUT_FILENO = global i32 0
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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i8 48, i8* %8
%9 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %12, i32 %13, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %31
22: ; preds = %2
br label %23
23: ; preds = %27, %22
%24 = load i32, i32* %6
%25 = call i32 @read(i32 %24, i8* %8, i32 1)
store i32 %25, i32* %7
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @STDOUT_FILENO
%29 = call i32 @write(i32 %28, i8* %8, i32 1)
br label %23
30: ; preds = %23
br label %31
31: ; preds = %30, %20
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34, %31
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [20 x i8] c"failed to open file\00"
@STDOUT_FILENO = global i32 0
@str = [34 x i8] c"This is test of redirection (<) !\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
store i8 48, i8* %3
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* %6, i32 %7, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = call i32 @read(i32 %11, i8* %3, i32 1)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %18, label %23
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %25
18: ; preds = %13, %18
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @write(i32 %19, i8* %3, i32 1)
%21 = call i32 @read(i32 %11, i8* %3, i32 1)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %18, label %23
23: ; preds = %18, %13
%24 = tail call i32 @close(i32 %11)
br label %25
25: ; preds = %16, %23
ret i32 0
}