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

/* Variables and functions */
int /*<<< orphan*/ cgiReadInt(int /*<<< orphan*/ *, int *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int cgiReadString(FILE *in, char **s) {
  int len;
  cgiReadInt(in, &len);
  *s = (char *)malloc(len + 1);
  if (!(*s)) {
    return 0;
  }
  if (fread(*s, 1, len, in) != len) {
    return 0;
  }
  (*s)[len] = '\0';
  return 1;
}


x86 assembly (clang, -O0):
<cgiReadString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1040 <cgiReadInt@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    117c <cgiReadString+0x4c>
movl   $0x0,-0x4(%rbp)
jmp    11bf <cgiReadString+0x8f>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x1c(%rbp),%eax
je     11a9 <cgiReadString+0x79>
movl   $0x0,-0x4(%rbp)
jmp    11bf <cgiReadString+0x8f>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cgiReadString>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xc(%rsp),%rsi
call   1040 <cgiReadInt@plt>
mov    0xc(%rsp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
xor    %ebp,%ebp
test   %rax,%rax
je     1186 <cgiReadString+0x56>
mov    0xc(%rsp),%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    0xc(%rsp),%eax
jne    1186 <cgiReadString+0x56>
mov    (%r14),%rcx
cltq
movb   $0x0,(%rcx,%rax,1)
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @cgiReadString(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 i32*, i32** %4
%8 = call i32 @cgiReadInt(i32* %7, i32* %6)
%9 = load i32, i32* %6
%10 = add nsw i32 %9, 1
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = load i8**, i8*** %5
store i8* %12, i8** %13
%14 = load i8**, i8*** %5
%15 = load i8*, i8** %14
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %2
store i32 0, i32* %3
br label %33
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = load i8*, i8** %19
%21 = load i32, i32* %6
%22 = load i32*, i32** %4
%23 = call i32 @fread(i8* %20, i32 1, i32 %21, i32* %22)
%24 = load i32, i32* %6
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %18
store i32 0, i32* %3
br label %33
27: ; preds = %18
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
store i32 1, i32* %3
br label %33
33: ; preds = %27, %26, %17
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
define i32 @cgiReadString(i32* %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @cgiReadInt(i32* %0, i32* %3)
%6 = load i32, i32* %3
%7 = add nsw i32 %6, 1
%8 = call i64 @malloc(i32 %7)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %1
%10 = icmp eq i64 %8, 0
br i1 %10, label %20, label %11
11: ; preds = %2
%12 = load i32, i32* %3
%13 = call i32 @fread(i8* %9, i32 1, i32 %12, i32* %0)
%14 = load i32, i32* %3
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i8*, i8** %1
%18 = sext i32 %13 to i64
%19 = getelementptr inbounds i8, i8* %17, i64 %18
store i8 0, i8* %19
br label %20
20: ; preds = %11, %2, %16
%21 = phi i32 [ 1, %16 ], [ 0, %2 ], [ 0, %11 ]
ret i32 %21
}