#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*/ uint8_t;
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ max(size_t, size_t);
int read(int /*<<< orphan*/, int /*<<< orphan*/ *, size_t);
size_t round_up(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
scalar_t__ xrealloc(int /*<<< orphan*/ *, size_t);
size_t read_once(uint8_t **buf, size_t *capacity, size_t *bytes_read,
                 size_t bytes_requested, const char *argv0) {
  if (*bytes_read + bytes_requested + 1 > *capacity) {
    size_t new_capacity = round_up(
        max(*bytes_read + bytes_requested + 1, *capacity + (*capacity >> 2)));
    *buf = (uint8_t *)xrealloc(*buf, new_capacity);
    *capacity = new_capacity;
  }
  while (1) {
    ssize_t r = read(STDIN_FILENO, *buf + *bytes_read, bytes_requested);
    if (r == 0) {
      return 0;
    } else if (r == -1) {
      if (errno == EAGAIN || errno == EINTR)
        continue;
      else {
        fprintf(stderr, "%s: could not read stdin: %s\n", argv0,
                strerror(errno));
        exit(1);
      }
    } else {
      *bytes_read += r;
      return r;
    }
  }
}


x86 assembly (clang, -O0):
<read_once>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
add    -0x28(%rbp),%rax
add    $0x1,%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jbe    1201 <read_once+0x91>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
shr    $0x2,%rax
add    %rax,%rsi
call   1040 <max@plt>
mov    %eax,%edi
call   1030 <round_up@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <xrealloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1206 <read_once+0x96>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    1247 <read_once+0xd7>
movq   $0x0,-0x8(%rbp)
jmp    12e3 <read_once+0x173>
cmpl   $0xffffffff,-0x3c(%rbp)
jne    12cd <read_once+0x15d>
mov    0x2d80(%rip),%rax
mov    (%rax),%rax
mov    0x2d56(%rip),%rcx
cmp    (%rcx),%rax
je     1285 <read_once+0x115>
mov    0x2d66(%rip),%rax
mov    (%rax),%rax
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%rax
jne    128a <read_once+0x11a>
jmp    1206 <read_once+0x96>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d33(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd42(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movslq -0x3c(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
movslq -0x3c(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_once>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,(%rsp)
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rdi,%rbx
mov    (%rdx),%rax
lea    (%rcx,%rax,1),%rdi
add    $0x1,%rdi
mov    (%rsi),%rax
cmp    %rax,%rdi
jbe    11c9 <read_once+0x59>
mov    %rsi,%r13
mov    %rax,%rsi
shr    $0x2,%rsi
add    %rax,%rsi
call   1040 <max@plt>
mov    %eax,%edi
call   1030 <round_up@plt>
mov    %rax,%rbp
mov    (%rbx),%rdi
mov    %rax,%rsi
call   1050 <xrealloc@plt>
mov    %rax,(%rbx)
mov    %rbp,0x0(%r13)
mov    0x2df8(%rip),%rbp
mov    0x2e01(%rip),%r13
mov    0x2dda(%rip),%r14
xchg   %ax,%ax
mov    0x0(%rbp),%edi
mov    (%r15),%rsi
shl    $0x2,%rsi
add    (%rbx),%rsi
mov    %r12,%rdx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    123c <read_once+0xcc>
mov    0x0(%r13),%rdi
cmp    (%r14),%rdi
je     11e0 <read_once+0x70>
mov    0x2dc6(%rip),%rax
cmp    (%rax),%rdi
je     11e0 <read_once+0x70>
mov    0x2dca(%rip),%rax
mov    (%rax),%ebx
call   1090 <strerror@plt>
lea    0xddc(%rip),%rsi
mov    %ebx,%edi
mov    (%rsp),%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
test   %eax,%eax
jne    1244 <read_once+0xd4>
xor    %eax,%eax
jmp    1249 <read_once+0xd9>
cltq
add    %rax,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [30 x i8] c"%s: could not read stdin: %s\0A\00"
define i64 @read_once(i32** %0, i64* %1, i64* %2, i64 %3, i8* %4) {
%6 = alloca i64
%7 = alloca i32**
%8 = alloca i64*
%9 = alloca i64*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i32** %0, i32*** %7
store i64* %1, i64** %8
store i64* %2, i64** %9
store i64 %3, i64* %10
store i8* %4, i8** %11
%14 = load i64*, i64** %9
%15 = load i64, i64* %14
%16 = load i64, i64* %10
%17 = add i64 %15, %16
%18 = add i64 %17, 1
%19 = load i64*, i64** %8
%20 = load i64, i64* %19
%21 = icmp ugt i64 %18, %20
br i1 %21, label %22, label %44
22: ; preds = %5
%23 = load i64*, i64** %9
%24 = load i64, i64* %23
%25 = load i64, i64* %10
%26 = add i64 %24, %25
%27 = add i64 %26, 1
%28 = load i64*, i64** %8
%29 = load i64, i64* %28
%30 = load i64*, i64** %8
%31 = load i64, i64* %30
%32 = lshr i64 %31, 2
%33 = add i64 %29, %32
%34 = call i32 @max(i64 %27, i64 %33)
%35 = call i64 @round_up(i32 %34)
store i64 %35, i64* %12
%36 = load i32**, i32*** %7
%37 = load i32*, i32** %36
%38 = load i64, i64* %12
%39 = call i64 @xrealloc(i32* %37, i64 %38)
%40 = inttoptr i64 %39 to i32*
%41 = load i32**, i32*** %7
store i32* %40, i32** %41
%42 = load i64, i64* %12
%43 = load i64*, i64** %8
store i64 %42, i64* %43
br label %44
44: ; preds = %22, %5
br label %45
45: ; preds = %44, %68
%46 = load i32, i32* @STDIN_FILENO
%47 = load i32**, i32*** %7
%48 = load i32*, i32** %47
%49 = load i64*, i64** %9
%50 = load i64, i64* %49
%51 = getelementptr inbounds i32, i32* %48, i64 %50
%52 = load i64, i64* %10
%53 = call i32 @read(i32 %46, i32* %51, i64 %52)
store i32 %53, i32* %13
%54 = load i32, i32* %13
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %45
store i64 0, i64* %6
br label %84
57: ; preds = %45
%58 = load i32, i32* %13
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %76
60: ; preds = %57
%61 = load i64, i64* @errno
%62 = load i64, i64* @EAGAIN
%63 = icmp eq i64 %61, %62
br i1 %63, label %68, label %64
64: ; preds = %60
%65 = load i64, i64* @errno
%66 = load i64, i64* @EINTR
%67 = icmp eq i64 %65, %66
br i1 %67, label %68, label %69
68: ; preds = %64, %60
br label %45
69: ; preds = %64
%70 = load i32, i32* @stderr
%71 = load i8*, i8** %11
%72 = load i64, i64* @errno
%73 = call i8* @strerror(i64 %72)
%74 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %71, i8* %73)
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %57
%77 = load i32, i32* %13
%78 = sext i32 %77 to i64
%79 = load i64*, i64** %9
%80 = load i64, i64* %79
%81 = add i64 %80, %78
store i64 %81, i64* %79
%82 = load i32, i32* %13
%83 = sext i32 %82 to i64
store i64 %83, i64* %6
br label %84
84: ; preds = %76, %56
%85 = load i64, i64* %6
ret i64 %85
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [30 x i8] c"%s: could not read stdin: %s\0A\00"
define i64 @read_once(i32** nocapture %0, i64* nocapture %1, i64* nocapture %2, i64 %3, i8* %4) {
%6 = load i64, i64* %2
%7 = add i64 %3, 1
%8 = add i64 %7, %6
%9 = load i64, i64* %1
%10 = icmp ugt i64 %8, %9
br i1 %10, label %11, label %19
11: ; preds = %5
%12 = lshr i64 %9, 2
%13 = add i64 %12, %9
%14 = tail call i32 @max(i64 %8, i64 %13)
%15 = tail call i64 @round_up(i32 %14)
%16 = load i32*, i32** %0
%17 = tail call i64 @xrealloc(i32* %16, i64 %15)
%18 = inttoptr i64 %17 to i32*
store i32* %18, i32** %0
store i64 %15, i64* %1
br label %19
19: ; preds = %11, %5
br label %20
20: ; preds = %19, %26
%21 = load i32, i32* @STDIN_FILENO
%22 = load i32*, i32** %0
%23 = load i64, i64* %2
%24 = getelementptr inbounds i32, i32* %22, i64 %23
%25 = tail call i32 @read(i32 %21, i32* %24, i64 %3)
switch i32 %25, label %38 [
i32 0, label %42
i32 -1, label %26
]
26: ; preds = %20
%27 = load i64, i64* @errno
%28 = load i64, i64* @EAGAIN
%29 = icmp eq i64 %27, %28
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %27, %30
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %20, label %33
33: ; preds = %26
%34 = load i32, i32* @stderr
%35 = tail call i8* @strerror(i64 %27)
%36 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %35)
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %20
%39 = sext i32 %25 to i64
%40 = load i64, i64* %2
%41 = add i64 %40, %39
store i64 %41, i64* %2
br label %42
42: ; preds = %20, %38
%43 = phi i64 [ %39, %38 ], [ 0, %20 ]
ret i64 %43
}