#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 */
unsigned char *calloc(int, int);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ util_utf16toutf8(unsigned char *, int, unsigned char *, int);
int util_utf16toutf8_len(unsigned char *, int);
unsigned char *util_utf16touft8_alloc(unsigned char *utf16, int len) {
  unsigned char *out;
  int new_len;
  new_len = util_utf16toutf8_len(utf16, len);
  if (new_len == -1)
    return NULL;
  out = calloc(1, new_len + 1);
  if (!util_utf16toutf8(out, new_len + 1, utf16, len)) {
    free(out);
    return NULL;
  }
  return out;
}


x86 assembly (clang, -O0):
<util_utf16touft8_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <util_utf16toutf8_len@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1175 <util_utf16touft8_alloc+0x35>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <util_utf16touft8_alloc+0x86>
mov    -0x24(%rbp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1060 <util_utf16toutf8@plt>
cmp    $0x0,%eax
jne    11be <util_utf16touft8_alloc+0x7e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <util_utf16touft8_alloc+0x86>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_utf16touft8_alloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
call   1040 <util_utf16toutf8_len@plt>
cmp    $0xffffffff,%eax
je     1187 <util_utf16touft8_alloc+0x47>
mov    %eax,%ebp
add    $0x1,%ebp
mov    $0x1,%edi
mov    %ebp,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1060 <util_utf16toutf8@plt>
test   %eax,%eax
jne    1189 <util_utf16touft8_alloc+0x49>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @util_utf16touft8_alloc(i8* %0, i32 %1) {
%3 = tail call i32 @util_utf16toutf8_len(i8* %0, i32 %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %12, label %5
5: ; preds = %2
%6 = add nuw nsw i32 %3, 1
%7 = tail call i8* @calloc(i32 1, i32 %6)
%8 = tail call i32 @util_utf16toutf8(i8* %7, i32 %6, i8* %0, i32 %1)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @free(i8* %7)
br label %12
12: ; preds = %5, %2, %10
%13 = phi i8* [ null, %10 ], [ null, %2 ], [ %7, %5 ]
ret i8* %13
}