#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 */
long find_array(char *, int, char *, long, long);
int /*<<< orphan*/ printf(char *, long, int);
long int ext_sb_detect(char *buffer, long int buff_size, long int offset) {
  long int mag_loc, sb_loc, blksz_loc;
  int num_block;
  char mag_num[2] = {0x53, 0xEF};
  mag_loc = find_array(mag_num, 2, buffer, buff_size, offset);
  if (mag_loc >= 0) {
    sb_loc = mag_loc - 56;
    blksz_loc = mag_loc - 32;
    num_block = sb_loc / (1 << (buffer[blksz_loc] + 10));
    printf("- EXT superblock found at byte %ld - filesystem block %d\n", sb_loc,
           num_block);
  }
  return mag_loc;
}


x86 assembly (clang, -O0):
<ext_sb_detect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0xec5(%rip),%ax
mov    %ax,-0x36(%rbp)
lea    -0x36(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x2,%esi
call   1040 <find_array@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jl     11ba <ext_sb_detect+0x9a>
mov    -0x20(%rbp),%rax
sub    $0x38,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
sub    $0x20,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    -0x30(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
add    $0xa,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
movslq %ecx,%rcx
cqto
idiv   %rcx
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
lea    0xe4d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ext_sb_detect>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r8
mov    %rsi,%rcx
mov    %rdi,%rbx
movw   $0xef53,0x6(%rsp)
lea    0x6(%rsp),%rdi
mov    $0x2,%esi
mov    %rbx,%rdx
call   1040 <find_array@plt>
mov    %rax,%r14
test   %rax,%rax
js     1191 <ext_sb_detect+0x71>
mov    %rax,%rsi
mov    -0x20(%r14,%rbx,1),%cl
add    $0xa,%cl
mov    $0x1,%edi
shl    %cl,%edi
add    $0xffffffffffffffc8,%rsi
movslq %edi,%rcx
mov    %rsi,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     117d <ext_sb_detect+0x5d>
mov    %rsi,%rax
cqto
idiv   %rcx
jmp    1183 <ext_sb_detect+0x63>
mov    %esi,%eax
xor    %edx,%edx
div    %edi
lea    0xe76(%rip),%rdi
mov    %eax,%edx
call   1030 <printf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.ext_sb_detect.mag_num = [2 x i8] c"S\EF"
@.str = [58 x i8] c"- EXT superblock found at byte %ld - filesystem block %d\0A\00"
define i64 @ext_sb_detect(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca [2 x i8]
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%12 = bitcast [2 x i8]* %11 to i8*
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%14 = load i8*, i8** %4
%15 = load i64, i64* %5
%16 = load i64, i64* %6
%17 = call i64 @find_array(i8* %13, i32 2, i8* %14, i64 %15, i64 %16)
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = icmp sge i64 %18, 0
br i1 %19, label %20, label %39
20: ; preds = %3
%21 = load i64, i64* %7
%22 = sub nsw i64 %21, 56
store i64 %22, i64* %8
%23 = load i64, i64* %7
%24 = sub nsw i64 %23, 32
store i64 %24, i64* %9
%25 = load i64, i64* %8
%26 = load i8*, i8** %4
%27 = load i64, i64* %9
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = add nsw i32 %30, 10
%32 = shl i32 1, %31
%33 = sext i32 %32 to i64
%34 = sdiv i64 %25, %33
%35 = trunc i64 %34 to i32
store i32 %35, i32* %10
%36 = load i64, i64* %8
%37 = load i32, i32* %10
%38 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i64 %36, i32 %37)
br label %39
39: ; preds = %20, %3
%40 = load i64, i64* %7
ret i64 %40
}

LLVM IR (-O2):
@.str = [58 x i8] c"- EXT superblock found at byte %ld - filesystem block %d\0A\00"
define i64 @ext_sb_detect(i8* %0, i64 %1, i64 %2) {
%4 = alloca i16
%5 = bitcast i16* %4 to i8*
store i16 -4269, i16* %4
%6 = call i64 @find_array(i8* %5, i32 2, i8* %0, i64 %1, i64 %2)
%7 = icmp sgt i64 %6, -1
br i1 %7, label %8, label %20
8: ; preds = %3
%9 = add nsw i64 %6, -56
%10 = add nsw i64 %6, -32
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = add nsw i32 %13, 10
%15 = shl nuw i32 1, %14
%16 = sext i32 %15 to i64
%17 = sdiv i64 %9, %16
%18 = trunc i64 %17 to i32
%19 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i64 %9, i32 %18)
br label %20
20: ; preds = %8, %3
ret i64 %6
}