#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 *);
int /*<<< orphan*/ *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, size_t);
int strlen(char *);
wchar_t *fatchartows(wchar_t *dst, char *src, int len, int *err) {
  wchar_t *dstscan;
  size_t srclen, dstlen;
  iconv_t cd;
  int res;
  if (len == -1)
    len = strlen(src) + 1;
  srclen = len * sizeof(char);
  dstlen = len * sizeof(wchar_t);
  if (dst == NULL)
    dst = malloc(dstlen);
  dstscan = dst;
  cd = iconv_open("WCHAR_T"
                  "//TRANSLIT",
                  "ASCII");
  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):
<fatchartows>:
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 <fatchartows+0x30>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %rax,-0x30(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11d0 <fatchartows+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    0xe2c(%rip),%rsi
call   1090 <iconv_open@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x28(%rbp),%rcx
lea    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%r8
call   1030 <iconv@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jle    123e <fatchartows+0xce>
movslq -0x40(%rbp),%rsi
lea    0xdfa(%rip),%rdi
call   1050 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
je     1239 <fatchartows+0xc9>
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
jmp    127e <fatchartows+0x10e>
cmpl   $0xffffffff,-0x40(%rbp)
jne    1279 <fatchartows+0x109>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
cmpq   $0x0,-0x20(%rbp)
je     1274 <fatchartows+0x104>
mov    0x2d7a(%rip),%rax
imul   $0x3e8,(%rax),%ecx
mov    -0x20(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
jmp    1279 <fatchartows+0x109>
jmp    127e <fatchartows+0x10e>
mov    -0x3c(%rbp),%edi
call   1070 <iconv_close@plt>
cmpq   $0x0,-0x30(%rbp)
je     12a1 <fatchartows+0x131>
mov    -0x30(%rbp),%rsi
lea    0xd9f(%rip),%rdi
call   1050 <printf@plt>
cmpq   $0x0,-0x38(%rbp)
je     12bc <fatchartows+0x14c>
mov    -0x38(%rbp),%rsi
lea    0xda2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fatchartows>:
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 <fatchartows+0x27>
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%edx
add    $0x1,%edx
movslq %edx,%rdi
mov    %rdi,0x10(%rsp)
shl    $0x2,%rdi
mov    %rdi,0x8(%rsp)
test   %r15,%r15
jne    11b5 <fatchartows+0x45>
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %r15,0x18(%rsp)
lea    0xe3f(%rip),%rdi
lea    0xe4a(%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    1208 <fatchartows+0x98>
mov    %ebx,%esi
lea    0xe1f(%rip),%rdi
call   1050 <printf@plt>
test   %r14,%r14
je     120d <fatchartows+0x9d>
add    %ebx,(%r14)
jmp    120d <fatchartows+0x9d>
cmp    $0xffffffff,%ebx
je     124e <fatchartows+0xde>
mov    %ebp,%edi
call   1070 <iconv_close@plt>
mov    0x10(%rsp),%rsi
test   %rsi,%rsi
je     122a <fatchartows+0xba>
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
mov    0x8(%rsp),%rsi
test   %rsi,%rsi
je     1240 <fatchartows+0xd0>
lea    0xe1e(%rip),%rdi
call   1050 <printf@plt>
mov    %r15,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde0(%rip),%rdi
call   1080 <perror@plt>
test   %r14,%r14
je     120d <fatchartows+0x9d>
mov    0x2d7a(%rip),%rax
imul   $0x3e8,(%rax),%ebx
jmp    1203 <fatchartows+0x93>

LLVM IR (-O0):
@.str = [18 x i8] c"WCHAR_T//TRANSLIT\00"
@.str.1 = [6 x i8] c"ASCII\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 i32* @fatchartows(i32* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %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 i8*, i8** %6
%18 = call i32 @strlen(i8* %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, 1
store i64 %23, i64* %10
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = mul i64 %25, 4
store i64 %26, i64* %11
%27 = load i32*, i32** %5
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = load i64, i64* %11
%31 = call i32* @malloc(i64 %30)
store i32* %31, i32** %5
br label %32
32: ; preds = %29, %20
%33 = load i32*, i32** %5
store i32* %33, i32** %9
%34 = call i32 @iconv_open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 %34, i32* %12
%35 = load i32, i32* %12
%36 = bitcast i32** %9 to i8**
%37 = call i32 @iconv(i32 %35, i8** %6, i64* %10, i8** %36, 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 i32*, i32** %5
ret i32* %82
}

LLVM IR (-O2):
@.str = [18 x i8] c"WCHAR_T//TRANSLIT\00"
@.str.1 = [6 x i8] c"ASCII\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 i32* @fatchartows(i32* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
store i8* %1, i8** %5
%9 = bitcast i32** %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 @strlen(i8* %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
store i64 %18, i64* %7
%19 = shl nsw i64 %18, 2
store i64 %19, i64* %8
%20 = icmp eq i32* %0, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32* @malloc(i64 %19)
br label %23
23: ; preds = %21, %16
%24 = phi i32* [ %22, %21 ], [ %0, %16 ]
store i32* %24, i32** %6
%25 = tail call i32 @iconv_open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%26 = bitcast i32** %6 to i8**
%27 = call i32 @iconv(i32 %25, i8** %5, i64* %7, i8** %26, 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 i32* %24
}