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

/* Variables and functions */
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
int read(int, unsigned char *, int);
char *wma_utf16toutf8(unsigned char *, int);
int wma_file_read_utf16(int fd, int len, char **utf8) {
  char *out;
  unsigned char *utf16;
  int ret;
  utf16 = (unsigned char *)malloc(len);
  if (!utf16)
    return 0;
  ret = read(fd, utf16, len);
  if ((ret < 0) || (ret != len))
    return 0;
  out = wma_utf16toutf8(utf16, len);
  *utf8 = out;
  free(utf16);
  return 1;
}


x86 assembly (clang, -O0):
<wma_file_read_utf16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1175 <wma_file_read_utf16+0x35>
movl   $0x0,-0x4(%rbp)
jmp    11d4 <wma_file_read_utf16+0x94>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     119d <wma_file_read_utf16+0x5d>
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     11a9 <wma_file_read_utf16+0x69>
movl   $0x0,-0x4(%rbp)
jmp    11d4 <wma_file_read_utf16+0x94>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <wma_utf16toutf8@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wma_file_read_utf16>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%r12d
mov    %esi,%edi
call   1060 <malloc@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     1192 <wma_file_read_utf16+0x52>
mov    %rax,%rbx
mov    %r12d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1192 <wma_file_read_utf16+0x52>
cmp    %ebp,%eax
jne    1192 <wma_file_read_utf16+0x52>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <wma_utf16toutf8@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @wma_file_read_utf16(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%11 = load i32, i32* %6
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %9
%14 = load i8*, i8** %9
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %3
store i32 0, i32* %4
br label %37
17: ; preds = %3
%18 = load i32, i32* %5
%19 = load i8*, i8** %9
%20 = load i32, i32* %6
%21 = call i32 @read(i32 %18, i8* %19, i32 %20)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp slt i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %17
%25 = load i32, i32* %10
%26 = load i32, i32* %6
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24, %17
store i32 0, i32* %4
br label %37
29: ; preds = %24
%30 = load i8*, i8** %9
%31 = load i32, i32* %6
%32 = call i8* @wma_utf16toutf8(i8* %30, i32 %31)
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = load i8**, i8*** %7
store i8* %33, i8** %34
%35 = load i8*, i8** %9
%36 = call i32 @free(i8* %35)
store i32 1, i32* %4
br label %37
37: ; preds = %29, %28, %16
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
define i32 @wma_file_read_utf16(i32 %0, i32 %1, i8** nocapture writeonly %2) {
%4 = tail call i64 @malloc(i32 %1)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %15, label %7
7: ; preds = %3
%8 = tail call i32 @read(i32 %0, i8* %5, i32 %1)
%9 = icmp sgt i32 %8, -1
%10 = icmp eq i32 %8, %1
%11 = and i1 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i8* @wma_utf16toutf8(i8* %5, i32 %1)
store i8* %13, i8** %2
%14 = tail call i32 @free(i8* %5)
br label %15
15: ; preds = %7, %3, %12
%16 = phi i32 [ 1, %12 ], [ 0, %3 ], [ 0, %7 ]
ret i32 %16
}