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

/* Variables and functions */
int errno;
int /*<<< orphan*/ mrb_alloc(struct mrb *, int);
char *mrb_maxsize(struct mrb *, int *);
int read(int, char *, int);
int mrb_read_max(struct mrb *q, int fd) {
  int free_space;
  char *buf = mrb_maxsize(q, &free_space);
  if (free_space == 0)
    return 0;
  int nread = read(fd, buf, free_space);
  if (nread <= 0)
    return errno;
  mrb_alloc(q, nread);
  return 0;
}


x86 assembly (clang, -O0):
<mrb_read_max>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <mrb_maxsize@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1166 <mrb_read_max+0x36>
movl   $0x0,-0x4(%rbp)
jmp    11a6 <mrb_read_max+0x76>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     1193 <mrb_read_max+0x63>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11a6 <mrb_read_max+0x76>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <mrb_alloc@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mrb_read_max>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xc(%rsp),%rsi
call   1040 <mrb_maxsize@plt>
mov    0xc(%rsp),%edx
xor    %ebp,%ebp
test   %edx,%edx
je     1176 <mrb_read_max+0x46>
mov    %r14d,%edi
mov    %rax,%rsi
call   1030 <read@plt>
test   %eax,%eax
jle    116d <mrb_read_max+0x3d>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <mrb_alloc@plt>
jmp    1176 <mrb_read_max+0x46>
mov    0x2e6c(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.mrb = type { i32 }
@errno = global i32 0
define i32 @mrb_read_max(%struct.mrb* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.mrb*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store %struct.mrb* %0, %struct.mrb** %4
store i32 %1, i32* %5
%9 = load %struct.mrb*, %struct.mrb** %4
%10 = call i8* @mrb_maxsize(%struct.mrb* %9, i32* %6)
store i8* %10, i8** %7
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 0, i32* %3
br label %27
14: ; preds = %2
%15 = load i32, i32* %5
%16 = load i8*, i8** %7
%17 = load i32, i32* %6
%18 = call i32 @read(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load i32, i32* @errno
store i32 %22, i32* %3
br label %27
23: ; preds = %14
%24 = load %struct.mrb*, %struct.mrb** %4
%25 = load i32, i32* %8
%26 = call i32 @mrb_alloc(%struct.mrb* %24, i32 %25)
store i32 0, i32* %3
br label %27
27: ; preds = %23, %21, %13
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.mrb = type { i32 }
@errno = global i32 0
define i32 @mrb_read_max(%struct.mrb* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i8* @mrb_maxsize(%struct.mrb* %0, i32* %3)
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %2
%9 = call i32 @read(i32 %1, i8* %5, i32 %6)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = load i32, i32* @errno
br label %15
13: ; preds = %8
%14 = call i32 @mrb_alloc(%struct.mrb* %0, i32 %9)
br label %15
15: ; preds = %11, %13, %2
%16 = phi i32 [ 0, %2 ], [ %12, %11 ], [ 0, %13 ]
ret i32 %16
}