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

/* Variables and functions */
scalar_t__ is_ascii_string(char const *);
scalar_t__ is_cp1251(char const *);
char *simple_recode(char const *, char *, char *, char *);
char *strncpy(char *, char const *, int);
scalar_t__ utf8_validate(char const *);
const char *auto_recode(const char *utf_str) {
  char *res;
  static char result_buf[512];
  if (is_ascii_string(utf_str)) {
    res = (char *)utf_str;
  } else if (utf8_validate(utf_str)) {
    const char *utf_src = utf_str;
    char iso_8859_1[512];
    if (simple_recode(utf_str, "UTF-8", "ISO8859-1", iso_8859_1) &&
        utf8_validate(iso_8859_1))
      utf_src = iso_8859_1;
    if (is_cp1251(utf_src)) {
      char tmp[512];
      res = simple_recode(utf_src, "UTF-8", "ISO8859-1", tmp);
      if (res)
        res = simple_recode(res, "CP1251", "UTF-8", result_buf);
      if (!res)
        res = (char *)utf_src;
    } else {
      res = strncpy(result_buf, utf_src, 512);
    }
  } else {
    res = simple_recode(utf_str, "CP1251", "UTF-8", result_buf);
    if (!res)
      res = (char *)utf_str;
  }
  return res;
}


