; LLVM IR from CactiLab/Sherloc-Cortex-M-CFVD :: Sherloc_runtime/evaluation/src/sglib_rbtree (rbtree)

; 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.rbtree = type { i32, i8, ptr, ptr }

; 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 i32 @sglib_rbtree_len(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca [128 x ptr], align 16
  %6 = alloca [128 x ptr], align 16
  %7 = alloca [128 x i8], align 16
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1024, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 1024, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 128, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %11, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %77, %1
  %13 = load ptr, ptr %8, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %87

15:                                               ; preds = %12
  br label %16

16:                                               ; preds = %41, %15
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %42

19:                                               ; preds = %16
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  %21 = load i32, ptr %9, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [128 x ptr], ptr %5, i64 0, i64 %22
  store ptr %20, ptr %23, align 8, !tbaa !5
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.rbtree, ptr %24, i32 0, i32 3
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  %27 = load i32, ptr %9, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [128 x ptr], ptr %6, i64 0, i64 %28
  store ptr %26, ptr %29, align 8, !tbaa !5
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [128 x i8], ptr %7, i64 0, i64 %31
  store i8 0, ptr %32, align 1, !tbaa !13
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.rbtree, ptr %33, i32 0, i32 2
  %35 = load ptr, ptr %34, align 8, !tbaa !14
  store ptr %35, ptr %8, align 8, !tbaa !5
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %9, align 4, !tbaa !9
  %38 = load i32, ptr %9, align 4, !tbaa !9
  %39 = icmp sge i32 %38, 128
  br i1 %39, label %40, label %41

40:                                               ; preds = %19
  br label %41

41:                                               ; preds = %40, %19
  br label %16, !llvm.loop !15

42:                                               ; preds = %16
  br label %43

43:                                               ; preds = %75, %42
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = add nsw i32 %44, -1
  store i32 %45, ptr %9, align 4, !tbaa !9
  %46 = load i32, ptr %9, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [128 x i8], ptr %7, i64 0, i64 %47
  %49 = load i8, ptr %48, align 1, !tbaa !13
  %50 = sext i8 %49 to i32
  %51 = icmp eq i32 %50, 0
  br i1 %51, label %52, label %60

52:                                               ; preds = %43
  %53 = load i32, ptr %9, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [128 x ptr], ptr %5, i64 0, i64 %54
  %56 = load ptr, ptr %55, align 8, !tbaa !5
  store ptr %56, ptr %10, align 8, !tbaa !5
  %57 = load i32, ptr %3, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %3, align 4, !tbaa !9
  %59 = load ptr, ptr %10, align 8, !tbaa !5
  br label %60

60:                                               ; preds = %52, %43
  %61 = load i32, ptr %9, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [128 x i8], ptr %7, i64 0, i64 %62
  %64 = load i8, ptr %63, align 1, !tbaa !13
  %65 = add i8 %64, 1
  store i8 %65, ptr %63, align 1, !tbaa !13
  br label %66

66:                                               ; preds = %60
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = icmp sgt i32 %67, 0
  br i1 %68, label %69, label %75

69:                                               ; preds = %66
  %70 = load i32, ptr %9, align 4, !tbaa !9
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [128 x ptr], ptr %6, i64 0, i64 %71
  %73 = load ptr, ptr %72, align 8, !tbaa !5
  %74 = icmp eq ptr %73, null
  br label %75

75:                                               ; preds = %69, %66
  %76 = phi i1 [ false, %66 ], [ %74, %69 ]
  br i1 %76, label %43, label %77, !llvm.loop !17

77:                                               ; preds = %75
  %78 = load i32, ptr %9, align 4, !tbaa !9
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [128 x ptr], ptr %6, i64 0, i64 %79
  %81 = load ptr, ptr %80, align 8, !tbaa !5
  store ptr %81, ptr %8, align 8, !tbaa !5
  %82 = load i32, ptr %9, align 4, !tbaa !9
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds [128 x ptr], ptr %6, i64 0, i64 %83
  store ptr null, ptr %84, align 8, !tbaa !5
  %85 = load i32, ptr %9, align 4, !tbaa !9
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %9, align 4, !tbaa !9
  br label %12, !llvm.loop !18

87:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 128, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 1024, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 1024, ptr %5) #2
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  %89 = load i32, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %89
}

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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"rbtree", !10, i64 0, !7, i64 4, !6, i64 8, !6, i64 16}
!13 = !{!7, !7, i64 0}
!14 = !{!12, !6, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}
!18 = distinct !{!18, !16}


