#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__ uint32_t;
typedef int int64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ be32toh(scalar_t__);
scalar_t__ fread(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, scalar_t__, scalar_t__);
int64_t find_offset(FILE *in_file) {
  char pattern[4] = {0x55, 0xaa, 0x06, 0x43};
  int64_t out_offset = -1;
  int found = 0;
  char buffer[512];
  fseek(in_file, 0, SEEK_SET);
  uint32_t cur_offset = 0;
  do {
    uint32_t bytes_read = fread(&buffer, 1, 512, in_file);
    if (bytes_read == 0)
      break;
    int i = 0;
    for (i = 0; i < 4; ++i) {
      if (buffer[i] != pattern[i])
        break;
    }
    if (i == 4) {
      uint32_t swap_offset = be32toh(*((uint32_t *)&buffer[4]));
      printf("Swap offset: %u Cur Offset: %u\n", swap_offset, cur_offset);
      if (swap_offset == cur_offset) {
        found = 1;
        out_offset = cur_offset;
      }
    }
    cur_offset += bytes_read;
  } while (found == 0);
  return out_offset;
}


x86 assembly (clang, -O0):
<find_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    0xeab(%rip),%eax
mov    %eax,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
movq   $0x0,-0x228(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1030 <fread@plt>
mov    %rax,-0x230(%rbp)
cmpq   $0x0,-0x230(%rbp)
jne    11b9 <find_offset+0x79>
jmp    129e <find_offset+0x15e>
movl   $0x0,-0x234(%rbp)
movl   $0x0,-0x234(%rbp)
cmpl   $0x4,-0x234(%rbp)
jge    121b <find_offset+0xdb>
movslq -0x234(%rbp),%rax
movsbl -0x220(%rbp,%rax,1),%eax
ff
movslq -0x234(%rbp),%rcx
movsbl -0xc(%rbp,%rcx,1),%ecx
cmp    %ecx,%eax
je     1202 <find_offset+0xc2>
jmp    121b <find_offset+0xdb>
jmp    1207 <find_offset+0xc7>
mov    -0x234(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x234(%rbp)
jmp    11cd <find_offset+0x8d>
cmpl   $0x4,-0x234(%rbp)
jne    127f <find_offset+0x13f>
mov    -0x21c(%rbp),%rdi
call   1050 <be32toh@plt>
mov    %rax,-0x240(%rbp)
mov    -0x240(%rbp),%rsi
mov    -0x228(%rbp),%rdx
lea    0xdb4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x240(%rbp),%rax
cmp    -0x228(%rbp),%rax
jne    127a <find_offset+0x13a>
movl   $0x1,-0x14(%rbp)
mov    -0x228(%rbp),%rax
mov    %eax,-0x10(%rbp)
jmp    127f <find_offset+0x13f>
mov    -0x230(%rbp),%rax
add    -0x228(%rbp),%rax
mov    %rax,-0x228(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1185 <find_offset+0x45>
mov    -0x10(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_offset>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%r14
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    $0xffffffff,%r13d
mov    %rsp,%r15
jmp    1187 <find_offset+0x47>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %rbp,%rbx
test   %eax,%eax
jne    11e4 <find_offset+0xa4>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11e4 <find_offset+0xa4>
mov    %rax,%rbp
movdqa (%rsp),%xmm0
movd   %xmm0,%ecx
xor    %eax,%eax
cmp    $0x4306aa55,%ecx
jne    1180 <find_offset+0x40>
mov    0x4(%rsp),%rdi
call   1050 <be32toh@plt>
mov    %rax,%r12
lea    0xe35(%rip),%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1040 <printf@plt>
xor    %eax,%eax
cmp    %rbx,%r12
sete   %al
cmove  %ebx,%r13d
jmp    1180 <find_offset+0x40>
mov    %r13d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.find_offset.pattern = [4 x i8] c"U\AA\06C"
@SEEK_SET = global i32 0
@.str = [32 x i8] c"Swap offset: %u Cur Offset: %u\0A\00"
define i32 @find_offset(i32* %0) {
%2 = alloca i32*
%3 = alloca [4 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
store i32* %0, i32** %2
%11 = bitcast [4 x i8]* %3 to i8*
store i32 -1, i32* %4
store i32 0, i32* %5
%12 = load i32*, i32** %2
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @fseek(i32* %12, i32 0, i32 %13)
store i64 0, i64* %7
br label %15
15: ; preds = %65, %1
%16 = bitcast [512 x i8]* %6 to i8**
%17 = load i32*, i32** %2
%18 = call i64 @fread(i8** %16, i32 1, i32 512, i32* %17)
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %68
22: ; preds = %15
store i32 0, i32* %9
store i32 0, i32* %9
br label %23
23: ; preds = %40, %22
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 4
br i1 %25, label %26, label %43
26: ; preds = %23
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %31, %36
br i1 %37, label %38, label %39
38: ; preds = %26
br label %43
39: ; preds = %26
br label %40
40: ; preds = %39
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %23
43: ; preds = %38, %23
%44 = load i32, i32* %9
%45 = icmp eq i32 %44, 4
br i1 %45, label %46, label %61
46: ; preds = %43
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 4
%48 = bitcast i8* %47 to i64*
%49 = load i64, i64* %48
%50 = call i64 @be32toh(i64 %49)
store i64 %50, i64* %10
%51 = load i64, i64* %10
%52 = load i64, i64* %7
%53 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i64 %51, i64 %52)
%54 = load i64, i64* %10
%55 = load i64, i64* %7
%56 = icmp eq i64 %54, %55
br i1 %56, label %57, label %60
57: ; preds = %46
store i32 1, i32* %5
%58 = load i64, i64* %7
%59 = trunc i64 %58 to i32
store i32 %59, i32* %4
br label %60
60: ; preds = %57, %46
br label %61
61: ; preds = %60, %43
%62 = load i64, i64* %8
%63 = load i64, i64* %7
%64 = add nsw i64 %63, %62
store i64 %64, i64* %7
br label %65
65: ; preds = %61
%66 = load i32, i32* %5
%67 = icmp eq i32 %66, 0
br i1 %67, label %15, label %68
68: ; preds = %65, %21
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [32 x i8] c"Swap offset: %u Cur Offset: %u\0A\00"
define i32 @find_offset(i32* nocapture %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @fseek(i32* %0, i32 0, i32 %4)
%6 = bitcast [512 x i8]* %2 to i8**
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 4
%8 = bitcast i8* %7 to i64*
%9 = bitcast [512 x i8]* %2 to <4 x i8>*
br label %10
10: ; preds = %28, %1
%11 = phi i64 [ 0, %1 ], [ %31, %28 ]
%12 = phi i32 [ -1, %1 ], [ %30, %28 ]
%13 = call i64 @fread(i8** %6, i32 1, i32 512, i32* %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %10
%16 = load <4 x i8>, <4 x i8>* %9
%17 = freeze <4 x i8> %16
%18 = bitcast <4 x i8> %17 to i32
%19 = icmp eq i32 %18, 1124510293
br i1 %19, label %20, label %28
20: ; preds = %15
%21 = load i64, i64* %8
%22 = tail call i64 @be32toh(i64 %21)
%23 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i64 %22, i64 %11)
%24 = icmp eq i64 %22, %11
%25 = trunc i64 %11 to i32
%26 = zext i1 %24 to i32
%27 = select i1 %24, i32 %25, i32 %12
br label %28
28: ; preds = %15, %20
%29 = phi i32 [ %26, %20 ], [ 0, %15 ]
%30 = phi i32 [ %27, %20 ], [ %12, %15 ]
%31 = add nsw i64 %13, %11
%32 = icmp eq i32 %29, 0
br i1 %32, label %10, label %33
33: ; preds = %10, %28
%34 = phi i32 [ %30, %28 ], [ %12, %10 ]
ret i32 %34
}