#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 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 *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(void) {
  char utf8[5];
  wchar_t ucs4[5];
  iconv_t cd;
  char *inbuf;
  char *outbuf;
  size_t inbytes;
  size_t outbytes;
  size_t n;
  strcpy(utf8, "abcd");
  cd = iconv_open("UCS4", "UTF8");
  if (cd == (iconv_t)-1) {
    perror("iconv_open");
    return 1;
  }
  inbuf = utf8;
  inbytes = 4;
  outbuf = (char *)ucs4;
  outbytes = 4 * sizeof(wchar_t);
  n = iconv(cd, &inbuf, &inbytes, &outbuf, &outbytes);
  if (n == (size_t)-1) {
    printf("iconv: %m\n");
    iconv_close(cd);
    return 1;
  }
  iconv_close(cd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x9(%rbp),%rdi
lea    0xe86(%rip),%rsi
call   1030 <strcpy@plt>
lea    0xe7f(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11b9 <main+0x59>
lea    0xe67(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1232 <main+0xd2>
lea    -0x9(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x4,-0x40(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x10,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x38(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   1040 <iconv@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0xffffffffffffffff,-0x50(%rbp)
jne    1222 <main+0xc2>
lea    0xe12(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <iconv_close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1232 <main+0xd2>
mov    -0x28(%rbp),%rdi
call   1060 <iconv_close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
lea    0xe92(%rip),%rsi
lea    0x2b(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
lea    0xe83(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1080 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     1207 <main+0xa7>
mov    %rax,%rbx
mov    %r14,0x20(%rsp)
movq   $0x4,0x10(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x18(%rsp)
movq   $0x10,0x8(%rsp)
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rbx,%rdi
call   1040 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
je     11e6 <main+0x86>
mov    %rbx,%rdi
call   1060 <iconv_close@plt>
xor    %eax,%eax
jmp    11ff <main+0x9f>
lea    0xe2d(%rip),%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <iconv_close@plt>
mov    $0x1,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
jmp    11fa <main+0x9a>

LLVM IR (-O0):
@.str = [5 x i8] c"abcd\00"
@.str.1 = [5 x i8] c"UCS4\00"
@.str.2 = [5 x i8] c"UTF8\00"
@.str.3 = [11 x i8] c"iconv_open\00"
@.str.4 = [11 x i8] c"iconv: %m\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [5 x i8]
%3 = alloca [5 x i32]
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 0, i32* %1
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%11 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = call i64 @iconv_open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i64 %12, i64* %4
%13 = load i64, i64* %4
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %32
17: ; preds = %0
%18 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
store i8* %18, i8** %5
store i64 4, i64* %7
%19 = getelementptr inbounds [5 x i32], [5 x i32]* %3, i64 0, i64 0
%20 = bitcast i32* %19 to i8*
store i8* %20, i8** %6
store i64 16, i64* %8
%21 = load i64, i64* %4
%22 = call i64 @iconv(i64 %21, i8** %5, i64* %7, i8** %6, i64* %8)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp eq i64 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %17
%26 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%27 = load i64, i64* %4
%28 = call i32 @iconv_close(i64 %27)
store i32 1, i32* %1
br label %32
29: ; preds = %17
%30 = load i64, i64* %4
%31 = call i32 @iconv_close(i64 %30)
store i32 0, i32* %1
br label %32
32: ; preds = %29, %25, %15
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [5 x i8] c"abcd\00"
@.str.1 = [5 x i8] c"UCS4\00"
@.str.2 = [5 x i8] c"UTF8\00"
@.str.3 = [11 x i8] c"iconv_open\00"
@.str.4 = [11 x i8] c"iconv: %m\0A\00"
define i32 @main() {
%1 = alloca [5 x i8]
%2 = alloca [5 x i32]
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%8 = bitcast [5 x i32]* %2 to i8*
%9 = bitcast i8** %3 to i8*
%10 = bitcast i8** %4 to i8*
%11 = bitcast i64* %5 to i8*
%12 = bitcast i64* %6 to i8*
%13 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = call i64 @iconv_open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i64 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %27
18: ; preds = %0
store i8* %7, i8** %3
store i64 4, i64* %5
%19 = bitcast i8** %4 to [5 x i32]**
store [5 x i32]* %2, [5 x i32]** %19
store i64 16, i64* %6
%20 = call i64 @iconv(i64 %14, i8** %3, i64* %5, i8** %4, i64* %6)
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%24 = call i32 @iconv_close(i64 %14)
br label %27
25: ; preds = %18
%26 = call i32 @iconv_close(i64 %14)
br label %27
27: ; preds = %25, %22, %16
%28 = phi i32 [ 1, %16 ], [ 1, %22 ], [ 0, %25 ]
ret i32 %28
}