; 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
define dso_local i64 @UsefulBuf_IsValue(ptr %0, i64 %1, i8 noundef zeroext %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca %struct.q_useful_buf_c, align 8
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 0
  store ptr %0, ptr %10, align 8
  %11 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 1
  store i64 %1, ptr %11, align 8
  store i8 %2, ptr %6, align 1, !tbaa !5
  %12 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 0
  %13 = load ptr, ptr %12, align 8
  %14 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 1
  %15 = load i64, ptr %14, align 8
  %16 = call i32 @UsefulBuf_IsNULLOrEmptyC(ptr %13, i64 %15)
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %3
  store i64 0, ptr %4, align 8
  br label %54

19:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %20 = getelementptr inbounds %struct.q_useful_buf_c, ptr %5, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !8
  %22 = getelementptr inbounds %struct.q_useful_buf_c, ptr %5, i32 0, i32 1
  %23 = load i64, ptr %22, align 8, !tbaa !12
  %24 = getelementptr inbounds i8, ptr %21, i64 %23
  store ptr %24, ptr %7, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %25 = getelementptr inbounds %struct.q_useful_buf_c, ptr %5, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !8
  store ptr %26, ptr %8, align 8, !tbaa !13
  br label %27

27:                                               ; preds = %47, %19
  %28 = load ptr, ptr %8, align 8, !tbaa !13
  %29 = load ptr, ptr %7, align 8, !tbaa !13
  %30 = icmp ult ptr %28, %29
  br i1 %30, label %32, label %31

31:                                               ; preds = %27
  store i32 2, ptr %9, align 4
  br label %50

32:                                               ; preds = %27
  %33 = load ptr, ptr %8, align 8, !tbaa !13
  %34 = load i8, ptr %33, align 1, !tbaa !5
  %35 = zext i8 %34 to i32
  %36 = load i8, ptr %6, align 1, !tbaa !5
  %37 = zext i8 %36 to i32
  %38 = icmp ne i32 %35, %37
  br i1 %38, label %39, label %46

39:                                               ; preds = %32
  %40 = load ptr, ptr %8, align 8, !tbaa !13
  %41 = getelementptr inbounds %struct.q_useful_buf_c, ptr %5, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !8
  %43 = ptrtoint ptr %40 to i64
  %44 = ptrtoint ptr %42 to i64
  %45 = sub i64 %43, %44
  store i64 %45, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %50

46:                                               ; preds = %32
  br label %47

47:                                               ; preds = %46
  %48 = load ptr, ptr %8, align 8, !tbaa !13
  %49 = getelementptr inbounds i8, ptr %48, i32 1
  store ptr %49, ptr %8, align 8, !tbaa !13
  br label %27, !llvm.loop !14

50:                                               ; preds = %39, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %51 = load i32, ptr %9, align 4
  switch i32 %51, label %53 [
    i32 2, label %52
  ]

52:                                               ; preds = %50
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %53

53:                                               ; preds = %52, %50
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %54

54:                                               ; preds = %53, %18
  %55 = load i64, ptr %4, align 8
  ret i64 %55
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @UsefulBuf_IsNULLOrEmptyC(ptr, i64) #1

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

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

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 = { inlinehint 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !10, i64 0}
!9 = !{!"q_useful_buf_c", !10, i64 0, !11, i64 8}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"long", !6, i64 0}
!12 = !{!9, !11, i64 8}
!13 = !{!10, !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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
define dso_local i64 @UsefulBuf_IsValue(ptr %0, i64 %1, i8 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = tail call i32 @UsefulBuf_IsNULLOrEmptyC(ptr %0, i64 %1)
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %5, label %.loopexit

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 %1
  %.not1416 = icmp sgt i64 %1, 0
  br i1 %.not1416, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %5, %12
  %.01117 = phi ptr [ %13, %12 ], [ %0, %5 ]
  %7 = load i8, ptr %.01117, align 1, !tbaa !5
  %.not13 = icmp eq i8 %7, %2
  br i1 %.not13, label %12, label %8

8:                                                ; preds = %.lr.ph
  %9 = ptrtoint ptr %.01117 to i64
  %10 = ptrtoint ptr %0 to i64
  %11 = sub i64 %9, %10
  br label %.loopexit

12:                                               ; preds = %.lr.ph
  %13 = getelementptr inbounds i8, ptr %.01117, i64 1
  %.not14 = icmp ult ptr %13, %6
  br i1 %.not14, label %.lr.ph, label %.loopexit, !llvm.loop !8

.loopexit:                                        ; preds = %12, %5, %8, %3
  %.2 = phi i64 [ 0, %3 ], [ %11, %8 ], [ -1, %5 ], [ -1, %12 ]
  ret i64 %.2
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @UsefulBuf_IsNULLOrEmptyC(ptr, i64) local_unnamed_addr #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" }
attributes #1 = { inlinehint 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 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass