#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*/ locale_table;

/* Variables and functions */
int /*<<< orphan*/ LC_CTYPE;
char *kanji_conv_auto(char *, char *);
char *setlocale(int /*<<< orphan*/, char *);
scalar_t__ strcasecmp(char *, char *);
char *kanji_conv_to_locale(char *str) {
  static char *jpcode = NULL;
  static char *locale_euc[] = {"ja",        "ja_JP",       "ja_JP.ujis",
                               "ja_JP.EUC", "ja_JP.eucJP", NULL};
  static char *locale_sjis[] = {"ja_JP.SJIS", "ja_JP.sjis", NULL};
  static struct LOCALE_TABLE {
    char *code;
    char **name_list;
  } locale_table[] = {{"EUC-JP", locale_euc},
                      {"ISO-2022-JP", locale_sjis},
                      {"SJIS", locale_sjis}};
  if (!str)
    return NULL;
  if (jpcode == NULL) {
    char *ctype = setlocale(LC_CTYPE, "");
    int i, j;
    for (j = 0; jpcode == NULL &&
                j < sizeof(locale_table) / sizeof(struct LOCALE_TABLE);
         j++) {
      char **name = locale_table[j].name_list;
      for (i = 0; name[i]; i++)
        if (strcasecmp(ctype, name[i]) == 0) {
          jpcode = locale_table[j].code;
          break;
        }
    }
    if (jpcode == NULL)
      jpcode = locale_table[1].code;
  }
  return kanji_conv_auto(str, jpcode);
}


x86 assembly (clang, -O0):
<kanji_conv_to_locale>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <kanji_conv_to_locale+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1282 <kanji_conv_to_locale+0x152>
cmpq   $0x0,0x2f6c(%rip)
jne    126e <kanji_conv_to_locale+0x13e>
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
lea    0xeab(%rip),%rsi
call   1050 <setlocale@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,0x2f3c(%rip)
mov    %al,-0x29(%rbp)
jne    11a3 <kanji_conv_to_locale+0x73>
movslq -0x20(%rbp),%rax
cmp    $0x3,%rax
setb   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11b3 <kanji_conv_to_locale+0x83>
jmp    124d <kanji_conv_to_locale+0x11d>
movslq -0x20(%rbp),%rcx
lea    0x2ed2(%rip),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     123a <kanji_conv_to_locale+0x10a>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1227 <kanji_conv_to_locale+0xf7>
movslq -0x20(%rbp),%rcx
lea    0x2e7f(%rip),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rax
mov    %rax,0x2ea6(%rip)
jmp    123a <kanji_conv_to_locale+0x10a>
jmp    122c <kanji_conv_to_locale+0xfc>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d4 <kanji_conv_to_locale+0xa4>
jmp    123f <kanji_conv_to_locale+0x10f>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1182 <kanji_conv_to_locale+0x52>
cmpq   $0x0,0x2e73(%rip)
jne    1269 <kanji_conv_to_locale+0x139>
mov    0x2e3e(%rip),%rax
mov    %rax,0x2e5f(%rip)
jmp    126e <kanji_conv_to_locale+0x13e>
mov    -0x10(%rbp),%rdi
mov    0x2e4f(%rip),%rsi
call   1040 <kanji_conv_auto@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kanji_conv_to_locale>:
test   %rdi,%rdi
je     1234 <kanji_conv_to_locale+0x104>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    0x2f42(%rip),%r15
test   %r15,%r15
jne    121b <kanji_conv_to_locale+0xeb>
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
lea    0xeb6(%rip),%rsi
call   1050 <setlocale@plt>
mov    0x2f1d(%rip),%r15
test   %r15,%r15
jne    121b <kanji_conv_to_locale+0xeb>
mov    %rax,%rbx
mov    %rbp,(%rsp)
xor    %r15d,%r15d
lea    0x2c53(%rip),%r12
xor    %r13d,%r13d
jmp    11b4 <kanji_conv_to_locale+0x84>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
test   %rax,%rax
jne    1204 <kanji_conv_to_locale+0xd4>
lea    0x1(%r13),%rcx
cmp    $0x2,%r13
mov    %rcx,%r13
jae    1204 <kanji_conv_to_locale+0xd4>
mov    %r13,%rbp
shl    $0x4,%rbp
mov    0x8(%rbp,%r12,1),%r14
mov    (%r14),%rsi
test   %rsi,%rsi
je     11a0 <kanji_conv_to_locale+0x70>
add    $0x8,%r14
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     11f0 <kanji_conv_to_locale+0xc0>
mov    (%r14),%rsi
add    $0x8,%r14
test   %rsi,%rsi
jne    11d0 <kanji_conv_to_locale+0xa0>
jmp    11a0 <kanji_conv_to_locale+0x70>
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp,%r12,1),%r15
mov    %r15,0x2e94(%rip)
mov    %r15,%rax
test   %rax,%rax
je     11a7 <kanji_conv_to_locale+0x77>
test   %rax,%rax
mov    (%rsp),%rbp
jne    121b <kanji_conv_to_locale+0xeb>
lea    0xe33(%rip),%r15
mov    %r15,0x2e75(%rip)
mov    %rbp,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <kanji_conv_auto@plt>
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.LOCALE_TABLE = type { i8*, i8** }
@kanji_conv_to_locale.jpcode = internal global i8* null
@kanji_conv_to_locale.locale_euc = internal global [6 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i32 0, i32 0), i8* null]
@.str = [3 x i8] c"ja\00"
@.str.1 = [6 x i8] c"ja_JP\00"
@.str.2 = [11 x i8] c"ja_JP.ujis\00"
@.str.3 = [10 x i8] c"ja_JP.EUC\00"
@.str.4 = [12 x i8] c"ja_JP.eucJP\00"
@kanji_conv_to_locale.locale_sjis = internal global [3 x i8*] [i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i32 0, i32 0), i8* null]
@.str.5 = [11 x i8] c"ja_JP.SJIS\00"
@.str.6 = [11 x i8] c"ja_JP.sjis\00"
@kanji_conv_to_locale.locale_table = internal global [3 x %struct.LOCALE_TABLE] [%struct.LOCALE_TABLE { i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i32 0, i32 0), i8** getelementptr inbounds ([6 x i8*], [6 x i8*]* @kanji_conv_to_locale.locale_euc, i32 0, i32 0) }, %struct.LOCALE_TABLE { i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i32 0, i32 0), i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @kanji_conv_to_locale.locale_sjis, i32 0, i32 0) }, %struct.LOCALE_TABLE { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i32 0, i32 0), i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @kanji_conv_to_locale.locale_sjis, i32 0, i32 0) }]
@.str.7 = [7 x i8] c"EUC-JP\00"
@.str.8 = [12 x i8] c"ISO-2022-JP\00"
@.str.9 = [5 x i8] c"SJIS\00"
@LC_CTYPE = global i32 0
@.str.10 = [1 x i8] zeroinitializer
define i8* @kanji_conv_to_locale(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* null, i8** %2
br label %72
11: ; preds = %1
%12 = load i8*, i8** @kanji_conv_to_locale.jpcode
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %68
14: ; preds = %11
%15 = load i32, i32* @LC_CTYPE
%16 = call i8* @setlocale(i32 %15, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.10, i64 0, i64 0))
store i8* %16, i8** %4
store i32 0, i32* %6
br label %17
17: ; preds = %59, %14
%18 = load i8*, i8** @kanji_conv_to_locale.jpcode
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = icmp ult i64 %22, 3
br label %24
24: ; preds = %20, %17
%25 = phi i1 [ false, %17 ], [ %23, %20 ]
br i1 %25, label %26, label %62
26: ; preds = %24
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [3 x %struct.LOCALE_TABLE], [3 x %struct.LOCALE_TABLE]* @kanji_conv_to_locale.locale_table, i64 0, i64 %28
%30 = getelementptr inbounds %struct.LOCALE_TABLE, %struct.LOCALE_TABLE* %29, i32 0, i32 1
%31 = load i8**, i8*** %30
store i8** %31, i8*** %7
store i32 0, i32* %5
br label %32
32: ; preds = %55, %26
%33 = load i8**, i8*** %7
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %33, i64 %35
%37 = load i8*, i8** %36
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %58
39: ; preds = %32
%40 = load i8*, i8** %4
%41 = load i8**, i8*** %7
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8*, i8** %41, i64 %43
%45 = load i8*, i8** %44
%46 = call i64 @strcasecmp(i8* %40, i8* %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %39
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [3 x %struct.LOCALE_TABLE], [3 x %struct.LOCALE_TABLE]* @kanji_conv_to_locale.locale_table, i64 0, i64 %50
%52 = getelementptr inbounds %struct.LOCALE_TABLE, %struct.LOCALE_TABLE* %51, i32 0, i32 0
%53 = load i8*, i8** %52
store i8* %53, i8** @kanji_conv_to_locale.jpcode
br label %58
54: ; preds = %39
br label %55
55: ; preds = %54
%56 = load i32, i32* %5
%57 = add nsw i32 %56, 1
store i32 %57, i32* %5
br label %32
58: ; preds = %48, %32
br label %59
59: ; preds = %58
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 1
store i32 %61, i32* %6
br label %17
62: ; preds = %24
%63 = load i8*, i8** @kanji_conv_to_locale.jpcode
%64 = icmp eq i8* %63, null
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = load i8*, i8** getelementptr inbounds ([3 x %struct.LOCALE_TABLE], [3 x %struct.LOCALE_TABLE]* @kanji_conv_to_locale.locale_table, i64 0, i64 1, i32 0)
store i8* %66, i8** @kanji_conv_to_locale.jpcode
br label %67
67: ; preds = %65, %62
br label %68
68: ; preds = %67, %11
%69 = load i8*, i8** %3
%70 = load i8*, i8** @kanji_conv_to_locale.jpcode
%71 = call i8* @kanji_conv_auto(i8* %69, i8* %70)
store i8* %71, i8** %2
br label %72
72: ; preds = %68, %10
%73 = load i8*, i8** %2
ret i8* %73
}

