#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;
typedef int /*<<< orphan*/ codepage_name;

/* Variables and functions */
int /*<<< orphan*/ CODESET;
scalar_t__ iconv_open(int /*<<< orphan*/, char *);
int /*<<< orphan*/ nl_langinfo(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
iconv_t iconv_init_codepage(int codepage) {
  iconv_t result;
  char codepage_name[16];
  snprintf(codepage_name, sizeof(codepage_name), "CP%d", codepage);
  result = iconv_open(nl_langinfo(CODESET), codepage_name);
  if (result == (iconv_t)-1)
    perror(codepage_name);
  return result;
}


x86 assembly (clang, -O0):
<iconv_init_codepage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x10,%esi
lea    0xea2(%rip),%rdx
call   1030 <snprintf@plt>
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
call   1040 <nl_langinfo@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1060 <iconv_open@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    1194 <iconv_init_codepage+0x54>
lea    -0x20(%rbp),%rdi
call   1050 <perror@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iconv_init_codepage>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ecx
lea    0xeb2(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x10,%esi
call   1030 <snprintf@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
call   1040 <nl_langinfo@plt>
mov    %eax,%edi
mov    %rbx,%rsi
call   1060 <iconv_open@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     1188 <iconv_init_codepage+0x48>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret
mov    %rsp,%rdi
call   1050 <perror@plt>
jmp    117f <iconv_init_codepage+0x3f>

LLVM IR (-O0):
@.str = [5 x i8] c"CP%d\00"
@CODESET = global i32 0
define i64 @iconv_init_codepage(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca [16 x i8]
store i32 %0, i32* %2
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* %2
%7 = call i32 @snprintf(i8* %5, i32 16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @CODESET
%9 = call i32 @nl_langinfo(i32 %8)
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%11 = call i64 @iconv_open(i32 %9, i8* %10)
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = icmp eq i64 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%16 = call i32 @perror(i8* %15)
br label %17
17: ; preds = %14, %1
%18 = load i64, i64* %3
ret i64 %18
}

LLVM IR (-O2):
@.str = [5 x i8] c"CP%d\00"
@CODESET = global i32 0
define i64 @iconv_init_codepage(i32 %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @CODESET
%6 = call i32 @nl_langinfo(i32 %5)
%7 = call i64 @iconv_open(i32 %6, i8* %3)
%8 = icmp eq i64 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* %3)
br label %11
11: ; preds = %9, %1
ret i64 %7
}