#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 */
char *get_charset();
int /*<<< orphan*/ strcasecmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncasecmp(char *, char *, int);
int check_charset(char *str, int len) {
  static char *mm_charset = NULL;
  static char *alt_charset = NULL;
  static int mm_len;
  static int alt_len;
  if (!mm_charset) {
    if (!(mm_charset = get_charset()))
      mm_charset = "US-ASCII";
    mm_len = strlen(mm_charset);
    if (!strncasecmp("ISO-8859-", mm_charset, 9) ||
        !strcasecmp("UTF-8", mm_charset)) {
      alt_charset = "US-ASCII";
      alt_len = strlen(alt_charset);
    }
  }
  if ((len == mm_len) && !strncasecmp(str, mm_charset, mm_len))
    return 1;
  if (alt_charset && (len == alt_len) &&
      !strncasecmp(str, alt_charset, alt_len))
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<check_charset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,0x2ef1(%rip)
jne    11f7 <check_charset+0xb7>
mov    $0x0,%al
call   1050 <get_charset@plt>
mov    %rax,0x2edd(%rip)
cmp    $0x0,%rax
jne    1183 <check_charset+0x43>
lea    0xe84(%rip),%rax
mov    %rax,0x2ec5(%rip)
mov    0x2ebe(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,0x2ec3(%rip)
mov    0x2eac(%rip),%rsi
lea    0xe66(%rip),%rdi
mov    $0x9,%edx
call   1060 <strncasecmp@plt>
cmp    $0x0,%eax
je     11d2 <check_charset+0x92>
mov    0x2e8b(%rip),%rsi
lea    0xe4f(%rip),%rdi
call   1030 <strcasecmp@plt>
cmp    $0x0,%eax
jne    11f2 <check_charset+0xb2>
lea    0xe27(%rip),%rax
mov    %rax,0x2e70(%rip)
mov    0x2e69(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,0x2e6a(%rip)
jmp    11f7 <check_charset+0xb7>
mov    -0x14(%rbp),%eax
cmp    0x2e58(%rip),%eax
jne    1231 <check_charset+0xf1>
mov    -0x10(%rbp),%rdi
mov    0x2e37(%rip),%rsi
mov    0x2e41(%rip),%edx
call   1060 <strncasecmp@plt>
cmp    $0x0,%eax
jne    1231 <check_charset+0xf1>
movl   $0x1,-0x4(%rbp)
jmp    1280 <check_charset+0x140>
cmpq   $0x0,0x2e17(%rip)
je     1279 <check_charset+0x139>
mov    -0x14(%rbp),%eax
cmp    0x2e14(%rip),%eax
jne    1279 <check_charset+0x139>
mov    -0x10(%rbp),%rdi
mov    0x2df7(%rip),%rsi
mov    0x2dfd(%rip),%edx
call   1060 <strncasecmp@plt>
cmp    $0x0,%eax
jne    1279 <check_charset+0x139>
movl   $0x1,-0x4(%rbp)
jmp    1280 <check_charset+0x140>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_charset>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
cmpq   $0x0,0x2ef3(%rip)
je     1181 <check_charset+0x41>
cmp    %ebx,0x2efb(%rip)
jne    1202 <check_charset+0xc2>
mov    0x2ede(%rip),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1060 <strncasecmp@plt>
test   %eax,%eax
jne    1202 <check_charset+0xc2>
jmp    1228 <check_charset+0xe8>
xor    %eax,%eax
call   1050 <get_charset@plt>
test   %rax,%rax
lea    0xe6e(%rip),%r15
cmove  %r15,%rax
mov    %rax,0x2eab(%rip)
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,0x2ead(%rip)
mov    0x2e96(%rip),%r12
lea    0xe50(%rip),%rdi
mov    %r12,%rsi
mov    $0x9,%edx
call   1060 <strncasecmp@plt>
test   %eax,%eax
je     11e1 <check_charset+0xa1>
lea    0xe42(%rip),%rdi
mov    %r12,%rsi
call   1030 <strcasecmp@plt>
test   %eax,%eax
jne    1157 <check_charset+0x17>
mov    %r15,0x2e68(%rip)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,0x2e66(%rip)
cmp    %ebx,0x2e5c(%rip)
je     1163 <check_charset+0x23>
mov    0x2e47(%rip),%rsi
test   %rsi,%rsi
je     1224 <check_charset+0xe4>
cmp    %ebx,0x2e48(%rip)
jne    1224 <check_charset+0xe4>
mov    %r14,%rdi
mov    %ebx,%edx
call   1060 <strncasecmp@plt>
test   %eax,%eax
je     1228 <check_charset+0xe8>
xor    %eax,%eax
jmp    122d <check_charset+0xed>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@check_charset.mm_charset = internal global i8* null
@check_charset.alt_charset = internal global i8* null
@check_charset.mm_len = internal global i32 0
@check_charset.alt_len = internal global i32 0
@.str = [9 x i8] c"US-ASCII\00"
@.str.1 = [10 x i8] c"ISO-8859-\00"
@.str.2 = [6 x i8] c"UTF-8\00"
define i32 @check_charset(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%6 = load i8*, i8** @check_charset.mm_charset
%7 = icmp ne i8* %6, null
br i1 %7, label %26, label %8
8: ; preds = %2
%9 = call i8* (...) @get_charset()
store i8* %9, i8** @check_charset.mm_charset
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %8
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** @check_charset.mm_charset
br label %12
12: ; preds = %11, %8
%13 = load i8*, i8** @check_charset.mm_charset
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* @check_charset.mm_len
%15 = load i8*, i8** @check_charset.mm_charset
%16 = call i32 @strncasecmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 9)
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = load i8*, i8** @check_charset.mm_charset
%20 = call i32 @strcasecmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %18, %12
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** @check_charset.alt_charset
%23 = load i8*, i8** @check_charset.alt_charset
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* @check_charset.alt_len
br label %25
25: ; preds = %22, %18
br label %26
26: ; preds = %25, %2
%27 = load i32, i32* %5
%28 = load i32, i32* @check_charset.mm_len
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %37
30: ; preds = %26
%31 = load i8*, i8** %4
%32 = load i8*, i8** @check_charset.mm_charset
%33 = load i32, i32* @check_charset.mm_len
%34 = call i32 @strncasecmp(i8* %31, i8* %32, i32 %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %30
store i32 1, i32* %3
br label %52
37: ; preds = %30, %26
%38 = load i8*, i8** @check_charset.alt_charset
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %51
40: ; preds = %37
%41 = load i32, i32* %5
%42 = load i32, i32* @check_charset.alt_len
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %51
44: ; preds = %40
%45 = load i8*, i8** %4
%46 = load i8*, i8** @check_charset.alt_charset
%47 = load i32, i32* @check_charset.alt_len
%48 = call i32 @strncasecmp(i8* %45, i8* %46, i32 %47)
%49 = icmp ne i32 %48, 0
br i1 %49, label %51, label %50
50: ; preds = %44
store i32 1, i32* %3
br label %52
51: ; preds = %44, %40, %37
store i32 0, i32* %3
br label %52
52: ; preds = %51, %50, %36
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@check_charset.mm_charset = internal unnamed_addr global i8* null
@check_charset.alt_charset = internal unnamed_addr global i8* null
@check_charset.mm_len = internal unnamed_addr global i32 0
@check_charset.alt_len = internal unnamed_addr global i32 0
@.str = [9 x i8] c"US-ASCII\00"
@.str.1 = [10 x i8] c"ISO-8859-\00"
@.str.2 = [6 x i8] c"UTF-8\00"
define i32 @check_charset(i8* nocapture %0, i32 %1) {
%3 = load i8*, i8** @check_charset.mm_charset
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %18
5: ; preds = %2
%6 = tail call i8* (...) @get_charset()
%7 = icmp eq i8* %6, null
%8 = select i1 %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %6
store i8* %8, i8** @check_charset.mm_charset
%9 = tail call i32 @strlen(i8* %8)
store i32 %9, i32* @check_charset.mm_len
%10 = load i8*, i8** @check_charset.mm_charset
%11 = tail call i32 @strncasecmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %10, i32 9)
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %5
%14 = tail call i32 @strcasecmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13, %5
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** @check_charset.alt_charset
%17 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 %17, i32* @check_charset.alt_len
br label %18
18: ; preds = %13, %16, %2
%19 = load i32, i32* @check_charset.mm_len
%20 = icmp eq i32 %19, %1
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i8*, i8** @check_charset.mm_charset
%23 = tail call i32 @strncasecmp(i8* %0, i8* %22, i32 %1)
%24 = icmp eq i32 %23, 0
br i1 %24, label %35, label %25
25: ; preds = %21, %18
%26 = load i8*, i8** @check_charset.alt_charset
%27 = icmp ne i8* %26, null
%28 = load i32, i32* @check_charset.alt_len
%29 = icmp eq i32 %28, %1
%30 = select i1 %27, i1 %29, i1 false
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = tail call i32 @strncasecmp(i8* %0, i8* %26, i32 %1)
%33 = icmp eq i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %31, %25
br label %35
35: ; preds = %31, %21, %34
%36 = phi i32 [ 0, %34 ], [ 1, %21 ], [ 1, %31 ]
ret i32 %36
}