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

/* Variables and functions */
int mbstowcs(void *, char *, int);
void *smm_alloc(int);
int /*<<< orphan*/ smm_errno_update(int);
void *smm_mbstowcs_alloc(char *mbs) {
  wchar_t *buf;
  int len;
  smm_errno_update(0);
  if (mbs == NULL) {
    return NULL;
  }
  if ((len = mbstowcs(NULL, mbs, 0)) == 0) {
    smm_errno_update((0xC0000000 | (9)));
    return NULL;
  }
  if ((buf = smm_alloc((len + 1) * sizeof(wchar_t))) == NULL) {
    smm_errno_update((0xC0000000 | (1)));
    return NULL;
  }
  mbstowcs(buf, mbs, len + 1);
  return buf;
}


x86 assembly (clang, -O0):
<smm_mbstowcs_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %edi,%edi
call   1050 <smm_errno_update@plt>
cmpq   $0x0,-0x10(%rbp)
jne    115b <smm_mbstowcs_alloc+0x2b>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <smm_mbstowcs_alloc+0xb0>
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
xor    %edx,%edx
call   1030 <mbstowcs@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    118d <smm_mbstowcs_alloc+0x5d>
mov    $0xc0000009,%edi
call   1050 <smm_errno_update@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <smm_mbstowcs_alloc+0xb0>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <smm_alloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11c5 <smm_mbstowcs_alloc+0x95>
mov    $0xc0000001,%edi
call   1050 <smm_errno_update@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <smm_mbstowcs_alloc+0xb0>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1030 <mbstowcs@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smm_mbstowcs_alloc>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <smm_errno_update@plt>
test   %r14,%r14
je     118b <smm_mbstowcs_alloc+0x5b>
xor    %edi,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <mbstowcs@plt>
test   %eax,%eax
je     1178 <smm_mbstowcs_alloc+0x48>
mov    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1040 <smm_alloc@plt>
test   %rax,%rax
je     117f <smm_mbstowcs_alloc+0x4f>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <mbstowcs@plt>
jmp    118b <smm_mbstowcs_alloc+0x5b>
mov    $0xc0000009,%edi
jmp    1184 <smm_mbstowcs_alloc+0x54>
mov    $0xc0000001,%edi
call   1050 <smm_errno_update@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @smm_mbstowcs_alloc(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = call i32 @smm_errno_update(i32 0)
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %33
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @mbstowcs(i8* null, i8* %11, i32 0)
store i32 %12, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @smm_errno_update(i32 -1073741815)
store i8* null, i8** %2
br label %33
16: ; preds = %10
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 1
%21 = trunc i64 %20 to i32
%22 = call i8* @smm_alloc(i32 %21)
store i8* %22, i8** %4
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @smm_errno_update(i32 -1073741823)
store i8* null, i8** %2
br label %33
26: ; preds = %16
%27 = load i8*, i8** %4
%28 = load i8*, i8** %3
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
%31 = call i32 @mbstowcs(i8* %27, i8* %28, i32 %30)
%32 = load i8*, i8** %4
store i8* %32, i8** %2
br label %33
33: ; preds = %26, %24, %14, %9
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
define i8* @smm_mbstowcs_alloc(i8* %0) {
%2 = tail call i32 @smm_errno_update(i32 0)
%3 = icmp eq i8* %0, null
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = tail call i32 @mbstowcs(i8* null, i8* %0, i32 0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @smm_errno_update(i32 -1073741815)
br label %17
9: ; preds = %4
%10 = add nsw i32 %5, 1
%11 = tail call i8* @smm_alloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @smm_errno_update(i32 -1073741823)
br label %17
15: ; preds = %9
%16 = tail call i32 @mbstowcs(i8* %11, i8* %0, i32 %10)
br label %17
17: ; preds = %1, %15, %13, %7
%18 = phi i8* [ null, %7 ], [ null, %13 ], [ %11, %15 ], [ null, %1 ]
ret i8* %18
}