#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 errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int);
int main() {
  FILE *in, *out;
  int lines = 0;
  char line[250];
  in = fopen("x_data.in", "r");
  if (!in) {
    fprintf(stderr, "Can't open x_data.in, errno: %d (%s)\n", errno,
            strerror(errno));
    exit(1);
  }
  out = fopen("job_rsc_fgets_std.output", "w");
  if (!out) {
    fprintf(stderr, "Can't open fgets.output, errno: %d (%s)\n", errno,
            strerror(errno));
    exit(1);
  }
  while (fgets(line, 249, in) != NULL) {
    lines++;
    fprintf(stdout, "Read a line (%d)\n", lines);
    fprintf(out, "%s", line);
    fflush(out);
  }
  fclose(in);
  fclose(out);
  fprintf(stdout, "All done.\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe70(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fe <main+0x8e>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x124(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x128(%rbp),%edi
mov    -0x124(%rbp),%edx
mov    %rax,%rcx
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe2d(%rip),%rdi
lea    0xe3f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1273 <main+0x103>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x130(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x130(%rbp),%edi
mov    -0x12c(%rbp),%edx
mov    %rax,%rcx
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1278 <main+0x108>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xf9,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12e3 <main+0x173>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
lea    -0x120(%rbp),%rdx
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fflush@plt>
jmp    1278 <main+0x108>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
lea    0xe78(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
jne    11bb <main+0x4b>
mov    0x2e40(%rip),%rax
mov    (%rax),%ebx
mov    0x2e2f(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1090 <strerror@plt>
lea    0xe53(%rip),%rsi
jmp    11f6 <main+0x86>
mov    %rax,%r14
lea    0xe6d(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
jne    120e <main+0x9e>
mov    0x2e03(%rip),%rax
mov    (%rax),%ebx
mov    0x2df2(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1090 <strerror@plt>
lea    0xe57(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0xf9,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1284 <main+0x114>
mov    $0x1,%ebp
lea    0xe44(%rip),%r15
lea    0xe4f(%rip),%r12
mov    %rsp,%r13
nopl   0x0(%rax)
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <fflush@plt>
mov    %r13,%rdi
mov    $0xf9,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
add    $0x1,%ebp
test   %rax,%rax
jne    1240 <main+0xd0>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"x_data.in\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [38 x i8] c"Can't open x_data.in, errno: %d (%s)\0A\00"
@errno = global i32 0
@.str.3 = [25 x i8] c"job_rsc_fgets_std.output\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [41 x i8] c"Can't open fgets.output, errno: %d (%s)\0A\00"
@stdout = global i32 0
@.str.6 = [18 x i8] c"Read a line (%d)\0A\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [11 x i8] c"All done.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca [250 x i8]
store i32 0, i32* %1
store i32 0, i32* %4
%6 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp ne i32* %7, null
br i1 %8, label %16, label %9
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %11, i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %17, i32** %3
%18 = load i32*, i32** %3
%19 = icmp ne i32* %18, null
br i1 %19, label %27, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %22, i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %16
br label %28
28: ; preds = %33, %27
%29 = getelementptr inbounds [250 x i8], [250 x i8]* %5, i64 0, i64 0
%30 = load i32*, i32** %2
%31 = call i32* @fgets(i8* %29, i32 249, i32* %30)
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %45
33: ; preds = %28
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
store i32 %35, i32* %4
%36 = load i32, i32* @stdout
%37 = load i32, i32* %4
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %37)
%39 = load i32*, i32** %3
%40 = ptrtoint i32* %39 to i32
%41 = getelementptr inbounds [250 x i8], [250 x i8]* %5, i64 0, i64 0
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %41)
%43 = load i32*, i32** %3
%44 = call i32 @fflush(i32* %43)
br label %28
45: ; preds = %28
%46 = load i32*, i32** %2
%47 = call i32 @fclose(i32* %46)
%48 = load i32*, i32** %3
%49 = call i32 @fclose(i32* %48)
%50 = load i32, i32* @stdout
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
%52 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [10 x i8] c"x_data.in\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [38 x i8] c"Can't open x_data.in, errno: %d (%s)\0A\00"
@errno = global i32 0
@.str.3 = [25 x i8] c"job_rsc_fgets_std.output\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [41 x i8] c"Can't open fgets.output, errno: %d (%s)\0A\00"
@stdout = global i32 0
@.str.6 = [18 x i8] c"Read a line (%d)\0A\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [11 x i8] c"All done.\0A\00"
define i32 @main() {
%1 = alloca [250 x i8]
%2 = getelementptr inbounds [250 x i8], [250 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %7, i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %20, label %14
14: ; preds = %11
%15 = call i32* @fgets(i8* %2, i32 249, i32* %3)
%16 = icmp eq i32* %15, null
br i1 %16, label %35, label %17
17: ; preds = %14
%18 = ptrtoint i32* %12 to i64
%19 = trunc i64 %18 to i32
br label %26
20: ; preds = %11
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %22, i8* %23)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %17, %26
%27 = phi i32 [ 0, %17 ], [ %28, %26 ]
%28 = add nuw nsw i32 %27, 1
%29 = load i32, i32* @stdout
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %28)
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%32 = call i32 @fflush(i32* %12)
%33 = call i32* @fgets(i8* %2, i32 249, i32* %3)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %26
35: ; preds = %26, %14
%36 = call i32 @fclose(i32* %3)
%37 = call i32 @fclose(i32* %12)
%38 = load i32, i32* @stdout
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
%40 = call i32 @exit(i32 0)
unreachable
}