#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 */
size_t iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char const *, char const *);
size_t strlen(char const *);
char *simple_recode(const char *str, const char *from, const char *to,
                    char *buf) {
  char *in = (char *)str, *out = buf;
  size_t inb, outb;
  iconv_t h = iconv_open(to, from);
  inb = strlen(str);
  outb = 512;
  if (h == (iconv_t)-1) {
    return NULL;
  }
  if (iconv(h, (char **)&in, &inb, &out, &outb) != (size_t)-1) {
    *out = 0;
    iconv_close(h);
    return buf;
  }
  iconv_close(h);
  return NULL;
}


x86 assembly (clang, -O0):
<simple_recode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <iconv_open@plt>
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
movq   $0x200,-0x48(%rbp)
cmpq   $0xffffffffffffffff,-0x50(%rbp)
jne    11a6 <simple_recode+0x66>
movq   $0x0,-0x8(%rbp)
jmp    11f7 <simple_recode+0xb7>
mov    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x38(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   1030 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
je     11e6 <simple_recode+0xa6>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x50(%rbp),%rdi
call   1050 <iconv_close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f7 <simple_recode+0xb7>
mov    -0x50(%rbp),%rdi
call   1050 <iconv_close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simple_recode>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rcx,%r14
mov    %rdi,%rbx
mov    %rdi,0x18(%rsp)
mov    %rcx,(%rsp)
mov    %rdx,%rdi
call   1060 <iconv_open@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,0x10(%rsp)
movq   $0x200,0x8(%rsp)
cmp    $0xffffffffffffffff,%r15
je     11a8 <simple_recode+0x68>
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rsp,%rcx
lea    0x8(%rsp),%r8
mov    %r15,%rdi
call   1030 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
je     11ad <simple_recode+0x6d>
mov    (%rsp),%rax
movb   $0x0,(%rax)
jmp    11b0 <simple_recode+0x70>
xor    %r14d,%r14d
jmp    11b8 <simple_recode+0x78>
xor    %r14d,%r14d
mov    %r15,%rdi
call   1050 <iconv_close@plt>
mov    %r14,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @simple_recode(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%15 = load i8*, i8** %6
store i8* %15, i8** %10
%16 = load i8*, i8** %9
store i8* %16, i8** %11
%17 = load i8*, i8** %8
%18 = load i8*, i8** %7
%19 = call i64 @iconv_open(i8* %17, i8* %18)
store i64 %19, i64* %14
%20 = load i8*, i8** %6
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %12
store i64 512, i64* %13
%22 = load i64, i64* %14
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %4
store i8* null, i8** %5
br label %37
25: ; preds = %4
%26 = load i64, i64* %14
%27 = call i64 @iconv(i64 %26, i8** %10, i64* %12, i8** %11, i64* %13)
%28 = icmp ne i64 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i8*, i8** %11
store i8 0, i8* %30
%31 = load i64, i64* %14
%32 = call i32 @iconv_close(i64 %31)
%33 = load i8*, i8** %9
store i8* %33, i8** %5
br label %37
34: ; preds = %25
%35 = load i64, i64* %14
%36 = call i32 @iconv_close(i64 %35)
store i8* null, i8** %5
br label %37
37: ; preds = %34, %29, %24
%38 = load i8*, i8** %5
ret i8* %38
}

LLVM IR (-O2):
define i8* @simple_recode(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = bitcast i8** %5 to i8*
store i8* %0, i8** %5
%10 = bitcast i8** %6 to i8*
store i8* %3, i8** %6
%11 = bitcast i64* %7 to i8*
%12 = bitcast i64* %8 to i8*
%13 = tail call i64 @iconv_open(i8* %2, i8* %1)
%14 = tail call i64 @strlen(i8* dereferenceable(1) %0)
store i64 %14, i64* %7
store i64 512, i64* %8
%15 = icmp eq i64 %13, -1
br i1 %15, label %24, label %16
16: ; preds = %4
%17 = call i64 @iconv(i64 %13, i8** %5, i64* %7, i8** %6, i64* %8)
%18 = icmp eq i64 %17, -1
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = load i8*, i8** %6
store i8 0, i8* %20
br label %21
21: ; preds = %16, %19
%22 = phi i8* [ %3, %19 ], [ null, %16 ]
%23 = call i32 @iconv_close(i64 %13)
br label %24
24: ; preds = %21, %4
%25 = phi i8* [ null, %4 ], [ %22, %21 ]
ret i8* %25
}