; 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 }
%struct.useful_out_buf = type { %struct.q_useful_buf, i64, i16, i8 }
%struct.q_useful_buf = type { ptr, i64 }

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

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

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

18:                                               ; preds = %4
  br label %111

19:                                               ; preds = %4
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.useful_out_buf, ptr %20, i32 0, i32 2
  %22 = load i16, ptr %21, align 8, !tbaa !15
  %23 = zext i16 %22 to i32
  %24 = icmp ne i32 %23, 2831
  br i1 %24, label %25, label %28

25:                                               ; preds = %19
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.useful_out_buf, ptr %26, i32 0, i32 3
  store i8 1, ptr %27, align 2, !tbaa !11
  br label %111

28:                                               ; preds = %19
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.useful_out_buf, ptr %29, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !16
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.useful_out_buf, ptr %32, i32 0, i32 0
  %34 = getelementptr inbounds %struct.q_useful_buf, ptr %33, i32 0, i32 1
  %35 = load i64, ptr %34, align 8, !tbaa !17
  %36 = icmp ugt i64 %31, %35
  br i1 %36, label %37, label %40

37:                                               ; preds = %28
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.useful_out_buf, ptr %38, i32 0, i32 3
  store i8 1, ptr %39, align 2, !tbaa !11
  br label %111

40:                                               ; preds = %28
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.q_useful_buf_c, ptr %5, i32 0, i32 1
  %43 = load i64, ptr %42, align 8, !tbaa !18
  %44 = call i32 @UsefulOutBuf_WillItFit(ptr noundef %41, i64 noundef %43)
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %49, label %46

46:                                               ; preds = %40
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.useful_out_buf, ptr %47, i32 0, i32 3
  store i8 1, ptr %48, align 2, !tbaa !11
  br label %111

49:                                               ; preds = %40
  %50 = load i64, ptr %7, align 8, !tbaa !9
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.useful_out_buf, ptr %51, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !16
  %54 = icmp ugt i64 %50, %53
  br i1 %54, label %55, label %58

55:                                               ; preds = %49
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.useful_out_buf, ptr %56, i32 0, i32 3
  store i8 1, ptr %57, align 2, !tbaa !11
  br label %111

58:                                               ; preds = %49
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.useful_out_buf, ptr %59, i32 0, i32 0
  %61 = getelementptr inbounds %struct.q_useful_buf, ptr %60, i32 0, i32 0
  %62 = load ptr, ptr %61, align 8, !tbaa !20
  %63 = load i64, ptr %7, align 8, !tbaa !9
  %64 = getelementptr inbounds i8, ptr %62, i64 %63
  store ptr %64, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.useful_out_buf, ptr %65, i32 0, i32 1
  %67 = load i64, ptr %66, align 8, !tbaa !16
  %68 = load i64, ptr %7, align 8, !tbaa !9
  %69 = sub i64 %67, %68
  store i64 %69, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.q_useful_buf_c, ptr %5, i32 0, i32 1
  %72 = load i64, ptr %71, align 8, !tbaa !18
  %73 = getelementptr inbounds i8, ptr %70, i64 %72
  store ptr %73, ptr %10, align 8, !tbaa !5
  %74 = load i64, ptr %9, align 8, !tbaa !9
  %75 = icmp ne i64 %74, 0
  br i1 %75, label %76, label %86

76:                                               ; preds = %58
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.useful_out_buf, ptr %77, i32 0, i32 0
  %79 = getelementptr inbounds %struct.q_useful_buf, ptr %78, i32 0, i32 0
  %80 = load ptr, ptr %79, align 8, !tbaa !20
  %81 = icmp ne ptr %80, null
  br i1 %81, label %82, label %86

82:                                               ; preds = %76
  %83 = load ptr, ptr %10, align 8, !tbaa !5
  %84 = load ptr, ptr %8, align 8, !tbaa !5
  %85 = load i64, ptr %9, align 8, !tbaa !9
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %83, ptr align 1 %84, i64 %85, i1 false)
  br label %86

86:                                               ; preds = %82, %76, %58
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %87 = load ptr, ptr %6, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.useful_out_buf, ptr %87, i32 0, i32 0
  %89 = getelementptr inbounds %struct.q_useful_buf, ptr %88, i32 0, i32 0
  %90 = load ptr, ptr %89, align 8, !tbaa !20
  %91 = load i64, ptr %7, align 8, !tbaa !9
  %92 = getelementptr inbounds i8, ptr %90, i64 %91
  store ptr %92, ptr %11, align 8, !tbaa !5
  %93 = load ptr, ptr %6, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.useful_out_buf, ptr %93, i32 0, i32 0
  %95 = getelementptr inbounds %struct.q_useful_buf, ptr %94, i32 0, i32 0
  %96 = load ptr, ptr %95, align 8, !tbaa !20
  %97 = icmp ne ptr %96, null
  br i1 %97, label %98, label %104

