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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getchar();
char *malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *getstr() {
  static char buf[4096];
  char *bp;
  int c;
  int bslash;
  char *sbuf;
  bp = buf;
  while ((c = getchar()) != EOF && c != '"')
    ;
  if (c == EOF)
    return NULL;
  bslash = 0;
  while ((c = getchar()) != EOF) {
    if (bslash) {
      *bp++ = c;
      bslash = 0;
    } else {
      if (c == '"')
        break;
      if (c == '\\')
        bslash = 1;
      *bp++ = c;
    }
    if (bp - buf >= sizeof(buf)) {
      fprintf(stderr, "String too long\n");
      exit(2);
    }
  }
  *bp++ = '\0';
  sbuf = malloc(bp - buf);
  if (!sbuf) {
    fprintf(stderr, "Out of memory\n");
    exit(1);
  }
  strcpy(sbuf, buf);
  return sbuf;
}


x86 assembly (clang, -O0):
<getstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0x2f01(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
mov    0x2e70(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x21(%rbp)
je     118d <getstr+0x3d>
cmpl   $0x22,-0x14(%rbp)
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    119d <getstr+0x4d>
jmp    11a2 <getstr+0x52>
jmp    1163 <getstr+0x13>
mov    -0x14(%rbp),%eax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%eax
jne    11c1 <getstr+0x71>
movq   $0x0,-0x8(%rbp)
jmp    12ee <getstr+0x19e>
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%eax
je     1281 <getstr+0x131>
cmpl   $0x0,-0x18(%rbp)
je     120d <getstr+0xbd>
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movl   $0x0,-0x18(%rbp)
jmp    1243 <getstr+0xf3>
cmpl   $0x22,-0x14(%rbp)
jne    121c <getstr+0xcc>
jmp    1281 <getstr+0x131>
cmpl   $0x5c,-0x14(%rbp)
jne    122d <getstr+0xdd>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
lea    0x2e12(%rip),%rcx
sub    %rcx,%rax
cmp    $0x1000,%rax
jb     127c <getstr+0x12c>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
jmp    11c8 <getstr+0x78>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
lea    0x2dc2(%rip),%rcx
sub    %rcx,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12d6 <getstr+0x186>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
lea    0xd4a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0x2d7f(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8a(%rip),%r14
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <getchar@plt>
mov    (%r14),%ecx
cmp    $0x22,%eax
je     1173 <getstr+0x23>
cmp    %ecx,%eax
jne    1160 <getstr+0x10>
xor    %ebx,%ebx
cmp    %ecx,%eax
je     11e5 <getstr+0x95>
lea    0x2ee0(%rip),%r15
xor    %ebx,%ebx
xor    %ebp,%ebp
jmp    11a3 <getstr+0x53>
mov    $0x1,%ecx
nopl   0x0(%rax,%rax,1)
mov    %al,(%rbx,%r15,1)
add    $0x1,%rbx
mov    %ecx,%ebp
cmp    $0x1000,%rbx
je     11f3 <getstr+0xa3>
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    (%r14),%eax
je     11bf <getstr+0x6f>
xor    %ecx,%ecx
test   %ebp,%ebp
jne    1190 <getstr+0x40>
cmp    $0x5c,%eax
je     1186 <getstr+0x36>
cmp    $0x22,%eax
jne    1190 <getstr+0x40>
movb   $0x0,(%rbx,%r15,1)
add    $0x1,%ebx
mov    %ebx,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1212 <getstr+0xc2>
mov    %rax,%rbx
lea    0x2e83(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@getstr.buf = internal global [4096 x i8] zeroinitializer
@EOF = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"String too long\0A\00"
@.str.1 = [15 x i8] c"Out of memory\0A\00"
define i8* @getstr() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @getstr.buf, i64 0, i64 0), i8** %2
br label %6
6: ; preds = %15, %0
%7 = call i32 (...) @getchar()
store i32 %7, i32* %3
%8 = load i32, i32* @EOF
%9 = icmp ne i32 %7, %8
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 34
br label %13
13: ; preds = %10, %6
%14 = phi i1 [ false, %6 ], [ %12, %10 ]
br i1 %14, label %15, label %16
15: ; preds = %13
br label %6
16: ; preds = %13
%17 = load i32, i32* %3
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
store i8* null, i8** %1
br label %75
21: ; preds = %16
store i32 0, i32* %4
br label %22
22: ; preds = %56, %21
%23 = call i32 (...) @getchar()
store i32 %23, i32* %3
%24 = load i32, i32* @EOF
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %57
26: ; preds = %22
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* %3
%31 = trunc i32 %30 to i8
%32 = load i8*, i8** %2
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %2
store i8 %31, i8* %32
store i32 0, i32* %4
br label %47
34: ; preds = %26
%35 = load i32, i32* %3
%36 = icmp eq i32 %35, 34
br i1 %36, label %37, label %38
37: ; preds = %34
br label %57
38: ; preds = %34
%39 = load i32, i32* %3
%40 = icmp eq i32 %39, 92
br i1 %40, label %41, label %42
41: ; preds = %38
store i32 1, i32* %4
br label %42
42: ; preds = %41, %38
%43 = load i32, i32* %3
%44 = trunc i32 %43 to i8
%45 = load i8*, i8** %2
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %2
store i8 %44, i8* %45
br label %47
47: ; preds = %42, %29
%48 = load i8*, i8** %2
%49 = ptrtoint i8* %48 to i64
%50 = sub i64 %49, ptrtoint ([4096 x i8]* @getstr.buf to i64)
%51 = icmp uge i64 %50, 4096
br i1 %51, label %52, label %56
52: ; preds = %47
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%55 = call i32 @exit(i32 2)
unreachable
56: ; preds = %47
br label %22
57: ; preds = %37, %22
%58 = load i8*, i8** %2
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %2
store i8 0, i8* %58
%60 = load i8*, i8** %2
%61 = ptrtoint i8* %60 to i64
%62 = sub i64 %61, ptrtoint ([4096 x i8]* @getstr.buf to i64)
%63 = trunc i64 %62 to i32
%64 = call i8* @malloc(i32 %63)
store i8* %64, i8** %5
%65 = load i8*, i8** %5
%66 = icmp ne i8* %65, null
br i1 %66, label %71, label %67
67: ; preds = %57
%68 = load i32, i32* @stderr
%69 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %57
%72 = load i8*, i8** %5
%73 = call i32 @strcpy(i8* %72, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @getstr.buf, i64 0, i64 0))
%74 = load i8*, i8** %5
store i8* %74, i8** %1
br label %75
75: ; preds = %71, %20
%76 = load i8*, i8** %1
ret i8* %76
}

LLVM IR (-O2):
@getstr.buf = internal global [4096 x i8] zeroinitializer
@EOF = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"String too long\0A\00"
@.str.1 = [15 x i8] c"Out of memory\0A\00"
define i8* @getstr() {
br label %1
1: ; preds = %1, %0
%2 = tail call i32 (...) @getchar()
%3 = load i32, i32* @EOF
%4 = icmp ne i32 %2, %3
%5 = icmp ne i32 %2, 34
%6 = and i1 %5, %4
br i1 %6, label %1, label %7
7: ; preds = %1
%8 = icmp eq i32 %2, %3
br i1 %8, label %41, label %9
9: ; preds = %7, %19
%10 = phi i8* [ %21, %19 ], [ getelementptr inbounds ([4096 x i8], [4096 x i8]* @getstr.buf, i64 0, i64 0), %7 ]
%11 = phi i32 [ %20, %19 ], [ 0, %7 ]
%12 = tail call i32 (...) @getchar()
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %28, label %15
15: ; preds = %9
%16 = icmp eq i32 %11, 0
br i1 %16, label %17, label %19
17: ; preds = %15
switch i32 %12, label %19 [
i32 34, label %28
i32 92, label %18
]
18: ; preds = %17
br label %19
19: ; preds = %18, %17, %15
%20 = phi i32 [ 0, %15 ], [ 1, %18 ], [ 0, %17 ]
%21 = getelementptr inbounds i8, i8* %10, i64 1
%22 = trunc i32 %12 to i8
store i8 %22, i8* %10
%23 = icmp eq i8* %10, getelementptr inbounds ([4096 x i8], [4096 x i8]* @getstr.buf, i64 0, i64 4095)
br i1 %23, label %24, label %9
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%27 = tail call i32 @exit(i32 2)
unreachable
28: ; preds = %17, %9
%29 = getelementptr inbounds i8, i8* %10, i64 1
store i8 0, i8* %10
%30 = ptrtoint i8* %29 to i64
%31 = trunc i64 %30 to i32
%32 = sub i32 %31, ptrtoint ([4096 x i8]* @getstr.buf to i32)
%33 = tail call i8* @malloc(i32 %32)
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = tail call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %28
%40 = tail call i32 @strcpy(i8* %33, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @getstr.buf, i64 0, i64 0))
br label %41
41: ; preds = %7, %39
%42 = phi i8* [ %33, %39 ], [ null, %7 ]
ret i8* %42
}