#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 */
scalar_t__ strncmp(char const *, char *, int);
unsigned short check_bom(const char *s) {
  do {
    if (0 == strncmp(s, "\xEF\xBB\xBF", sizeof("\xEF\xBB\xBF") - 1)) {
      return sizeof("\xEF\xBB\xBF") - 1;
    }
  } while (0);
  do {
    if (0 == strncmp(s, "\xFE\xFF", sizeof("\xFE\xFF") - 1)) {
      return sizeof("\xFE\xFF") - 1;
    }
  } while (0);
  do {
    if (0 == strncmp(s, "\xFF\xFE", sizeof("\xFF\xFE") - 1)) {
      return sizeof("\xFF\xFE") - 1;
    }
  } while (0);
  return 0;
}


x86 assembly (clang, -O0):
<check_bom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xed9(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    114a <check_bom+0x3a>
movw   $0x3,-0x2(%rbp)
jmp    11c5 <check_bom+0xb5>
jmp    114f <check_bom+0x3f>
jmp    1154 <check_bom+0x44>
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1182 <check_bom+0x72>
movw   $0x2,-0x2(%rbp)
jmp    11c5 <check_bom+0xb5>
jmp    1187 <check_bom+0x77>
jmp    118c <check_bom+0x7c>
mov    -0x10(%rbp),%rdi
lea    0xe70(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11ba <check_bom+0xaa>
movw   $0x2,-0x2(%rbp)
jmp    11c5 <check_bom+0xb5>
jmp    11bf <check_bom+0xaf>
movw   $0x0,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_bom>:
push   %rbx
mov    %rdi,%rbx
lea    0xee5(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1166 <check_bom+0x56>
lea    0xed3(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     116c <check_bom+0x5c>
lea    0xebd(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
sete   %al
add    %eax,%eax
pop    %rbx
ret
mov    $0x3,%ax
pop    %rbx
ret
mov    $0x2,%ax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"\EF\BB\BF\00"
@.str.1 = [3 x i8] c"\FE\FF\00"
@.str.2 = [3 x i8] c"\FF\FE\00"
define zeroext i16 @check_bom(i8* %0) {
%2 = alloca i16
%3 = alloca i8*
store i8* %0, i8** %3
br label %4
4: ; preds = %1
%5 = load i8*, i8** %3
%6 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%7 = icmp eq i64 0, %6
br i1 %7, label %8, label %9
8: ; preds = %4
store i16 3, i16* %2
br label %25
9: ; preds = %4
br label %10
10: ; preds = %9
br label %11
11: ; preds = %10
%12 = load i8*, i8** %3
%13 = call i64 @strncmp(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%14 = icmp eq i64 0, %13
br i1 %14, label %15, label %16
15: ; preds = %11
store i16 2, i16* %2
br label %25
16: ; preds = %11
br label %17
17: ; preds = %16
br label %18
18: ; preds = %17
%19 = load i8*, i8** %3
%20 = call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%21 = icmp eq i64 0, %20
br i1 %21, label %22, label %23
22: ; preds = %18
store i16 2, i16* %2
br label %25
23: ; preds = %18
br label %24
24: ; preds = %23
store i16 0, i16* %2
br label %25
25: ; preds = %24, %22, %15, %8
%26 = load i16, i16* %2
ret i16 %26
}

LLVM IR (-O2):
@.str = [4 x i8] c"\EF\BB\BF\00"
@.str.1 = [3 x i8] c"\FE\FF\00"
@.str.2 = [3 x i8] c"\FF\FE\00"
define zeroext i16 @check_bom(i8* %0) {
%2 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%3 = icmp eq i64 %2, 0
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %4
%8 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%9 = icmp eq i64 %8, 0
%10 = select i1 %9, i16 2, i16 0
br label %11
11: ; preds = %7, %4, %1
%12 = phi i16 [ 3, %1 ], [ 2, %4 ], [ %10, %7 ]
ret i16 %12
}