#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 */
struct stat {
  scalar_t__ st_size;
};

/* Variables and functions */
int BUFSIZ;
scalar_t__ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
char *realloc(char *, int);
scalar_t__ stat(char const *, struct stat *);
char *strtok_r(char *, char *, char **);
char *read_file_lines(const char *path) {
  static char *buffer = NULL;
  static char *saveptr = NULL;
  char *token;
  if (buffer == NULL) {
    struct stat sb;
    if (stat(path, &sb) == EXIT_SUCCESS) {
      int fh = open(path, O_RDONLY);
      if (fh != -1) {
        if (sb.st_size > 0) {
          buffer = malloc(sb.st_size);
          if (buffer)
            read(fh, buffer, sb.st_size);
          close(fh);
        } else {
          buffer = malloc(BUFSIZ);
          int readed = read(fh, buffer, BUFSIZ);
          char *bufptr;
          while (readed == BUFSIZ) {
            bufptr = realloc(buffer, BUFSIZ);
            readed = read(fh, bufptr, BUFSIZ);
          }
          close(fh);
        }
      }
    }
    token = strtok_r(buffer, "\n", &saveptr);
  } else
    token = strtok_r(NULL, "\n", &saveptr);
  assert(buffer != NULL);
  if (token == NULL) {
    free(buffer);
    buffer = NULL;
    saveptr = NULL;
  }
  return token;
}


