#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 scalar_t__ ssize_t;
typedef unsigned long long off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ ismft(char *);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
scalar_t__ pread(int, char *, int, unsigned long long);
int /*<<< orphan*/ printf(char *, unsigned long);
int findmft(const char *device, unsigned long long *pos, unsigned int jump) {
  int fd = open(device, O_RDONLY);
  if (fd < 0) {
    return -1;
  }
  char *buffer = (char *)malloc(1024);
  off_t ptr = *pos * 512;
  ssize_t rn;
  int found = 0;
  long unsigned int counter = 0;
  while (!found) {
    if (counter % 1000 == 0) {
      printf("\r%lu", counter);
    }
    counter++;
    rn = pread(fd, buffer, 512, ptr);
    if (rn < 0) {
      return -1;
    } else if (rn == 0) {
      return 0;
    }
    if (ismft(buffer)) {
      *pos = *pos + (ptr - *pos * 512) / 512;
      found = 1;
    } else {
      ptr += jump * 512;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<findmft>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    118e <findmft+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <findmft+0x162>
mov    $0x400,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
shl    $0x9,%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
movq   $0x0,-0x48(%rbp)
cmpl   $0x0,-0x3c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d0 <findmft+0x80>
jmp    12ab <findmft+0x15b>
mov    -0x48(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
cmp    $0x0,%rdx
jne    11f8 <findmft+0xa8>
mov    -0x48(%rbp),%rsi
lea    0xe0d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x200,%edx
call   1070 <pread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    1234 <findmft+0xe4>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <findmft+0x162>
cmpq   $0x0,-0x38(%rbp)
jne    124b <findmft+0xfb>
movl   $0x0,-0x4(%rbp)
jmp    12b2 <findmft+0x162>
jmp    1250 <findmft+0x100>
mov    -0x28(%rbp),%rdi
call   1050 <ismft@plt>
cmp    $0x0,%rax
je     1296 <findmft+0x146>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rdx
mov    (%rdx),%rdx
shl    $0x9,%rdx
sub    %rdx,%rax
shr    $0x9,%rax
add    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x3c(%rbp)
jmp    12a6 <findmft+0x156>
mov    -0x1c(%rbp),%eax
shl    $0x9,%eax
mov    %eax,%eax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11ba <findmft+0x6a>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findmft>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%r13d
mov    $0xffffffff,%eax
test   %r13d,%r13d
js     1242 <findmft+0xf2>
mov    $0x400,%edi
call   1040 <malloc@plt>
mov    %rax,%rbp
mov    %rbx,(%rsp)
mov    (%rbx),%rbx
shl    $0x9,%rbx
shl    $0x9,%r12d
xor    %r15d,%r15d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
shr    $0x4,%rdx
imul   $0x3e8,%rdx,%rax
add    %r15,%rax
jne    11e3 <findmft+0x93>
lea    0xe25(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
mov    %rbx,%rcx
call   1070 <pread@plt>
test   %rax,%rax
js     1217 <findmft+0xc7>
je     121e <findmft+0xce>
mov    %rbp,%rdi
call   1050 <ismft@plt>
test   %rax,%rax
jne    1222 <findmft+0xd2>
add    %r12,%rbx
add    $0xffffffffffffffff,%r15
add    $0x1,%r14
jmp    11b0 <findmft+0x60>
mov    $0xffffffff,%eax
jmp    1242 <findmft+0xf2>
xor    %eax,%eax
jmp    1242 <findmft+0xf2>
mov    (%rsp),%rdx
mov    (%rdx),%rax
mov    %rax,%rcx
shl    $0x9,%rcx
sub    %rcx,%rbx
shr    $0x9,%rbx
add    %rax,%rbx
mov    %rbx,(%rdx)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"\0D%lu\00"
define i32 @findmft(i8* %0, i64* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i64
store i8* %0, i8** %5
store i64* %1, i64** %6
store i32 %2, i32* %7
%14 = load i8*, i8** %5
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 -1, i32* %4
br label %75
20: ; preds = %3
%21 = call i64 @malloc(i32 1024)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %9
%23 = load i64*, i64** %6
%24 = load i64, i64* %23
%25 = mul i64 %24, 512
store i64 %25, i64* %10
store i32 0, i32* %12
store i64 0, i64* %13
br label %26
26: ; preds = %73, %20
%27 = load i32, i32* %12
%28 = icmp ne i32 %27, 0
%29 = xor i1 %28, true
br i1 %29, label %30, label %74
30: ; preds = %26
%31 = load i64, i64* %13
%32 = urem i64 %31, 1000
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i64, i64* %13
%36 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %35)
br label %37
37: ; preds = %34, %30
%38 = load i64, i64* %13
%39 = add i64 %38, 1
store i64 %39, i64* %13
%40 = load i32, i32* %8
%41 = load i8*, i8** %9
%42 = load i64, i64* %10
%43 = call i64 @pread(i32 %40, i8* %41, i32 512, i64 %42)
store i64 %43, i64* %11
%44 = load i64, i64* %11
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %37
store i32 -1, i32* %4
br label %75
47: ; preds = %37
%48 = load i64, i64* %11
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %47
store i32 0, i32* %4
br label %75
51: ; preds = %47
br label %52
52: ; preds = %51
%53 = load i8*, i8** %9
%54 = call i64 @ismft(i8* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %67
56: ; preds = %52
%57 = load i64*, i64** %6
%58 = load i64, i64* %57
%59 = load i64, i64* %10
%60 = load i64*, i64** %6
%61 = load i64, i64* %60
%62 = mul i64 %61, 512
%63 = sub i64 %59, %62
%64 = udiv i64 %63, 512
%65 = add i64 %58, %64
%66 = load i64*, i64** %6
store i64 %65, i64* %66
store i32 1, i32* %12
br label %73
67: ; preds = %52
%68 = load i32, i32* %7
%69 = mul i32 %68, 512
%70 = zext i32 %69 to i64
%71 = load i64, i64* %10
%72 = add i64 %71, %70
store i64 %72, i64* %10
br label %73
73: ; preds = %67, %56
br label %26
74: ; preds = %26
store i32 1, i32* %4
br label %75
75: ; preds = %74, %50, %46, %19
%76 = load i32, i32* %4
ret i32 %76
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"\0D%lu\00"
define i32 @findmft(i8* nocapture %0, i64* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %38, label %7
7: ; preds = %3
%8 = tail call i64 @malloc(i32 1024)
%9 = inttoptr i64 %8 to i8*
%10 = load i64, i64* %1
%11 = shl i64 %10, 9
%12 = shl i32 %2, 9
%13 = zext i32 %12 to i64
br label %14
14: ; preds = %36, %7
%15 = phi i64 [ 0, %7 ], [ %22, %36 ]
%16 = phi i64 [ %11, %7 ], [ %37, %36 ]
%17 = urem i64 %15, 1000
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %15)
br label %21
21: ; preds = %19, %14
%22 = add i64 %15, 1
%23 = tail call i64 @pread(i32 %5, i8* %9, i32 512, i64 %16)
%24 = icmp slt i64 %23, 0
br i1 %24, label %38, label %25
25: ; preds = %21
%26 = icmp eq i64 %23, 0
br i1 %26, label %38, label %27
27: ; preds = %25
%28 = tail call i64 @ismft(i8* %9)
%29 = icmp eq i64 %28, 0
br i1 %29, label %36, label %30
30: ; preds = %27
%31 = load i64, i64* %1
%32 = mul i64 %31, -512
%33 = add i64 %32, %16
%34 = lshr exact i64 %33, 9
%35 = add i64 %34, %31
store i64 %35, i64* %1
br label %38
36: ; preds = %27
%37 = add i64 %16, %13
br label %14
38: ; preds = %21, %25, %30, %3
%39 = phi i32 [ -1, %3 ], [ 1, %30 ], [ 0, %25 ], [ -1, %21 ]
ret i32 %39
}