#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 long uint32_t;

/* Variables and functions */
long find_array(char *, int, char *, long, long);
int /*<<< orphan*/ printf(char *, long, int);
long read_four_byte_buff(char *);
long int xfs_sb_detect(char *buffer, long int buff_size, long int offset) {
  long int mag_loc;
  int blk_val = 0;
  uint32_t blk_size;
  char *blk_size_loc, mag_num[4] = {0x58, 0x46, 0x53, 0x42};
  mag_loc = find_array(mag_num, 4, buffer, buff_size, offset);
  blk_size_loc = &buffer[mag_loc + 4];
  blk_size = read_four_byte_buff(blk_size_loc);
  if (mag_loc >= 0) {
    if (mag_loc > blk_size)
      blk_val = mag_loc / blk_size;
    printf("- XFS superblock found at byte %ld - filesystem block %d\n",
           mag_loc, blk_val);
  }
  return mag_loc;
}


x86 assembly (clang, -O0):
<xfs_sb_detect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0xeaf(%rip),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x3c(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x4,%esi
call   1050 <find_array@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    $0x4,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <read_four_byte_buff@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jl     11cb <xfs_sb_detect+0x9b>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jle    11b8 <xfs_sb_detect+0x88>
mov    -0x20(%rbp),%rax
cqto
idivq  -0x30(%rbp)
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xe3e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfs_sb_detect>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r8
mov    %rsi,%rcx
mov    %rdi,%rbx
movl   $0x42534658,0x4(%rsp)
42
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1050 <find_array@plt>
mov    %rax,%r14
lea    (%rax,%rbx,1),%rdi
add    $0x4,%rdi
call   1040 <read_four_byte_buff@plt>
test   %r14,%r14
js     11a7 <xfs_sb_detect+0x77>
mov    %rax,%rcx
xor    %edx,%edx
cmp    %rax,%r14
jle    1198 <xfs_sb_detect+0x68>
mov    %r14,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     118f <xfs_sb_detect+0x5f>
mov    %r14,%rax
cqto
idiv   %rcx
mov    %rax,%rdx
jmp    1198 <xfs_sb_detect+0x68>
mov    %r14d,%eax
xor    %edx,%edx
div    %ecx
mov    %eax,%edx
lea    0xe61(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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