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

/* Variables and functions */
int U_ZERO_ERROR;
int /*<<< orphan*/ *ucsdet_detect(int /*<<< orphan*/ *, int *);
char *ucsdet_getName(int /*<<< orphan*/ const *, int *);
int /*<<< orphan*/ ucsdet_setText(int /*<<< orphan*/ *, char *, int, int *);
const char *detectCharset(void *detector, void *input, int input_len,
                          int *status) {
  const UCharsetMatch *bestGuess;
  const char *bestGuessedCharset = NULL;
  ucsdet_setText((UCharsetDetector *)detector, (char *)input, input_len,
                 status);
  if (*status != U_ZERO_ERROR) {
    return NULL;
  }
  bestGuess = ucsdet_detect((UCharsetDetector *)detector, status);
  if (*status != U_ZERO_ERROR) {
    return NULL;
  }
  if (bestGuess == 0) {
    return NULL;
  }
  bestGuessedCharset = ucsdet_getName(bestGuess, status);
  if (*status != U_ZERO_ERROR) {
    return NULL;
  }
  return bestGuessedCharset;
}


x86 assembly (clang, -O0):
<detectCharset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
call   1050 <ucsdet_setText@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%eax
je     1185 <detectCharset+0x55>
movq   $0x0,-0x8(%rbp)
jmp    120b <detectCharset+0xdb>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <ucsdet_detect@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%eax
je     11b8 <detectCharset+0x88>
movq   $0x0,-0x8(%rbp)
jmp    120b <detectCharset+0xdb>
cmpq   $0x0,-0x30(%rbp)
jne    11d0 <detectCharset+0xa0>
movq   $0x0,-0x8(%rbp)
jmp    120b <detectCharset+0xdb>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <ucsdet_getName@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%eax
je     1203 <detectCharset+0xd3>
movq   $0x0,-0x8(%rbp)
jmp    120b <detectCharset+0xdb>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detectCharset>:
push   %r15
push   %r14
push   %rbx
mov    %rcx,%rbx
mov    %rdi,%r14
call   1050 <ucsdet_setText@plt>
mov    (%rbx),%eax
mov    0x2e9f(%rip),%r15
cmp    (%r15),%eax
jne    1182 <detectCharset+0x52>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <ucsdet_detect@plt>
mov    %rax,%rdi
mov    (%rbx),%ecx
xor    %eax,%eax
cmp    (%r15),%ecx
jne    1184 <detectCharset+0x54>
test   %rdi,%rdi
je     1184 <detectCharset+0x54>
mov    %rbx,%rsi
call   1040 <ucsdet_getName@plt>
mov    %rax,%rcx
mov    (%rbx),%edx
xor    %eax,%eax
cmp    (%r15),%edx
cmove  %rcx,%rax
jmp    1184 <detectCharset+0x54>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@U_ZERO_ERROR = global i32 0
define i8* @detectCharset(i8* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i8* null, i8** %11
%12 = load i8*, i8** %6
%13 = bitcast i8* %12 to i32*
%14 = load i8*, i8** %7
%15 = load i32, i32* %8
%16 = load i32*, i32** %9
%17 = call i32 @ucsdet_setText(i32* %13, i8* %14, i32 %15, i32* %16)
%18 = load i32*, i32** %9
%19 = load i32, i32* %18
%20 = load i32, i32* @U_ZERO_ERROR
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %4
store i8* null, i8** %5
br label %48
23: ; preds = %4
%24 = load i8*, i8** %6
%25 = bitcast i8* %24 to i32*
%26 = load i32*, i32** %9
%27 = call i32* @ucsdet_detect(i32* %25, i32* %26)
store i32* %27, i32** %10
%28 = load i32*, i32** %9
%29 = load i32, i32* %28
%30 = load i32, i32* @U_ZERO_ERROR
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %23
store i8* null, i8** %5
br label %48
33: ; preds = %23
%34 = load i32*, i32** %10
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %37
36: ; preds = %33
store i8* null, i8** %5
br label %48
37: ; preds = %33
%38 = load i32*, i32** %10
%39 = load i32*, i32** %9
%40 = call i8* @ucsdet_getName(i32* %38, i32* %39)
store i8* %40, i8** %11
%41 = load i32*, i32** %9
%42 = load i32, i32* %41
%43 = load i32, i32* @U_ZERO_ERROR
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %37
store i8* null, i8** %5
br label %48
46: ; preds = %37
%47 = load i8*, i8** %11
store i8* %47, i8** %5
br label %48
48: ; preds = %46, %45, %36, %32, %22
%49 = load i8*, i8** %5
ret i8* %49
}

LLVM IR (-O2):
@U_ZERO_ERROR = global i32 0
define i8* @detectCharset(i8* %0, i8* %1, i32 %2, i32* %3) {
%5 = bitcast i8* %0 to i32*
%6 = tail call i32 @ucsdet_setText(i32* %5, i8* %1, i32 %2, i32* %3)
%7 = load i32, i32* %3
%8 = load i32, i32* @U_ZERO_ERROR
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %23
10: ; preds = %4
%11 = tail call i32* @ucsdet_detect(i32* %5, i32* %3)
%12 = load i32, i32* %3
%13 = load i32, i32* @U_ZERO_ERROR
%14 = icmp ne i32 %12, %13
%15 = icmp eq i32* %11, null
%16 = select i1 %14, i1 true, i1 %15
br i1 %16, label %23, label %17
17: ; preds = %10
%18 = tail call i8* @ucsdet_getName(i32* %11, i32* %3)
%19 = load i32, i32* %3
%20 = load i32, i32* @U_ZERO_ERROR
%21 = icmp eq i32 %19, %20
%22 = select i1 %21, i8* %18, i8* null
br label %23
23: ; preds = %17, %10, %4
%24 = phi i8* [ null, %4 ], [ null, %10 ], [ %22, %17 ]
ret i8* %24
}