; 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 }
%struct.sglib_rbtree_iterator = type { ptr, [128 x i8], [128 x ptr], i16, i16, ptr, ptr }

@array = external dso_local global [100 x i32], align 16

; 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 dso_local void @sglib_rbtree_add(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @sglib_rbtree_find_member(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @sglib_rbtree_it_init(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @sglib_rbtree_it_init_inorder(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @sglib_rbtree_it_next(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @benchmark_sg_rbtree() #1 {
  %1 = alloca i32, align 4
  %2 = alloca %struct.rbtree, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.sglib_rbtree_iterator, align 8
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 24, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 1184, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !5
  store ptr null, ptr %4, align 8, !tbaa !9
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %30, %0
  %9 = load i32, ptr %1, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 100
  br i1 %10, label %11, label %33

11:                                               ; preds = %8
  %12 = load i32, ptr %1, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [100 x i32], ptr @array, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = getelementptr inbounds %struct.rbtree, ptr %2, i32 0, i32 0
  store i32 %15, ptr %16, align 8, !tbaa !11
  %17 = load ptr, ptr %4, align 8, !tbaa !9
  %18 = call ptr @sglib_rbtree_find_member(ptr noundef %17, ptr noundef %2)
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %29

20:                                               ; preds = %11
  %21 = call ptr @malloc_beebs(i64 noundef 24)
  store ptr %21, ptr %3, align 8, !tbaa !9
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [100 x i32], ptr @array, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = load ptr, ptr %3, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.rbtree, ptr %26, i32 0, i32 0
  store i32 %25, ptr %27, align 8, !tbaa !11
  %28 = load ptr, ptr %3, align 8, !tbaa !9
  call void @sglib_rbtree_add(ptr noundef %4, ptr noundef %28)
  br label %29

29:                                               ; preds = %20, %11
  br label %30

30:                                               ; preds = %29
  %31 = load i32, ptr %1, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %1, align 4, !tbaa !5
  br label %8, !llvm.loop !13

33:                                               ; preds = %8
  %34 = load ptr, ptr %4, align 8, !tbaa !9
  %35 = call ptr @sglib_rbtree_it_init_inorder(ptr noundef %6, ptr noundef %34)
  store ptr %35, ptr %5, align 8, !tbaa !9
  br label %36

36:                                               ; preds = %45, %33
  %37 = load ptr, ptr %5, align 8, !tbaa !9
  %38 = icmp ne ptr %37, null
  br i1 %38, label %39, label %47

39:                                               ; preds = %36
  %40 = load ptr, ptr %5, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.rbtree, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !11
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = add nsw i32 %43, %42
  store i32 %44, ptr %7, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %39
  %46 = call ptr @sglib_rbtree_it_next(ptr noundef %6)
  store ptr %46, ptr %5, align 8, !tbaa !9
  br label %36, !llvm.loop !15

47:                                               ; preds = %36
  %48 = load ptr, ptr %4, align 8, !tbaa !9
  %49 = call ptr @sglib_rbtree_it_init(ptr noundef %6, ptr noundef %48)
  store ptr %49, ptr %5, align 8, !tbaa !9
  br label %50

50:                                               ; preds = %55, %47
  %51 = load ptr, ptr %5, align 8, !tbaa !9
  %52 = icmp ne ptr %51, null
  br i1 %52, label %53, label %57

53:                                               ; preds = %50
  %54 = load ptr, ptr %5, align 8, !tbaa !9
  call void @free_beebs(ptr noundef %54)
  br label %55

55:                                               ; preds = %53
  %56 = call ptr @sglib_rbtree_it_next(ptr noundef %6)
  store ptr %56, ptr %5, align 8, !tbaa !9
  br label %50, !llvm.loop !16

57:                                               ; preds = %50
  %58 = load i32, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 1184, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 24, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret i32 %58
}

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

; Function Attrs: nounwind uwtable
declare hidden void @free_beebs(ptr noundef) #1

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"rbtree", !6, i64 0, !7, i64 4, !10, i64 8, !10, i64 16}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


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

%struct.rbtree = type { i32, i8, ptr, ptr }
%struct.sglib_rbtree_iterator = type { ptr, [128 x i8], [128 x ptr], i16, i16, ptr, ptr }

@array = external dso_local local_unnamed_addr global [100 x i32], align 16

; 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: nounwind uwtable
declare dso_local void @sglib_rbtree_add(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @sglib_rbtree_find_member(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @sglib_rbtree_it_init(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @sglib_rbtree_it_init_inorder(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @sglib_rbtree_it_next(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @benchmark_sg_rbtree() local_unnamed_addr #1 {
  %1 = alloca %struct.rbtree, align 8
  %2 = alloca ptr, align 8
  %3 = alloca %struct.sglib_rbtree_iterator, align 8
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %1) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #2
  call void @llvm.lifetime.start.p0(i64 1184, ptr nonnull %3) #2
  store ptr null, ptr %2, align 8, !tbaa !5
  br label %4

4:                                                ; preds = %0, %13
  %indvars.iv = phi i64 [ 0, %0 ], [ %indvars.iv.next, %13 ]
  %5 = getelementptr inbounds [100 x i32], ptr @array, i64 0, i64 %indvars.iv
  %6 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %6, ptr %1, align 8, !tbaa !11
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = call ptr @sglib_rbtree_find_member(ptr noundef %7, ptr noundef nonnull %1)
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %13

10:                                               ; preds = %4
  %11 = call ptr @malloc_beebs(i64 noundef 24)
  %12 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %12, ptr %11, align 8, !tbaa !11
  call void @sglib_rbtree_add(ptr noundef nonnull %2, ptr noundef nonnull %11)
  br label %13

13:                                               ; preds = %4, %10
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 100
  br i1 %exitcond.not, label %14, label %4, !llvm.loop !13

14:                                               ; preds = %13
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = call ptr @sglib_rbtree_it_init_inorder(ptr noundef nonnull %3, ptr noundef %15)
  %.not15 = icmp eq ptr %16, null
  br i1 %.not15, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %14, %.lr.ph
  %.017 = phi i32 [ %18, %.lr.ph ], [ 0, %14 ]
  %.01116 = phi ptr [ %19, %.lr.ph ], [ %16, %14 ]
  %17 = load i32, ptr %.01116, align 8, !tbaa !11
  %18 = add nsw i32 %17, %.017
  %19 = call ptr @sglib_rbtree_it_next(ptr noundef nonnull %3)
  %.not = icmp eq ptr %19, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %14
  %.0.lcssa = phi i32 [ 0, %14 ], [ %18, %.lr.ph ]
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = call ptr @sglib_rbtree_it_init(ptr noundef nonnull %3, ptr noundef %20)
  %.not1318 = icmp eq ptr %21, null
  br i1 %.not1318, label %._crit_edge22, label %.lr.ph21

.lr.ph21:                                         ; preds = %._crit_edge, %.lr.ph21
  %.119 = phi ptr [ %22, %.lr.ph21 ], [ %21, %._crit_edge ]
  call void @free_beebs(ptr noundef nonnull %.119)
  %22 = call ptr @sglib_rbtree_it_next(ptr noundef nonnull %3)
  %.not13 = icmp eq ptr %22, null
  br i1 %.not13, label %._crit_edge22, label %.lr.ph21, !llvm.loop !16

._crit_edge22:                                    ; preds = %.lr.ph21, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 1184, ptr nonnull %3) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #2
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %1) #2
  ret i32 %.0.lcssa
}

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

; Function Attrs: nounwind uwtable
declare hidden void @free_beebs(ptr noundef) local_unnamed_addr #1

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


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