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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
char *detect_bom(int /*<<< orphan*/ *);
long find_first_non_us_ascii(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
scalar_t__ is_iso_8859_15(int /*<<< orphan*/ *);
scalar_t__ is_utf_8(int /*<<< orphan*/ *);
scalar_t__ is_windows_1252(int /*<<< orphan*/ *);
scalar_t__ strcmp(char const *, char *);
char const *detect_encoding(FILE *fp) {
  long interesting_offset;
  const char *bom = detect_bom(fp);
  if (strcmp(bom, "UNKNOWN") != 0) {
    return bom;
  }
  interesting_offset = find_first_non_us_ascii(fp);
  if (interesting_offset == -1)
    return "US-ASCII";
  fseek(fp, interesting_offset, SEEK_SET);
  if (is_utf_8(fp))
    return "UTF-8";
  fseek(fp, interesting_offset, SEEK_SET);
  if (is_iso_8859_15(fp))
    return "ISO-8859-15";
  fseek(fp, interesting_offset, SEEK_SET);
  if (is_windows_1252(fp))
    return "WINDOWS-1252";
  return "UNKNOWN";
}


x86 assembly (clang, -O0):
<detect_encoding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <detect_bom@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe6c(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     11b0 <detect_encoding+0x40>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    128e <detect_encoding+0x11e>
mov    -0x10(%rbp),%rdi
call   1040 <find_first_non_us_ascii@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11d8 <detect_encoding+0x68>
lea    0xe39(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    128e <detect_encoding+0x11e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1030 <is_utf_8@plt>
cmp    $0x0,%rax
je     1211 <detect_encoding+0xa1>
lea    0xe09(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    128e <detect_encoding+0x11e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1080 <is_iso_8859_15@plt>
cmp    $0x0,%rax
je     124a <detect_encoding+0xda>
lea    0xdd6(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    128e <detect_encoding+0x11e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1060 <is_windows_1252@plt>
cmp    $0x0,%rax
je     1283 <detect_encoding+0x113>
lea    0xda9(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    128e <detect_encoding+0x11e>
lea    0xd76(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detect_encoding>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1050 <detect_bom@plt>
mov    %rax,%rbx
lea    0xe76(%rip),%r14
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11a9 <detect_encoding+0x39>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r12,%rdi
call   1040 <find_first_non_us_ascii@plt>
cmp    $0xffffffffffffffff,%rax
je     11e4 <detect_encoding+0x74>
mov    %rax,%r15
mov    0x2e1f(%rip),%rbx
mov    (%rbx),%edx
mov    %r12,%rdi
mov    %rax,%rsi
call   1090 <fseek@plt>
mov    %r12,%rdi
call   1030 <is_utf_8@plt>
test   %rax,%rax
je     11ed <detect_encoding+0x7d>
lea    0xe2f(%rip),%rbx
jmp    119a <detect_encoding+0x2a>
lea    0xe1d(%rip),%rbx
jmp    119a <detect_encoding+0x2a>
mov    (%rbx),%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <fseek@plt>
mov    %r12,%rdi
call   1080 <is_iso_8859_15@plt>
test   %rax,%rax
je     1210 <detect_encoding+0xa0>
lea    0xe09(%rip),%rbx
jmp    119a <detect_encoding+0x2a>
mov    (%rbx),%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <fseek@plt>
mov    %r12,%rdi
call   1060 <is_windows_1252@plt>
test   %rax,%rax
lea    0xdf4(%rip),%rbx
cmove  %r14,%rbx
jmp    119a <detect_encoding+0x2a>

LLVM IR (-O0):
@.str = [8 x i8] c"UNKNOWN\00"
@.str.1 = [9 x i8] c"US-ASCII\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"UTF-8\00"
@.str.3 = [12 x i8] c"ISO-8859-15\00"
@.str.4 = [13 x i8] c"WINDOWS-1252\00"
define i8* @detect_encoding(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = call i8* @detect_bom(i32* %6)
store i8* %7, i8** %5
%8 = load i8*, i8** %5
%9 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i8*, i8** %5
store i8* %12, i8** %2
br label %47
13: ; preds = %1
%14 = load i32*, i32** %3
%15 = call i64 @find_first_non_us_ascii(i32* %14)
store i64 %15, i64* %4
%16 = load i64, i64* %4
%17 = icmp eq i64 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %13
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %47
19: ; preds = %13
%20 = load i32*, i32** %3
%21 = load i64, i64* %4
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %20, i64 %21, i32 %22)
%24 = load i32*, i32** %3
%25 = call i64 @is_utf_8(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %19
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %47
28: ; preds = %19
%29 = load i32*, i32** %3
%30 = load i64, i64* %4
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @fseek(i32* %29, i64 %30, i32 %31)
%33 = load i32*, i32** %3
%34 = call i64 @is_iso_8859_15(i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %28
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %47
37: ; preds = %28
%38 = load i32*, i32** %3
%39 = load i64, i64* %4
%40 = load i32, i32* @SEEK_SET
%41 = call i32 @fseek(i32* %38, i64 %39, i32 %40)
%42 = load i32*, i32** %3
%43 = call i64 @is_windows_1252(i32* %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %37
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8** %2
br label %47
46: ; preds = %37
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %2
br label %47
47: ; preds = %46, %45, %36, %27, %18, %11
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
@.str = [8 x i8] c"UNKNOWN\00"
@.str.1 = [9 x i8] c"US-ASCII\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"UTF-8\00"
@.str.3 = [12 x i8] c"ISO-8859-15\00"
@.str.4 = [13 x i8] c"WINDOWS-1252\00"
define i8* @detect_encoding(i32* %0) {
%2 = tail call i8* @detect_bom(i32* %0)
%3 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %24
5: ; preds = %1
%6 = tail call i64 @find_first_non_us_ascii(i32* %0)
%7 = icmp eq i64 %6, -1
br i1 %7, label %24, label %8
8: ; preds = %5
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %0, i64 %6, i32 %9)
%11 = tail call i64 @is_utf_8(i32* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %8
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %0, i64 %6, i32 %14)
%16 = tail call i64 @is_iso_8859_15(i32* %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %13
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @fseek(i32* %0, i64 %6, i32 %19)
%21 = tail call i64 @is_windows_1252(i32* %0)
%22 = icmp eq i64 %21, 0
%23 = select i1 %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0)
br label %24
24: ; preds = %18, %13, %8, %5, %1
%25 = phi i8* [ %2, %1 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %5 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), %13 ], [ %23, %18 ]
ret i8* %25
}