LLVM IR (-O2):
%struct.LOCALE_TABLE = type { i8*, i8** }
@kanji_conv_to_locale.jpcode = internal unnamed_addr global i8* null
@kanji_conv_to_locale.locale_euc = internal global [6 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i32 0, i32 0), i8* null]
@.str = [3 x i8] c"ja\00"
@.str.1 = [6 x i8] c"ja_JP\00"
@.str.2 = [11 x i8] c"ja_JP.ujis\00"
@.str.3 = [10 x i8] c"ja_JP.EUC\00"
@.str.4 = [12 x i8] c"ja_JP.eucJP\00"
@kanji_conv_to_locale.locale_sjis = internal global [3 x i8*] [i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i32 0, i32 0), i8* null]
@.str.5 = [11 x i8] c"ja_JP.SJIS\00"
@.str.6 = [11 x i8] c"ja_JP.sjis\00"
@kanji_conv_to_locale.locale_table = internal unnamed_addr constant [3 x %struct.LOCALE_TABLE] [%struct.LOCALE_TABLE { i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i32 0, i32 0), i8** getelementptr inbounds ([6 x i8*], [6 x i8*]* @kanji_conv_to_locale.locale_euc, i32 0, i32 0) }, %struct.LOCALE_TABLE { i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i32 0, i32 0), i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @kanji_conv_to_locale.locale_sjis, i32 0, i32 0) }, %struct.LOCALE_TABLE { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i32 0, i32 0), i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @kanji_conv_to_locale.locale_sjis, i32 0, i32 0) }]
@.str.7 = [7 x i8] c"EUC-JP\00"
@.str.8 = [12 x i8] c"ISO-2022-JP\00"
@.str.9 = [5 x i8] c"SJIS\00"
@LC_CTYPE = global i32 0
@.str.10 = [1 x i8] zeroinitializer
define i8* @kanji_conv_to_locale(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %43, label %3
3: ; preds = %1
%4 = load i8*, i8** @kanji_conv_to_locale.jpcode
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %40
6: ; preds = %3
%7 = load i32, i32* @LC_CTYPE
%8 = tail call i8* @setlocale(i32 %7, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.10, i64 0, i64 0))
%9 = load i8*, i8** @kanji_conv_to_locale.jpcode
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %40
11: ; preds = %6, %31
%12 = phi i8* [ %32, %31 ], [ null, %6 ]
%13 = phi i64 [ %34, %31 ], [ 0, %6 ]
%14 = getelementptr inbounds [3 x %struct.LOCALE_TABLE], [3 x %struct.LOCALE_TABLE]* @kanji_conv_to_locale.locale_table, i64 0, i64 %13, i32 1
%15 = load i8**, i8*** %14
%16 = load i8*, i8** %15
%17 = icmp eq i8* %16, null
br i1 %17, label %31, label %22
18: ; preds = %22
%19 = getelementptr inbounds i8*, i8** %15, i64 %27
%20 = load i8*, i8** %19
%21 = icmp eq i8* %20, null
br i1 %21, label %31, label %22
22: ; preds = %11, %18
%23 = phi i64 [ %27, %18 ], [ 0, %11 ]
%24 = phi i8* [ %20, %18 ], [ %16, %11 ]
%25 = tail call i64 @strcasecmp(i8* %8, i8* %24)
%26 = icmp eq i64 %25, 0
%27 = add nuw i64 %23, 1
br i1 %26, label %28, label %18
28: ; preds = %22
%29 = getelementptr inbounds [3 x %struct.LOCALE_TABLE], [3 x %struct.LOCALE_TABLE]* @kanji_conv_to_locale.locale_table, i64 0, i64 %13, i32 0
%30 = load i8*, i8** %29
store i8* %30, i8** @kanji_conv_to_locale.jpcode
br label %31
31: ; preds = %18, %11, %28
%32 = phi i8* [ %12, %11 ], [ %30, %28 ], [ %12, %18 ]
%33 = phi i8* [ null, %11 ], [ %30, %28 ], [ null, %18 ]
%34 = add nuw nsw i64 %13, 1
%35 = icmp eq i8* %33, null
%36 = icmp ult i64 %13, 2
%37 = select i1 %35, i1 %36, i1 false
br i1 %37, label %11, label %38
38: ; preds = %31
br i1 %35, label %39, label %40
39: ; preds = %38
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8** @kanji_conv_to_locale.jpcode
br label %40
40: ; preds = %6, %38, %39, %3
%41 = phi i8* [ %32, %38 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), %39 ], [ %4, %3 ], [ %9, %6 ]
%42 = tail call i8* @kanji_conv_auto(i8* %0, i8* %41)
br label %43
43: ; preds = %1, %40
%44 = phi i8* [ %42, %40 ], [ null, %1 ]
ret i8* %44
}