#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 scalar_t__ iconv_t;

/* Variables and functions */
size_t iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
bool unicode_to_utf8(char *inbuf, size_t *inlen, char *outbuf, size_t *outlen) {
  char *encTo = "UTF-8//IGNORE";
  char *encFrom = "UNICODE";
  iconv_t cd = iconv_open(encTo, encFrom);
  if (cd == (iconv_t)-1) {
    perror("iconv_open");
  }
  printf("inbuf=%s\n", inbuf);
  printf("inlen=%d\n", *inlen);
  char *tmpin = inbuf;
  char *tmpout = outbuf;
  size_t insize = *inlen;
  size_t outsize = *outlen;
  size_t ret = iconv(cd, &tmpin, inlen, &tmpout, outlen);
  if (ret == -1) {
    perror("iconv");
  }
  printf("outbuf=%s\n", outbuf);
  printf("outlen=%d\n", *outlen);
  int i = 0;
  for (i = 0; i < (outsize - (*outlen)); i++) {
    printf("%x\n", outbuf[i]);
  }
  iconv_close(cd);
  return 0;
}


x86 assembly (clang, -O0):
<unicode_to_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    0xe91(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe94(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <iconv_open@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0xffffffffffffffff,-0x38(%rbp)
jne    11a6 <unicode_to_utf8+0x56>
lea    0xe75(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%rsi
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%r8
lea    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%rcx
call   1030 <iconv@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0xffffffffffffffff,-0x60(%rbp)
jne    1227 <unicode_to_utf8+0xd7>
lea    0xe13(%rip),%rdi
call   1060 <perror@plt>
mov    -0x18(%rbp),%rsi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x64(%rbp)
movl   $0x0,-0x64(%rbp)
movslq -0x64(%rbp),%rax
mov    -0x58(%rbp),%rcx
mov    -0x20(%rbp),%rdx
sub    (%rdx),%rcx
cmp    %rcx,%rax
jae    129c <unicode_to_utf8+0x14c>
mov    -0x18(%rbp),%rax
movslq -0x64(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    125c <unicode_to_utf8+0x10c>
mov    -0x38(%rbp),%rdi
call   1050 <iconv_close@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unicode_to_utf8>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
lea    0xe8f(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1070 <iconv_open@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     1247 <unicode_to_utf8+0xf7>
lea    0xe90(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r12),%rsi
lea    0xe85(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13,0x10(%rsp)
mov    %r14,0x8(%rsp)
mov    (%r15),%rbp
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rcx
mov    %rbx,%r13
mov    %rbx,%rdi
mov    %r12,%rdx
mov    %r15,%r8
call   1030 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
je     1258 <unicode_to_utf8+0x108>
lea    0xe59(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r15),%rsi
lea    0xe50(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    (%r15),%rbp
je     122e <unicode_to_utf8+0xde>
lea    0xe48(%rip),%r12
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r14,%rbx,1),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
mov    %rbp,%rax
sub    (%r15),%rax
cmp    %rbx,%rax
ja     1210 <unicode_to_utf8+0xc0>
mov    %r13,%rdi
call   1050 <iconv_close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc8(%rip),%rdi
call   1060 <perror@plt>
jmp    118a <unicode_to_utf8+0x3a>
lea    0xdd6(%rip),%rdi
call   1060 <perror@plt>
jmp    11db <unicode_to_utf8+0x8b>

LLVM IR (-O0):
@.str = [14 x i8] c"UTF-8//IGNORE\00"
@.str.1 = [8 x i8] c"UNICODE\00"
@.str.2 = [11 x i8] c"iconv_open\00"
@.str.3 = [10 x i8] c"inbuf=%s\0A\00"
@.str.4 = [10 x i8] c"inlen=%d\0A\00"
@.str.5 = [6 x i8] c"iconv\00"
@.str.6 = [11 x i8] c"outbuf=%s\0A\00"
@.str.7 = [11 x i8] c"outlen=%d\0A\00"
@.str.8 = [4 x i8] c"%x\0A\00"
define i32 @unicode_to_utf8(i8* %0, i64* %1, i8* %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca i32
store i8* %0, i8** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
store i64* %3, i64** %8
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %9
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %10
%18 = load i8*, i8** %9
%19 = load i8*, i8** %10
%20 = call i64 @iconv_open(i8* %18, i8* %19)
store i64 %20, i64* %11
%21 = load i64, i64* %11
%22 = icmp eq i64 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %4
%24 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %4
%26 = load i8*, i8** %5
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = load i64*, i64** %6
%29 = load i64, i64* %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i64 %29)
%31 = load i8*, i8** %5
store i8* %31, i8** %12
%32 = load i8*, i8** %7
store i8* %32, i8** %13
%33 = load i64*, i64** %6
%34 = load i64, i64* %33
store i64 %34, i64* %14
%35 = load i64*, i64** %8
%36 = load i64, i64* %35
store i64 %36, i64* %15
%37 = load i64, i64* %11
%38 = load i64*, i64** %6
%39 = load i64*, i64** %8
%40 = call i64 @iconv(i64 %37, i8** %12, i64* %38, i8** %13, i64* %39)
store i64 %40, i64* %16
%41 = load i64, i64* %16
%42 = icmp eq i64 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %25
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %45
45: ; preds = %43, %25
%46 = load i8*, i8** %7
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %46)
%48 = load i64*, i64** %8
%49 = load i64, i64* %48
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i64 %49)
store i32 0, i32* %17
store i32 0, i32* %17
br label %51
51: ; preds = %67, %45
%52 = load i32, i32* %17
%53 = sext i32 %52 to i64
%54 = load i64, i64* %15
%55 = load i64*, i64** %8
%56 = load i64, i64* %55
%57 = sub i64 %54, %56
%58 = icmp ult i64 %53, %57
br i1 %58, label %59, label %70
59: ; preds = %51
%60 = load i8*, i8** %7
%61 = load i32, i32* %17
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %59
%68 = load i32, i32* %17
%69 = add nsw i32 %68, 1
store i32 %69, i32* %17
br label %51
70: ; preds = %51
%71 = load i64, i64* %11
%72 = call i32 @iconv_close(i64 %71)
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"UTF-8//IGNORE\00"
@.str.1 = [8 x i8] c"UNICODE\00"
@.str.2 = [11 x i8] c"iconv_open\00"
@.str.3 = [10 x i8] c"inbuf=%s\0A\00"
@.str.4 = [10 x i8] c"inlen=%d\0A\00"
@.str.5 = [6 x i8] c"iconv\00"
@.str.6 = [11 x i8] c"outbuf=%s\0A\00"
@.str.7 = [11 x i8] c"outlen=%d\0A\00"
@.str.8 = [4 x i8] c"%x\0A\00"
define i32 @unicode_to_utf8(i8* %0, i64* %1, i8* %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = tail call i64 @iconv_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i64 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %4
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%13 = load i64, i64* %1
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i64 %13)
%15 = bitcast i8** %5 to i8*
store i8* %0, i8** %5
%16 = bitcast i8** %6 to i8*
store i8* %2, i8** %6
%17 = load i64, i64* %3
%18 = call i64 @iconv(i64 %7, i8** %5, i64* %1, i8** %6, i64* %3)
%19 = icmp eq i64 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %22
22: ; preds = %20, %11
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%24 = load i64, i64* %3
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i64 %24)
%26 = load i64, i64* %3
%27 = icmp eq i64 %17, %26
br i1 %27, label %38, label %28
28: ; preds = %22, %28
%29 = phi i64 [ %34, %28 ], [ 0, %22 ]
%30 = getelementptr inbounds i8, i8* %2, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 %32)
%34 = add nuw i64 %29, 1
%35 = load i64, i64* %3
%36 = sub i64 %17, %35
%37 = icmp ugt i64 %36, %34
br i1 %37, label %28, label %38
38: ; preds = %28, %22
%39 = call i32 @iconv_close(i64 %7)
ret i32 0
}