; LLVM IR from Seeed-Studio/Seeed_Arduino_edgeimpulse :: src/QCBOR/src (UsefulBuf)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.q_useful_buf_c = type { ptr, i64 }

; Function Attrs: nounwind uwtable
declare dso_local i32 @UsefulBuf_Compare(ptr, i64, ptr, i64) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @UsefulBuf_FindBytes(ptr %0, i64 %1, ptr %2, i64 %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca %struct.q_useful_buf_c, align 8
  %7 = alloca %struct.q_useful_buf_c, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca %struct.q_useful_buf_c, align 8
  %11 = getelementptr inbounds { ptr, i64 }, ptr %6, i32 0, i32 0
  store ptr %0, ptr %11, align 8
  %12 = getelementptr inbounds { ptr, i64 }, ptr %6, i32 0, i32 1
  store i64 %1, ptr %12, align 8
  %13 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 0
  store ptr %2, ptr %13, align 8
  %14 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 1
  store i64 %3, ptr %14, align 8
  %15 = getelementptr inbounds %struct.q_useful_buf_c, ptr %6, i32 0, i32 1
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.q_useful_buf_c, ptr %7, i32 0, i32 1
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = icmp ult i64 %16, %18
  br i1 %19, label %20, label %21

20:                                               ; preds = %4
  store i64 -1, ptr %5, align 8
  br label %59

21:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !11
  br label %22

22:                                               ; preds = %53, %21
  %23 = load i64, ptr %8, align 8, !tbaa !11
  %24 = getelementptr inbounds %struct.q_useful_buf_c, ptr %6, i32 0, i32 1
  %25 = load i64, ptr %24, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.q_useful_buf_c, ptr %7, i32 0, i32 1
  %27 = load i64, ptr %26, align 8, !tbaa !5
  %28 = sub i64 %25, %27
  %29 = icmp ule i64 %23, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %22
  store i32 2, ptr %9, align 4
  br label %56

31:                                               ; preds = %22
  %32 = getelementptr inbounds %struct.q_useful_buf_c, ptr %10, i32 0, i32 0
  %33 = getelementptr inbounds %struct.q_useful_buf_c, ptr %6, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !12
  %35 = load i64, ptr %8, align 8, !tbaa !11
  %36 = getelementptr inbounds i8, ptr %34, i64 %35
  store ptr %36, ptr %32, align 8, !tbaa !12
  %37 = getelementptr inbounds %struct.q_useful_buf_c, ptr %10, i32 0, i32 1
  %38 = getelementptr inbounds %struct.q_useful_buf_c, ptr %7, i32 0, i32 1
  %39 = load i64, ptr %38, align 8, !tbaa !5
  store i64 %39, ptr %37, align 8, !tbaa !5
  %40 = getelementptr inbounds { ptr, i64 }, ptr %10, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8
  %42 = getelementptr inbounds { ptr, i64 }, ptr %10, i32 0, i32 1
  %43 = load i64, ptr %42, align 8
  %44 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8
  %46 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 1
  %47 = load i64, ptr %46, align 8
  %48 = call i32 @UsefulBuf_Compare(ptr %41, i64 %43, ptr %45, i64 %47)
  %49 = icmp ne i32 %48, 0
  br i1 %49, label %52, label %50

50:                                               ; preds = %31
  %51 = load i64, ptr %8, align 8, !tbaa !11
  store i64 %51, ptr %5, align 8
  store i32 1, ptr %9, align 4
  br label %56

52:                                               ; preds = %31
  br label %53

53:                                               ; preds = %52
  %54 = load i64, ptr %8, align 8, !tbaa !11
  %55 = add i64 %54, 1
  store i64 %55, ptr %8, align 8, !tbaa !11
  br label %22, !llvm.loop !13

56:                                               ; preds = %50, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %57 = load i32, ptr %9, align 4
  switch i32 %57, label %61 [
    i32 2, label %58
    i32 1, label %59
  ]

58:                                               ; preds = %56
  store i64 -1, ptr %5, align 8
  br label %59

59:                                               ; preds = %58, %56, %20
  %60 = load i64, ptr %5, align 8
  ret i64 %60

61:                                               ; preds = %56
  unreachable
}

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !10, i64 8}
!6 = !{!"q_useful_buf_c", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{!6, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
declare dso_local i32 @UsefulBuf_Compare(ptr, i64, ptr, i64) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @UsefulBuf_FindBytes(ptr %0, i64 %1, ptr %2, i64 %3) local_unnamed_addr #0 {
  %5 = icmp ult i64 %1, %3
  br i1 %5, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %4
  %6 = sub i64 %1, %3
  br label %7

7:                                                ; preds = %.preheader, %10
  %.01418 = phi i64 [ 0, %.preheader ], [ %11, %10 ]
  %8 = getelementptr inbounds i8, ptr %0, i64 %.01418
  %9 = tail call i32 @UsefulBuf_Compare(ptr %8, i64 %3, ptr %2, i64 %3)
  %.not17 = icmp eq i32 %9, 0
  br i1 %.not17, label %.loopexit, label %10

10:                                               ; preds = %7
  %11 = add i64 %.01418, 1
  %.not = icmp ugt i64 %11, %6
  br i1 %.not, label %.loopexit, label %7, !llvm.loop !5

.loopexit:                                        ; preds = %10, %7, %4
  %.1 = phi i64 [ -1, %4 ], [ %.01418, %7 ], [ -1, %10 ]
  ret i64 %.1
}

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass