#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 rb {
  int dummy;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ ENOBUFS;
int /*<<< orphan*/ errno;
scalar_t__ pop(struct rb *, char **);
scalar_t__ rb_is_full(struct rb *);
scalar_t__ top_up(struct rb *, int);
ssize_t get_line(int fd, struct rb *rb, char **string) {
  ssize_t y, z;
  y = top_up(rb, fd);
  if (y < 0)
    return y;
  z = pop(rb, string);
  if (z != 0)
    return z;
  if (rb_is_full(rb))
    errno = ENOBUFS;
  else if (y > 0)
    errno = EAGAIN;
  else
    return 0;
  return -1;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <top_up@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    116b <get_line+0x3b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11fa <get_line+0xca>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <pop@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1194 <get_line+0x64>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11fa <get_line+0xca>
mov    -0x18(%rbp),%rdi
call   1040 <rb_is_full@plt>
cmp    $0x0,%rax
je     11be <get_line+0x8e>
mov    0x2e4a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e21(%rip),%rax
mov    %ecx,(%rax)
jmp    11f2 <get_line+0xc2>
cmpq   $0x0,-0x28(%rbp)
jle    11e0 <get_line+0xb0>
mov    0x2df8(%rip),%rax
mov    (%rax),%ecx
mov    0x2dff(%rip),%rax
mov    %ecx,(%rax)
jmp    11ed <get_line+0xbd>
movq   $0x0,-0x8(%rbp)
jmp    11fa <get_line+0xca>
jmp    11f2 <get_line+0xc2>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%esi
mov    %rbx,%rdi
call   1030 <top_up@plt>
mov    %rax,%r14
test   %rax,%rax
js     1173 <get_line+0x43>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <pop@plt>
test   %rax,%rax
jne    1196 <get_line+0x66>
mov    %rbx,%rdi
call   1040 <rb_is_full@plt>
test   %rax,%rax
je     1178 <get_line+0x48>
mov    0x2e87(%rip),%rax
jmp    1184 <get_line+0x54>
mov    %r14,%rax
jmp    1196 <get_line+0x66>
test   %r14,%r14
je     119c <get_line+0x6c>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    0x2e4b(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
jmp    1196 <get_line+0x66>

LLVM IR (-O0):
%struct.rb = type { i32 }
@ENOBUFS = global i32 0
@errno = global i32 0
@EAGAIN = global i32 0
define i64 @get_line(i32 %0, %struct.rb* %1, i8** %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca %struct.rb*
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store %struct.rb* %1, %struct.rb** %6
store i8** %2, i8*** %7
%10 = load %struct.rb*, %struct.rb** %6
%11 = load i32, i32* %5
%12 = call i64 @top_up(%struct.rb* %10, i32 %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i64, i64* %8
store i64 %16, i64* %4
br label %39
17: ; preds = %3
%18 = load %struct.rb*, %struct.rb** %6
%19 = load i8**, i8*** %7
%20 = call i64 @pop(%struct.rb* %18, i8** %19)
store i64 %20, i64* %9
%21 = load i64, i64* %9
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i64, i64* %9
store i64 %24, i64* %4
br label %39
25: ; preds = %17
%26 = load %struct.rb*, %struct.rb** %6
%27 = call i64 @rb_is_full(%struct.rb* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i32, i32* @ENOBUFS
store i32 %30, i32* @errno
br label %38
31: ; preds = %25
%32 = load i64, i64* %8
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load i32, i32* @EAGAIN
store i32 %35, i32* @errno
br label %37
36: ; preds = %31
store i64 0, i64* %4
br label %39
37: ; preds = %34
br label %38
38: ; preds = %37, %29
store i64 -1, i64* %4
br label %39
39: ; preds = %38, %36, %23, %15
%40 = load i64, i64* %4
ret i64 %40
}

LLVM IR (-O2):
%struct.rb = type { i32 }
@ENOBUFS = global i32 0
@errno = global i32 0
@EAGAIN = global i32 0
define i64 @get_line(i32 %0, %struct.rb* %1, i8** %2) {
%4 = tail call i64 @top_up(%struct.rb* %1, i32 %0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = tail call i64 @pop(%struct.rb* %1, i8** %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %6
%10 = tail call i64 @rb_is_full(%struct.rb* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = icmp eq i64 %4, 0
br i1 %13, label %17, label %14
14: ; preds = %12, %9
%15 = phi i32* [ @ENOBUFS, %9 ], [ @EAGAIN, %12 ]
%16 = load i32, i32* %15
store i32 %16, i32* @errno
br label %17
17: ; preds = %12, %6, %3, %14
%18 = phi i64 [ -1, %14 ], [ %4, %3 ], [ %7, %6 ], [ 0, %12 ]
ret i64 %18
}