#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 calc_value();
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int read_string();
int /*<<< orphan*/ stdin;
int main() {
  freopen("./sample.txt", "r", stdin);
  while (1) {
    int read_next = read_string();
    if (!read_next)
      break;
    int sum = calc_value();
    printf("%d\n", sum);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
lea    0xea1(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1040 <freopen@plt>
mov    $0x0,%al
call   1060 <read_string@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1184 <main+0x44>
jmp    11a2 <main+0x62>
mov    $0x0,%al
call   1050 <calc_value@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe77(%rip),%rdi
call   1030 <printf@plt>
jmp    116b <main+0x2b>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%edx
lea    0xeaf(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1040 <freopen@plt>
xor    %eax,%eax
call   1060 <read_string@plt>
test   %eax,%eax
je     118c <main+0x4c>
lea    0xea0(%rip),%rbx
nop
xor    %eax,%eax
call   1050 <calc_value@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <read_string@plt>
test   %eax,%eax
jne    1170 <main+0x30>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"./sample.txt\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @stdin
%5 = call i32 @freopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %4)
br label %6
6: ; preds = %0, %11
%7 = call i32 (...) @read_string()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp ne i32 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %6
br label %15
11: ; preds = %6
%12 = call i32 (...) @calc_value()
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %6
15: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"./sample.txt\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = load i32, i32* @stdin
%2 = tail call i32 @freopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%3 = tail call i32 (...) @read_string()
%4 = icmp eq i32 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %0, %5
%6 = tail call i32 (...) @calc_value()
%7 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%8 = tail call i32 (...) @read_string()
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %5
10: ; preds = %5, %0
ret i32 0
}