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

/* Variables and functions */
char *bmalloc(size_t);
size_t os_wcs_to_utf8(int /*<<< orphan*/ const *, size_t, char *, size_t);
size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr) {
  if (str) {
    size_t out_len = os_wcs_to_utf8(str, len, NULL, 0);
    *pstr = bmalloc((out_len + 1) * sizeof(char));
    return os_wcs_to_utf8(str, len, *pstr, out_len + 1);
  } else {
    *pstr = NULL;
    return 0;
  }
}


x86 assembly (clang, -O0):
<os_wcs_to_utf8_ptr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1197 <os_wcs_to_utf8_ptr+0x77>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <os_wcs_to_utf8@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1030 <bmalloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rcx
add    $0x1,%rcx
call   1040 <os_wcs_to_utf8@plt>
mov    %rax,-0x8(%rbp)
jmp    11aa <os_wcs_to_utf8_ptr+0x8a>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_wcs_to_utf8_ptr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdi,%rdi
je     116a <os_wcs_to_utf8_ptr+0x4a>
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <os_wcs_to_utf8@plt>
lea    0x1(%rax),%r12
mov    %r12,%rdi
call   1030 <bmalloc@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %r12,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <os_wcs_to_utf8@plt>
movq   $0x0,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i64 @os_wcs_to_utf8_ptr(i32* %0, i64 %1, i8** nocapture writeonly %2) {
%4 = icmp eq i32* %0, null
br i1 %4, label %10, label %5
5: ; preds = %3
%6 = tail call i64 @os_wcs_to_utf8(i32* %0, i64 %1, i8* null, i64 0)
%7 = add i64 %6, 1
%8 = tail call i8* @bmalloc(i64 %7)
store i8* %8, i8** %2
%9 = tail call i64 @os_wcs_to_utf8(i32* %0, i64 %1, i8* %8, i64 %7)
br label %11
10: ; preds = %3
store i8* null, i8** %2
br label %11
11: ; preds = %10, %5
%12 = phi i64 [ %9, %5 ], [ 0, %10 ]
ret i64 %12
}