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

; 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._QCBOREncodeContext = type { %struct.useful_out_buf, i8, %struct.__QCBORTrackNesting }
%struct.useful_out_buf = type { %struct.q_useful_buf, i64, i16, i8 }
%struct.q_useful_buf = type { ptr, i64 }
%struct.__QCBORTrackNesting = type { [16 x %struct.anon], ptr }
%struct.anon = type { i32, i16, i8 }

; 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
declare hidden void @InsertEncodedTypeAndNumber(ptr noundef, i8 noundef zeroext, i32 noundef, i64 noundef, i64 noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local void @QCBOREncode_CloseMapOrArray(ptr noundef %0, i8 noundef zeroext %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca %struct.q_useful_buf_c, align 8
  %12 = alloca %struct.q_useful_buf_c, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %13, i32 0, i32 1
  %15 = load i8, ptr %14, align 8, !tbaa !10
  %16 = zext i8 %15 to i32
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %90

18:                                               ; preds = %3
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %19, i32 0, i32 2
  %21 = call i32 @Nesting_IsInNest(ptr noundef %20)
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %26, label %23

23:                                               ; preds = %18
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %24, i32 0, i32 1
  store i8 4, ptr %25, align 8, !tbaa !10
  br label %89

26:                                               ; preds = %18
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %27, i32 0, i32 2
  %29 = call zeroext i8 @Nesting_GetMajorType(ptr noundef %28)
  %30 = zext i8 %29 to i32
  %31 = load i8, ptr %5, align 1, !tbaa !9
  %32 = zext i8 %31 to i32
  %33 = icmp ne i32 %30, %32
  br i1 %33, label %34, label %37

34:                                               ; preds = %26
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %35, i32 0, i32 1
  store i8 15, ptr %36, align 8, !tbaa !10
  br label %88

37:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %38, i32 0, i32 2
  %40 = call i32 @Nesting_GetStartPos(ptr noundef %39)
  %41 = zext i32 %40 to i64
  store i64 %41, ptr %7, align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %42, i32 0, i32 0
  %44 = call i64 @UsefulOutBuf_GetEndPosition(ptr noundef %43)
  store i64 %44, ptr %8, align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %45 = load i64, ptr %8, align 8, !tbaa !17
  %46 = load i64, ptr %7, align 8, !tbaa !17
  %47 = sub i64 %45, %46
  store i64 %47, ptr %9, align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %48 = load i8, ptr %5, align 1, !tbaa !9
  %49 = zext i8 %48 to i32
  %50 = icmp eq i32 %49, 2
  br i1 %50, label %51, label %53

51:                                               ; preds = %37
  %52 = load i64, ptr %9, align 8, !tbaa !17
  br label %58

53:                                               ; preds = %37
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %54, i32 0, i32 2
  %56 = call zeroext i16 @Nesting_GetCount(ptr noundef %55)
  %57 = zext i16 %56 to i64
  br label %58

58:                                               ; preds = %53, %51
  %59 = phi i64 [ %52, %51 ], [ %57, %53 ]
  store i64 %59, ptr %10, align 8, !tbaa !17
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = load i8, ptr %5, align 1, !tbaa !9
  %62 = load i64, ptr %10, align 8, !tbaa !17
  %63 = load i64, ptr %7, align 8, !tbaa !17
  call void @InsertEncodedTypeAndNumber(ptr noundef %60, i8 noundef zeroext %61, i32 noundef 0, i64 noundef %62, i64 noundef %63)
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = icmp ne ptr %64, null
  br i1 %65, label %66, label %85

66:                                               ; preds = %58
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #5
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %67, i32 0, i32 0
  %69 = call { ptr, i64 } @UsefulOutBuf_OutUBuf(ptr noundef %68)
  %70 = getelementptr inbounds { ptr, i64 }, ptr %11, i32 0, i32 0
  %71 = extractvalue { ptr, i64 } %69, 0
  store ptr %71, ptr %70, align 8
  %72 = getelementptr inbounds { ptr, i64 }, ptr %11, i32 0, i32 1
  %73 = extractvalue { ptr, i64 } %69, 1
  store i64 %73, ptr %72, align 8
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %12) #5
  %75 = load i64, ptr %7, align 8, !tbaa !17
  %76 = getelementptr inbounds { ptr, i64 }, ptr %11, i32 0, i32 0
  %77 = load ptr, ptr %76, align 8
  %78 = getelementptr inbounds { ptr, i64 }, ptr %11, i32 0, i32 1
  %79 = load i64, ptr %78, align 8
  %80 = call { ptr, i64 } @UsefulBuf_Tail(ptr %77, i64 %79, i64 noundef %75)
  %81 = getelementptr inbounds { ptr, i64 }, ptr %12, i32 0, i32 0
  %82 = extractvalue { ptr, i64 } %80, 0
  store ptr %82, ptr %81, align 8
  %83 = getelementptr inbounds { ptr, i64 }, ptr %12, i32 0, i32 1
  %84 = extractvalue { ptr, i64 } %80, 1
  store i64 %84, ptr %83, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %74, ptr align 8 %12, i64 16, i1 false), !tbaa.struct !18
  call void @llvm.lifetime.end.p0(i64 16, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #5
  br label %85

85:                                               ; preds = %66, %58
  %86 = load ptr, ptr %4, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %86, i32 0, i32 2
  call void @Nesting_Decrease(ptr noundef %87)
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %88

88:                                               ; preds = %85, %34
  br label %89

89:                                               ; preds = %88, %23
  br label %90

90:                                               ; preds = %89, %3
  ret void
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i8 @Nesting_GetMajorType(ptr noundef) #2

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i16 @Nesting_GetCount(ptr noundef) #2

declare { ptr, i64 } @UsefulOutBuf_OutUBuf(ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden { ptr, i64 } @UsefulBuf_Tail(ptr, i64, i64 noundef) #2

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @Nesting_Decrease(ptr noundef) #2

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 = { "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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !7, i64 32}
!11 = !{!"_QCBOREncodeContext", !12, i64 0, !7, i64 32, !16, i64 40}
!12 = !{!"useful_out_buf", !13, i64 0, !14, i64 16, !15, i64 24, !7, i64 26}
!13 = !{!"q_useful_buf", !6, i64 0, !14, i64 8}
!14 = !{!"long", !7, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = !{!"__QCBORTrackNesting", !7, i64 0, !6, i64 128}
!17 = !{!14, !14, i64 0}
!18 = !{i64 0, i64 8, !5, i64 8, i64 8, !17}


; 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 hidden void @InsertEncodedTypeAndNumber(ptr noundef, i8 noundef zeroext, i32 noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @QCBOREncode_CloseMapOrArray(ptr noundef %0, i8 noundef zeroext %1, ptr noundef writeonly %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 32
  %5 = load i8, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i8 %5, 0
  br i1 %6, label %7, label %34

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 40
  %9 = tail call i32 @Nesting_IsInNest(ptr noundef nonnull %8)
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %10, label %11

10:                                               ; preds = %7
  store i8 4, ptr %4, align 8, !tbaa !5
  br label %34

11:                                               ; preds = %7
  %12 = tail call zeroext i8 @Nesting_GetMajorType(ptr noundef nonnull %8)
  %.not24 = icmp eq i8 %12, %1
  br i1 %.not24, label %14, label %13

13:                                               ; preds = %11
  store i8 15, ptr %4, align 8, !tbaa !5
  br label %34

14:                                               ; preds = %11
  %15 = tail call i32 @Nesting_GetStartPos(ptr noundef nonnull %8)
  %16 = zext i32 %15 to i64
  %17 = tail call i64 @UsefulOutBuf_GetEndPosition(ptr noundef nonnull %0)
  %18 = icmp eq i8 %1, 2
  br i1 %18, label %19, label %21

19:                                               ; preds = %14
  %20 = sub i64 %17, %16
  br label %24

21:                                               ; preds = %14
  %22 = tail call zeroext i16 @Nesting_GetCount(ptr noundef nonnull %8)
  %23 = zext i16 %22 to i64
  br label %24

24:                                               ; preds = %21, %19
  %25 = phi i64 [ %20, %19 ], [ %23, %21 ]
  tail call void @InsertEncodedTypeAndNumber(ptr noundef nonnull %0, i8 noundef zeroext %1, i32 noundef 0, i64 noundef %25, i64 noundef %16)
  %.not25 = icmp eq ptr %2, null
  br i1 %.not25, label %33, label %26

26:                                               ; preds = %24
  %27 = tail call { ptr, i64 } @UsefulOutBuf_OutUBuf(ptr noundef nonnull %0) #3
  %28 = extractvalue { ptr, i64 } %27, 0
  %29 = extractvalue { ptr, i64 } %27, 1
  %30 = tail call { ptr, i64 } @UsefulBuf_Tail(ptr %28, i64 %29, i64 noundef %16)
  %31 = extractvalue { ptr, i64 } %30, 0
  %32 = extractvalue { ptr, i64 } %30, 1
  store ptr %31, ptr %2, align 8, !tbaa !15
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %2, i64 8
  store i64 %32, ptr %.sroa.4.0..sroa_idx, align 8, !tbaa !16
  br label %33

33:                                               ; preds = %26, %24
  tail call void @Nesting_Decrease(ptr noundef nonnull %8)
  br label %34

34:                                               ; preds = %10, %33, %13, %3
  ret void
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i8 @Nesting_GetMajorType(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i16 @Nesting_GetCount(ptr noundef) local_unnamed_addr #1

declare { ptr, i64 } @UsefulOutBuf_OutUBuf(ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden { ptr, i64 } @UsefulBuf_Tail(ptr, i64, i64 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @Nesting_Decrease(ptr noundef) 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" }
attributes #2 = { "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 = { 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 32}
!6 = !{!"_QCBOREncodeContext", !7, i64 0, !10, i64 32, !14, i64 40}
!7 = !{!"useful_out_buf", !8, i64 0, !12, i64 16, !13, i64 24, !10, i64 26}
!8 = !{!"q_useful_buf", !9, i64 0, !12, i64 8}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = !{!"long", !10, i64 0}
!13 = !{!"short", !10, i64 0}
!14 = !{!"__QCBORTrackNesting", !10, i64 0, !9, i64 128}
!15 = !{!9, !9, i64 0}
!16 = !{!12, !12, i64 0}


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