#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, char *);
int main(void) {
  FILE *fp = NULL;
  char *line = NULL;
  size_t len = 0;
  int read = 0;
  line = (char *)malloc(256 * sizeof(char));
  len = 256;
  memset(line, '\0', len);
  fp = fopen("test.txt", "r");
  if (fp == NULL) {
    free(line);
    return -1;
  }
  while ((read = getline(&line, &len, fp)) != -1) {
    printf("line = [%s]\n", line);
    memset(line, '\0', 256);
  }
  fclose(fp);
  free(line);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x100,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xe34(%rip),%rdi
lea    0xe36(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fc <main+0x8c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <main+0xec>
jmp    1201 <main+0x91>
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1243 <main+0xd3>
mov    -0x18(%rbp),%rsi
lea    0xde2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
jmp    1201 <main+0x91>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,0x8(%rsp)
movq   $0x100,0x10(%rsp)
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    0xe54(%rip),%rdi
lea    0xe56(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1237 <main+0xc7>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
je     1221 <main+0xb1>
lea    0xe2d(%rip),%r14
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1050 <printf@plt>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
jne    11f0 <main+0x80>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1244 <main+0xd4>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"line = [%s]\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
store i32* null, i32** %2
store i8* null, i8** %3
store i64 0, i64* %4
store i32 0, i32* %5
%6 = call i64 @malloc(i32 256)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %3
store i64 256, i64* %4
%8 = load i8*, i8** %3
%9 = load i64, i64* %4
%10 = trunc i64 %9 to i32
%11 = call i32 @memset(i8* %8, i8 signext 0, i32 %10)
%12 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %2
%13 = load i32*, i32** %2
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = load i8*, i8** %3
%17 = call i32 @free(i8* %16)
store i32 -1, i32* %1
br label %33
18: ; preds = %0
br label %19
19: ; preds = %23, %18
%20 = load i32*, i32** %2
%21 = call i32 @getline(i8** %3, i64* %4, i32* %20)
store i32 %21, i32* %5
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i8*, i8** %3
%25 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %3
%27 = call i32 @memset(i8* %26, i8 signext 0, i32 256)
br label %19
28: ; preds = %19
%29 = load i32*, i32** %2
%30 = call i32 @fclose(i32* %29)
%31 = load i8*, i8** %3
%32 = call i32 @free(i8* %31)
store i32 0, i32* %1
br label %33
33: ; preds = %28, %15
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"line = [%s]\0A\00"
define i32 @main() {
%1 = alloca i8*
%2 = alloca i64
%3 = bitcast i8** %1 to i8*
%4 = bitcast i64* %2 to i8*
%5 = tail call i64 @malloc(i32 256)
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %1
store i64 256, i64* %2
%7 = tail call i32 @memset(i8* %6, i8 signext 0, i32 256)
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %0
%11 = call i32 @getline(i8** %1, i64* %2, i32* %8)
%12 = icmp eq i32 %11, -1
br i1 %12, label %22, label %15
13: ; preds = %0
%14 = tail call i32 @free(i8* %6)
br label %26
15: ; preds = %10, %15
%16 = load i8*, i8** %1
%17 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = load i8*, i8** %1
%19 = call i32 @memset(i8* %18, i8 signext 0, i32 256)
%20 = call i32 @getline(i8** %1, i64* %2, i32* %8)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %15
22: ; preds = %15, %10
%23 = call i32 @fclose(i32* %8)
%24 = load i8*, i8** %1
%25 = call i32 @free(i8* %24)
br label %26
26: ; preds = %22, %13
%27 = phi i32 [ -1, %13 ], [ 0, %22 ]
ret i32 %27
}