#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*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int strlen(char *);
char *read_next_line(char *filename, char reset) {
  static int last_pos = 0;
  int bytesread;
  char *line = NULL;
  char **pline = &line;
  FILE *file_fp;
  size_t initsize = 1;
  if (reset)
    last_pos = 0;
  if ((file_fp = fopen(filename, "r")) == NULL) {
    printf("Cannot open file: %s\n", filename);
    exit(2);
  }
  fseek(file_fp, last_pos, SEEK_SET);
  bytesread = getline(pline, &initsize, file_fp);
  line = *pline;
  if (bytesread == -1) {
    last_pos = 0;
    free(line);
    fclose(file_fp);
    return NULL;
  }
  last_pos = ftell(file_fp);
  fclose(file_fp);
  if (line[strlen(line) - 1] == '\n')
    line[strlen(line) - 1] = 0x00;
  return line;
}


x86 assembly (clang, -O0):
<read_next_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x1,-0x38(%rbp)
cmpb   $0x0,-0x11(%rbp)
je     11ce <read_next_line+0x3e>
movl   $0x0,0x2e9e(%rip)
mov    -0x10(%rbp),%rdi
lea    0xe27(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1206 <read_next_line+0x76>
mov    -0x10(%rbp),%rsi
lea    0xe0b(%rip),%rdi
call   1060 <printf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    0x2e5c(%rip),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rsi
call   10a0 <getline@plt>
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1270 <read_next_line+0xe0>
movl   $0x0,0x2e1b(%rip)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12db <read_next_line+0x14b>
mov    -0x30(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,0x2ded(%rip)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12d3 <read_next_line+0x143>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_next_line>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
movq   $0x1,0x10(%rsp)
test   %sil,%sil
je     11bb <read_next_line+0x2b>
movl   $0x0,0x2eb1(%rip)
lea    0xe3e(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1271 <read_next_line+0xe1>
mov    %rax,%rbx
mov    0x2e90(%rip),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1080 <fseek@plt>
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     124b <read_next_line+0xbb>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %eax,0x2e5a(%rip)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1244 <read_next_line+0xb4>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    0x8(%rsp),%rax
jmp    1269 <read_next_line+0xd9>
movl   $0x0,0x2e17(%rip)
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd8a(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@read_next_line.last_pos = internal global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"Cannot open file: %s\0A\00"
@SEEK_SET = global i32 0
define i8* @read_next_line(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca i64
store i8* %0, i8** %4
store i8 %1, i8* %5
store i8* null, i8** %7
store i8** %7, i8*** %8
store i64 1, i64* %10
%11 = load i8, i8* %5
%12 = icmp ne i8 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 0, i32* @read_next_line.last_pos
br label %14
14: ; preds = %13, %2
%15 = load i8*, i8** %4
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %9
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i8*, i8** %4
%20 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %14
%23 = load i32*, i32** %9
%24 = load i32, i32* @read_next_line.last_pos
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @fseek(i32* %23, i32 %24, i32 %25)
%27 = load i8**, i8*** %8
%28 = load i32*, i32** %9
%29 = call i32 @getline(i8** %27, i64* %10, i32* %28)
store i32 %29, i32* %6
%30 = load i8**, i8*** %8
%31 = load i8*, i8** %30
store i8* %31, i8** %7
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %39
34: ; preds = %22
store i32 0, i32* @read_next_line.last_pos
%35 = load i8*, i8** %7
%36 = call i32 @free(i8* %35)
%37 = load i32*, i32** %9
%38 = call i32 @fclose(i32* %37)
store i8* null, i8** %3
br label %62
39: ; preds = %22
%40 = load i32*, i32** %9
%41 = call i32 @ftell(i32* %40)
store i32 %41, i32* @read_next_line.last_pos
%42 = load i32*, i32** %9
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %7
%45 = load i8*, i8** %7
%46 = call i32 @strlen(i8* %45)
%47 = sub nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %44, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 10
br i1 %52, label %53, label %60
53: ; preds = %39
%54 = load i8*, i8** %7
%55 = load i8*, i8** %7
%56 = call i32 @strlen(i8* %55)
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %54, i64 %58
store i8 0, i8* %59
br label %60
60: ; preds = %53, %39
%61 = load i8*, i8** %7
store i8* %61, i8** %3
br label %62
62: ; preds = %60, %34
%63 = load i8*, i8** %3
ret i8* %63
}

LLVM IR (-O2):
@read_next_line.last_pos = internal unnamed_addr global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"Cannot open file: %s\0A\00"
@SEEK_SET = global i32 0
define i8* @read_next_line(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast i64* %4 to i8*
store i64 1, i64* %4
%7 = icmp eq i8 %1, 0
br i1 %7, label %9, label %8
8: ; preds = %2
store i32 0, i32* @read_next_line.last_pos
br label %9
9: ; preds = %8, %2
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = tail call i32 @exit(i32 2)
unreachable
15: ; preds = %9
%16 = load i32, i32* @read_next_line.last_pos
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %10, i32 %16, i32 %17)
%19 = call i32 @getline(i8** %3, i64* %4, i32* %10)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %15
store i32 0, i32* @read_next_line.last_pos
%22 = load i8*, i8** %3
%23 = call i32 @free(i8* %22)
%24 = call i32 @fclose(i32* %10)
br label %43
25: ; preds = %15
%26 = call i32 @ftell(i32* %10)
store i32 %26, i32* @read_next_line.last_pos
%27 = call i32 @fclose(i32* %10)
%28 = load i8*, i8** %3
%29 = call i32 @strlen(i8* %28)
%30 = add nsw i32 %29, -1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 10
br i1 %34, label %35, label %41
35: ; preds = %25
%36 = load i8*, i8** %3
%37 = call i32 @strlen(i8* %36)
%38 = add nsw i32 %37, -1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %35, %25
%42 = load i8*, i8** %3
br label %43
43: ; preds = %41, %21
%44 = phi i8* [ null, %21 ], [ %42, %41 ]
ret i8* %44
}