#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;
typedef int /*<<< orphan*/ iconv_t;

/* Variables and functions */
int errno;
int iconv(int /*<<< orphan*/, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(int /*<<< orphan*/);
int /*<<< orphan*/ iconv_open(char *, char *);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, size_t);
int wcslen(int /*<<< orphan*/ *);
char *fatwstochar(char *dst, wchar_t *src, int len, int *err) {
  char *dstscan;
  size_t srclen, dstlen;
  iconv_t cd;
  int res;
  if (len == -1)
    len = wcslen(src) + 1;
  srclen = len * sizeof(wchar_t);
  dstlen = len * sizeof(char);
  if (dst == NULL)
    dst = malloc(dstlen);
  dstscan = dst;
  cd = iconv_open("ASCII"
                  "//TRANSLIT",
                  "WCHAR_T");
  res = iconv(cd, (char **)&src, (size_t *)&srclen, (char **)&dstscan, &dstlen);
  if (res > 0) {
    printf("%d characters not converted\n", res);
    if (err)
      *err += res;
  } else if (res == -1) {
    perror("iconv");
    if (err)
      *err += 1000 * errno;
  }
  iconv_close(cd);
  if (srclen != 0)
    printf("%zu characters not converted\n", srclen);
  if (dstlen != 0)
    printf("%zu characters not filled\n", dstlen);
  return dst;
}


