; 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._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: inlinehint nounwind uwtable
declare hidden i32 @Nesting_Increment(ptr noundef) #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: inlinehint nounwind uwtable
declare hidden i64 @UsefulOutBuf_GetEndPosition(ptr noundef) #0

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

17:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %18, i32 0, i32 0
  %20 = call i64 @UsefulOutBuf_GetEndPosition(ptr noundef %19)
  store i64 %20, ptr %5, align 8, !tbaa !17
  %21 = load i64, ptr %5, align 8, !tbaa !17
  %22 = icmp uge i64 %21, 4294967195
  br i1 %22, label %23, label %26

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

26:                                               ; preds = %17
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %27, i32 0, i32 2
  %29 = load i8, ptr %4, align 1, !tbaa !9
  %30 = load i64, ptr %5, align 8, !tbaa !17
  %31 = trunc i64 %30 to i32
  %32 = call i32 @Nesting_Increase(ptr noundef %28, i8 noundef zeroext %29, i32 noundef %31)
  %33 = trunc i32 %32 to i8
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %34, i32 0, i32 1
  store i8 %33, ptr %35, align 8, !tbaa !10
  br label %36

36:                                               ; preds = %26, %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %37

37:                                               ; preds = %36, %2
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @Nesting_Increase(ptr noundef, i8 noundef zeroext, i32 noundef) #0

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = { 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}


; 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: inlinehint nounwind uwtable
declare hidden i32 @Nesting_Increment(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @QCBOREncode_OpenMapOrArray(ptr noundef %0, i8 noundef zeroext %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %0, i64 40
  %4 = tail call i32 @Nesting_Increment(ptr noundef nonnull %3)
  %5 = trunc i32 %4 to i8
  %6 = getelementptr inbounds i8, ptr %0, i64 32
  store i8 %5, ptr %6, align 8, !tbaa !5
  %7 = and i32 %4, 255
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %17

9:                                                ; preds = %2
  %10 = tail call i64 @UsefulOutBuf_GetEndPosition(ptr noundef nonnull %0)
  %11 = icmp ugt i64 %10, 4294967194
  br i1 %11, label %16, label %12

12:                                               ; preds = %9
  %13 = trunc nuw i64 %10 to i32
  %14 = tail call i32 @Nesting_Increase(ptr noundef nonnull %3, i8 noundef zeroext %1, i32 noundef %13)
  %15 = trunc i32 %14 to i8
  br label %16

16:                                               ; preds = %9, %12
  %storemerge = phi i8 [ %15, %12 ], [ 7, %9 ]
  store i8 %storemerge, ptr %6, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %16, %2
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @Nesting_Increase(ptr noundef, i8 noundef zeroext, i32 noundef) local_unnamed_addr #0

attributes #0 = { 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 #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" }

!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}


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