#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 filebuf {
  int dummy;
};

/* Variables and functions */
int av_filebuf_check(struct filebuf **, int, long);
scalar_t__ av_filebuf_eof(struct filebuf *);
int av_filebuf_readline(struct filebuf *, char **);
int av_filebuf_getline(struct filebuf *fb, char **linep, long timeoutms) {
  int res;
  char *line;
  *linep = ((void *)0);
  while (1) {
    res = av_filebuf_readline(fb, &line);
    if (res < 0)
      return res;
    if (res == 1)
      break;
    if (av_filebuf_eof(fb))
      return 1;
    res = av_filebuf_check(&fb, 1, timeoutms);
    if (res < 0)
      return res;
    if (res == 0)
      return 0;
  }
  *linep = line;
  return 1;
}


x86 assembly (clang, -O0):
<av_filebuf_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <av_filebuf_readline@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1174 <av_filebuf_getline+0x44>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f9 <av_filebuf_getline+0xc9>
cmpl   $0x1,-0x24(%rbp)
jne    1183 <av_filebuf_getline+0x53>
jmp    11e7 <av_filebuf_getline+0xb7>
mov    -0x10(%rbp),%rdi
call   1030 <av_filebuf_eof@plt>
cmp    $0x0,%rax
je     11a2 <av_filebuf_getline+0x72>
movl   $0x1,-0x4(%rbp)
jmp    11f9 <av_filebuf_getline+0xc9>
mov    -0x20(%rbp),%rdx
lea    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1040 <av_filebuf_check@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11cc <av_filebuf_getline+0x9c>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f9 <av_filebuf_getline+0xc9>
cmpl   $0x0,-0x24(%rbp)
jne    11e2 <av_filebuf_getline+0xb2>
movl   $0x0,-0x4(%rbp)
jmp    11f9 <av_filebuf_getline+0xc9>
jmp    114f <av_filebuf_getline+0x1f>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<av_filebuf_getline>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,0x8(%rsp)
movq   $0x0,(%rsi)
lea    0x10(%rsp),%rsi
call   1050 <av_filebuf_readline@plt>
test   %eax,%eax
js     11be <av_filebuf_getline+0x8e>
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x1,%eax
je     11ad <av_filebuf_getline+0x7d>
mov    0x8(%rsp),%rdi
call   1030 <av_filebuf_eof@plt>
test   %rax,%rax
jne    11b5 <av_filebuf_getline+0x85>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   1040 <av_filebuf_check@plt>
test   %eax,%eax
js     11be <av_filebuf_getline+0x8e>
je     11bc <av_filebuf_getline+0x8c>
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
call   1050 <av_filebuf_readline@plt>
test   %eax,%eax
jns    1170 <av_filebuf_getline+0x40>
jmp    11be <av_filebuf_getline+0x8e>
mov    0x10(%rsp),%rax
mov    %rax,(%r14)
mov    $0x1,%eax
jmp    11be <av_filebuf_getline+0x8e>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.filebuf = type { i32 }
define i32 @av_filebuf_getline(%struct.filebuf* %0, i8** %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.filebuf*
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store %struct.filebuf* %0, %struct.filebuf** %5
store i8** %1, i8*** %6
store i64 %2, i64* %7
%10 = load i8**, i8*** %6
store i8* null, i8** %10
br label %11
11: ; preds = %3, %38
%12 = load %struct.filebuf*, %struct.filebuf** %5
%13 = call i32 @av_filebuf_readline(%struct.filebuf* %12, i8** %9)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = load i32, i32* %8
store i32 %17, i32* %4
br label %42
18: ; preds = %11
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %22
21: ; preds = %18
br label %39
22: ; preds = %18
%23 = load %struct.filebuf*, %struct.filebuf** %5
%24 = call i64 @av_filebuf_eof(%struct.filebuf* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 1, i32* %4
br label %42
27: ; preds = %22
%28 = load i64, i64* %7
%29 = call i32 @av_filebuf_check(%struct.filebuf** %5, i32 1, i64 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = load i32, i32* %8
store i32 %33, i32* %4
br label %42
34: ; preds = %27
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34
store i32 0, i32* %4
br label %42
38: ; preds = %34
br label %11
39: ; preds = %21
%40 = load i8*, i8** %9
%41 = load i8**, i8*** %6
store i8* %40, i8** %41
store i32 1, i32* %4
br label %42
42: ; preds = %39, %37, %32, %26, %16
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
%struct.filebuf = type { i32 }
define i32 @av_filebuf_getline(%struct.filebuf* %0, i8** nocapture writeonly %1, i64 %2) {
%4 = alloca %struct.filebuf*
%5 = alloca i8*
store %struct.filebuf* %0, %struct.filebuf** %4
%6 = bitcast i8** %5 to i8*
store i8* null, i8** %1
%7 = call i32 @av_filebuf_readline(%struct.filebuf* %0, i8** %5)
%8 = icmp slt i32 %7, 0
br i1 %8, label %27, label %9
9: ; preds = %3, %21
%10 = phi i32 [ %23, %21 ], [ %7, %3 ]
%11 = icmp eq i32 %10, 1
br i1 %11, label %25, label %12
12: ; preds = %9
%13 = load %struct.filebuf*, %struct.filebuf** %4
%14 = call i64 @av_filebuf_eof(%struct.filebuf* %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %12
%17 = call i32 @av_filebuf_check(%struct.filebuf** %4, i32 1, i64 %2)
%18 = icmp slt i32 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %16
%20 = icmp eq i32 %17, 0
br i1 %20, label %27, label %21
21: ; preds = %19
%22 = load %struct.filebuf*, %struct.filebuf** %4
%23 = call i32 @av_filebuf_readline(%struct.filebuf* %22, i8** %5)
%24 = icmp slt i32 %23, 0
br i1 %24, label %27, label %9
25: ; preds = %9
%26 = load i8*, i8** %5
store i8* %26, i8** %1
br label %27
27: ; preds = %21, %12, %16, %19, %3, %25
%28 = phi i32 [ 1, %25 ], [ %7, %3 ], [ 0, %19 ], [ %17, %16 ], [ 1, %12 ], [ %23, %21 ]
ret i32 %28
}