; 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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @sglib_rbtree_len(ptr noundef readonly %0) local_unnamed_addr #1 {
  %2 = alloca [128 x ptr], align 16
  %3 = alloca [128 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %3) #3
  %.not33 = icmp eq ptr %0, null
  br i1 %.not33, label %._crit_edge, label %.preheader30

.preheader30:                                     ; preds = %1, %.critedge
  %.036 = phi i32 [ %spec.select, %.critedge ], [ 0, %1 ]
  %.02335 = phi i32 [ %.225.lcssa, %.critedge ], [ 0, %1 ]
  %.02634 = phi ptr [ %20, %.critedge ], [ %0, %1 ]
  %4 = sext i32 %.02335 to i64
  br label %5

5:                                                ; preds = %.preheader30, %5
  %indvars.iv39.in = phi i32 [ %.02335, %.preheader30 ], [ %indvars.iv39, %5 ]
  %indvars.iv = phi i64 [ %4, %.preheader30 ], [ %indvars.iv.next, %5 ]
  %.12731 = phi ptr [ %.02634, %.preheader30 ], [ %11, %5 ]
  %indvars.iv39 = add i32 %indvars.iv39.in, 1
  %6 = getelementptr inbounds i8, ptr %.12731, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = getelementptr inbounds [128 x ptr], ptr %2, i64 0, i64 %indvars.iv
  store ptr %7, ptr %8, align 8, !tbaa !11
  %9 = getelementptr inbounds [128 x i8], ptr %3, i64 0, i64 %indvars.iv
  store i8 0, ptr %9, align 1, !tbaa !12
  %10 = getelementptr inbounds i8, ptr %.12731, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %.not29 = icmp eq ptr %11, null
  br i1 %.not29, label %.preheader.preheader, label %5, !llvm.loop !14

.preheader.preheader:                             ; preds = %5
  %12 = sext i32 %indvars.iv39 to i64
  %smin = tail call i32 @llvm.smin.i32(i32 %indvars.iv39, i32 1)
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %21
  %indvars.iv41 = phi i64 [ %12, %.preheader.preheader ], [ %indvars.iv.next42, %21 ]
  %.1 = phi i32 [ %.036, %.preheader.preheader ], [ %spec.select, %21 ]
  %indvars.iv.next42 = add nsw i64 %indvars.iv41, -1
  %13 = getelementptr inbounds [128 x i8], ptr %3, i64 0, i64 %indvars.iv.next42
  %14 = load i8, ptr %13, align 1, !tbaa !12
  %15 = icmp eq i8 %14, 0
  %16 = zext i1 %15 to i32
  %spec.select = add nsw i32 %.1, %16
  %17 = add i8 %14, 1
  store i8 %17, ptr %13, align 1, !tbaa !12
  %18 = icmp sgt i64 %indvars.iv41, 1
  %19 = getelementptr inbounds [128 x ptr], ptr %2, i64 0, i64 %indvars.iv.next42
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  br i1 %18, label %21, label %.critedge

21:                                               ; preds = %.preheader
  %22 = icmp eq ptr %20, null
  br i1 %22, label %.preheader, label %.critedge.loopexit, !llvm.loop !16

.critedge.loopexit:                               ; preds = %21
  %23 = trunc nsw i64 %indvars.iv41 to i32
  br label %.critedge

.critedge:                                        ; preds = %.preheader, %.critedge.loopexit
  %.225.lcssa = phi i32 [ %23, %.critedge.loopexit ], [ %smin, %.preheader ]
  %24 = getelementptr inbounds [128 x ptr], ptr %2, i64 0, i64 %indvars.iv.next42
  store ptr null, ptr %24, align 8, !tbaa !11
  %.not = icmp eq ptr %20, null
  br i1 %.not, label %._crit_edge, label %.preheader30, !llvm.loop !17

._crit_edge:                                      ; preds = %.critedge, %1
  %.0.lcssa = phi i32 [ 0, %1 ], [ %spec.select, %.critedge ]
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %2) #3
  ret i32 %.0.lcssa
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 16}
!6 = !{!"rbtree", !7, i64 0, !8, i64 4, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{!8, !8, i64 0}
!13 = !{!6, !10, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = distinct !{!17, !15}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass