#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 */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char *, char *);
scalar_t__ memcmp(char *, char const *, int);
int /*<<< orphan*/ printf(char *, unsigned char);
int /*<<< orphan*/ puts(char *);
size_t strlen(char const *);
int main(int argc, char **argv) {
  const char in[] = "\x41\x42\x43\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa";
  const char exp[] = "\x41\x42\x43\x82\xa0\x82\xa2\x82\xa4";
  size_t outbufsize = 10;
  size_t inleft, outleft;
  char *in_p = (char *)in;
  char out[outbufsize];
  char *out_p = out;
  iconv_t cd;
  int i;
  inleft = strlen(in);
  outleft = outbufsize;
  cd = iconv_open("SJIS", "eucJP");
  if (cd == (iconv_t)-1) {
    puts("iconv_open failed");
    exit(1);
  }
  iconv(cd, &in_p, &inleft, &out_p, &outleft);
  for (i = 0; i < outbufsize - outleft; ++i)
    printf(" %02x", (unsigned char)out[i]);
  puts("");
  iconv_close(cd);
  return outbufsize - outleft != 9 || memcmp(out, exp, 9) != 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movabs $0xaaa4a8a4a6a4a4,%rax
mov    %rax,-0x1a(%rbp)
movabs $0xa4a4a4a2a4434241,%rax
a4 a4 a4
mov    %rax,-0x20(%rbp)
movabs $0x82a282a082434241,%rax
82 a2 82
mov    %rax,-0x30(%rbp)
movw   $0xa4,-0x28(%rbp)
movq   $0xa,-0x38(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rcx
mov    %rsp,%rax
mov    %rax,-0x58(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x80(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x60(%rbp)
mov    %rax,-0x68(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    0xdf8(%rip),%rdi
lea    0xdf6(%rip),%rsi
call   10a0 <iconv_open@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0xffffffffffffffff,-0x70(%rbp)
jne    1251 <main+0xd1>
lea    0xde1(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x70(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x68(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   1030 <iconv@plt>
movl   $0x0,-0x74(%rbp)
movslq -0x74(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    -0x48(%rbp),%rcx
cmp    %rcx,%rax
jae    12ac <main+0x12c>
mov    -0x80(%rbp),%rax
movslq -0x74(%rbp),%rcx
lea    0xda0(%rip),%rdi
movzbl (%rax,%rcx,1),%esi
call   1060 <printf@plt>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    1271 <main+0xf1>
lea    0xd6f(%rip),%rdi
call   1040 <puts@plt>
mov    -0x70(%rbp),%rdi
call   1080 <iconv_close@plt>
mov    -0x38(%rbp),%rcx
sub    -0x48(%rbp),%rcx
mov    $0x1,%al
cmp    $0x9,%rcx
mov    %al,-0x81(%rbp)
jne    12fa <main+0x17a>
mov    -0x80(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x9,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x81(%rbp)
mov    -0x81(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsp,%r15
movabs $0xaaa4a8a4a6a4a4,%rax
mov    %rax,0x4e(%rsp)
movabs $0xa4a4a4a2a4434241,%rax
a4 a4 a4
mov    %rax,0x48(%rsp)
movabs $0x82a282a082434241,%rax
82 a2 82
mov    %rax,0x20(%rsp)
movw   $0xa4,0x28(%rsp)
lea    0x48(%rsp),%rdi
mov    %rdi,0x38(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x30(%rsp)
call   1060 <strlen@plt>
mov    %rax,0x40(%rsp)
movq   $0xa,0x8(%rsp)
lea    0xe18(%rip),%rdi
lea    0xe16(%rip),%rsi
call   10b0 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     12bd <main+0x12d>
mov    %rax,%r14
lea    0x38(%rsp),%rsi
lea    0x40(%rsp),%rdx
lea    0x30(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rax,%rdi
call   1040 <iconv@plt>
cmpq   $0xa,0x8(%rsp)
je     1270 <main+0xe0>
xor    %ebx,%ebx
lea    0xdef(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
movzbl 0x10(%rsp,%rbx,1),%esi
mov    %r12,%rdi
call   1070 <printf@plt>
add    $0x1,%rbx
mov    $0xa,%eax
sub    0x8(%rsp),%rax
cmp    %rbx,%rax
ja     1250 <main+0xc0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   1090 <iconv_close@plt>
mov    $0x1,%eax
cmpq   $0x1,0x8(%rsp)
jne    12ae <main+0x11e>
lea    0x10(%rsp),%rdi
lea    0x20(%rsp),%rsi
mov    $0x9,%edx
call   1080 <memcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
mov    %r15,%rsp
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd5f(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.in = [14 x i8] c"ABC\A4\A2\A4\A4\A4\A6\A4\A8\A4\AA\00"
@__const.main.exp = [10 x i8] c"ABC\82\A0\82\A2\82\A4\00"
@.str = [5 x i8] c"SJIS\00"
@.str.1 = [6 x i8] c"eucJP\00"
@.str.2 = [18 x i8] c"iconv_open failed\00"
@.str.3 = [6 x i8] c" %02x\00"
@.str.4 = [1 x i8] zeroinitializer
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [14 x i8]
%7 = alloca [10 x i8]
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = bitcast [14 x i8]* %6 to i8*
%18 = bitcast [10 x i8]* %7 to i8*
store i64 10, i64* %8
%19 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
store i8* %19, i8** %11
%20 = load i64, i64* %8
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %12
%22 = alloca i8, i64 %20
store i64 %20, i64* %13
store i8* %22, i8** %14
%23 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%24 = call i64 @strlen(i8* %23)
store i64 %24, i64* %9
%25 = load i64, i64* %8
store i64 %25, i64* %10
%26 = call i64 @iconv_open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i64 %26, i64* %15
%27 = load i64, i64* %15
%28 = icmp eq i64 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %2
%30 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %2
%33 = load i64, i64* %15
%34 = call i32 @iconv(i64 %33, i8** %11, i64* %9, i8** %14, i64* %10)
store i32 0, i32* %16
br label %35
35: ; preds = %48, %32
%36 = load i32, i32* %16
%37 = sext i32 %36 to i64
%38 = load i64, i64* %8
%39 = load i64, i64* %10
%40 = sub i64 %38, %39
%41 = icmp ult i64 %37, %40
br i1 %41, label %42, label %51
42: ; preds = %35
%43 = load i32, i32* %16
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %22, i64 %44
%46 = load i8, i8* %45
%47 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %46)
br label %48
48: ; preds = %42
%49 = load i32, i32* %16
%50 = add nsw i32 %49, 1
store i32 %50, i32* %16
br label %35
51: ; preds = %35
%52 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
%53 = load i64, i64* %15
%54 = call i32 @iconv_close(i64 %53)
%55 = load i64, i64* %8
%56 = load i64, i64* %10
%57 = sub i64 %55, %56
%58 = icmp ne i64 %57, 9
br i1 %58, label %63, label %59
59: ; preds = %51
%60 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%61 = call i64 @memcmp(i8* %22, i8* %60, i32 9)
%62 = icmp ne i64 %61, 0
br label %63
63: ; preds = %59, %51
%64 = phi i1 [ true, %51 ], [ %62, %59 ]
%65 = zext i1 %64 to i32
store i32 %65, i32* %3
%66 = load i8*, i8** %12
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@__const.main.in = [14 x i8] c"ABC\A4\A2\A4\A4\A4\A6\A4\A8\A4\AA\00"
@__const.main.exp = [10 x i8] c"ABC\82\A0\82\A2\82\A4\00"
@.str = [5 x i8] c"SJIS\00"
@.str.1 = [6 x i8] c"eucJP\00"
@.str.2 = [18 x i8] c"iconv_open failed\00"
@.str.3 = [6 x i8] c" %02x\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [14 x i8]
%4 = alloca [10 x i8]
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%11 = bitcast i64* %5 to i8*
%12 = bitcast i64* %6 to i8*
%13 = bitcast i8** %7 to i8*
store i8* %9, i8** %7
%14 = call i8* @llvm.stacksave()
%15 = alloca [10 x i8]
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %15, i64 0, i64 0
%17 = bitcast i8** %8 to i8*
store i8* %16, i8** %8
%18 = call i64 @strlen(i8* %9)
store i64 %18, i64* %5
store i64 10, i64* %6
%19 = call i64 @iconv_open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = call i32 @iconv(i64 %19, i8** %7, i64* %5, i8** %8, i64* %6)
%26 = load i64, i64* %6
%27 = icmp eq i64 %26, 10
br i1 %27, label %37, label %28
28: ; preds = %24, %28
%29 = phi i64 [ %33, %28 ], [ 0, %24 ]
%30 = getelementptr inbounds [10 x i8], [10 x i8]* %15, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %31)
%33 = add nuw i64 %29, 1
%34 = load i64, i64* %6
%35 = sub i64 10, %34
%36 = icmp ugt i64 %35, %33
br i1 %36, label %28, label %37
37: ; preds = %28, %24
%38 = call i32 @putchar(i32 10)
%39 = call i32 @iconv_close(i64 %19)
%40 = load i64, i64* %6
%41 = icmp eq i64 %40, 1
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = call i64 @memcmp(i8* %16, i8* %10, i32 9)
%44 = icmp ne i64 %43, 0
%45 = zext i1 %44 to i32
br label %46
46: ; preds = %42, %37
%47 = phi i32 [ 1, %37 ], [ %45, %42 ]
ret i32 %47
}