#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 int /*<<< orphan*/ pat;
typedef int off_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
scalar_t__ memcmp(unsigned char const *, unsigned char *, int);
int /*<<< orphan*/ memmove(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int read(int, unsigned char *, int);
off_t trim_fpga_image(int fd, off_t len) {
  const unsigned char pat[] = {0xff, 0xff, 0xff, 0xff, 0xaa, 0x99, 0x55, 0x66};
  unsigned char buf[sizeof(pat)];
  memset(buf, 0, sizeof(buf));
  off_t i;
  for (i = 0; i < len; i++) {
    memmove(buf, buf + 1, sizeof(buf) - 1);
    if (read(fd, &buf[sizeof(buf) - 1], 1) < 1) {
      return -1;
    }
    if (memcmp(pat, buf, sizeof(pat)) == 0) {
      lseek(fd, -sizeof(pat), SEEK_CUR);
      return len - (i + 1 - sizeof(pat));
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<trim_fpga_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0xe9b(%rip),%rax
mov    %rax,-0x14(%rbp)
lea    -0x1c(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    122d <trim_fpga_image+0xdd>
lea    -0x1c(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
add    $0x1,%rsi
mov    $0x7,%edx
call   1070 <memmove@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
add    $0x7,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jge    11cc <trim_fpga_image+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <trim_fpga_image+0xe4>
lea    -0x14(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x8,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
jne    121a <trim_fpga_image+0xca>
mov    -0x8(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffff8,%esi
call   1030 <lseek@plt>
movslq -0xc(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
sub    $0x8,%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    1234 <trim_fpga_image+0xe4>
jmp    121f <trim_fpga_image+0xcf>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1180 <trim_fpga_image+0x30>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_fpga_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %edi,%r12d
movabs $0x665599aaffffffff,%rax
99 55 66
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
test   %ebx,%ebx
jle    11e1 <trim_fpga_image+0x91>
lea    0x9(%rsp),%r14
lea    0xf(%rsp),%r15
add    $0x7,%ebx
lea    0x8(%rsp),%rbp
lea    0x10(%rsp),%r13
xchg   %ax,%ax
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x7,%edx
call   1070 <memmove@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11e1 <trim_fpga_image+0x91>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
je     11e8 <trim_fpga_image+0x98>
add    $0xffffffff,%ebx
cmp    $0x7,%ebx
jne    11a0 <trim_fpga_image+0x50>
mov    $0xffffffff,%ebx
jmp    11fe <trim_fpga_image+0xae>
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    $0xfffffff8,%esi
call   1030 <lseek@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.trim_fpga_image.pat = [8 x i8] c"\FF\FF\FF\FF\AA\99Uf"
@SEEK_CUR = global i32 0
define i32 @trim_fpga_image(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [8 x i8]
%7 = alloca [8 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = bitcast [8 x i8]* %6 to i8*
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%11 = call i32 @memset(i8* %10, i32 0, i32 8)
store i32 0, i32* %8
br label %12
12: ; preds = %44, %2
%13 = load i32, i32* %8
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %47
16: ; preds = %12
%17 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%18 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = call i32 @memmove(i8* %17, i8* %19, i32 7)
%21 = load i32, i32* %4
%22 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 7
%23 = call i32 @read(i32 %21, i8* %22, i32 1)
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %26
25: ; preds = %16
store i32 -1, i32* %3
br label %48
26: ; preds = %16
%27 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%28 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%29 = call i64 @memcmp(i8* %27, i8* %28, i32 8)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %43
31: ; preds = %26
%32 = load i32, i32* %4
%33 = load i32, i32* @SEEK_CUR
%34 = call i32 @lseek(i32 %32, i32 -8, i32 %33)
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = sub i64 %39, 8
%41 = sub i64 %36, %40
%42 = trunc i64 %41 to i32
store i32 %42, i32* %3
br label %48
43: ; preds = %26
br label %44
44: ; preds = %43
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
br label %12
47: ; preds = %12
store i32 -1, i32* %3
br label %48
48: ; preds = %47, %31, %25
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
define i32 @trim_fpga_image(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca [8 x i8]
%5 = bitcast i64* %3 to i8*
store i64 7373968924607512575, i64* %3
%6 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 8)
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %28
9: ; preds = %2
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 1
%11 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 7
br label %12
12: ; preds = %9, %25
%13 = phi i32 [ 0, %9 ], [ %26, %25 ]
%14 = call i32 @memmove(i8* %6, i8* %10, i32 7)
%15 = call i32 @read(i32 %0, i8* %11, i32 1)
%16 = icmp slt i32 %15, 1
br i1 %16, label %28, label %17
17: ; preds = %12
%18 = call i64 @memcmp(i8* %5, i8* %6, i32 8)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i32, i32* @SEEK_CUR
%22 = call i32 @lseek(i32 %0, i32 -8, i32 %21)
%23 = add i32 %1, 7
%24 = sub i32 %23, %13
br label %28
25: ; preds = %17
%26 = add nuw nsw i32 %13, 1
%27 = icmp eq i32 %26, %1
br i1 %27, label %28, label %12
28: ; preds = %12, %25, %2, %20
%29 = phi i32 [ %24, %20 ], [ -1, %2 ], [ -1, %25 ], [ -1, %12 ]
ret i32 %29
}