#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;
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*/ *);
char const *detect_encoding(FILE *fp) {
  long interesting_offset;
  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   1040 <find_first_non_us_ascii@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    1184 <detect_encoding+0x34>
lea    0xe85(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    123a <detect_encoding+0xea>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1030 <is_utf_8@plt>
cmp    $0x0,%rax
je     11bd <detect_encoding+0x6d>
lea    0xe55(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    123a <detect_encoding+0xea>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1060 <is_iso_8859_15@plt>
cmp    $0x0,%rax
je     11f6 <detect_encoding+0xa6>
lea    0xe22(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    123a <detect_encoding+0xea>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1050 <is_windows_1252@plt>
cmp    $0x0,%rax
je     122f <detect_encoding+0xdf>
lea    0xdf5(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    123a <detect_encoding+0xea>
lea    0xdf2(%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   %rbx
mov    %rdi,%rbx
call   1040 <find_first_non_us_ascii@plt>
cmp    $0xffffffffffffffff,%rax
je     1191 <detect_encoding+0x41>
mov    %rax,%r14
mov    0x2e73(%rip),%r15
mov    (%r15),%edx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1030 <is_utf_8@plt>
test   %rax,%rax
je     119a <detect_encoding+0x4a>
lea    0xe7a(%rip),%rax
jmp    11e9 <detect_encoding+0x99>
lea    0xe68(%rip),%rax
jmp    11e9 <detect_encoding+0x99>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1060 <is_iso_8859_15@plt>
test   %rax,%rax
je     11be <detect_encoding+0x6e>
lea    0xe53(%rip),%rax
jmp    11e9 <detect_encoding+0x99>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <is_windows_1252@plt>
test   %rax,%rax
lea    0xe4a(%rip),%rcx
lea    0xe36(%rip),%rax
cmove  %rcx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"US-ASCII\00"
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"UTF-8\00"
@.str.2 = [12 x i8] c"ISO-8859-15\00"
@.str.3 = [13 x i8] c"WINDOWS-1252\00"
@.str.4 = [8 x i8] c"UNKNOWN\00"
define i8* @detect_encoding(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
store i32* %0, i32** %3
%5 = load i32*, i32** %3
%6 = call i64 @find_first_non_us_ascii(i32* %5)
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp eq i64 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %2
br label %38
10: ; preds = %1
%11 = load i32*, i32** %3
%12 = load i64, i64* %4
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @fseek(i32* %11, i64 %12, i32 %13)
%15 = load i32*, i32** %3
%16 = call i64 @is_utf_8(i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %10
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %38
19: ; preds = %10
%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_iso_8859_15(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %19
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %38
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_windows_1252(i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %28
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %38
37: ; preds = %28
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8** %2
br label %38
38: ; preds = %37, %36, %27, %18, %9
%39 = load i8*, i8** %2
ret i8* %39
}

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