#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  FILE *stream = NULL;
  if (argc < 2)
    return 1;
  if (!(stream = fopen(argv[1], "r"))) {
    perror("open failed");
    return 2;
  }
  while ((stream = freopen(argv[1], "r", stream))) {
    char *s;
    fscanf(stream, "%ms", &s);
    printf("%s\n", s);
    free(s);
    sleep(1);
  }
  return 0;
}


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)
movq   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a4 <main+0x34>
movl   $0x1,-0x4(%rbp)
jmp    124c <main+0xdc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe4d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11de <main+0x6e>
lea    0xe35(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    124c <main+0xdc>
jmp    11e3 <main+0x73>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe0a(%rip),%rsi
call   1050 <freopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1245 <main+0xd5>
mov    -0x18(%rbp),%rdi
lea    0xdfa(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x20(%rbp),%rsi
lea    0xdea(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    11e3 <main+0x73>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%eax
cmp    $0x2,%edi
jl     1226 <main+0xb6>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe69(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1235 <main+0xc5>
mov    0x8(%rbx),%rdi
lea    0xe50(%rip),%rsi
mov    %rax,%rdx
call   1050 <freopen@plt>
mov    %rax,%rbp
xor    %eax,%eax
test   %rbp,%rbp
je     1226 <main+0xb6>
lea    0xe45(%rip),%r15
mov    %rsp,%r12
lea    0xe3f(%rip),%r13
lea    0xe26(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <fscanf@plt>
mov    (%rsp),%rsi
mov    %r13,%rdi
call   1040 <printf@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    0x8(%rbx),%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1050 <freopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11e0 <main+0x70>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc6(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%eax
jmp    1226 <main+0xb6>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"open failed\00"
@.str.2 = [4 x i8] c"%ms\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 1, i32* %3
br label %36
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %6
%16 = icmp ne i32* %15, null
br i1 %16, label %19, label %17
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %36
19: ; preds = %11
br label %20
20: ; preds = %27, %19
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32*, i32** %6
%25 = call i32* @freopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32* %24)
store i32* %25, i32** %6
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %35
27: ; preds = %20
%28 = load i32*, i32** %6
%29 = call i32 @fscanf(i32* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %7)
%30 = load i8*, i8** %7
%31 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %7
%33 = call i32 @free(i8* %32)
%34 = call i32 @sleep(i32 1)
br label %20
35: ; preds = %20
store i32 0, i32* %3
br label %36
36: ; preds = %35, %17, %10
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"open failed\00"
@.str.2 = [4 x i8] c"%ms\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = icmp slt i32 %0, 2
br i1 %4, label %29, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %16, label %10
10: ; preds = %5
%11 = load i8*, i8** %6
%12 = call i32* @freopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32* %8)
%13 = icmp eq i32* %12, null
br i1 %13, label %29, label %14
14: ; preds = %10
%15 = bitcast i8** %3 to i8*
br label %18
16: ; preds = %5
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %29
18: ; preds = %14, %18
%19 = phi i32* [ %12, %14 ], [ %27, %18 ]
%20 = call i32 @fscanf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %3)
%21 = load i8*, i8** %3
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %3
%24 = call i32 @free(i8* %23)
%25 = call i32 @sleep(i32 1)
%26 = load i8*, i8** %6
%27 = call i32* @freopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32* %19)
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %18
29: ; preds = %18, %10, %2, %16
%30 = phi i32 [ 2, %16 ], [ 1, %2 ], [ 0, %10 ], [ 0, %18 ]
ret i32 %30
}