; 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: 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

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

; Function Attrs: nounwind uwtable
define dso_local void @QCBOREncode_CloseMapOrArrayIndefiniteLength(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 %struct.q_useful_buf_c, align 8
  %8 = 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
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %9, i32 0, i32 1
  %11 = load i8, ptr %10, align 8, !tbaa !10
  %12 = zext i8 %11 to i32
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %66

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

19:                                               ; preds = %14
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %20, i32 0, i32 1
  store i8 4, ptr %21, align 8, !tbaa !10
  br label %65

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

30:                                               ; preds = %22
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %31, i32 0, i32 1
  store i8 15, ptr %32, align 8, !tbaa !10
  br label %64

33:                                               ; preds = %22
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %35, i32 0, i32 0
  %37 = call i64 @UsefulOutBuf_GetEndPosition(ptr noundef %36)
  call void @InsertEncodedTypeAndNumber(ptr noundef %34, i8 noundef zeroext 7, i32 noundef 0, i64 noundef 31, i64 noundef %37)
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = icmp ne ptr %38, null
  br i1 %39, label %40, label %61

40:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #5
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %41, i32 0, i32 0
  %43 = call { ptr, i64 } @UsefulOutBuf_OutUBuf(ptr noundef %42)
  %44 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 0
  %45 = extractvalue { ptr, i64 } %43, 0
  store ptr %45, ptr %44, align 8
  %46 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 1
  %47 = extractvalue { ptr, i64 } %43, 1
  store i64 %47, ptr %46, align 8
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #5
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %49, i32 0, i32 0
  %51 = call i64 @UsefulOutBuf_GetEndPosition(ptr noundef %50)
  %52 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8
  %54 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 1
  %55 = load i64, ptr %54, align 8
  %56 = call { ptr, i64 } @UsefulBuf_Tail(ptr %53, i64 %55, i64 noundef %51)
  %57 = getelementptr inbounds { ptr, i64 }, ptr %8, i32 0, i32 0
  %58 = extractvalue { ptr, i64 } %56, 0
  store ptr %58, ptr %57, align 8
  %59 = getelementptr inbounds { ptr, i64 }, ptr %8, i32 0, i32 1
  %60 = extractvalue { ptr, i64 } %56, 1
  store i64 %60, ptr %59, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %48, ptr align 8 %8, i64 16, i1 false), !tbaa.struct !17
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #5
  br label %61

61:                                               ; preds = %40, %33
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %62, i32 0, i32 2
  call void @Nesting_Decrease(ptr noundef %63)
  br label %64

64:                                               ; preds = %61, %30
  br label %65

65:                                               ; preds = %64, %19
  br label %66

66:                                               ; preds = %65, %3
  ret void
}

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 = !{i64 0, i64 8, !5, i64 8, i64 8, !18}
!18 = !{!14, !14, 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
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: 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

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

; Function Attrs: nounwind uwtable
define dso_local void @QCBOREncode_CloseMapOrArrayIndefiniteLength(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 %25

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 %25

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

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

14:                                               ; preds = %11
  %15 = tail call i64 @UsefulOutBuf_GetEndPosition(ptr noundef nonnull %0)
  tail call void @InsertEncodedTypeAndNumber(ptr noundef nonnull %0, i8 noundef zeroext 7, i32 noundef 0, i64 noundef 31, i64 noundef %15)
  %.not15 = icmp eq ptr %2, null
  br i1 %.not15, label %24, label %16

16:                                               ; preds = %14
  %17 = tail call { ptr, i64 } @UsefulOutBuf_OutUBuf(ptr noundef nonnull %0) #3
  %18 = extractvalue { ptr, i64 } %17, 0
  %19 = extractvalue { ptr, i64 } %17, 1
  %20 = tail call i64 @UsefulOutBuf_GetEndPosition(ptr noundef nonnull %0)
  %21 = tail call { ptr, i64 } @UsefulBuf_Tail(ptr %18, i64 %19, i64 noundef %20)
  %22 = extractvalue { ptr, i64 } %21, 0
  %23 = extractvalue { ptr, i64 } %21, 1
  store ptr %22, ptr %2, align 8, !tbaa !15
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %2, i64 8
  store i64 %23, ptr %.sroa.4.0..sroa_idx, align 8, !tbaa !16
  br label %24

24:                                               ; preds = %16, %14
  tail call void @Nesting_Decrease(ptr noundef nonnull %8)
  br label %25

25:                                               ; preds = %10, %24, %13, %3
  ret void
}

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