#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 scalar_t__ uint32_t;

/* Variables and functions */
char *calloc(scalar_t__, int);
int /*<<< orphan*/ free(char *);
scalar_t__ read_FD(int, char *, scalar_t__);
scalar_t__ uint32ReadFromFD(int, scalar_t__ *);
size_t astringReadFromFD(int fd, char **data) {
  size_t count = 0;
  uint32_t length;
  count += uint32ReadFromFD(fd, &length);
  if (*data != NULL)
    free(*data);
  *data = calloc(length + 1, sizeof(char));
  count += read_FD(fd, *data, length);
  return count;
}


x86 assembly (clang, -O0):
<astringReadFromFD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1040 <uint32ReadFromFD@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1185 <astringReadFromFD+0x45>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <read_FD@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<astringReadFromFD>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
call   1040 <uint32ReadFromFD@plt>
mov    %rax,%r14
mov    (%rbx),%rdi
test   %rdi,%rdi
je     1167 <astringReadFromFD+0x27>
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,(%rbx)
mov    0x8(%rsp),%rdx
mov    %ebp,%edi
mov    %rax,%rsi
call   1060 <read_FD@plt>
add    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i64 @astringReadFromFD(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i64 0, i64* %5
%7 = load i32, i32* %3
%8 = call i64 @uint32ReadFromFD(i32 %7, i64* %6)
%9 = load i64, i64* %5
%10 = add i64 %9, %8
store i64 %10, i64* %5
%11 = load i8**, i8*** %4
%12 = load i8*, i8** %11
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8**, i8*** %4
%16 = load i8*, i8** %15
%17 = call i32 @free(i8* %16)
br label %18
18: ; preds = %14, %2
%19 = load i64, i64* %6
%20 = add nsw i64 %19, 1
%21 = call i8* @calloc(i64 %20, i32 1)
%22 = load i8**, i8*** %4
store i8* %21, i8** %22
%23 = load i32, i32* %3
%24 = load i8**, i8*** %4
%25 = load i8*, i8** %24
%26 = load i64, i64* %6
%27 = call i64 @read_FD(i32 %23, i8* %25, i64 %26)
%28 = load i64, i64* %5
%29 = add i64 %28, %27
store i64 %29, i64* %5
%30 = load i64, i64* %5
ret i64 %30
}

LLVM IR (-O2):
define i64 @astringReadFromFD(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = call i64 @uint32ReadFromFD(i32 %0, i64* %3)
%6 = load i8*, i8** %1
%7 = icmp eq i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = call i32 @free(i8* %6)
br label %10
10: ; preds = %8, %2
%11 = load i64, i64* %3
%12 = add nsw i64 %11, 1
%13 = call i8* @calloc(i64 %12, i32 1)
store i8* %13, i8** %1
%14 = load i64, i64* %3
%15 = call i64 @read_FD(i32 %0, i8* %13, i64 %14)
%16 = add i64 %15, %5
ret i64 %16
}