#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*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ luaL_addsize(int);
char *luaL_openspace(int);
int strlen(char *);
int read_line(FILE *f) {
  int n;
  char *b;
  do {
    b = luaL_openspace(1024);
    if (!fgets(b, 1024, f))
      return 0;
    n = strlen(b);
    luaL_addsize(n);
  } while (b[n - 1] != '\n');
  luaL_addsize(-1);
  return 1;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x400,%edi
call   1030 <luaL_openspace@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    1181 <read_line+0x41>
movl   $0x0,-0x4(%rbp)
jmp    11c0 <read_line+0x80>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <luaL_addsize@plt>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    114c <read_line+0xc>
mov    $0xffffffff,%edi
call   1060 <luaL_addsize@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
nopw   0x0(%rax,%rax,1)
mov    $0x400,%edi
call   1030 <luaL_openspace@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %eax,%eax
je     119d <read_line+0x5d>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <luaL_addsize@plt>
movslq %ebp,%rax
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1150 <read_line+0x10>
mov    $0xffffffff,%edi
call   1060 <luaL_addsize@plt>
mov    $0x1,%eax
jmp    119f <read_line+0x5f>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_line(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
store i32* %0, i32** %3
br label %6
6: ; preds = %18, %1
%7 = call i8* @luaL_openspace(i32 1024)
store i8* %7, i8** %5
%8 = load i8*, i8** %5
%9 = load i32*, i32** %3
%10 = call i32 @fgets(i8* %8, i32 1024, i32* %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %6
store i32 0, i32* %2
br label %29
13: ; preds = %6
%14 = load i8*, i8** %5
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = call i32 @luaL_addsize(i32 %16)
br label %18
18: ; preds = %13
%19 = load i8*, i8** %5
%20 = load i32, i32* %4
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 10
br i1 %26, label %6, label %27
27: ; preds = %18
%28 = call i32 @luaL_addsize(i32 -1)
store i32 1, i32* %2
br label %29
29: ; preds = %27, %12
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
define i32 @read_line(i32* nocapture %0) {
br label %2
2: ; preds = %6, %1
%3 = tail call i8* @luaL_openspace(i32 1024)
%4 = tail call i32 @fgets(i8* %3, i32 1024, i32* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %16, label %6
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %3)
%8 = tail call i32 @luaL_addsize(i32 %7)
%9 = add nsw i32 %7, -1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %3, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %2
14: ; preds = %6
%15 = tail call i32 @luaL_addsize(i32 -1)
br label %16
16: ; preds = %2, %14
%17 = phi i32 [ 1, %14 ], [ 0, %2 ]
ret i32 %17
}