#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 ssize_t;

/* Variables and functions */
int __read_all__(int, unsigned char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
unsigned char *realloc(unsigned char *, int);
char *strerror(int /*<<< orphan*/);
ssize_t __read_until__(int fd, int ch, unsigned char **data) {
  unsigned char *buf, *buf2;
  ssize_t n, len, buf_size;
  *data = NULL;
  buf_size = 100;
  buf = malloc(buf_size);
  if (buf == NULL) {
    return -1;
  }
  len = 0;
  while ((n = __read_all__(fd, buf + len, 1)) == 1) {
    if (buf[len++] == ch)
      break;
    if (len + 1 == buf_size) {
      buf_size *= 2;
      buf2 = realloc(buf, buf_size);
      if (buf2 == NULL) {
        free(buf);
        return -1;
      }
      buf = buf2;
    }
  }
  if (n <= 0) {
    free(buf);
    if (n == 0)
      printf("read_until: closed\n");
    else
      printf("read_until: read error: %s\n", strerror(errno));
    return n;
  }
  buf2 = realloc(buf, len + 1);
  if (buf2 == NULL)
    printf("read_until: realloc: shrink failed\n");
  else
    buf = buf2;
  *data = buf;
  return len;
}


x86 assembly (clang, -O0):
<__read_until__>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x64,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a7 <__read_until__+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <__read_until__+0x18f>
movl   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x30(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1070 <__read_all__@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x1,%eax
jne    124b <__read_until__+0xeb>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x30(%rbp)
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    -0xc(%rbp),%eax
jne    11f6 <__read_until__+0x96>
jmp    124b <__read_until__+0xeb>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
cmp    -0x34(%rbp),%eax
jne    1246 <__read_until__+0xe6>
mov    -0x34(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123e <__read_until__+0xde>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <__read_until__+0x18f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11ae <__read_until__+0x4e>
cmpl   $0x0,-0x2c(%rbp)
jg     12a5 <__read_until__+0x145>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    127b <__read_until__+0x11b>
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    129a <__read_until__+0x13a>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ef <__read_until__+0x18f>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%esi
add    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12d6 <__read_until__+0x176>
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12de <__read_until__+0x17e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__read_until__>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %esi,0xc(%rsp)
mov    %edi,%ebx
movq   $0x0,(%rdx)
mov    $0x64,%edi
call   1060 <malloc@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     12d4 <__read_until__+0x164>
mov    %rax,%rbp
mov    %ebx,%edi
mov    %rax,%rsi
mov    $0x1,%edx
call   1080 <__read_all__@plt>
cmp    $0x1,%eax
jne    1245 <__read_until__+0xd5>
mov    %ebx,0x8(%rsp)
mov    %r14,0x10(%rsp)
mov    $0x64,%r14d
mov    $0x1,%r12d
mov    %rbp,%r15
jmp    1205 <__read_until__+0x95>
nopl   0x0(%rax)
mov    %r15,%rbx
mov    %r12,%rbp
add    %rbx,%rbp
mov    0x8(%rsp),%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1080 <__read_all__@plt>
mov    %r13,%r12
mov    %rbx,%r15
cmp    $0x1,%eax
jne    1283 <__read_until__+0x113>
movzbl 0x0(%rbp),%eax
cmp    0xc(%rsp),%eax
je     1258 <__read_until__+0xe8>
lea    0x1(%r12),%r13
mov    %r14d,%eax
cmp    %rax,%r13
jne    11e0 <__read_until__+0x70>
add    %r14d,%r14d
mov    %r15,%rdi
mov    %r14d,%esi
call   1070 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e3 <__read_until__+0x73>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%r12d
jmp    12d4 <__read_until__+0x164>
xor    %r13d,%r13d
mov    %eax,%r12d
test   %r12d,%r12d
jle    1297 <__read_until__+0x127>
mov    %rbp,%r15
mov    %r13d,%r12d
jmp    125d <__read_until__+0xed>
mov    0x10(%rsp),%r14
lea    0x1(%r12),%esi
mov    %r15,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
jne    127e <__read_until__+0x10e>
lea    0xda6(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rax
mov    %rax,(%r14)
jmp    12d4 <__read_until__+0x164>
add    $0xffffffff,%r13d
mov    %rbx,%rbp
mov    %eax,%r12d
mov    0x10(%rsp),%r14
test   %r12d,%r12d
jg     1250 <__read_until__+0xe0>
mov    %rbp,%rdi
call   1030 <free@plt>
test   %r12d,%r12d
je     12c5 <__read_until__+0x155>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd47(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12d4 <__read_until__+0x164>
lea    0xd73(%rip),%rdi
call   1040 <puts@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"read_until: closed\0A\00"
@.str.1 = [28 x i8] c"read_until: read error: %s\0A\00"
@errno = global i32 0
@.str.2 = [36 x i8] c"read_until: realloc: shrink failed\0A\00"
define i32 @__read_until__(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%13 = load i8**, i8*** %7
store i8* null, i8** %13
store i32 100, i32* %12
%14 = load i32, i32* %12
%15 = call i8* @malloc(i32 %14)
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %89
19: ; preds = %3
store i32 0, i32* %11
br label %20
20: ; preds = %57, %19
%21 = load i32, i32* %5
%22 = load i8*, i8** %8
%23 = load i32, i32* %11
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = call i32 @__read_all__(i32 %21, i8* %25, i32 1)
store i32 %26, i32* %10
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %58
28: ; preds = %20
%29 = load i8*, i8** %8
%30 = load i32, i32* %11
%31 = add nsw i32 %30, 1
store i32 %31, i32* %11
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = load i32, i32* %6
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %28
br label %58
39: ; preds = %28
%40 = load i32, i32* %11
%41 = add nsw i32 %40, 1
%42 = load i32, i32* %12
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %57
44: ; preds = %39
%45 = load i32, i32* %12
%46 = mul nsw i32 %45, 2
store i32 %46, i32* %12
%47 = load i8*, i8** %8
%48 = load i32, i32* %12
%49 = call i8* @realloc(i8* %47, i32 %48)
store i8* %49, i8** %9
%50 = load i8*, i8** %9
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %55
52: ; preds = %44
%53 = load i8*, i8** %8
%54 = call i32 @free(i8* %53)
store i32 -1, i32* %4
br label %89
55: ; preds = %44
%56 = load i8*, i8** %9
store i8* %56, i8** %8
br label %57
57: ; preds = %55, %39
br label %20
58: ; preds = %38, %20
%59 = load i32, i32* %10
%60 = icmp sle i32 %59, 0
br i1 %60, label %61, label %74
61: ; preds = %58
%62 = load i8*, i8** %8
%63 = call i32 @free(i8* %62)
%64 = load i32, i32* %10
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %72
68: ; preds = %61
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %70)
br label %72
72: ; preds = %68, %66
%73 = load i32, i32* %10
store i32 %73, i32* %4
br label %89
74: ; preds = %58
%75 = load i8*, i8** %8
%76 = load i32, i32* %11
%77 = add nsw i32 %76, 1
%78 = call i8* @realloc(i8* %75, i32 %77)
store i8* %78, i8** %9
%79 = load i8*, i8** %9
%80 = icmp eq i8* %79, null
br i1 %80, label %81, label %83
81: ; preds = %74
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %85
83: ; preds = %74
%84 = load i8*, i8** %9
store i8* %84, i8** %8
br label %85
85: ; preds = %83, %81
%86 = load i8*, i8** %8
%87 = load i8**, i8*** %7
store i8* %86, i8** %87
%88 = load i32, i32* %11
store i32 %88, i32* %4
br label %89
89: ; preds = %85, %72, %52, %18
%90 = load i32, i32* %4
ret i32 %90
}

LLVM IR (-O2):
@.str.1 = [28 x i8] c"read_until: read error: %s\0A\00"
@errno = global i32 0
@str = [35 x i8] c"read_until: realloc: shrink failed\00"
@str.3 = [19 x i8] c"read_until: closed\00"
define i32 @__read_until__(i32 %0, i32 %1, i8** nocapture writeonly %2) {
store i8* null, i8** %2
%4 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%5 = icmp eq i8* %4, null
br i1 %5, label %62, label %6
6: ; preds = %3
%7 = tail call i32 @__read_all__(i32 %0, i8* %4, i32 1)
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %36
9: ; preds = %6, %28
%10 = phi i64 [ %14, %28 ], [ 0, %6 ]
%11 = phi i8* [ %31, %28 ], [ %4, %6 ]
%12 = phi i32 [ %30, %28 ], [ 100, %6 ]
%13 = phi i8* [ %29, %28 ], [ %4, %6 ]
%14 = add nuw i64 %10, 1
%15 = load i8, i8* %11
%16 = zext i8 %15 to i32
%17 = icmp eq i32 %16, %1
br i1 %17, label %50, label %18
18: ; preds = %9
%19 = add nuw nsw i64 %10, 2
%20 = zext i32 %12 to i64
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = shl nsw i32 %12, 1
%24 = tail call i8* @realloc(i8* %13, i32 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 @free(i8* %13)
br label %62
28: ; preds = %22, %18
%29 = phi i8* [ %13, %18 ], [ %24, %22 ]
%30 = phi i32 [ %12, %18 ], [ %23, %22 ]
%31 = getelementptr inbounds i8, i8* %29, i64 %14
%32 = tail call i32 @__read_all__(i32 %0, i8* %31, i32 1)
%33 = icmp eq i32 %32, 1
br i1 %33, label %9, label %34
34: ; preds = %28
%35 = trunc i64 %14 to i32
br label %36
36: ; preds = %34, %6
%37 = phi i8* [ %4, %6 ], [ %29, %34 ]
%38 = phi i32 [ 0, %6 ], [ %35, %34 ]
%39 = phi i32 [ %7, %6 ], [ %32, %34 ]
%40 = icmp slt i32 %39, 1
br i1 %40, label %41, label %52
41: ; preds = %36
%42 = tail call i32 @free(i8* %37)
%43 = icmp eq i32 %39, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.3, i64 0, i64 0))
br label %62
46: ; preds = %41
%47 = load i32, i32* @errno
%48 = tail call i8* @strerror(i32 %47)
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %48)
br label %62
50: ; preds = %9
%51 = trunc i64 %14 to i32
br label %52
52: ; preds = %50, %36
%53 = phi i8* [ %37, %36 ], [ %13, %50 ]
%54 = phi i32 [ %38, %36 ], [ %51, %50 ]
%55 = add nsw i32 %54, 1
%56 = tail call i8* @realloc(i8* %53, i32 %55)
%57 = icmp eq i8* %56, null
br i1 %57, label %58, label %60
58: ; preds = %52
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %60
60: ; preds = %52, %58
%61 = phi i8* [ %53, %58 ], [ %56, %52 ]
store i8* %61, i8** %2
br label %62
62: ; preds = %44, %46, %3, %60, %26
%63 = phi i32 [ %54, %60 ], [ -1, %26 ], [ -1, %3 ], [ %39, %46 ], [ 0, %44 ]
ret i32 %63
}