#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 */
struct bitfield {
  int dummy;
};

/* Variables and functions */
unsigned int bfcmp(struct bitfield *, struct bitfield const *,
                   int /*<<< orphan*/ *);
int /*<<< orphan*/ bfdel(struct bitfield *);
unsigned int bfsize(struct bitfield const *);
struct bitfield *bfsub(struct bitfield const *, int, unsigned int);
int bfpos(const struct bitfield *haystack, const struct bitfield *needle) {
  unsigned int heystack_size = bfsize(haystack);
  unsigned int needle_size = bfsize(needle);
  int i;
  int diff = heystack_size - needle_size;
  if (diff < 0) {
    return -2;
  }
  for (i = 0; i <= diff; i++) {
    struct bitfield *needle_candidate = bfsub(haystack, i, needle_size + i);
    unsigned int result;
    result = bfcmp(needle_candidate, needle, NULL);
    bfdel(needle_candidate);
    if (result == 0) {
      return i;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<bfpos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <bfsize@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <bfsize@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1187 <bfpos+0x47>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11fc <bfpos+0xbc>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jg     11f5 <bfpos+0xb5>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
add    -0x24(%rbp),%edx
call   1030 <bfsub@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <bfcmp@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <bfdel@plt>
cmpl   $0x0,-0x34(%rbp)
jne    11e2 <bfpos+0xa2>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fc <bfpos+0xbc>
jmp    11e7 <bfpos+0xa7>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    118e <bfpos+0x4e>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bfpos>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,(%rsp)
call   1060 <bfsize@plt>
mov    %eax,%r13d
mov    %rbx,%r15
mov    %rbx,%rdi
call   1060 <bfsize@plt>
sub    %eax,%r13d
js     11be <bfpos+0x7e>
mov    %eax,%r12d
add    $0x1,%r13d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%r12,%rbx,1),%edx
mov    (%rsp),%rdi
mov    %ebx,%esi
call   1030 <bfsub@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1040 <bfcmp@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1050 <bfdel@plt>
test   %r14d,%r14d
je     11c3 <bfpos+0x83>
add    $0x1,%ebx
cmp    %ebx,%r13d
jne    1180 <bfpos+0x40>
mov    $0xffffffff,%ebx
jmp    11c3 <bfpos+0x83>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bitfield = type { i32 }
define i32 @bfpos(%struct.bitfield* %0, %struct.bitfield* %1) {
%3 = alloca i32
%4 = alloca %struct.bitfield*
%5 = alloca %struct.bitfield*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.bitfield*
%11 = alloca i32
store %struct.bitfield* %0, %struct.bitfield** %4
store %struct.bitfield* %1, %struct.bitfield** %5
%12 = load %struct.bitfield*, %struct.bitfield** %4
%13 = call i32 @bfsize(%struct.bitfield* %12)
store i32 %13, i32* %6
%14 = load %struct.bitfield*, %struct.bitfield** %5
%15 = call i32 @bfsize(%struct.bitfield* %14)
store i32 %15, i32* %7
%16 = load i32, i32* %6
%17 = load i32, i32* %7
%18 = sub i32 %16, %17
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %2
store i32 -2, i32* %3
br label %48
22: ; preds = %2
store i32 0, i32* %8
br label %23
23: ; preds = %44, %22
%24 = load i32, i32* %8
%25 = load i32, i32* %9
%26 = icmp sle i32 %24, %25
br i1 %26, label %27, label %47
27: ; preds = %23
%28 = load %struct.bitfield*, %struct.bitfield** %4
%29 = load i32, i32* %8
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = add i32 %30, %31
%33 = call %struct.bitfield* @bfsub(%struct.bitfield* %28, i32 %29, i32 %32)
store %struct.bitfield* %33, %struct.bitfield** %10
%34 = load %struct.bitfield*, %struct.bitfield** %10
%35 = load %struct.bitfield*, %struct.bitfield** %5
%36 = call i32 @bfcmp(%struct.bitfield* %34, %struct.bitfield* %35, i32* null)
store i32 %36, i32* %11
%37 = load %struct.bitfield*, %struct.bitfield** %10
%38 = call i32 @bfdel(%struct.bitfield* %37)
%39 = load i32, i32* %11
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %27
%42 = load i32, i32* %8
store i32 %42, i32* %3
br label %48
43: ; preds = %27
br label %44
44: ; preds = %43
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
br label %23
47: ; preds = %23
store i32 -1, i32* %3
br label %48
48: ; preds = %47, %41, %21
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.bitfield = type { i32 }
define i32 @bfpos(%struct.bitfield* %0, %struct.bitfield* %1) {
%3 = tail call i32 @bfsize(%struct.bitfield* %0)
%4 = tail call i32 @bfsize(%struct.bitfield* %1)
%5 = sub i32 %3, %4
%6 = icmp slt i32 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %2
%8 = add i32 %3, 1
%9 = sub i32 %8, %4
br label %10
10: ; preds = %7, %17
%11 = phi i32 [ %18, %17 ], [ 0, %7 ]
%12 = add i32 %11, %4
%13 = tail call %struct.bitfield* @bfsub(%struct.bitfield* %0, i32 %11, i32 %12)
%14 = tail call i32 @bfcmp(%struct.bitfield* %13, %struct.bitfield* %1, i32* null)
%15 = tail call i32 @bfdel(%struct.bitfield* %13)
%16 = icmp eq i32 %14, 0
br i1 %16, label %20, label %17
17: ; preds = %10
%18 = add nuw i32 %11, 1
%19 = icmp eq i32 %18, %9
br i1 %19, label %20, label %10
20: ; preds = %10, %17, %2
%21 = phi i32 [ -2, %2 ], [ %11, %10 ], [ -1, %17 ]
ret i32 %21
}