#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 */
int /*<<< orphan*/ bfree(int /*<<< orphan*/ *);
size_t os_mbs_to_wcs_ptr(char const *, size_t, int /*<<< orphan*/ **);
size_t os_wcs_to_utf8_ptr(int /*<<< orphan*/ *, size_t, char **);
size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr) {
  char *dst = NULL;
  size_t out_len = 0;
  if (str) {
    wchar_t *wstr = NULL;
    size_t wlen = os_mbs_to_wcs_ptr(str, len, &wstr);
    out_len = os_wcs_to_utf8_ptr(wstr, wlen, &dst);
    bfree(wstr);
  }
  *pstr = dst;
  return out_len;
}


x86 assembly (clang, -O0):
<os_mbs_to_utf8_ptr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     119a <os_mbs_to_utf8_ptr+0x6a>
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1030 <os_mbs_to_wcs_ptr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <os_wcs_to_utf8_ptr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <bfree@plt>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_mbs_to_utf8_ptr>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
movq   $0x0,0x10(%rsp)
test   %rdi,%rdi
je     1181 <os_mbs_to_utf8_ptr+0x51>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdx
call   1030 <os_mbs_to_wcs_ptr@plt>
mov    0x8(%rsp),%rdi
lea    0x10(%rsp),%rdx
mov    %rax,%rsi
call   1050 <os_wcs_to_utf8_ptr@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rdi
call   1040 <bfree@plt>
mov    0x10(%rsp),%rax
jmp    1185 <os_mbs_to_utf8_ptr+0x55>
xor    %eax,%eax
xor    %ebx,%ebx
mov    %rax,(%r14)
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i64 @os_mbs_to_utf8_ptr(i8* %0, i64 %1, i8** nocapture writeonly %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%7 = icmp eq i8* %0, null
br i1 %7, label %16, label %8
8: ; preds = %3
%9 = bitcast i32** %5 to i8*
store i32* null, i32** %5
%10 = call i64 @os_mbs_to_wcs_ptr(i8* %0, i64 %1, i32** %5)
%11 = load i32*, i32** %5
%12 = call i64 @os_wcs_to_utf8_ptr(i32* %11, i64 %10, i8** %4)
%13 = load i32*, i32** %5
%14 = call i32 @bfree(i32* %13)
%15 = load i8*, i8** %4
br label %16
16: ; preds = %8, %3
%17 = phi i8* [ %15, %8 ], [ null, %3 ]
%18 = phi i64 [ %12, %8 ], [ 0, %3 ]
store i8* %17, i8** %2
ret i64 %18
}