#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 bpf_insn {
  scalar_t__ code;
  scalar_t__ imm;
};

/* Variables and functions */
int probe_filter_length(struct bpf_insn *fp) {
  int len = 0;
  for (len = 512 - 1; len > 0; --len)
    if (fp[len].code != 0 || fp[len].imm != 0)
      break;
  return len + 1;
}


x86 assembly (clang, -O0):
<probe_filter_length>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x1ff,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    116b <probe_filter_length+0x6b>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
cmpq   $0x0,(%rax)
jne    1153 <probe_filter_length+0x53>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
cmpq   $0x0,0x8(%rax)
je     1158 <probe_filter_length+0x58>
jmp    116b <probe_filter_length+0x6b>
jmp    115d <probe_filter_length+0x5d>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1116 <probe_filter_length+0x16>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<probe_filter_length>:
lea    0x1ff8(%rdi),%rdx
mov    $0x1ff,%ecx
cmpq   $0x0,-0x8(%rdx)
jne    1151 <probe_filter_length+0x51>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpq   $0x0,(%rdx)
jne    1151 <probe_filter_length+0x51>
cmp    $0x2,%ecx
jb     1157 <probe_filter_length+0x57>
lea    -0x1(%rcx),%eax
mov    %eax,%esi
shl    $0x4,%rsi
cmpq   $0x0,(%rdi,%rsi,1)
jne    1159 <probe_filter_length+0x59>
cmpq   $0x0,0x8(%rdi,%rsi,1)
jne    1159 <probe_filter_length+0x59>
add    $0xfffffffe,%ecx
add    $0xffffffffffffffe0,%rdx
cmpq   $0x0,-0x8(%rdx)
je     1120 <probe_filter_length+0x20>
mov    %ecx,%eax
add    $0x1,%eax
ret
xor    %eax,%eax
add    $0x1,%eax
ret

LLVM IR (-O0):
%struct.bpf_insn = type { i64, i64 }
define i32 @probe_filter_length(%struct.bpf_insn* %0) {
%2 = alloca %struct.bpf_insn*
%3 = alloca i32
store %struct.bpf_insn* %0, %struct.bpf_insn** %2
store i32 0, i32* %3
store i32 511, i32* %3
br label %4
4: ; preds = %25, %1
%5 = load i32, i32* %3
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %28
7: ; preds = %4
%8 = load %struct.bpf_insn*, %struct.bpf_insn** %2
%9 = load i32, i32* %3
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %8, i64 %10
%12 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %11, i32 0, i32 0
%13 = load i64, i64* %12
%14 = icmp ne i64 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %7
%16 = load %struct.bpf_insn*, %struct.bpf_insn** %2
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %16, i64 %18
%20 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %19, i32 0, i32 1
%21 = load i64, i64* %20
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %15, %7
br label %28
24: ; preds = %15
br label %25
25: ; preds = %24
%26 = load i32, i32* %3
%27 = add nsw i32 %26, -1
store i32 %27, i32* %3
br label %4
28: ; preds = %23, %4
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
ret i32 %30
}

LLVM IR (-O2):
%struct.bpf_insn = type { i64, i64 }
define i32 @probe_filter_length(%struct.bpf_insn* nocapture %0) {
br label %2
2: ; preds = %24, %1
%3 = phi i32 [ 511, %1 ], [ %25, %24 ]
%4 = zext i32 %3 to i64
%5 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %0, i64 %4, i32 0
%6 = load i64, i64* %5
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %26
8: ; preds = %2
%9 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %0, i64 %4, i32 1
%10 = load i64, i64* %9
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %8
%13 = add nsw i32 %3, -1
%14 = icmp ugt i32 %3, 1
br i1 %14, label %15, label %26
15: ; preds = %12
%16 = zext i32 %13 to i64
%17 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %0, i64 %16, i32 0
%18 = load i64, i64* %17
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %0, i64 %16, i32 1
%22 = load i64, i64* %21
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = add nsw i32 %3, -2
br label %2
26: ; preds = %20, %15, %2, %8, %12
%27 = phi i32 [ %3, %2 ], [ %3, %8 ], [ 0, %12 ], [ %13, %15 ], [ %13, %20 ]
%28 = add nuw nsw i32 %27, 1
ret i32 %28
}