; 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: inlinehint nounwind uwtable
declare hidden void @AppendEncodedTypeAndNumber(ptr noundef, i8 noundef zeroext, i64 noundef) #0

; 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: nounwind uwtable
define dso_local void @QCBOREncode_AddBuffer(ptr noundef %0, i8 noundef zeroext %1, ptr %2, i64 %3) #2 {
  %5 = alloca %struct.q_useful_buf_c, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 0
  store ptr %2, ptr %9, align 8
  %10 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 1
  store i64 %3, ptr %10, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i8 %1, ptr %7, align 1, !tbaa !9
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %11, i32 0, i32 1
  %13 = load i8, ptr %12, align 8, !tbaa !10
  %14 = zext i8 %13 to i32
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %49

16:                                               ; preds = %4
  %17 = load i8, ptr %7, align 1, !tbaa !9
  %18 = zext i8 %17 to i32
  %19 = icmp ne i32 %18, 9
  br i1 %19, label %20, label %31

20:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #3
  %21 = load i8, ptr %7, align 1, !tbaa !9
  store i8 %21, ptr %8, align 1, !tbaa !9
  %22 = load i8, ptr %8, align 1, !tbaa !9
  %23 = zext i8 %22 to i32
  %24 = icmp eq i32 %23, 11
  br i1 %24, label %25, label %26

25:                                               ; preds = %20
  store i8 2, ptr %8, align 1, !tbaa !9
  br label %26

26:                                               ; preds = %25, %20
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = load i8, ptr %8, align 1, !tbaa !9
  %29 = getelementptr inbounds %struct.q_useful_buf_c, ptr %5, i32 0, i32 1
  %30 = load i64, ptr %29, align 8, !tbaa !17
  call void @AppendEncodedTypeAndNumber(ptr noundef %27, i8 noundef zeroext %28, i64 noundef %30)
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #3
  br label %31

31:                                               ; preds = %26, %16
  %32 = load i8, ptr %7, align 1, !tbaa !9
  %33 = zext i8 %32 to i32
  %34 = icmp ne i32 %33, 11
  br i1 %34, label %35, label %42

35:                                               ; preds = %31
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %36, i32 0, i32 0
  %38 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8
  %40 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 1
  %41 = load i64, ptr %40, align 8
  call void @UsefulOutBuf_AppendUsefulBuf(ptr noundef %37, ptr %39, i64 %41)
  br label %42

42:                                               ; preds = %35, %31
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %43, i32 0, i32 2
  %45 = call i32 @Nesting_Increment(ptr noundef %44)
  %46 = trunc i32 %45 to i8
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct._QCBOREncodeContext, ptr %47, i32 0, i32 1
  store i8 %46, ptr %48, align 8, !tbaa !10
  br label %49

49:                                               ; preds = %42, %4
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @UsefulOutBuf_AppendUsefulBuf(ptr noundef, ptr, i64) #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 = !{!18, !14, i64 8}
!18 = !{!"q_useful_buf_c", !6, i64 0, !14, i64 8}


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

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

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

8:                                                ; preds = %4
  %cond = icmp eq i8 %1, 9
  br i1 %cond, label %11, label %9

9:                                                ; preds = %8
  %10 = icmp eq i8 %1, 11
  %spec.store.select = select i1 %10, i8 2, i8 %1
  tail call void @AppendEncodedTypeAndNumber(ptr noundef nonnull %0, i8 noundef zeroext %spec.store.select, i64 noundef %3)
  br i1 %10, label %12, label %11

11:                                               ; preds = %8, %9
  tail call void @UsefulOutBuf_AppendUsefulBuf(ptr noundef nonnull %0, ptr %2, i64 %3)
  br label %12

12:                                               ; preds = %11, %9
  %13 = getelementptr inbounds i8, ptr %0, i64 40
  %14 = tail call i32 @Nesting_Increment(ptr noundef nonnull %13)
  %15 = trunc i32 %14 to i8
  store i8 %15, ptr %5, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %12, %4
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @UsefulOutBuf_AppendUsefulBuf(ptr noundef, ptr, i64) 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,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass