#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 */

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ error(char *, char const *);
int /*<<< orphan*/ exit(int);
char *malloc(size_t);
int /*<<< orphan*/ perror(char const *);
int read(int /*<<< orphan*/, char *, int);
char *realloc(char *, size_t);
char *getstr(const char *err) {
  size_t buf_len = 100;
  char *buf = malloc(buf_len), *end = buf;
  if (!buf) {
    error("Out of space reading %s\n", err);
    exit(1);
  }
  do {
    int rd = read(STDIN_FILENO, end, 1);
    if (rd <= 0) {
      if (rd == 0)
        error("EOF reading %s\n", err);
      else
        perror(err);
      exit(1);
    }
    end += rd;
    if ((buf + buf_len - end) < (buf_len >> 3)) {
      size_t end_offs = end - buf;
      buf_len += buf_len;
      buf = realloc(buf, buf_len);
      if (!buf) {
        error("Out of space reading %s\n", err);
        exit(1);
      }
      end = buf + end_offs;
    }
  } while (*(end - 1));
  return buf;
}


x86 assembly (clang, -O0):
<getstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x64,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ae <getstr+0x4e>
mov    -0x8(%rbp),%rsi
lea    0xe61(%rip),%rdi
call   1060 <error@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11b3 <getstr+0x53>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     1209 <getstr+0xa9>
cmpl   $0x0,-0x24(%rbp)
jne    11f6 <getstr+0x96>
mov    -0x8(%rbp),%rsi
lea    0xe2d(%rip),%rdi
call   1060 <error@plt>
jmp    11ff <getstr+0x9f>
mov    -0x8(%rbp),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    -0x10(%rbp),%rcx
shr    $0x3,%rcx
cmp    %rcx,%rax
jae    1297 <getstr+0x137>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    128b <getstr+0x12b>
mov    -0x8(%rbp),%rsi
lea    0xd84(%rip),%rdi
call   1060 <error@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    129c <getstr+0x13c>
mov    -0x20(%rbp),%rax
cmpb   $0x0,-0x1(%rax)
jne    11b3 <getstr+0x53>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
mov    $0x64,%r15d
mov    $0x64,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1212 <getstr+0xb2>
mov    %rax,%rbx
mov    0x2e51(%rip),%r13
mov    %rax,%r12
jmp    11a6 <getstr+0x46>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmpb   $0x0,-0x1(%rbx)
je     11f1 <getstr+0x91>
mov    0x0(%r13),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    11fe <getstr+0x9e>
mov    %eax,%eax
add    %rax,%rbx
lea    (%r12,%r15,1),%rax
sub    %rbx,%rax
mov    %r15,%rcx
shr    $0x3,%rcx
cmp    %rcx,%rax
jae    11a0 <getstr+0x40>
add    %r15,%r15
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     1212 <getstr+0xb2>
sub    %r12,%rbx
add    %rax,%rbx
mov    %rax,%r12
jmp    11a0 <getstr+0x40>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
je     122b <getstr+0xcb>
mov    %r14,%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xde7(%rip),%rdi
mov    %r14,%rsi
call   1060 <error@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xde7(%rip),%rdi
jmp    1219 <getstr+0xb9>

