#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ consumeNewline(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ readBytes(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ readLong(int /*<<< orphan*/ *, char, int *);
int readString(FILE *fp, char **target) {
  ssize_t len;
  *target = NULL;
  if (!readLong(fp, '$', &len)) {
    return 0;
  }
  len += 2;
  *target = (char *)malloc(len);
  if (!readBytes(fp, *target, len)) {
    return 0;
  }
  if (!consumeNewline(*target + len - 2)) {
    return 0;
  }
  (*target)[len - 2] = '\0';
  return 1;
}


x86 assembly (clang, -O0):
<readString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x24,%esi
lea    -0x1c(%rbp),%rdx
call   1060 <readLong@plt>
cmp    $0x0,%eax
jne    1182 <readString+0x42>
movl   $0x0,-0x4(%rbp)
jmp    120c <readString+0xcc>
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <readBytes@plt>
cmp    $0x0,%eax
jne    11c5 <readString+0x85>
movl   $0x0,-0x4(%rbp)
jmp    120c <readString+0xcc>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
add    $0xfffffffffffffffe,%rdi
call   1030 <consumeNewline@plt>
cmp    $0x0,%eax
jne    11f1 <readString+0xb1>
movl   $0x0,-0x4(%rbp)
jmp    120c <readString+0xcc>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readString>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
movq   $0x0,(%rsi)
lea    0xc(%rsp),%rdx
mov    $0x24,%esi
call   1060 <readLong@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     11bd <readString+0x7d>
mov    0xc(%rsp),%edi
add    $0x2,%edi
mov    %edi,0xc(%rsp)
call   1040 <malloc@plt>
mov    %rax,(%rbx)
mov    0xc(%rsp),%edx
mov    %rbp,%rdi
mov    %rax,%rsi
call   1050 <readBytes@plt>
test   %eax,%eax
je     11bd <readString+0x7d>
mov    (%rbx),%rax
movslq 0xc(%rsp),%rcx
lea    (%rax,%rcx,1),%rdi
add    $0xfffffffffffffffe,%rdi
call   1030 <consumeNewline@plt>
test   %eax,%eax
je     11bd <readString+0x7d>
mov    (%rbx),%rax
movslq 0xc(%rsp),%rcx
movb   $0x0,-0x2(%rcx,%rax,1)
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readString(i32* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i32
store i32* %0, i32** %4
store i8** %1, i8*** %5
%7 = load i8**, i8*** %5
store i8* null, i8** %7
%8 = load i32*, i32** %4
%9 = call i32 @readLong(i32* %8, i8 signext 36, i32* %6)
%10 = icmp ne i32 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %2
store i32 0, i32* %3
br label %43
12: ; preds = %2
%13 = load i32, i32* %6
%14 = add nsw i32 %13, 2
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = load i8**, i8*** %5
store i8* %17, i8** %18
%19 = load i32*, i32** %4
%20 = load i8**, i8*** %5
%21 = load i8*, i8** %20
%22 = load i32, i32* %6
%23 = call i32 @readBytes(i32* %19, i8* %21, i32 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %12
store i32 0, i32* %3
br label %43
26: ; preds = %12
%27 = load i8**, i8*** %5
%28 = load i8*, i8** %27
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = getelementptr inbounds i8, i8* %31, i64 -2
%33 = call i32 @consumeNewline(i8* %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %26
store i32 0, i32* %3
br label %43
36: ; preds = %26
%37 = load i8**, i8*** %5
%38 = load i8*, i8** %37
%39 = load i32, i32* %6
%40 = sub nsw i32 %39, 2
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
store i8 0, i8* %42
store i32 1, i32* %3
br label %43
43: ; preds = %36, %35, %25, %11
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
define i32 @readString(i32* %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i8* null, i8** %1
%5 = call i32 @readLong(i32* %0, i8 signext 36, i32* %3)
%6 = icmp eq i32 %5, 0
br i1 %6, label %29, label %7
7: ; preds = %2
%8 = load i32, i32* %3
%9 = add nsw i32 %8, 2
store i32 %9, i32* %3
%10 = call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %1
%12 = load i32, i32* %3
%13 = call i32 @readBytes(i32* %0, i8* %11, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %29, label %15
15: ; preds = %7
%16 = load i8*, i8** %1
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = getelementptr inbounds i8, i8* %19, i64 -2
%21 = call i32 @consumeNewline(i8* %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %15
%24 = load i8*, i8** %1
%25 = load i32, i32* %3
%26 = add nsw i32 %25, -2
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %15, %7, %2, %23
%30 = phi i32 [ 1, %23 ], [ 0, %2 ], [ 0, %7 ], [ 0, %15 ]
ret i32 %30
}