#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*/ wchar_t;
typedef int uint32_t;

/* Variables and functions */
void *calloc(size_t, int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          size_t);
scalar_t__ read_FD(int, char *, int);
scalar_t__ uint32ReadFromFD(int, int *);
int /*<<< orphan*/ *utf8_to_wchar(char *, int, size_t *);
size_t ustringReadFromFD(int fd, wchar_t **data) {
  size_t wchar_count, count = 0;
  uint32_t uint8_len;
  count += uint32ReadFromFD(fd, &uint8_len);
  char *uint8_buf = calloc(1, uint8_len + 1);
  count += read_FD(fd, uint8_buf, uint8_len);
  const wchar_t *wchar = utf8_to_wchar(uint8_buf, uint8_len, &wchar_count);
  if (*data != NULL)
    free(*data);
  *data = calloc(wchar_count + 1, sizeof(wchar_t));
  memcpy(*data, wchar, (wchar_count + 1) * sizeof(wchar_t));
  return count;
}


x86 assembly (clang, -O0):
<ustringReadFromFD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1050 <uint32ReadFromFD@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1080 <read_FD@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    -0x18(%rbp),%rdx
call   1040 <utf8_to_wchar@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11e4 <ustringReadFromFD+0x84>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
shl    $0x2,%rdx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ustringReadFromFD>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xc(%rsp),%rsi
call   1050 <uint32ReadFromFD@plt>
mov    %rax,%r15
mov    0xc(%rsp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%edx
mov    %ebp,%edi
mov    %rax,%rsi
call   1080 <read_FD@plt>
mov    %rax,%r13
mov    0xc(%rsp),%esi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
call   1040 <utf8_to_wchar@plt>
mov    %rax,%r12
mov    (%r14),%rdi
test   %rdi,%rdi
je     11c6 <ustringReadFromFD+0x66>
call   1030 <free@plt>
add    %r15,%r13
mov    0x10(%rsp),%rdi
add    $0x1,%rdi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,(%r14)
mov    0x10(%rsp),%rcx
lea    0x4(,%rcx,4),%rdx
mov    %rax,%rdi
mov    %r12,%rsi
call   1070 <memcpy@plt>
mov    %r13,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @ustringReadFromFD(i32 %0, i32** %1) {
%3 = alloca i32
%4 = alloca i32**
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i32 %0, i32* %3
store i32** %1, i32*** %4
store i64 0, i64* %6
%10 = load i32, i32* %3
%11 = call i64 @uint32ReadFromFD(i32 %10, i32* %7)
%12 = load i64, i64* %6
%13 = add i64 %12, %11
store i64 %13, i64* %6
%14 = load i32, i32* %7
%15 = add nsw i32 %14, 1
%16 = call i8* @calloc(i64 1, i32 %15)
store i8* %16, i8** %8
%17 = load i32, i32* %3
%18 = load i8*, i8** %8
%19 = load i32, i32* %7
%20 = call i64 @read_FD(i32 %17, i8* %18, i32 %19)
%21 = load i64, i64* %6
%22 = add i64 %21, %20
store i64 %22, i64* %6
%23 = load i8*, i8** %8
%24 = load i32, i32* %7
%25 = call i32* @utf8_to_wchar(i8* %23, i32 %24, i64* %5)
store i32* %25, i32** %9
%26 = load i32**, i32*** %4
%27 = load i32*, i32** %26
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %33
29: ; preds = %2
%30 = load i32**, i32*** %4
%31 = load i32*, i32** %30
%32 = call i32 @free(i32* %31)
br label %33
33: ; preds = %29, %2
%34 = load i64, i64* %5
%35 = add i64 %34, 1
%36 = call i8* @calloc(i64 %35, i32 4)
%37 = bitcast i8* %36 to i32*
%38 = load i32**, i32*** %4
store i32* %37, i32** %38
%39 = load i32**, i32*** %4
%40 = load i32*, i32** %39
%41 = load i32*, i32** %9
%42 = load i64, i64* %5
%43 = add i64 %42, 1
%44 = mul i64 %43, 4
%45 = call i32 @memcpy(i32* %40, i32* %41, i64 %44)
%46 = load i64, i64* %6
ret i64 %46
}

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