LLVM IR (-O0):
@.str = [25 x i8] c"Out of space reading %s\0A\00"
@STDIN_FILENO = global i32 0
@.str.1 = [16 x i8] c"EOF reading %s\0A\00"
define i8* @getstr(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %2
store i64 100, i64* %3
%8 = load i64, i64* %3
%9 = call i8* @malloc(i64 %8)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
store i8* %10, i8** %5
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %17, label %13
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
br label %18
18: ; preds = %73, %17
%19 = load i32, i32* @STDIN_FILENO
%20 = load i8*, i8** %5
%21 = call i32 @read(i32 %19, i8* %20, i32 1)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %18
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %2
%29 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %33
30: ; preds = %24
%31 = load i8*, i8** %2
%32 = call i32 @perror(i8* %31)
br label %33
33: ; preds = %30, %27
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %18
%36 = load i32, i32* %6
%37 = load i8*, i8** %5
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8* %39, i8** %5
%40 = load i8*, i8** %4
%41 = load i64, i64* %3
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = load i8*, i8** %5
%44 = ptrtoint i8* %42 to i64
%45 = ptrtoint i8* %43 to i64
%46 = sub i64 %44, %45
%47 = load i64, i64* %3
%48 = lshr i64 %47, 3
%49 = icmp ult i64 %46, %48
br i1 %49, label %50, label %72
50: ; preds = %35
%51 = load i8*, i8** %5
%52 = load i8*, i8** %4
%53 = ptrtoint i8* %51 to i64
%54 = ptrtoint i8* %52 to i64
%55 = sub i64 %53, %54
store i64 %55, i64* %7
%56 = load i64, i64* %3
%57 = load i64, i64* %3
%58 = add i64 %57, %56
store i64 %58, i64* %3
%59 = load i8*, i8** %4
%60 = load i64, i64* %3
%61 = call i8* @realloc(i8* %59, i64 %60)
store i8* %61, i8** %4
%62 = load i8*, i8** %4
%63 = icmp ne i8* %62, null
br i1 %63, label %68, label %64
64: ; preds = %50
%65 = load i8*, i8** %2
%66 = call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %65)
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %50
%69 = load i8*, i8** %4
%70 = load i64, i64* %7
%71 = getelementptr inbounds i8, i8* %69, i64 %70
store i8* %71, i8** %5
br label %72
72: ; preds = %68, %35
br label %73
73: ; preds = %72
%74 = load i8*, i8** %5
%75 = getelementptr inbounds i8, i8* %74, i64 -1
%76 = load i8, i8* %75
%77 = icmp ne i8 %76, 0
br i1 %77, label %18, label %78
78: ; preds = %73
%79 = load i8*, i8** %4
ret i8* %79
}

LLVM IR (-O2):
@.str = [25 x i8] c"Out of space reading %s\0A\00"
@STDIN_FILENO = global i32 0
@.str.1 = [16 x i8] c"EOF reading %s\0A\00"
define i8* @getstr(i8* %0) {
%2 = tail call dereferenceable_or_null(100) i8* @malloc(i64 100)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1, %42
%8 = phi i8* [ %43, %42 ], [ %2, %1 ]
%9 = phi i8* [ %44, %42 ], [ %2, %1 ]
%10 = phi i64 [ %45, %42 ], [ 100, %1 ]
%11 = load i32, i32* @STDIN_FILENO
%12 = tail call i32 @read(i32 %11, i8* %8, i32 1)
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %22
14: ; preds = %7
%15 = icmp eq i32 %12, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %20
18: ; preds = %14
%19 = tail call i32 @perror(i8* %0)
br label %20
20: ; preds = %18, %16
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %7
%23 = zext i32 %12 to i64
%24 = getelementptr inbounds i8, i8* %8, i64 %23
%25 = getelementptr inbounds i8, i8* %9, i64 %10
%26 = ptrtoint i8* %25 to i64
%27 = ptrtoint i8* %24 to i64
%28 = sub i64 %26, %27
%29 = lshr i64 %10, 3
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %42
31: ; preds = %22
%32 = shl i64 %10, 1
%33 = tail call i8* @realloc(i8* %9, i64 %32)
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = tail call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %31
%39 = ptrtoint i8* %9 to i64
%40 = sub i64 %27, %39
%41 = getelementptr inbounds i8, i8* %33, i64 %40
br label %42
42: ; preds = %38, %22
%43 = phi i8* [ %41, %38 ], [ %24, %22 ]
%44 = phi i8* [ %33, %38 ], [ %9, %22 ]
%45 = phi i64 [ %32, %38 ], [ %10, %22 ]
%46 = getelementptr inbounds i8, i8* %43, i64 -1
%47 = load i8, i8* %46
%48 = icmp eq i8 %47, 0
br i1 %48, label %49, label %7
49: ; preds = %42
ret i8* %44
}