#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*/ LoggerWarn(char *, char *);
scalar_t__ OS_UTF8ToANSI_DO(char *, int /*<<< orphan*/ *, unsigned long);
scalar_t__ malloc_s(unsigned long);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          unsigned long);
unsigned long strlen(char *);
wchar_t *UTF8ToANSI(char *utf8Text) {
  unsigned long length = strlen(utf8Text);
  wchar_t *ansiText = (wchar_t *)malloc_s((length + 2) * sizeof(wchar_t));
  memset(ansiText, 0, (length + 2) * sizeof(wchar_t));
  if (OS_UTF8ToANSI_DO(utf8Text, ansiText, length)) {
    LoggerWarn("A possible overflow happened when UTF8ToANSI:%s", utf8Text);
  }
  return ansiText;
}


x86 assembly (clang, -O0):
<UTF8ToANSI>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x2,%rdi
shl    $0x2,%rdi
call   1070 <malloc_s@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
add    $0x2,%rdx
shl    $0x2,%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <OS_UTF8ToANSI_DO@plt>
cmp    $0x0,%rax
je     11c0 <UTF8ToANSI+0x70>
mov    -0x8(%rbp),%rsi
lea    0xe45(%rip),%rdi
call   1060 <LoggerWarn@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UTF8ToANSI>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%r15
lea    0x8(,%rax,4),%rbx
mov    %rbx,%rdi
call   1070 <malloc_s@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1040 <OS_UTF8ToANSI_DO@plt>
test   %rax,%rax
je     11a5 <UTF8ToANSI+0x55>
lea    0xe63(%rip),%rdi
mov    %r14,%rsi
call   1060 <LoggerWarn@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [48 x i8] c"A possible overflow happened when UTF8ToANSI:%s\00"
define i32* @UTF8ToANSI(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @strlen(i8* %5)
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = add i64 %7, 2
%9 = mul i64 %8, 4
%10 = call i64 @malloc_s(i64 %9)
%11 = inttoptr i64 %10 to i32*
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = load i64, i64* %3
%14 = add i64 %13, 2
%15 = mul i64 %14, 4
%16 = call i32 @memset(i32* %12, i32 0, i64 %15)
%17 = load i8*, i8** %2
%18 = load i32*, i32** %4
%19 = load i64, i64* %3
%20 = call i64 @OS_UTF8ToANSI_DO(i8* %17, i32* %18, i64 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %1
%23 = load i8*, i8** %2
%24 = call i32 @LoggerWarn(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %1
%26 = load i32*, i32** %4
ret i32* %26
}

LLVM IR (-O2):
@.str = [48 x i8] c"A possible overflow happened when UTF8ToANSI:%s\00"
define i32* @UTF8ToANSI(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = shl i64 %2, 2
%4 = add i64 %3, 8
%5 = tail call i64 @malloc_s(i64 %4)
%6 = inttoptr i64 %5 to i32*
%7 = tail call i32 @memset(i32* %6, i32 0, i64 %4)
%8 = tail call i64 @OS_UTF8ToANSI_DO(i8* %0, i32* %6, i64 %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = tail call i32 @LoggerWarn(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %10, %1
ret i32* %6
}