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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ iconv(int /*<<< orphan*/, char **, size_t *, char **,
                         size_t *);
int /*<<< orphan*/ iconv_close(int /*<<< orphan*/);
int /*<<< orphan*/ iconv_open(char const *, char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int main(void) {
  const char *dummy_codesets[] = {"ISO_8859-1", "ISO_8859-2", "ISO_8859-3",
                                  "ISO_8859-4", "ISO_8859-5", "ISO_8859-6",
                                  "ISO_8859-7", "ISO_8859-8"};
  iconv_t dummy_cd[8], cd_a;
  int i;
  char buffer[1024], *to = buffer;
  char *from = (char *)"foobar";
  size_t to_left = 1024, from_left = 6;
  for (i = 0; i < 8; i++)
    if ((dummy_cd[i] = iconv_open(dummy_codesets[i], "UTF8")) == (iconv_t)-1)
      exit(1);
  if ((cd_a = iconv_open("EUC-JP", "UTF8")) == (iconv_t)-1)
    exit(1);
  iconv_close(cd_a);
  for (i = 0; i < 8; i++)
    iconv_close(dummy_cd[i]);
  if ((cd_a = iconv_open("EUC-JP", "UTF8")) == (iconv_t)-1)
    exit(1);
  puts("This used to crash");
  printf("%zd\n", iconv(cd_a, &from, &from_left, &to, &to_left));
  iconv_close(cd_a);
  puts("works now");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4a0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
lea    0x2c53(%rip),%rsi
mov    $0x40,%edx
call   1060 <memcpy@plt>
lea    -0x480(%rbp),%rax
mov    %rax,-0x488(%rbp)
lea    0xeac(%rip),%rax
mov    %rax,-0x490(%rbp)
movq   $0x400,-0x498(%rbp)
movq   $0x6,-0x4a0(%rbp)
movl   $0x0,-0x78(%rbp)
cmpl   $0x8,-0x78(%rbp)
jge    121d <main+0xad>
movslq -0x78(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rdi
lea    0xe75(%rip),%rsi
call   1090 <iconv_open@plt>
movslq -0x78(%rbp),%rcx
mov    %eax,-0x70(%rbp,%rcx,4)
cmp    $0xffffffff,%eax
jne    120a <main+0x9a>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    120f <main+0x9f>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    11d0 <main+0x60>
lea    0xe40(%rip),%rdi
lea    0xe34(%rip),%rsi
call   1090 <iconv_open@plt>
mov    %eax,-0x74(%rbp)
cmp    $0xffffffff,%eax
jne    1246 <main+0xd6>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x74(%rbp),%edi
call   1070 <iconv_close@plt>
movl   $0x0,-0x78(%rbp)
cmpl   $0x8,-0x78(%rbp)
jge    127a <main+0x10a>
movslq -0x78(%rbp),%rax
mov    -0x70(%rbp,%rax,4),%edi
call   1070 <iconv_close@plt>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    1255 <main+0xe5>
lea    0xde3(%rip),%rdi
lea    0xdd7(%rip),%rsi
call   1090 <iconv_open@plt>
mov    %eax,-0x74(%rbp)
cmp    $0xffffffff,%eax
jne    12a3 <main+0x133>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdc1(%rip),%rdi
call   1040 <puts@plt>
mov    -0x74(%rbp),%edi
lea    -0x490(%rbp),%rsi
lea    -0x4a0(%rbp),%rdx
lea    -0x488(%rbp),%rcx
lea    -0x498(%rbp),%r8
call   1030 <iconv@plt>
mov    %eax,%esi
lea    0xda2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x74(%rbp),%edi
call   1070 <iconv_close@plt>
lea    0xd93(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x4a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
lea    0x30(%rsp),%rax
mov    %rax,0x28(%rsp)
lea    0xed6(%rip),%rax
mov    %rax,0x20(%rsp)
movq   $0x400,0x18(%rsp)
movq   $0x6,0x10(%rsp)
lea    0xe60(%rip),%rdi
lea    0xeb8(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%eax
je     136a <main+0x20a>
lea    0xe4b(%rip),%rdi
lea    0xe98(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %eax,0x8(%rsp)
cmp    $0xffffffff,%eax
je     136a <main+0x20a>
lea    0xe36(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1080 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     136a <main+0x20a>
mov    %eax,%r12d
lea    0xe22(%rip),%rdi
lea    0xe59(%rip),%rsi
call   1080 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     136a <main+0x20a>
mov    %eax,%r13d
lea    0xe0e(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   1080 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     136a <main+0x20a>
mov    %eax,%ebx
lea    0xdfb(%rip),%rdi
lea    0xe1c(%rip),%rsi
call   1080 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     136a <main+0x20a>
mov    %eax,%ebp
lea    0xde8(%rip),%rdi
lea    0xdfe(%rip),%rsi
call   1080 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     136a <main+0x20a>
mov    %eax,%r14d
lea    0xdd4(%rip),%rdi
lea    0xddf(%rip),%rsi
call   1080 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     136a <main+0x20a>
mov    %eax,%r15d
lea    0xdcc(%rip),%rdi
lea    0xdc0(%rip),%rsi
call   1080 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     136a <main+0x20a>
mov    %eax,%edi
call   1060 <iconv_close@plt>
mov    0xc(%rsp),%edi
call   1060 <iconv_close@plt>
mov    0x8(%rsp),%edi
call   1060 <iconv_close@plt>
mov    %r12d,%edi
call   1060 <iconv_close@plt>
mov    %r13d,%edi
call   1060 <iconv_close@plt>
mov    %ebx,%edi
call   1060 <iconv_close@plt>
mov    %ebp,%edi
call   1060 <iconv_close@plt>
mov    %r14d,%edi
call   1060 <iconv_close@plt>
mov    %r15d,%edi
call   1060 <iconv_close@plt>
lea    0xd69(%rip),%rdi
lea    0xd5d(%rip),%rsi
call   1080 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     136a <main+0x20a>
mov    %eax,%ebx
lea    0xd56(%rip),%rdi
call   1040 <puts@plt>
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x28(%rsp),%rcx
lea    0x18(%rsp),%r8
mov    %ebx,%edi
call   1030 <iconv@plt>
lea    0xd42(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <iconv_close@plt>
lea    0xd32(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x438,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"ISO_8859-1\00"
@.str.1 = [11 x i8] c"ISO_8859-2\00"
@.str.2 = [11 x i8] c"ISO_8859-3\00"
@.str.3 = [11 x i8] c"ISO_8859-4\00"
@.str.4 = [11 x i8] c"ISO_8859-5\00"
@.str.5 = [11 x i8] c"ISO_8859-6\00"
@.str.6 = [11 x i8] c"ISO_8859-7\00"
@.str.7 = [11 x i8] c"ISO_8859-8\00"
@__const.main.dummy_codesets = [8 x i8*] [i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i32 0, i32 0)]
@.str.8 = [7 x i8] c"foobar\00"
@.str.9 = [5 x i8] c"UTF8\00"
@.str.10 = [7 x i8] c"EUC-JP\00"
@.str.11 = [19 x i8] c"This used to crash\00"
@.str.12 = [5 x i8] c"%zd\0A\00"
@.str.13 = [10 x i8] c"works now\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [8 x i8*]
%3 = alloca [8 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %1
%11 = bitcast [8 x i8*]* %2 to i8*
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i8* %12, i8** %7
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8** %8
store i64 1024, i64* %9
store i64 6, i64* %10
store i32 0, i32* %5
br label %13
13: ; preds = %29, %0
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 8
br i1 %15, label %16, label %32
16: ; preds = %13
%17 = load i32, i32* %5
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [8 x i8*], [8 x i8*]* %2, i64 0, i64 %18
%20 = load i8*, i8** %19
%21 = call i32 @iconv_open(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [8 x i32], [8 x i32]* %3, i64 0, i64 %23
store i32 %21, i32* %24
%25 = icmp eq i32 %21, -1
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
br label %29
29: ; preds = %28
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %13
32: ; preds = %13
%33 = call i32 @iconv_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
store i32 %33, i32* %4
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %32
%38 = load i32, i32* %4
%39 = call i32 @iconv_close(i32 %38)
store i32 0, i32* %5
br label %40
40: ; preds = %49, %37
%41 = load i32, i32* %5
%42 = icmp slt i32 %41, 8
br i1 %42, label %43, label %52
43: ; preds = %40
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [8 x i32], [8 x i32]* %3, i64 0, i64 %45
%47 = load i32, i32* %46
%48 = call i32 @iconv_close(i32 %47)
br label %49
49: ; preds = %43
%50 = load i32, i32* %5
%51 = add nsw i32 %50, 1
store i32 %51, i32* %5
br label %40
52: ; preds = %40
%53 = call i32 @iconv_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
store i32 %53, i32* %4
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %52
%58 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.11, i64 0, i64 0))
%59 = load i32, i32* %4
%60 = call i32 @iconv(i32 %59, i8** %8, i64* %10, i8** %7, i64* %9)
%61 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %4
%63 = call i32 @iconv_close(i32 %62)
%64 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.13, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"ISO_8859-1\00"
@.str.1 = [11 x i8] c"ISO_8859-2\00"
@.str.2 = [11 x i8] c"ISO_8859-3\00"
@.str.3 = [11 x i8] c"ISO_8859-4\00"
@.str.4 = [11 x i8] c"ISO_8859-5\00"
@.str.5 = [11 x i8] c"ISO_8859-6\00"
@.str.6 = [11 x i8] c"ISO_8859-7\00"
@.str.7 = [11 x i8] c"ISO_8859-8\00"
@.str.8 = [7 x i8] c"foobar\00"
@.str.9 = [5 x i8] c"UTF8\00"
@.str.10 = [7 x i8] c"EUC-JP\00"
@.str.11 = [19 x i8] c"This used to crash\00"
@.str.12 = [5 x i8] c"%zd\0A\00"
@.str.13 = [10 x i8] c"works now\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%7 = bitcast i8** %2 to i8*
store i8* %6, i8** %2
%8 = bitcast i8** %3 to i8*
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8** %3
%9 = bitcast i64* %4 to i8*
store i64 1024, i64* %4
%10 = bitcast i64* %5 to i8*
store i64 6, i64* %5
%11 = call i32 @iconv_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%12 = icmp eq i32 %11, -1
br i1 %12, label %37, label %13
13: ; preds = %0
%14 = call i32 @iconv_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%15 = icmp eq i32 %14, -1
br i1 %15, label %37, label %16
16: ; preds = %13
%17 = call i32 @iconv_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%18 = icmp eq i32 %17, -1
br i1 %18, label %37, label %19
19: ; preds = %16
%20 = call i32 @iconv_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%21 = icmp eq i32 %20, -1
br i1 %21, label %37, label %22
22: ; preds = %19
%23 = call i32 @iconv_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%24 = icmp eq i32 %23, -1
br i1 %24, label %37, label %25
25: ; preds = %22
%26 = call i32 @iconv_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%27 = icmp eq i32 %26, -1
br i1 %27, label %37, label %28
28: ; preds = %25
%29 = call i32 @iconv_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%30 = icmp eq i32 %29, -1
br i1 %30, label %37, label %31
31: ; preds = %28
%32 = call i32 @iconv_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%33 = icmp eq i32 %32, -1
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = call i32 @iconv_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%36 = icmp eq i32 %35, -1
br i1 %36, label %39, label %41
37: ; preds = %31, %28, %25, %22, %19, %16, %13, %0
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %34
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %34
%42 = call i32 @iconv_close(i32 %35)
%43 = call i32 @iconv_close(i32 %11)
%44 = call i32 @iconv_close(i32 %14)
%45 = call i32 @iconv_close(i32 %17)
%46 = call i32 @iconv_close(i32 %20)
%47 = call i32 @iconv_close(i32 %23)
%48 = call i32 @iconv_close(i32 %26)
%49 = call i32 @iconv_close(i32 %29)
%50 = call i32 @iconv_close(i32 %32)
%51 = call i32 @iconv_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %41
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %41
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.11, i64 0, i64 0))
%57 = call i32 @iconv(i32 %51, i8** %3, i64* %5, i8** %2, i64* %4)
%58 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0), i32 %57)
%59 = call i32 @iconv_close(i32 %51)
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.13, i64 0, i64 0))
ret i32 0
}