#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 fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ ungetc(int, int /*<<< orphan*/ *);
const char *guess_bom(FILE *in) {
  int c, c2, c3;
  c = fgetc(in);
  if (c == 0xFE || c == 0xFF) {
    c2 = fgetc(in);
    if (c == 0xFE && c2 == 0xFF)
      return "UTF-16BE";
    if (c == 0xFF && c2 == 0xFE)
      return "UTF-16LE";
    ungetc(c2, in);
  } else if (c == 0xEF) {
    c2 = fgetc(in);
    c3 = fgetc(in);
    if (c2 == 0xBB && c3 == 0xBF)
      return "UTF-8";
    ungetc(c3, in);
    ungetc(c2, in);
  }
  ungetc(c, in);
  return NULL;
}


x86 assembly (clang, -O0):
<guess_bom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xfe,-0x14(%rbp)
je     1152 <guess_bom+0x32>
cmpl   $0xff,-0x14(%rbp)
jne    11c3 <guess_bom+0xa3>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xfe,-0x14(%rbp)
jne    1188 <guess_bom+0x68>
cmpl   $0xff,-0x18(%rbp)
jne    1188 <guess_bom+0x68>
lea    0xe81(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1243 <guess_bom+0x123>
cmpl   $0xff,-0x14(%rbp)
jne    11b2 <guess_bom+0x92>
cmpl   $0xfe,-0x18(%rbp)
jne    11b2 <guess_bom+0x92>
lea    0xe60(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1243 <guess_bom+0x123>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <ungetc@plt>
jmp    122f <guess_bom+0x10f>
cmpl   $0xef,-0x14(%rbp)
jne    122a <guess_bom+0x10a>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xbb,-0x18(%rbp)
jne    1212 <guess_bom+0xf2>
cmpl   $0xbf,-0x1c(%rbp)
jne    1212 <guess_bom+0xf2>
lea    0xe09(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1243 <guess_bom+0x123>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <ungetc@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <ungetc@plt>
jmp    122f <guess_bom+0x10f>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <ungetc@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guess_bom>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <fgetc@plt>
mov    %eax,%ebp
and    $0xfffffffe,%eax
cmp    $0xfe,%eax
jne    115d <guess_bom+0x3d>
mov    %rbx,%rdi
call   1030 <fgetc@plt>
mov    %eax,%r14d
cmp    $0xfe,%ebp
jne    1191 <guess_bom+0x71>
cmp    $0xff,%r14d
jne    1191 <guess_bom+0x71>
lea    0xea5(%rip),%rax
jmp    11cc <guess_bom+0xac>
cmp    $0xef,%ebp
jne    11c0 <guess_bom+0xa0>
mov    %rbx,%rdi
call   1030 <fgetc@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <fgetc@plt>
cmp    $0xbb,%r14d
jne    11ab <guess_bom+0x8b>
cmp    $0xbf,%eax
jne    11ab <guess_bom+0x8b>
lea    0xe83(%rip),%rax
jmp    11cc <guess_bom+0xac>
cmp    $0xff,%ebp
jne    11b5 <guess_bom+0x95>
cmp    $0xfe,%r14d
jne    11b5 <guess_bom+0x95>
lea    0xe60(%rip),%rax
jmp    11cc <guess_bom+0xac>
mov    %eax,%edi
mov    %rbx,%rsi
call   1040 <ungetc@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <ungetc@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <ungetc@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [9 x i8] c"UTF-16BE\00"
@.str.1 = [9 x i8] c"UTF-16LE\00"
@.str.2 = [6 x i8] c"UTF-8\00"
define i8* @guess_bom(i32* nocapture %0) {
%2 = tail call i32 @fgetc(i32* %0)
%3 = icmp eq i32 %2, 255
%4 = and i32 %2, -2
%5 = icmp eq i32 %4, 254
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = icmp eq i32 %2, 254
%8 = tail call i32 @fgetc(i32* %0)
%9 = icmp eq i32 %8, 255
%10 = and i1 %7, %9
br i1 %10, label %29, label %11
11: ; preds = %6
%12 = icmp eq i32 %8, 254
%13 = and i1 %3, %12
br i1 %13, label %29, label %24
14: ; preds = %1
%15 = icmp eq i32 %2, 239
br i1 %15, label %16, label %27
16: ; preds = %14
%17 = tail call i32 @fgetc(i32* %0)
%18 = tail call i32 @fgetc(i32* %0)
%19 = icmp eq i32 %17, 187
%20 = icmp eq i32 %18, 191
%21 = and i1 %19, %20
br i1 %21, label %29, label %22
22: ; preds = %16
%23 = tail call i32 @ungetc(i32 %18, i32* %0)
br label %24
24: ; preds = %11, %22
%25 = phi i32 [ %17, %22 ], [ %8, %11 ]
%26 = tail call i32 @ungetc(i32 %25, i32* %0)
br label %27
27: ; preds = %24, %14
%28 = tail call i32 @ungetc(i32 %2, i32* %0)
br label %29
29: ; preds = %16, %11, %6, %27
%30 = phi i8* [ null, %27 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %6 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %11 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %16 ]
ret i8* %30
}