#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 _iconv_error(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ free(char *);
int iconv(int /*<<< orphan*/, char const **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(int /*<<< orphan*/);
int /*<<< orphan*/ iconv_open(char const *, char const *);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, char const *, char const *, ...);
scalar_t__ strcmp(char *, char const *);
size_t strlen(char const *);
int _iconv(char const *progname, char const *tocode, char const *fromcode,
           char const *buf, char const *expected) {
  int ret;
  iconv_t cd;
  size_t inbytesleft;
  char *outbuf;
  char *p;
  size_t outbytesleft;
  printf("%s: Testing %s: \"%s\" to \"%s\"\n", progname, "iconv()", fromcode,
         tocode);
  inbytesleft = strlen(buf);
  outbytesleft = inbytesleft;
  if ((outbuf = malloc(outbytesleft + 1)) == NULL)
    return _iconv_error(NULL, 1);
  p = outbuf;
  outbuf[outbytesleft] = '\0';
  if ((cd = iconv_open(tocode, fromcode)) == (iconv_t)-1) {
    free(outbuf);
    return _iconv_error(NULL, 2);
  }
  if ((ret = iconv(cd, &buf, &inbytesleft, &p, &outbytesleft)) != 0)
    ret = _iconv_error(NULL, 3);
  else if (strcmp(outbuf, expected) != 0) {
    printf("Obtained: \"%s\"\nExpected: \"%s\"\n", outbuf, expected);
    ret = _iconv_error(NULL, 4);
  }
  iconv_close(cd);
  free(outbuf);
  return ret;
}


x86 assembly (clang, -O0):
<_iconv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe41(%rip),%rdi
lea    0xe58(%rip),%rdx
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    1213 <_iconv+0x83>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1,%esi
call   1090 <_iconv_error@plt>
mov    %eax,-0x4(%rbp)
jmp    12f3 <_iconv+0x163>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x58(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10b0 <iconv_open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0xffffffff,%eax
jne    125f <_iconv+0xcf>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2,%esi
call   1090 <_iconv_error@plt>
mov    %eax,-0x4(%rbp)
jmp    12f3 <_iconv+0x163>
mov    -0x38(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x50(%rbp),%rcx
lea    -0x58(%rbp),%r8
call   1040 <iconv@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     1299 <_iconv+0x109>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x3,%esi
call   1090 <_iconv_error@plt>
mov    %eax,-0x34(%rbp)
jmp    12dc <_iconv+0x14c>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12d7 <_iconv+0x147>
mov    -0x48(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
call   1090 <_iconv_error@plt>
mov    %eax,-0x34(%rbp)
jmp    12dc <_iconv+0x14c>
mov    -0x38(%rbp),%edi
call   10a0 <iconv_close@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_iconv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8,%r14
mov    %rcx,%rbp
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%rsi
mov    %rcx,0x18(%rsp)
lea    0xe49(%rip),%rdi
lea    0xe60(%rip),%rdx
mov    %r12,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %rax,0x10(%rsp)
mov    %rax,(%rsp)
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1281 <_iconv+0xf1>
mov    %rax,%r15
mov    %rax,0x8(%rsp)
movb   $0x0,(%rax,%rbp,1)
mov    %rbx,%rdi
mov    %r12,%rsi
call   10b0 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     128a <_iconv+0xfa>
mov    %eax,%ebp
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rsp,%r8
mov    %eax,%edi
call   1040 <iconv@plt>
mov    $0x3,%esi
test   %eax,%eax
jne    125a <_iconv+0xca>
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     12aa <_iconv+0x11a>
lea    0xdde(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x4,%esi
xor    %edi,%edi
call   1090 <_iconv_error@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   10a0 <iconv_close@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
mov    $0x1,%esi
jmp    1299 <_iconv+0x109>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %edi,%edi
mov    $0x2,%esi
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <_iconv_error@plt>
xor    %ebx,%ebx
jmp    1263 <_iconv+0xd3>

LLVM IR (-O0):
@.str = [30 x i8] c"%s: Testing %s: \22%s\22 to \22%s\22\0A\00"
@.str.1 = [8 x i8] c"iconv()\00"
@.str.2 = [31 x i8] c"Obtained: \22%s\22\0AExpected: \22%s\22\0A\00"
define i32 @_iconv(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i64
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
%18 = load i8*, i8** %7
%19 = load i8*, i8** %9
%20 = load i8*, i8** %8
%21 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %18, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %20)
%22 = load i8*, i8** %10
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %14
%24 = load i64, i64* %14
store i64 %24, i64* %17
%25 = load i64, i64* %17
%26 = add i64 %25, 1
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %15
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %5
%30 = call i32 @_iconv_error(i32* null, i32 1)
store i32 %30, i32* %6
br label %67
31: ; preds = %5
%32 = load i8*, i8** %15
store i8* %32, i8** %16
%33 = load i8*, i8** %15
%34 = load i64, i64* %17
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %8
%37 = load i8*, i8** %9
%38 = call i32 @iconv_open(i8* %36, i8* %37)
store i32 %38, i32* %13
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %31
%41 = load i8*, i8** %15
%42 = call i32 @free(i8* %41)
%43 = call i32 @_iconv_error(i32* null, i32 2)
store i32 %43, i32* %6
br label %67
44: ; preds = %31
%45 = load i32, i32* %13
%46 = call i32 @iconv(i32 %45, i8** %10, i64* %14, i8** %16, i64* %17)
store i32 %46, i32* %12
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @_iconv_error(i32* null, i32 3)
store i32 %49, i32* %12
br label %61
50: ; preds = %44
%51 = load i8*, i8** %15
%52 = load i8*, i8** %11
%53 = call i64 @strcmp(i8* %51, i8* %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %50
%56 = load i8*, i8** %15
%57 = load i8*, i8** %11
%58 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %56, i8* %57)
%59 = call i32 @_iconv_error(i32* null, i32 4)
store i32 %59, i32* %12
br label %60
60: ; preds = %55, %50
br label %61
61: ; preds = %60, %48
%62 = load i32, i32* %13
%63 = call i32 @iconv_close(i32 %62)
%64 = load i8*, i8** %15
%65 = call i32 @free(i8* %64)
%66 = load i32, i32* %12
store i32 %66, i32* %6
br label %67
67: ; preds = %61, %40, %29
%68 = load i32, i32* %6
ret i32 %68
}

LLVM IR (-O2):
@.str = [30 x i8] c"%s: Testing %s: \22%s\22 to \22%s\22\0A\00"
@.str.1 = [8 x i8] c"iconv()\00"
@.str.2 = [31 x i8] c"Obtained: \22%s\22\0AExpected: \22%s\22\0A\00"
define i32 @_iconv(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i8* %3, i8** %6
%10 = bitcast i64* %7 to i8*
%11 = bitcast i8** %8 to i8*
%12 = bitcast i64* %9 to i8*
%13 = tail call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %1)
%14 = tail call i64 @strlen(i8* dereferenceable(1) %3)
store i64 %14, i64* %7
store i64 %14, i64* %9
%15 = add i64 %14, 1
%16 = tail call i8* @malloc(i64 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %5
%19 = tail call i32 @_iconv_error(i32* null, i32 1)
br label %42
20: ; preds = %5
store i8* %16, i8** %8
%21 = getelementptr inbounds i8, i8* %16, i64 %14
store i8 0, i8* %21
%22 = tail call i32 @iconv_open(i8* %1, i8* %2)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = tail call i32 @free(i8* %16)
%26 = tail call i32 @_iconv_error(i32* null, i32 2)
br label %42
27: ; preds = %20
%28 = call i32 @iconv(i32 %22, i8** %6, i64* %7, i8** %8, i64* %9)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = call i64 @strcmp(i8* %16, i8* %4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %30
%34 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %16, i8* %4)
br label %35
35: ; preds = %27, %33
%36 = phi i32 [ 4, %33 ], [ 3, %27 ]
%37 = call i32 @_iconv_error(i32* null, i32 %36)
br label %38
38: ; preds = %35, %30
%39 = phi i32 [ 0, %30 ], [ %37, %35 ]
%40 = call i32 @iconv_close(i32 %22)
%41 = call i32 @free(i8* %16)
br label %42
42: ; preds = %38, %24, %18
%43 = phi i32 [ %19, %18 ], [ %26, %24 ], [ %39, %38 ]
ret i32 %43
}