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

/* Variables and functions */
char *GC_malloc(unsigned int);
char *GC_realloc(char *, unsigned int);
int /*<<< orphan*/ die(char *, int);
char next_char(struct scanner_input *);
char *read_quoted_string(struct scanner_input *I) {
  unsigned int len = 32, pos = 0;
  char *s = GC_malloc(len), c;
  if (s == NULL)
    die("read_quoted_string(): GC_malloc() failed!", 1);
  do {
    c = next_char(I);
    if (pos >= len - 1) {
      char *NewStr;
      len *= 2;
      NewStr = GC_realloc(s, len);
      if (NewStr == NULL)
        die("read_quoted_string: GC_realloc() failed!", 1);
      s = NewStr;
    }
    s[pos++] = c;
  } while (c != '\"');
  s[pos - 1] = '\0';
  return s;
}


x86 assembly (clang, -O0):
<read_quoted_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x20,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <GC_malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1182 <read_quoted_string+0x42>
lea    0xe88(%rip),%rdi
mov    $0x1,%esi
call   1060 <die@plt>
jmp    1187 <read_quoted_string+0x47>
mov    -0x8(%rbp),%rdi
call   1030 <next_char@plt>
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jb     11e1 <read_quoted_string+0xa1>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <GC_realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d9 <read_quoted_string+0x99>
lea    0xe5b(%rip),%rdi
mov    $0x1,%esi
call   1060 <die@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x19(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
movsbl -0x19(%rbp),%eax
cmp    $0x22,%eax
jne    1187 <read_quoted_string+0x47>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_quoted_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x20,%r15d
mov    $0x20,%edi
call   1040 <GC_malloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1177 <read_quoted_string+0x37>
lea    0xe93(%rip),%rdi
mov    $0x1,%esi
call   1060 <die@plt>
mov    $0xffffffff,%ebp
lea    0xea7(%rip),%r12
jmp    11ac <read_quoted_string+0x6c>
mov    %r12,%rdi
mov    $0x1,%esi
call   1060 <die@plt>
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%eax
mov    %bl,0x0(%r13,%rax,1)
cmp    $0x22,%bl
je     11d9 <read_quoted_string+0x99>
add    $0x1,%ebp
mov    %r14,%rdi
call   1030 <next_char@plt>
mov    %eax,%ebx
lea    -0x1(%r15),%eax
cmp    %eax,%ebp
jb     11a0 <read_quoted_string+0x60>
add    %r15d,%r15d
mov    %r13,%rdi
mov    %r15d,%esi
call   1050 <GC_realloc@plt>
test   %rax,%rax
je     1185 <read_quoted_string+0x45>
mov    %rax,%r13
jmp    11a0 <read_quoted_string+0x60>
movb   $0x0,0x0(%r13,%rax,1)
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.scanner_input = type { i32 }
@.str = [42 x i8] c"read_quoted_string(): GC_malloc() failed!\00"
@.str.1 = [41 x i8] c"read_quoted_string: GC_realloc() failed!\00"
define i8* @read_quoted_string(%struct.scanner_input* %0) {
%2 = alloca %struct.scanner_input*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8*
store %struct.scanner_input* %0, %struct.scanner_input** %2
store i32 32, i32* %3
store i32 0, i32* %4
%8 = load i32, i32* %3
%9 = call i8* @GC_malloc(i32 %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @die(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 1)
br label %14
14: ; preds = %12, %1
br label %15
15: ; preds = %41, %14
%16 = load %struct.scanner_input*, %struct.scanner_input** %2
%17 = call signext i8 @next_char(%struct.scanner_input* %16)
store i8 %17, i8* %6
%18 = load i32, i32* %4
%19 = load i32, i32* %3
%20 = sub i32 %19, 1
%21 = icmp uge i32 %18, %20
br i1 %21, label %22, label %34
22: ; preds = %15
%23 = load i32, i32* %3
%24 = mul i32 %23, 2
store i32 %24, i32* %3
%25 = load i8*, i8** %5
%26 = load i32, i32* %3
%27 = call i8* @GC_realloc(i8* %25, i32 %26)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @die(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %32
32: ; preds = %30, %22
%33 = load i8*, i8** %7
store i8* %33, i8** %5
br label %34
34: ; preds = %32, %15
%35 = load i8, i8* %6
%36 = load i8*, i8** %5
%37 = load i32, i32* %4
%38 = add i32 %37, 1
store i32 %38, i32* %4
%39 = zext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 %35, i8* %40
br label %41
41: ; preds = %34
%42 = load i8, i8* %6
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 34
br i1 %44, label %15, label %45
45: ; preds = %41
%46 = load i8*, i8** %5
%47 = load i32, i32* %4
%48 = sub i32 %47, 1
%49 = zext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
store i8 0, i8* %50
%51 = load i8*, i8** %5
ret i8* %51
}

LLVM IR (-O2):
%struct.scanner_input = type { i32 }
@.str = [42 x i8] c"read_quoted_string(): GC_malloc() failed!\00"
@.str.1 = [41 x i8] c"read_quoted_string: GC_realloc() failed!\00"
define i8* @read_quoted_string(%struct.scanner_input* %0) {
%2 = tail call i8* @GC_malloc(i32 32)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @die(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 1)
br label %6
6: ; preds = %4, %1
br label %7
7: ; preds = %6, %20
%8 = phi i8* [ %21, %20 ], [ %2, %6 ]
%9 = phi i32 [ %23, %20 ], [ 0, %6 ]
%10 = phi i32 [ %22, %20 ], [ 32, %6 ]
%11 = tail call signext i8 @next_char(%struct.scanner_input* %0)
%12 = add i32 %10, -1
%13 = icmp ult i32 %9, %12
br i1 %13, label %20, label %14
14: ; preds = %7
%15 = shl i32 %10, 1
%16 = tail call i8* @GC_realloc(i8* %8, i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @die(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %20
20: ; preds = %14, %18, %7
%21 = phi i8* [ %8, %7 ], [ null, %18 ], [ %16, %14 ]
%22 = phi i32 [ %10, %7 ], [ %15, %18 ], [ %15, %14 ]
%23 = add i32 %9, 1
%24 = zext i32 %9 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 %11, i8* %25
%26 = icmp eq i8 %11, 34
br i1 %26, label %27, label %7
27: ; preds = %20
%28 = zext i32 %9 to i64
%29 = getelementptr inbounds i8, i8* %21, i64 %28
store i8 0, i8* %29
ret i8* %21
}