x86 assembly (clang, -O0):
<read_file_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,0x2ecc(%rip)
jne    12e0 <read_file_lines+0x150>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%rax
jne    12bd <read_file_lines+0x12d>
mov    -0x8(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     12b8 <read_file_lines+0x128>
cmpq   $0x0,-0x18(%rbp)
jle    1233 <read_file_lines+0xa3>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,0x2e6d(%rip)
cmpq   $0x0,0x2e65(%rip)
je     1226 <read_file_lines+0x96>
mov    -0x1c(%rbp),%edi
mov    0x2e55(%rip),%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
call   1060 <read@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
jmp    12b3 <read_file_lines+0x123>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1090 <malloc@plt>
mov    %rax,0x2e28(%rip)
mov    -0x1c(%rbp),%edi
mov    0x2e1e(%rip),%rsi
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
call   1060 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2d6b(%rip),%rcx
cmp    (%rcx),%eax
jne    12ab <read_file_lines+0x11b>
mov    0x2df4(%rip),%rdi
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
call   10a0 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
call   1060 <read@plt>
mov    %eax,-0x20(%rbp)
jmp    1263 <read_file_lines+0xd3>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
jmp    12b8 <read_file_lines+0x128>
jmp    12bd <read_file_lines+0x12d>
mov    0x2dac(%rip),%rdi
lea    0xd35(%rip),%rsi
lea    0x2da6(%rip),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x10(%rbp)
jmp    12fb <read_file_lines+0x16b>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd15(%rip),%rsi
lea    0x2d86(%rip),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,0x2d6d(%rip)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
jne    133d <read_file_lines+0x1ad>
mov    0x2d4e(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2d3e(%rip)
movq   $0x0,0x2d3b(%rip)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_lines>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmpq   $0x0,0x2ed0(%rip)
je     11b4 <read_file_lines+0x24>
lea    0xe57(%rip),%rsi
lea    0x2ec8(%rip),%rdx
xor    %edi,%edi
jmp    1230 <read_file_lines+0xa0>
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
jne    121b <read_file_lines+0x8b>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     121b <read_file_lines+0x8b>
mov    %eax,%ebx
mov    0x8(%rsp),%r14
test   %r14,%r14
jle    127e <read_file_lines+0xee>
mov    %r14d,%edi
call   1090 <malloc@plt>
mov    %rax,0x2e6e(%rip)
test   %rax,%rax
je     1214 <read_file_lines+0x84>
mov    %ebx,%edi
mov    %rax,%rsi
mov    %r14d,%edx
call   1060 <read@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e4e(%rip),%rdi
lea    0xdd7(%rip),%rsi
lea    0x2e48(%rip),%rdx
call   1050 <strtok_r@plt>
mov    %rax,%rbx
xor    %edi,%edi
cmpq   $0x0,0x2e2e(%rip)
setne  %dil
call   1070 <assert@plt>
test   %rbx,%rbx
jne    1272 <read_file_lines+0xe2>
mov    0x2e19(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2e09(%rip)
movq   $0x0,0x2e06(%rip)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d53(%rip),%rbp
mov    0x0(%rbp),%r14d
mov    %r14d,%edi
call   1090 <malloc@plt>
mov    %rax,0x2dd8(%rip)
mov    %ebx,%edi
mov    %rax,%rsi
mov    %r14d,%edx
call   1060 <read@plt>
cmp    0x0(%rbp),%eax
jne    1214 <read_file_lines+0x84>
mov    0x2dbb(%rip),%rdi
mov    %eax,%esi
call   10a0 <realloc@plt>
mov    0x0(%rbp),%edx
mov    %ebx,%edi
mov    %rax,%rsi
jmp    12a0 <read_file_lines+0x110>

LLVM IR (-O0):
%struct.stat = type { i64 }
@read_file_lines.buffer = internal global i8* null
@read_file_lines.saveptr = internal global i8* null
@EXIT_SUCCESS = global i64 0
@O_RDONLY = global i32 0
@BUFSIZ = global i32 0
@.str = [2 x i8] c"\0A\00"
define i8* @read_file_lines(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %2
%8 = load i8*, i8** @read_file_lines.buffer
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %69
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i64 @stat(i8* %11, %struct.stat* %4)
%13 = load i64, i64* @EXIT_SUCCESS
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %66
15: ; preds = %10
%16 = load i8*, i8** %2
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %65
21: ; preds = %15
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%23 = load i64, i64* %22
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %42
25: ; preds = %21
%26 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%27 = load i64, i64* %26
%28 = trunc i64 %27 to i32
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** @read_file_lines.buffer
%30 = load i8*, i8** @read_file_lines.buffer
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %39
32: ; preds = %25
%33 = load i32, i32* %5
%34 = load i8*, i8** @read_file_lines.buffer
%35 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%36 = load i64, i64* %35
%37 = trunc i64 %36 to i32
%38 = call i32 @read(i32 %33, i8* %34, i32 %37)
br label %39
39: ; preds = %32, %25
%40 = load i32, i32* %5
%41 = call i32 @close(i32 %40)
br label %64
42: ; preds = %21
%43 = load i32, i32* @BUFSIZ
%44 = call i8* @malloc(i32 %43)
store i8* %44, i8** @read_file_lines.buffer
%45 = load i32, i32* %5
%46 = load i8*, i8** @read_file_lines.buffer
%47 = load i32, i32* @BUFSIZ
%48 = call i32 @read(i32 %45, i8* %46, i32 %47)
store i32 %48, i32* %6
br label %49
49: ; preds = %53, %42
%50 = load i32, i32* %6
%51 = load i32, i32* @BUFSIZ
%52 = icmp eq i32 %50, %51
br i1 %52, label %53, label %61
53: ; preds = %49
%54 = load i8*, i8** @read_file_lines.buffer
%55 = load i32, i32* @BUFSIZ
%56 = call i8* @realloc(i8* %54, i32 %55)
store i8* %56, i8** %7
%57 = load i32, i32* %5
%58 = load i8*, i8** %7
%59 = load i32, i32* @BUFSIZ
%60 = call i32 @read(i32 %57, i8* %58, i32 %59)
store i32 %60, i32* %6
br label %49
61: ; preds = %49
%62 = load i32, i32* %5
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61, %39
br label %65
65: ; preds = %64, %15
br label %66
66: ; preds = %65, %10
%67 = load i8*, i8** @read_file_lines.buffer
%68 = call i8* @strtok_r(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** @read_file_lines.saveptr)
store i8* %68, i8** %3
br label %71
69: ; preds = %1
%70 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** @read_file_lines.saveptr)
store i8* %70, i8** %3
br label %71
71: ; preds = %69, %66
%72 = load i8*, i8** @read_file_lines.buffer
%73 = icmp ne i8* %72, null
%74 = zext i1 %73 to i32
%75 = call i32 @assert(i32 %74)
%76 = load i8*, i8** %3
%77 = icmp eq i8* %76, null
br i1 %77, label %78, label %81
78: ; preds = %71
%79 = load i8*, i8** @read_file_lines.buffer
%80 = call i32 @free(i8* %79)
store i8* null, i8** @read_file_lines.buffer
store i8* null, i8** @read_file_lines.saveptr
br label %81
81: ; preds = %78, %71
%82 = load i8*, i8** %3
ret i8* %82
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@read_file_lines.buffer = internal unnamed_addr global i8* null
@read_file_lines.saveptr = internal global i8* null
@EXIT_SUCCESS = global i64 0
@O_RDONLY = global i32 0
@BUFSIZ = global i32 0
@.str = [2 x i8] c"\0A\00"
define i8* @read_file_lines(i8* %0) {
%2 = alloca %struct.stat
%3 = load i8*, i8** @read_file_lines.buffer
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %43
5: ; preds = %1
%6 = bitcast %struct.stat* %2 to i8*
%7 = call i64 @stat(i8* %0, %struct.stat* %2)
%8 = load i64, i64* @EXIT_SUCCESS
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %40
10: ; preds = %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %0, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %40, label %14
14: ; preds = %10
%15 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%16 = load i64, i64* %15
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = trunc i64 %16 to i32
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** @read_file_lines.buffer
%21 = icmp eq i8* %20, null
br i1 %21, label %38, label %22
22: ; preds = %18
%23 = call i32 @read(i32 %12, i8* %20, i32 %19)
br label %38
24: ; preds = %14
%25 = load i32, i32* @BUFSIZ
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** @read_file_lines.buffer
%27 = call i32 @read(i32 %12, i8* %26, i32 %25)
%28 = load i32, i32* @BUFSIZ
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %38
30: ; preds = %24, %30
%31 = phi i32 [ %35, %30 ], [ %27, %24 ]
%32 = load i8*, i8** @read_file_lines.buffer
%33 = call i8* @realloc(i8* %32, i32 %31)
%34 = load i32, i32* @BUFSIZ
%35 = call i32 @read(i32 %12, i8* %33, i32 %34)
%36 = load i32, i32* @BUFSIZ
%37 = icmp eq i32 %35, %36
br i1 %37, label %30, label %38
38: ; preds = %30, %24, %18, %22
%39 = call i32 @close(i32 %12)
br label %40
40: ; preds = %38, %10, %5
%41 = load i8*, i8** @read_file_lines.buffer
%42 = call i8* @strtok_r(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** @read_file_lines.saveptr)
br label %45
43: ; preds = %1
%44 = tail call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** @read_file_lines.saveptr)
br label %45
45: ; preds = %43, %40
%46 = phi i8* [ %42, %40 ], [ %44, %43 ]
%47 = load i8*, i8** @read_file_lines.buffer
%48 = icmp ne i8* %47, null
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
%51 = icmp eq i8* %46, null
br i1 %51, label %52, label %55
52: ; preds = %45
%53 = load i8*, i8** @read_file_lines.buffer
%54 = call i32 @free(i8* %53)
store i8* null, i8** @read_file_lines.buffer
store i8* null, i8** @read_file_lines.saveptr
br label %55
55: ; preds = %52, %45
ret i8* %46
}