98:                                               ; preds = %86
  %99 = load ptr, ptr %11, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.q_useful_buf_c, ptr %5, i32 0, i32 0
  %101 = load ptr, ptr %100, align 8, !tbaa !21
  %102 = getelementptr inbounds %struct.q_useful_buf_c, ptr %5, i32 0, i32 1
  %103 = load i64, ptr %102, align 8, !tbaa !18
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %99, ptr align 1 %101, i64 %103, i1 false)
  br label %104

104:                                              ; preds = %98, %86
  %105 = getelementptr inbounds %struct.q_useful_buf_c, ptr %5, i32 0, i32 1
  %106 = load i64, ptr %105, align 8, !tbaa !18
  %107 = load ptr, ptr %6, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.useful_out_buf, ptr %107, i32 0, i32 1
  %109 = load i64, ptr %108, align 8, !tbaa !16
  %110 = add i64 %109, %106
  store i64 %110, ptr %108, align 8, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %111

111:                                              ; preds = %104, %55, %46, %37, %25, %18
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @UsefulOutBuf_WillItFit(ptr noundef, i64 noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !7, i64 26}
!12 = !{!"useful_out_buf", !13, i64 0, !10, i64 16, !14, i64 24, !7, i64 26}
!13 = !{!"q_useful_buf", !6, i64 0, !10, i64 8}
!14 = !{!"short", !7, i64 0}
!15 = !{!12, !14, i64 24}
!16 = !{!12, !10, i64 16}
!17 = !{!12, !10, i64 8}
!18 = !{!19, !10, i64 8}
!19 = !{!"q_useful_buf_c", !6, i64 0, !10, i64 8}
!20 = !{!12, !6, i64 0}
!21 = !{!19, !6, i64 0}


; 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 void @UsefulOutBuf_InsertUsefulBuf(ptr noundef %0, ptr nocapture readonly %1, i64 %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 26
  %6 = load i8, ptr %5, align 2, !tbaa !5
  %.not = icmp eq i8 %6, 0
  br i1 %.not, label %7, label %38

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 24
  %9 = load i16, ptr %8, align 8, !tbaa !13
  %.not30 = icmp eq i16 %9, 2831
  br i1 %.not30, label %11, label %10

10:                                               ; preds = %7
  store i8 1, ptr %5, align 2, !tbaa !5
  br label %38

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load i64, ptr %12, align 8, !tbaa !14
  %14 = getelementptr inbounds i8, ptr %0, i64 8
  %15 = load i64, ptr %14, align 8, !tbaa !15
  %16 = icmp ugt i64 %13, %15
  br i1 %16, label %17, label %18

17:                                               ; preds = %11
  store i8 1, ptr %5, align 2, !tbaa !5
  br label %38

18:                                               ; preds = %11
  %19 = tail call i32 @UsefulOutBuf_WillItFit(ptr noundef nonnull %0, i64 noundef %2)
  %.not31 = icmp eq i32 %19, 0
  br i1 %.not31, label %20, label %21

20:                                               ; preds = %18
  store i8 1, ptr %5, align 2, !tbaa !5
  br label %38

21:                                               ; preds = %18
  %22 = load i64, ptr %12, align 8, !tbaa !14
  %23 = icmp ult i64 %22, %3
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  store i8 1, ptr %5, align 2, !tbaa !5
  br label %38

25:                                               ; preds = %21
  %26 = load ptr, ptr %0, align 8, !tbaa !16
  %.not32 = icmp eq i64 %22, %3
  %.not33 = icmp eq ptr %26, null
  %or.cond = select i1 %.not32, i1 true, i1 %.not33
  br i1 %or.cond, label %31, label %27

27:                                               ; preds = %25
  %28 = getelementptr inbounds i8, ptr %26, i64 %3
  %29 = getelementptr inbounds i8, ptr %28, i64 %2
  %30 = sub i64 %22, %3
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 1 %29, ptr nonnull align 1 %28, i64 %30, i1 false)
  %.pre = load ptr, ptr %0, align 8, !tbaa !16
  br label %31

31:                                               ; preds = %27, %25
  %32 = phi ptr [ %.pre, %27 ], [ %26, %25 ]
  %.not34 = icmp eq ptr %32, null
  br i1 %.not34, label %35, label %33

33:                                               ; preds = %31
  %34 = getelementptr inbounds i8, ptr %32, i64 %3
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 1 %34, ptr align 1 %1, i64 %2, i1 false)
  br label %35

35:                                               ; preds = %33, %31
  %36 = load i64, ptr %12, align 8, !tbaa !14
  %37 = add i64 %36, %2
  store i64 %37, ptr %12, align 8, !tbaa !14
  br label %38

38:                                               ; preds = %4, %35, %24, %20, %17, %10
  ret void
}

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !9, i64 26}
!6 = !{!"useful_out_buf", !7, i64 0, !11, i64 16, !12, i64 24, !9, i64 26}
!7 = !{!"q_useful_buf", !8, i64 0, !11, i64 8}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"short", !9, i64 0}
!13 = !{!6, !12, i64 24}
!14 = !{!6, !11, i64 16}
!15 = !{!6, !11, i64 8}
!16 = !{!6, !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass