#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 */
char *smm_alloc(int);
int /*<<< orphan*/ smm_errno_update(int);
int wcstombs(char *, void *, int);
char *smm_wcstombs_alloc(void *wcs) {
  char *buf;
  int len;
  smm_errno_update(0);
  if (wcs == NULL) {
    return NULL;
  }
  if ((len = wcstombs(NULL, wcs, 0)) == 0) {
    smm_errno_update((0xC0000000 | (9)));
    return NULL;
  }
  if ((buf = smm_alloc(len + 1)) == NULL) {
    smm_errno_update((0xC0000000 | (1)));
    return NULL;
  }
  wcstombs(buf, wcs, len + 1);
  return buf;
}


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

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

LLVM IR (-O0):
define i8* @smm_wcstombs_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 %30
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @wcstombs(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 %30
16: ; preds = %10
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
%19 = call i8* @smm_alloc(i32 %18)
store i8* %19, i8** %4
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @smm_errno_update(i32 -1073741823)
store i8* null, i8** %2
br label %30
23: ; preds = %16
%24 = load i8*, i8** %4
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
%28 = call i32 @wcstombs(i8* %24, i8* %25, i32 %27)
%29 = load i8*, i8** %4
store i8* %29, i8** %2
br label %30
30: ; preds = %23, %21, %14, %9
%31 = load i8*, i8** %2
ret i8* %31
}

LLVM IR (-O2):
define i8* @smm_wcstombs_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 @wcstombs(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 @wcstombs(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
}