#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*/ assert(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ lengthen_string(char **, int *, scalar_t__);
char *scvector(int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ *stdin;
scalar_t__ strlen(char *);
scalar_t__ strncmp(char *, char *, scalar_t__);
char *read_all_stdin(const char *fname) {
  FILE *instream;
  if (fname) {
    instream = fopen(fname, "r");
    assert(instream);
  } else
    instream = stdin;
  char line_in[2048], *big_line, *cur_spot;
  int line_len, total_len, i, finished;
  total_len = 1;
  big_line = scvector(0, 1);
  big_line[0] = '\0';
  finished = 0;
  while ((fgets(line_in, 2048 - 1, instream)) && (finished == 0)) {
    if (strncmp(line_in, "#finished", strlen("#finished") - 1) == 0)
      finished = 1;
    for (i = 0; i < (int)strlen(line_in); i++)
      if ((line_in[i] == '#') || (line_in[i] == '!') || (line_in[i] == '\n'))
        line_in[i] = '\0';
    lengthen_string(&big_line, &total_len, strlen(line_in) + 3);
    sprintf(big_line, "%s %s", big_line, line_in);
  }
  if (fname)
    fclose(instream);
  return big_line;
}


x86 assembly (clang, -O0):
<read_all_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11cc <read_all_stdin+0x3c>
mov    -0x8(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <assert@plt>
jmp    11da <read_all_stdin+0x4a>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x828(%rbp)
xor    %edi,%edi
mov    $0x1,%esi
call   1090 <scvector@plt>
mov    %rax,-0x818(%rbp)
mov    -0x818(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x830(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x7ff,%esi
call   1070 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x831(%rbp)
je     1245 <read_all_stdin+0xb5>
cmpl   $0x0,-0x830(%rbp)
sete   %al
mov    %al,-0x831(%rbp)
mov    -0x831(%rbp),%al
test   $0x1,%al
jne    1258 <read_all_stdin+0xc8>
jmp    138f <read_all_stdin+0x1ff>
lea    -0x810(%rbp),%rax
mov    %rax,-0x840(%rbp)
lea    0xd95(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x840(%rbp),%rdi
mov    %rax,%rdx
sub    $0x1,%rdx
lea    0xd7b(%rip),%rsi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12a0 <read_all_stdin+0x110>
movl   $0x1,-0x830(%rbp)
movl   $0x0,-0x82c(%rbp)
mov    -0x82c(%rbp),%eax
mov    %eax,-0x844(%rbp)
lea    -0x810(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x844(%rbp),%eax
cmp    %ecx,%eax
jge    1343 <read_all_stdin+0x1b3>
movslq -0x82c(%rbp),%rax
movsbl -0x810(%rbp,%rax,1),%eax
ff
cmp    $0x23,%eax
je     131b <read_all_stdin+0x18b>
movslq -0x82c(%rbp),%rax
movsbl -0x810(%rbp,%rax,1),%eax
ff
cmp    $0x21,%eax
je     131b <read_all_stdin+0x18b>
movslq -0x82c(%rbp),%rax
movsbl -0x810(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    132a <read_all_stdin+0x19a>
movslq -0x82c(%rbp),%rax
movb   $0x0,-0x810(%rbp,%rax,1)
jmp    132f <read_all_stdin+0x19f>
mov    -0x82c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x82c(%rbp)
jmp    12aa <read_all_stdin+0x11a>
lea    -0x810(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdx
add    $0x3,%rdx
lea    -0x818(%rbp),%rdi
lea    -0x828(%rbp),%rsi
call   1030 <lengthen_string@plt>
mov    -0x818(%rbp),%rdi
mov    -0x818(%rbp),%rdx
lea    -0x810(%rbp),%rcx
lea    0xc87(%rip),%rsi
call   10b0 <sprintf@plt>
jmp    120b <read_all_stdin+0x7b>
cmpq   $0x0,-0x8(%rbp)
je     13a3 <read_all_stdin+0x213>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x818(%rbp),%rax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all_stdin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
test   %rdi,%rdi
mov    %rdi,0x18(%rsp)
je     11c4 <read_all_stdin+0x34>
lea    0xe4e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <assert@plt>
jmp    11ce <read_all_stdin+0x3e>
mov    0x2e1d(%rip),%rax
mov    (%rax),%r12
movl   $0x1,0x14(%rsp)
xor    %edi,%edi
mov    $0x1,%esi
call   1090 <scvector@plt>
mov    %rax,0x8(%rsp)
movb   $0x0,(%rax)
lea    0x20(%rsp),%rdi
mov    $0x7ff,%esi
mov    %r12,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12c7 <read_all_stdin+0x137>
lea    0x20(%rsp),%rbx
movabs $0xa00000400,%r13
lea    0xdf1(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    $0x8,%edx
mov    %rbx,%rdi
lea    0xdd3(%rip),%rsi
call   1040 <strncmp@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    1280 <read_all_stdin+0xf0>
xor    %ebp,%ebp
jmp    1264 <read_all_stdin+0xd4>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbp
mov    %rbx,%rdi
call   1060 <strlen@plt>
movslq %eax,%rcx
cmp    %rcx,%rbp
jge    1280 <read_all_stdin+0xf0>
movzbl 0x20(%rsp,%rbp,1),%eax
cmp    $0x23,%rax
ja     1250 <read_all_stdin+0xc0>
bt     %rax,%r13
jae    1250 <read_all_stdin+0xc0>
movb   $0x0,0x20(%rsp,%rbp,1)
jmp    1250 <read_all_stdin+0xc0>
nopl   0x0(%rax)
add    $0x3,%rax
lea    0x8(%rsp),%rdi
lea    0x14(%rsp),%rsi
mov    %rax,%rdx
call   1030 <lengthen_string@plt>
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
mov    %rdi,%rdx
mov    %rbx,%rcx
call   10b0 <sprintf@plt>
mov    %rbx,%rdi
mov    $0x7ff,%esi
mov    %r12,%rdx
call   1070 <fgets@plt>
test   %r15,%r15
je     12c7 <read_all_stdin+0x137>
test   %rax,%rax
jne    1220 <read_all_stdin+0x90>
cmpq   $0x0,0x18(%rsp)
je     12d7 <read_all_stdin+0x147>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%rax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stdin = global i32* null
@.str.1 = [10 x i8] c"#finished\00"
@.str.2 = [6 x i8] c"%s %s\00"
define i8* @read_all_stdin(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [2048 x i8]
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %2
%11 = load i8*, i8** %2
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %3
%16 = load i32*, i32** %3
%17 = call i32 @assert(i32* %16)
br label %20
18: ; preds = %1
%19 = load i32*, i32** @stdin
store i32* %19, i32** %3
br label %20
20: ; preds = %18, %13
store i32 1, i32* %8
%21 = call i8* @scvector(i32 0, i32 1)
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 0, i8* %23
store i32 0, i32* %10
br label %24
24: ; preds = %77, %20
%25 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%26 = load i32*, i32** %3
%27 = call i64 @fgets(i8* %25, i32 2047, i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, 0
br label %32
32: ; preds = %29, %24
%33 = phi i1 [ false, %24 ], [ %31, %29 ]
br i1 %33, label %34, label %86
34: ; preds = %32
%35 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%36 = call i64 @strlen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%37 = sub nsw i64 %36, 1
%38 = call i64 @strncmp(i8* %35, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %34
store i32 1, i32* %10
br label %41
41: ; preds = %40, %34
store i32 0, i32* %9
br label %42
42: ; preds = %74, %41
%43 = load i32, i32* %9
%44 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%45 = call i64 @strlen(i8* %44)
%46 = trunc i64 %45 to i32
%47 = icmp slt i32 %43, %46
br i1 %47, label %48, label %77
48: ; preds = %42
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 35
br i1 %54, label %69, label %55
55: ; preds = %48
%56 = load i32, i32* %9
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 %57
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 33
br i1 %61, label %69, label %62
62: ; preds = %55
%63 = load i32, i32* %9
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 %64
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 10
br i1 %68, label %69, label %73
69: ; preds = %62, %55, %48
%70 = load i32, i32* %9
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 %71
store i8 0, i8* %72
br label %73
73: ; preds = %69, %62
br label %74
74: ; preds = %73
%75 = load i32, i32* %9
%76 = add nsw i32 %75, 1
store i32 %76, i32* %9
br label %42
77: ; preds = %42
%78 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%79 = call i64 @strlen(i8* %78)
%80 = add nsw i64 %79, 3
%81 = call i32 @lengthen_string(i8** %5, i32* %8, i64 %80)
%82 = load i8*, i8** %5
%83 = load i8*, i8** %5
%84 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%85 = call i32 @sprintf(i8* %82, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %83, i8* %84)
br label %24
86: ; preds = %32
%87 = load i8*, i8** %2
%88 = icmp ne i8* %87, null
br i1 %88, label %89, label %92
89: ; preds = %86
%90 = load i32*, i32** %3
%91 = call i32 @fclose(i32* %90)
br label %92
92: ; preds = %89, %86
%93 = load i8*, i8** %5
ret i8* %93
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stdin = global i32* null
@.str.1 = [10 x i8] c"#finished\00"
@.str.2 = [6 x i8] c"%s %s\00"
define i8* @read_all_stdin(i8* %0) {
%2 = alloca [2048 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = icmp eq i8* %0, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @assert(i32* %7)
br label %11
9: ; preds = %1
%10 = load i32*, i32** @stdin
br label %11
11: ; preds = %9, %6
%12 = phi i32* [ %7, %6 ], [ %10, %9 ]
%13 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%14 = bitcast i8** %3 to i8*
%15 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%16 = tail call i8* @scvector(i32 0, i32 1)
store i8* %16, i8** %3
store i8 0, i8* %16
%17 = call i64 @fgets(i8* %13, i32 2047, i32* %12)
%18 = icmp eq i64 %17, 0
br i1 %18, label %45, label %19
19: ; preds = %11, %36
%20 = call i64 @strncmp(i8* %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64 8)
%21 = icmp ne i64 %20, 0
%22 = call i64 @strlen(i8* %13)
%23 = trunc i64 %22 to i32
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %36
25: ; preds = %19, %30
%26 = phi i64 [ %31, %30 ], [ 0, %19 ]
%27 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 %26
%28 = load i8, i8* %27
switch i8 %28, label %30 [
i8 35, label %29
i8 33, label %29
i8 10, label %29
]
29: ; preds = %25, %25, %25
store i8 0, i8* %27
br label %30
30: ; preds = %25, %29
%31 = add nuw nsw i64 %26, 1
%32 = call i64 @strlen(i8* %13)
%33 = shl i64 %32, 32
%34 = ashr exact i64 %33, 32
%35 = icmp slt i64 %31, %34
br i1 %35, label %25, label %36
36: ; preds = %30, %19
%37 = phi i64 [ %22, %19 ], [ %32, %30 ]
%38 = add nsw i64 %37, 3
%39 = call i32 @lengthen_string(i8** %3, i32* %4, i64 %38)
%40 = load i8*, i8** %3
%41 = call i32 @sprintf(i8* dereferenceable(1) %40, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %40, i8* %13)
%42 = call i64 @fgets(i8* %13, i32 2047, i32* %12)
%43 = icmp ne i64 %42, 0
%44 = and i1 %21, %43
br i1 %44, label %19, label %45
45: ; preds = %36, %11
br i1 %5, label %48, label %46
46: ; preds = %45
%47 = call i32 @fclose(i32* %12)
br label %48
48: ; preds = %46, %45
%49 = load i8*, i8** %3
ret i8* %49
}