#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 uint32_t;
struct imd_header {
  int dummy;
};

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ debug(char *, int);
int imd_read_le32(void *);
int imd_validate_tags(void *, int, int);
int imd_search_validate(void *buf, int size, struct imd_header **start) {
  int start_ofs = 0;
  int i, ret;
  for (i = start_ofs; i < size - 32; i++) {
    uint32_t type;
    type = imd_read_le32(buf + i);
    if (type != 0x640c0001)
      continue;
    debug("Start tag found at offset %d\n", i);
    ret = imd_validate_tags(buf, size, i);
    if (!ret) {
      *start = buf + i;
      return 0;
    }
  }
  return -EINVAL;
}


x86 assembly (clang, -O0):
<imd_search_validate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x20,%ecx
cmp    %ecx,%eax
jge    11e2 <imd_search_validate+0xb2>
mov    -0x10(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
call   1040 <imd_read_le32@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x640c0001,-0x30(%rbp)
je     1186 <imd_search_validate+0x56>
jmp    11d4 <imd_search_validate+0xa4>
mov    -0x28(%rbp),%esi
lea    0xe70(%rip),%rdi
call   1030 <debug@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1050 <imd_validate_tags@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11cf <imd_search_validate+0x9f>
mov    -0x10(%rbp),%rcx
movslq -0x28(%rbp),%rax
add    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11f0 <imd_search_validate+0xc0>
jmp    11d4 <imd_search_validate+0xa4>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1150 <imd_search_validate+0x20>
xor    %eax,%eax
mov    0x2df5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<imd_search_validate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
cmp    $0x21,%esi
jl     11a2 <imd_search_validate+0x72>
mov    %esi,%r15d
mov    %rdi,%r12
lea    -0x20(%r15),%r14d
xor    %ebx,%ebx
lea    0xea9(%rip),%r13
jmp    1169 <imd_search_validate+0x39>
nopl   0x0(%rax)
add    $0x1,%rbx
cmp    %rbx,%r14
je     11a2 <imd_search_validate+0x72>
lea    (%r12,%rbx,1),%rbp
mov    %rbp,%rdi
call   1040 <imd_read_le32@plt>
cmp    $0x640c0001,%eax
jne    1160 <imd_search_validate+0x30>
mov    %r13,%rdi
mov    %ebx,%esi
call   1030 <debug@plt>
mov    %r12,%rdi
mov    %r15d,%esi
mov    %ebx,%edx
call   1050 <imd_validate_tags@plt>
test   %eax,%eax
jne    1160 <imd_search_validate+0x30>
mov    (%rsp),%rax
mov    %rbp,(%rax)
xor    %eax,%eax
jmp    11ad <imd_search_validate+0x7d>
mov    0x2e37(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.imd_header = type { i32 }
@.str = [30 x i8] c"Start tag found at offset %d\0A\00"
@EINVAL = global i32 0
define i32 @imd_search_validate(i8* %0, i32 %1, %struct.imd_header** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.imd_header**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.imd_header** %2, %struct.imd_header*** %7
store i32 0, i32* %8
%12 = load i32, i32* %8
store i32 %12, i32* %9
br label %13
13: ; preds = %44, %3
%14 = load i32, i32* %9
%15 = load i32, i32* %6
%16 = sub nsw i32 %15, 32
%17 = icmp slt i32 %14, %16
br i1 %17, label %18, label %47
18: ; preds = %13
%19 = load i8*, i8** %5
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = getelementptr i8, i8* %19, i64 %21
%23 = call i32 @imd_read_le32(i8* %22)
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = icmp ne i32 %24, 1678508033
br i1 %25, label %26, label %27
26: ; preds = %18
br label %44
27: ; preds = %18
%28 = load i32, i32* %9
%29 = call i32 @debug(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = load i32, i32* %9
%33 = call i32 @imd_validate_tags(i8* %30, i32 %31, i32 %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp ne i32 %34, 0
br i1 %35, label %43, label %36
36: ; preds = %27
%37 = load i8*, i8** %5
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr i8, i8* %37, i64 %39
%41 = bitcast i8* %40 to %struct.imd_header*
%42 = load %struct.imd_header**, %struct.imd_header*** %7
store %struct.imd_header* %41, %struct.imd_header** %42
store i32 0, i32* %4
br label %50
43: ; preds = %27
br label %44
44: ; preds = %43, %26
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
store i32 %46, i32* %9
br label %13
47: ; preds = %13
%48 = load i32, i32* @EINVAL
%49 = sub nsw i32 0, %48
store i32 %49, i32* %4
br label %50
50: ; preds = %47, %36
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
%struct.imd_header = type { i32 }
@.str = [30 x i8] c"Start tag found at offset %d\0A\00"
@EINVAL = global i32 0
define i32 @imd_search_validate(i8* %0, i32 %1, %struct.imd_header** nocapture writeonly %2) {
%4 = icmp sgt i32 %1, 32
br i1 %4, label %5, label %23
5: ; preds = %3
%6 = add nsw i32 %1, -32
%7 = zext i32 %6 to i64
br label %8
8: ; preds = %5, %20
%9 = phi i64 [ 0, %5 ], [ %21, %20 ]
%10 = getelementptr i8, i8* %0, i64 %9
%11 = tail call i32 @imd_read_le32(i8* %10)
%12 = icmp eq i32 %11, 1678508033
br i1 %12, label %13, label %20
13: ; preds = %8
%14 = trunc i64 %9 to i32
%15 = tail call i32 @debug(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = tail call i32 @imd_validate_tags(i8* %0, i32 %1, i32 %14)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = bitcast %struct.imd_header** %2 to i8**
store i8* %10, i8** %19
br label %26
20: ; preds = %8, %13
%21 = add nuw nsw i64 %9, 1
%22 = icmp eq i64 %21, %7
br i1 %22, label %23, label %8
23: ; preds = %20, %3
%24 = load i32, i32* @EINVAL
%25 = sub nsw i32 0, %24
br label %26
26: ; preds = %18, %23
%27 = phi i32 [ 0, %18 ], [ %25, %23 ]
ret i32 %27
}