x86 assembly (clang, -O0):
<fatwstochar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a0 <fatwstochar+0x30>
mov    -0x10(%rbp),%rdi
call   1050 <wcslen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x30(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11d0 <fatwstochar+0x60>
mov    -0x38(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    0xe21(%rip),%rdi
lea    0xe2a(%rip),%rsi
call   1090 <iconv_open@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   1030 <iconv@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jle    123e <fatwstochar+0xce>
movslq -0x40(%rbp),%rsi
lea    0xdfa(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
je     1239 <fatwstochar+0xc9>
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
jmp    127e <fatwstochar+0x10e>
cmpl   $0xffffffff,-0x40(%rbp)
jne    1279 <fatwstochar+0x109>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
cmpq   $0x0,-0x20(%rbp)
je     1274 <fatwstochar+0x104>
mov    0x2d7a(%rip),%rax
imul   $0x3e8,(%rax),%ecx
mov    -0x20(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
jmp    1279 <fatwstochar+0x109>
jmp    127e <fatwstochar+0x10e>
mov    -0x3c(%rbp),%edi
call   1070 <iconv_close@plt>
cmpq   $0x0,-0x30(%rbp)
je     12a1 <fatwstochar+0x131>
mov    -0x30(%rbp),%rsi
lea    0xd9f(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x38(%rbp)
je     12bc <fatwstochar+0x14c>
mov    -0x38(%rbp),%rsi
lea    0xda2(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fatwstochar>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rdi,%r15
mov    %rsi,0x20(%rsp)
cmp    $0xffffffff,%edx
jne    1197 <fatwstochar+0x27>
mov    %rsi,%rdi
call   1050 <wcslen@plt>
mov    %eax,%edx
add    $0x1,%edx
movslq %edx,%rdi
lea    0x0(,%rdi,4),%rax
mov    %rax,0x10(%rsp)
mov    %rdi,0x8(%rsp)
test   %r15,%r15
jne    11b9 <fatwstochar+0x49>
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %r15,0x18(%rsp)
lea    0xe3b(%rip),%rdi
lea    0xe44(%rip),%rsi
call   1090 <iconv_open@plt>
mov    %eax,%ebp
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %eax,%edi
call   1030 <iconv@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    120c <fatwstochar+0x9c>
mov    %ebx,%esi
lea    0xe1b(%rip),%rdi
call   1040 <printf@plt>
test   %r14,%r14
je     1211 <fatwstochar+0xa1>
add    %ebx,(%r14)
jmp    1211 <fatwstochar+0xa1>
cmp    $0xffffffff,%ebx
je     1252 <fatwstochar+0xe2>
mov    %ebp,%edi
call   1070 <iconv_close@plt>
mov    0x10(%rsp),%rsi
test   %rsi,%rsi
je     122e <fatwstochar+0xbe>
lea    0xe12(%rip),%rdi
call   1040 <printf@plt>
mov    0x8(%rsp),%rsi
test   %rsi,%rsi
je     1244 <fatwstochar+0xd4>
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
mov    %r15,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xddc(%rip),%rdi
call   1080 <perror@plt>
test   %r14,%r14
je     1211 <fatwstochar+0xa1>
mov    0x2d76(%rip),%rax
imul   $0x3e8,(%rax),%ebx
jmp    1207 <fatwstochar+0x97>

LLVM IR (-O0):
@.str = [16 x i8] c"ASCII//TRANSLIT\00"
@.str.1 = [8 x i8] c"WCHAR_T\00"
@.str.2 = [29 x i8] c"%d characters not converted\0A\00"
@.str.3 = [6 x i8] c"iconv\00"
@errno = global i32 0
@.str.4 = [30 x i8] c"%zu characters not converted\0A\00"
@.str.5 = [27 x i8] c"%zu characters not filled\0A\00"
define i8* @fatwstochar(i8* %0, i32* %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %4
%17 = load i32*, i32** %6
%18 = call i32 @wcslen(i32* %17)
%19 = add nsw i32 %18, 1
store i32 %19, i32* %7
br label %20
20: ; preds = %16, %4
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 4
store i64 %23, i64* %10
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = mul i64 %25, 1
store i64 %26, i64* %11
%27 = load i8*, i8** %5
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = load i64, i64* %11
%31 = call i8* @malloc(i64 %30)
store i8* %31, i8** %5
br label %32
32: ; preds = %29, %20
%33 = load i8*, i8** %5
store i8* %33, i8** %9
%34 = call i32 @iconv_open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 %34, i32* %12
%35 = load i32, i32* %12
%36 = bitcast i32** %6 to i8**
%37 = call i32 @iconv(i32 %35, i8** %36, i64* %10, i8** %9, i64* %11)
store i32 %37, i32* %13
%38 = load i32, i32* %13
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %52
40: ; preds = %32
%41 = load i32, i32* %13
%42 = sext i32 %41 to i64
%43 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 %42)
%44 = load i32*, i32** %8
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %51
46: ; preds = %40
%47 = load i32, i32* %13
%48 = load i32*, i32** %8
%49 = load i32, i32* %48
%50 = add nsw i32 %49, %47
store i32 %50, i32* %48
br label %51
51: ; preds = %46, %40
br label %67
52: ; preds = %32
%53 = load i32, i32* %13
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %66
55: ; preds = %52
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32*, i32** %8
%58 = icmp ne i32* %57, null
br i1 %58, label %59, label %65
59: ; preds = %55
%60 = load i32, i32* @errno
%61 = mul nsw i32 1000, %60
%62 = load i32*, i32** %8
%63 = load i32, i32* %62
%64 = add nsw i32 %63, %61
store i32 %64, i32* %62
br label %65
65: ; preds = %59, %55
br label %66
66: ; preds = %65, %52
br label %67
67: ; preds = %66, %51
%68 = load i32, i32* %12
%69 = call i32 @iconv_close(i32 %68)
%70 = load i64, i64* %10
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %67
%73 = load i64, i64* %10
%74 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 %73)
br label %75
75: ; preds = %72, %67
%76 = load i64, i64* %11
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i64, i64* %11
%80 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i64 %79)
br label %81
81: ; preds = %78, %75
%82 = load i8*, i8** %5
ret i8* %82
}

LLVM IR (-O2):
@.str = [16 x i8] c"ASCII//TRANSLIT\00"
@.str.1 = [8 x i8] c"WCHAR_T\00"
@.str.2 = [29 x i8] c"%d characters not converted\0A\00"
@.str.3 = [6 x i8] c"iconv\00"
@errno = global i32 0
@.str.4 = [30 x i8] c"%zu characters not converted\0A\00"
@.str.5 = [27 x i8] c"%zu characters not filled\0A\00"
define i8* @fatwstochar(i8* %0, i32* %1, i32 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32* %1, i32** %5
%9 = bitcast i8** %6 to i8*
%10 = bitcast i64* %7 to i8*
%11 = bitcast i64* %8 to i8*
%12 = icmp eq i32 %2, -1
br i1 %12, label %13, label %16
13: ; preds = %4
%14 = tail call i32 @wcslen(i32* %1)
%15 = add nsw i32 %14, 1
br label %16
16: ; preds = %13, %4
%17 = phi i32 [ %15, %13 ], [ %2, %4 ]
%18 = sext i32 %17 to i64
%19 = shl nsw i64 %18, 2
store i64 %19, i64* %7
store i64 %18, i64* %8
%20 = icmp eq i8* %0, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i8* @malloc(i64 %18)
br label %23
23: ; preds = %21, %16
%24 = phi i8* [ %22, %21 ], [ %0, %16 ]
store i8* %24, i8** %6
%25 = tail call i32 @iconv_open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%26 = bitcast i32** %5 to i8**
%27 = call i32 @iconv(i32 %25, i8** %26, i64* %7, i8** %6, i64* %8)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = zext i32 %27 to i64
%31 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 %30)
%32 = icmp eq i32* %3, null
br i1 %32, label %45, label %41
33: ; preds = %23
%34 = icmp eq i32 %27, -1
br i1 %34, label %35, label %45
35: ; preds = %33
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp eq i32* %3, null
br i1 %37, label %45, label %38
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = mul nsw i32 %39, 1000
br label %41
41: ; preds = %29, %38
%42 = phi i32 [ %40, %38 ], [ %27, %29 ]
%43 = load i32, i32* %3
%44 = add nsw i32 %43, %42
store i32 %44, i32* %3
br label %45
45: ; preds = %41, %33, %35, %29
%46 = call i32 @iconv_close(i32 %25)
%47 = load i64, i64* %7
%48 = icmp eq i64 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %45
%50 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 %47)
br label %51
51: ; preds = %49, %45
%52 = load i64, i64* %8
%53 = icmp eq i64 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %51
%55 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i64 %52)
br label %56
56: ; preds = %54, %51
ret i8* %24
}