#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 */
scalar_t__ E2BIG;
scalar_t__ errno;
int iconv(int /*<<< orphan*/, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_open(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  char inbuf[10000];
  wchar_t outbuf[10000];
  iconv_t cd;
  int i;
  char *inptr;
  char *outptr;
  size_t inbytes_left, outbytes_left;
  int count;
  int result = 0;
  for (i = 0; i < 10000; i++)
    inbuf[i] = 'a';
  cd = iconv_open("UCS-4LE", "UTF-8");
  inbytes_left = 10000;
  outbytes_left = 10000 * 4;
  inptr = inbuf;
  outptr = (char *)outbuf;
  count = iconv(cd, &inptr, &inbytes_left, &outptr, &outbytes_left);
  if (count < 0) {
    if (errno == E2BIG)
      printf("Received E2BIG\n");
    else
      printf("Received something else\n");
    printf("inptr change: %td\n", inptr - inbuf);
    printf("inlen change: %zd\n", 10000 - inbytes_left);
    printf("outptr change: %td\n", outptr - (char *)outbuf);
    printf("outlen change: %zd\n", 10000 * 4 - outbytes_left);
    result = 1;
  } else
    printf("Succeeded\n");
  return result;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc390,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0xc390(%rbp)
movl   $0x0,-0xc368(%rbp)
cmpl   $0x2710,-0xc368(%rbp)
jge    1190 <main+0x60>
movslq -0xc368(%rbp),%rax
movb   $0x61,-0x2720(%rbp,%rax,1)
61
mov    -0xc368(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc368(%rbp)
jmp    115d <main+0x2d>
lea    0xe69(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   1050 <iconv_open@plt>
mov    %eax,-0xc364(%rbp)
movq   $0x2710,-0xc380(%rbp)
movq   $0x9c40,-0xc388(%rbp)
lea    -0x2720(%rbp),%rax
mov    %rax,-0xc370(%rbp)
lea    -0xc360(%rbp),%rax
mov    %rax,-0xc378(%rbp)
mov    -0xc364(%rbp),%edi
lea    -0xc370(%rbp),%rsi
lea    -0xc380(%rbp),%rdx
lea    -0xc378(%rbp),%rcx
lea    -0xc388(%rbp),%r8
call   1030 <iconv@plt>
mov    %eax,-0xc38c(%rbp)
cmpl   $0x0,-0xc38c(%rbp)
jge    12d1 <main+0x1a1>
mov    0x2dbc(%rip),%rax
mov    (%rax),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
jne    1242 <main+0x112>
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1250 <main+0x120>
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc370(%rbp),%rsi
lea    -0x2720(%rbp),%rax
sub    %rax,%rsi
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2710,%esi
sub    -0xc380(%rbp),%rsi
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc378(%rbp),%rsi
lea    -0xc360(%rbp),%rax
sub    %rax,%rsi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x9c40,%esi
sub    -0xc388(%rbp),%rsi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0xc390(%rbp)
jmp    12df <main+0x1af>
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc390(%rbp),%eax
add    $0xc390,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0xc370,%rsp
lea    0x20(%rsp),%r14
mov    $0x2710,%ebx
mov    $0x2710,%edx
mov    %r14,%rdi
mov    $0x61,%esi
call   1060 <memset@plt>
lea    0xe81(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1070 <iconv_open@plt>
movq   $0x2710,0x8(%rsp)
movq   $0x9c40,(%rsp)
mov    %r14,0x18(%rsp)
lea    0x2730(%rsp),%r15
mov    %r15,0x10(%rsp)
lea    0x18(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %rsp,%r8
mov    %eax,%edi
call   1030 <iconv@plt>
test   %eax,%eax
js     11de <main+0x8e>
lea    0xe8a(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    1267 <main+0x117>
mov    0x2df3(%rip),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
lea    0xe85(%rip),%rax
lea    0xe66(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
mov    0x18(%rsp),%rsi
sub    %r14,%rsi
lea    0xdf6(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
sub    0x8(%rsp),%rbx
lea    0xdf6(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%rsi
sub    %r15,%rsi
lea    0xdf0(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x9c40,%esi
sub    (%rsp),%rsi
lea    0xded(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0xc370,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"UCS-4LE\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@errno = global i64 0
@E2BIG = global i64 0
@.str.2 = [16 x i8] c"Received E2BIG\0A\00"
@.str.3 = [25 x i8] c"Received something else\0A\00"
@.str.4 = [19 x i8] c"inptr change: %td\0A\00"
@.str.5 = [19 x i8] c"inlen change: %zd\0A\00"
@.str.6 = [20 x i8] c"outptr change: %td\0A\00"
@.str.7 = [20 x i8] c"outlen change: %zd\0A\00"
@.str.8 = [11 x i8] c"Succeeded\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [10000 x i8]
%7 = alloca [10000 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %15
store i32 0, i32* %9
br label %16
16: ; preds = %23, %2
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 10000
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [10000 x i8], [10000 x i8]* %6, i64 0, i64 %21
store i8 97, i8* %22
br label %23
23: ; preds = %19
%24 = load i32, i32* %9
%25 = add nsw i32 %24, 1
store i32 %25, i32* %9
br label %16
26: ; preds = %16
%27 = call i32 @iconv_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 %27, i32* %8
store i64 10000, i64* %12
store i64 40000, i64* %13
%28 = getelementptr inbounds [10000 x i8], [10000 x i8]* %6, i64 0, i64 0
store i8* %28, i8** %10
%29 = getelementptr inbounds [10000 x i32], [10000 x i32]* %7, i64 0, i64 0
%30 = bitcast i32* %29 to i8*
store i8* %30, i8** %11
%31 = load i32, i32* %8
%32 = call i32 @iconv(i32 %31, i8** %10, i64* %12, i8** %11, i64* %13)
store i32 %32, i32* %14
%33 = load i32, i32* %14
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %63
35: ; preds = %26
%36 = load i64, i64* @errno
%37 = load i64, i64* @E2BIG
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %43
41: ; preds = %35
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %39
%44 = load i8*, i8** %10
%45 = getelementptr inbounds [10000 x i8], [10000 x i8]* %6, i64 0, i64 0
%46 = ptrtoint i8* %44 to i64
%47 = ptrtoint i8* %45 to i64
%48 = sub i64 %46, %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i64 %48)
%50 = load i64, i64* %12
%51 = sub i64 10000, %50
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i64 %51)
%53 = load i8*, i8** %11
%54 = getelementptr inbounds [10000 x i32], [10000 x i32]* %7, i64 0, i64 0
%55 = bitcast i32* %54 to i8*
%56 = ptrtoint i8* %53 to i64
%57 = ptrtoint i8* %55 to i64
%58 = sub i64 %56, %57
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 %58)
%60 = load i64, i64* %13
%61 = sub i64 40000, %60
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i64 %61)
store i32 1, i32* %15
br label %65
63: ; preds = %26
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
br label %65
65: ; preds = %63, %43
%66 = load i32, i32* %15
ret i32 %66
}

LLVM IR (-O2):
@.str = [8 x i8] c"UCS-4LE\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@errno = global i64 0
@E2BIG = global i64 0
@.str.4 = [19 x i8] c"inptr change: %td\0A\00"
@.str.5 = [19 x i8] c"inlen change: %zd\0A\00"
@.str.6 = [20 x i8] c"outptr change: %td\0A\00"
@.str.7 = [20 x i8] c"outlen change: %zd\0A\00"
@str = [10 x i8] c"Succeeded\00"
@str.9 = [24 x i8] c"Received something else\00"
@str.10 = [15 x i8] c"Received E2BIG\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [10000 x i8]
%4 = getelementptr inbounds [10000 x i8], [10000 x i8]* %3, i64 0, i64 0
%5 = alloca [10000 x i32]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = getelementptr inbounds [10000 x i8], [10000 x i8]* %3, i64 0, i64 0
%11 = bitcast [10000 x i32]* %5 to i8*
%12 = bitcast i8** %6 to i8*
%13 = bitcast i8** %7 to i8*
%14 = bitcast i64* %8 to i8*
%15 = bitcast i64* %9 to i8*
%16 = tail call i32 @iconv_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i64 10000, i64* %8
store i64 40000, i64* %9
store i8* %10, i8** %6
%17 = bitcast i8** %7 to [10000 x i32]**
store [10000 x i32]* %5, [10000 x i32]** %17
%18 = call i32 @iconv(i32 %16, i8** %6, i64* %8, i8** %7, i64* %9)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %42
20: ; preds = %2
%21 = load i64, i64* @errno
%22 = load i64, i64* @E2BIG
%23 = icmp eq i64 %21, %22
%24 = select i1 %23, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @str.9, i64 0, i64 0)
%25 = call i32 @puts(i8* dereferenceable(1) %24)
%26 = load i8*, i8** %6
%27 = ptrtoint i8* %26 to i64
%28 = ptrtoint [10000 x i8]* %3 to i64
%29 = sub i64 %27, %28
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i64 %29)
%31 = load i64, i64* %8
%32 = sub i64 10000, %31
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i64 %32)
%34 = load i8*, i8** %7
%35 = ptrtoint i8* %34 to i64
%36 = ptrtoint [10000 x i32]* %5 to i64
%37 = sub i64 %35, %36
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 %37)
%39 = load i64, i64* %9
%40 = sub i64 40000, %39
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i64 %40)
br label %44
42: ; preds = %2
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %44
44: ; preds = %42, %20
%45 = phi i32 [ 1, %20 ], [ 0, %42 ]
ret i32 %45
}