x86 assembly (clang, -O0):
<auto_recode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <is_ascii_string@plt>
cmp    $0x0,%rax
je     117f <auto_recode+0x2f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    12ba <auto_recode+0x16a>
mov    -0x8(%rbp),%rdi
call   1040 <utf8_validate@plt>
cmp    $0x0,%rax
je     127b <auto_recode+0x12b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x220(%rbp),%rcx
lea    0xe54(%rip),%rsi
lea    0xe53(%rip),%rdx
call   1050 <simple_recode@plt>
cmp    $0x0,%rax
je     11e3 <auto_recode+0x93>
lea    -0x220(%rbp),%rdi
call   1040 <utf8_validate@plt>
cmp    $0x0,%rax
je     11e3 <auto_recode+0x93>
lea    -0x220(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <is_cp1251@plt>
cmp    $0x0,%rax
je     125d <auto_recode+0x10d>
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rcx
lea    0xdf8(%rip),%rsi
lea    0xdf7(%rip),%rdx
call   1050 <simple_recode@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1245 <auto_recode+0xf5>
mov    -0x10(%rbp),%rdi
lea    0xde2(%rip),%rsi
lea    0xdcb(%rip),%rdx
lea    0x2e24(%rip),%rcx
call   1050 <simple_recode@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1258 <auto_recode+0x108>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1276 <auto_recode+0x126>
mov    -0x18(%rbp),%rsi
lea    0x2df8(%rip),%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %rax,-0x10(%rbp)
jmp    12b5 <auto_recode+0x165>
mov    -0x8(%rbp),%rdi
lea    0xd8a(%rip),%rsi
lea    0xd73(%rip),%rdx
lea    0x2dcc(%rip),%rcx
call   1050 <simple_recode@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12b0 <auto_recode+0x160>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    12b5 <auto_recode+0x165>
jmp    12ba <auto_recode+0x16a>
mov    -0x10(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<auto_recode>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
call   1060 <is_ascii_string@plt>
test   %rax,%rax
jne    1233 <auto_recode+0xe3>
mov    %rbx,%rdi
call   1040 <utf8_validate@plt>
test   %rax,%rax
je     11f2 <auto_recode+0xa2>
lea    0xe81(%rip),%rsi
lea    0xe80(%rip),%rdx
lea    0x200(%rsp),%r14
mov    %rbx,%rdi
mov    %r14,%rcx
call   1050 <simple_recode@plt>
test   %rax,%rax
je     11ad <auto_recode+0x5d>
mov    %r14,%rdi
call   1040 <utf8_validate@plt>
test   %rax,%rax
cmovne %r14,%rbx
mov    %rbx,%rdi
call   1070 <is_cp1251@plt>
test   %rax,%rax
je     1211 <auto_recode+0xc1>
lea    0xe3f(%rip),%rsi
lea    0xe3e(%rip),%rdx
mov    %rsp,%rcx
mov    %rbx,%rdi
call   1050 <simple_recode@plt>
test   %rax,%rax
je     122a <auto_recode+0xda>
lea    0xe31(%rip),%rsi
lea    0xe1a(%rip),%rdx
lea    0x2e73(%rip),%rcx
mov    %rax,%rdi
jmp    120a <auto_recode+0xba>
lea    0xe17(%rip),%rsi
lea    0xe00(%rip),%rdx
lea    0x2e59(%rip),%rcx
mov    %rbx,%rdi
call   1050 <simple_recode@plt>
jmp    122c <auto_recode+0xdc>
lea    0x2e48(%rip),%rdi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %rax,%rbx
jmp    1233 <auto_recode+0xe3>
xor    %eax,%eax
test   %rax,%rax
cmovne %rax,%rbx
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@auto_recode.result_buf = internal global [512 x i8] zeroinitializer
@.str = [6 x i8] c"UTF-8\00"
@.str.1 = [10 x i8] c"ISO8859-1\00"
@.str.2 = [7 x i8] c"CP1251\00"
define i8* @auto_recode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [512 x i8]
%6 = alloca [512 x i8]
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i64 @is_ascii_string(i8* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i8*, i8** %2
store i8* %11, i8** %3
br label %60
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = call i64 @utf8_validate(i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %51
16: ; preds = %12
%17 = load i8*, i8** %2
store i8* %17, i8** %4
%18 = load i8*, i8** %2
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%20 = call i8* @simple_recode(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%24 = call i64 @utf8_validate(i8* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
store i8* %27, i8** %4
br label %28
28: ; preds = %26, %22, %16
%29 = load i8*, i8** %4
%30 = call i64 @is_cp1251(i8* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %47
32: ; preds = %28
%33 = load i8*, i8** %4
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%35 = call i8* @simple_recode(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i8* %35, i8** %3
%36 = load i8*, i8** %3
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i8*, i8** %3
%40 = call i8* @simple_recode(i8* %39, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([512 x i8], [512 x i8]* @auto_recode.result_buf, i64 0, i64 0))
store i8* %40, i8** %3
br label %41
41: ; preds = %38, %32
%42 = load i8*, i8** %3
%43 = icmp ne i8* %42, null
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = load i8*, i8** %4
store i8* %45, i8** %3
br label %46
46: ; preds = %44, %41
br label %50
47: ; preds = %28
%48 = load i8*, i8** %4
%49 = call i8* @strncpy(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @auto_recode.result_buf, i64 0, i64 0), i8* %48, i32 512)
store i8* %49, i8** %3
br label %50
50: ; preds = %47, %46
br label %59
51: ; preds = %12
%52 = load i8*, i8** %2
%53 = call i8* @simple_recode(i8* %52, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([512 x i8], [512 x i8]* @auto_recode.result_buf, i64 0, i64 0))
store i8* %53, i8** %3
%54 = load i8*, i8** %3
%55 = icmp ne i8* %54, null
br i1 %55, label %58, label %56
56: ; preds = %51
%57 = load i8*, i8** %2
store i8* %57, i8** %3
br label %58
58: ; preds = %56, %51
br label %59
59: ; preds = %58, %50
br label %60
60: ; preds = %59, %10
%61 = load i8*, i8** %3
ret i8* %61
}

LLVM IR (-O2):
@auto_recode.result_buf = internal global [512 x i8] zeroinitializer
@.str = [6 x i8] c"UTF-8\00"
@.str.1 = [10 x i8] c"ISO8859-1\00"
@.str.2 = [7 x i8] c"CP1251\00"
define i8* @auto_recode(i8* %0) {
%2 = alloca [512 x i8]
%3 = alloca [512 x i8]
%4 = tail call i64 @is_ascii_string(i8* %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %39
6: ; preds = %1
%7 = tail call i64 @utf8_validate(i8* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %35, label %9
9: ; preds = %6
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%11 = call i8* @simple_recode(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = call i64 @utf8_validate(i8* %10)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i8* %0, i8* %10
br label %17
17: ; preds = %13, %9
%18 = phi i8* [ %0, %9 ], [ %16, %13 ]
%19 = call i64 @is_cp1251(i8* %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %17
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%23 = call i8* @simple_recode(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = call i8* @simple_recode(i8* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([512 x i8], [512 x i8]* @auto_recode.result_buf, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
%28 = phi i8* [ %26, %25 ], [ null, %21 ]
%29 = icmp eq i8* %28, null
%30 = select i1 %29, i8* %18, i8* %28
br label %33
31: ; preds = %17
%32 = call i8* @strncpy(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @auto_recode.result_buf, i64 0, i64 0), i8* %18, i32 512)
br label %33
33: ; preds = %31, %27
%34 = phi i8* [ %30, %27 ], [ %32, %31 ]
br label %39
35: ; preds = %6
%36 = tail call i8* @simple_recode(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([512 x i8], [512 x i8]* @auto_recode.result_buf, i64 0, i64 0))
%37 = icmp eq i8* %36, null
%38 = select i1 %37, i8* %0, i8* %36
br label %39
39: ; preds = %35, %1, %33
%40 = phi i8* [ %34, %33 ], [ %0, %1 ], [ %38, %35 ]
ret i8* %40
}