#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 *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ util_utf8toutf16(unsigned char *, int, unsigned char *, int);
int util_utf8toutf16_len(unsigned char *);
unsigned char *util_utf8toutf16_alloc(unsigned char *utf8) {
  unsigned char *out;
  int new_len;
  new_len = util_utf8toutf16_len(utf8);
  if (new_len == -1)
    return NULL;
  out = calloc(1, new_len + 2);
  if (!util_utf8toutf16(out, new_len + 2, utf8, (int)strlen((char *)utf8))) {
    free(out);
    return NULL;
  }
  return out;
}


x86 assembly (clang, -O0):
<util_utf8toutf16_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <util_utf8toutf16_len@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    117f <util_utf8toutf16_alloc+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11ee <util_utf8toutf16_alloc+0x9e>
mov    -0x1c(%rbp),%esi
add    $0x2,%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
call   1070 <util_utf8toutf16@plt>
cmp    $0x0,%eax
jne    11e6 <util_utf8toutf16_alloc+0x96>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ee <util_utf8toutf16_alloc+0x9e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_utf8toutf16_alloc>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1040 <util_utf8toutf16_len@plt>
cmp    $0xffffffff,%eax
je     1198 <util_utf8toutf16_alloc+0x48>
mov    %eax,%ebp
add    $0x2,%ebp
mov    $0x1,%edi
mov    %ebp,%esi
call   1060 <calloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1070 <util_utf8toutf16@plt>
test   %eax,%eax
jne    119b <util_utf8toutf16_alloc+0x4b>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @util_utf8toutf16_alloc(i8* %0) {
%2 = tail call i32 @util_utf8toutf16_len(i8* %0)
%3 = icmp eq i32 %2, -1
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = add nsw i32 %2, 2
%6 = tail call i8* @calloc(i32 1, i32 %5)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = trunc i64 %7 to i32
%9 = tail call i32 @util_utf8toutf16(i8* %6, i32 %5, i8* %0, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = tail call i32 @free(i8* %6)
br label %13
13: ; preds = %4, %1, %11
%14 = phi i8* [ null, %11 ], [ null, %1 ], [ %6, %4 ]
ret i8* %14
}