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

; 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.sglib_iListType_iterator = type { ptr, ptr, ptr, ptr }
%struct.ilist = type { i32, 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_iListType_add(ptr noundef, ptr noundef) #1

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

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

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

6:                                                ; preds = %18, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 100
  br i1 %8, label %9, label %21

9:                                                ; preds = %6
  %10 = call ptr @malloc_beebs(i64 noundef 16)
  store ptr %10, ptr %2, align 8, !tbaa !9
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100 x i32], ptr @array, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = load ptr, ptr %2, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.ilist, ptr %15, i32 0, i32 0
  store i32 %14, ptr %16, align 8, !tbaa !11
  %17 = load ptr, ptr %2, align 8, !tbaa !9
  call void @sglib_iListType_add(ptr noundef %3, ptr noundef %17)
  br label %18

18:                                               ; preds = %9
  %19 = load i32, ptr %1, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !13

21:                                               ; preds = %6
  %22 = load ptr, ptr %3, align 8, !tbaa !9
  %23 = call ptr @sglib_iListType_it_init(ptr noundef %4, ptr noundef %22)
  store ptr %23, ptr %2, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %33, %21
  %25 = load ptr, ptr %2, align 8, !tbaa !9
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %35

27:                                               ; preds = %24
  %28 = load ptr, ptr %2, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.ilist, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 8, !tbaa !11
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = add nsw i32 %31, %30
  store i32 %32, ptr %5, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %27
  %34 = call ptr @sglib_iListType_it_next(ptr noundef %4)
  store ptr %34, ptr %2, align 8, !tbaa !9
  br label %24, !llvm.loop !15

35:                                               ; preds = %24
  %36 = load ptr, ptr %3, align 8, !tbaa !9
  %37 = call ptr @sglib_iListType_it_init(ptr noundef %4, ptr noundef %36)
  store ptr %37, ptr %2, align 8, !tbaa !9
  br label %38

38:                                               ; preds = %43, %35
  %39 = load ptr, ptr %2, align 8, !tbaa !9
  %40 = icmp ne ptr %39, null
  br i1 %40, label %41, label %45

41:                                               ; preds = %38
  %42 = load ptr, ptr %2, align 8, !tbaa !9
  call void @free_beebs(ptr noundef %42)
  br label %43

43:                                               ; preds = %41
  %44 = call ptr @sglib_iListType_it_next(ptr noundef %4)
  store ptr %44, ptr %2, align 8, !tbaa !9
  br label %38, !llvm.loop !16

45:                                               ; preds = %38
  %46 = load i32, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 32, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret i32 %46
}

; 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 = !{!"ilist", !6, i64 0, !10, i64 8}
!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.sglib_iListType_iterator = type { ptr, ptr, 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_iListType_add(ptr noundef, ptr noundef) local_unnamed_addr #1

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

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

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

3:                                                ; preds = %0, %3
  %indvars.iv = phi i64 [ 1, %0 ], [ %indvars.iv.next, %3 ]
  %4 = call ptr @malloc_beebs(i64 noundef 16)
  %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 %4, align 8, !tbaa !11
  call void @sglib_iListType_add(ptr noundef nonnull %1, ptr noundef nonnull %4)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 100
  br i1 %exitcond.not, label %7, label %3, !llvm.loop !13

7:                                                ; preds = %3
  %8 = load ptr, ptr %1, align 8, !tbaa !5
  %9 = call ptr @sglib_iListType_it_init(ptr noundef nonnull %2, ptr noundef %8)
  %.not14 = icmp eq ptr %9, null
  br i1 %.not14, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %7, %.lr.ph
  %.016 = phi i32 [ %11, %.lr.ph ], [ 0, %7 ]
  %.01015 = phi ptr [ %12, %.lr.ph ], [ %9, %7 ]
  %10 = load i32, ptr %.01015, align 8, !tbaa !11
  %11 = add nsw i32 %10, %.016
  %12 = call ptr @sglib_iListType_it_next(ptr noundef nonnull %2)
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %7
  %.0.lcssa = phi i32 [ 0, %7 ], [ %11, %.lr.ph ]
  %13 = load ptr, ptr %1, align 8, !tbaa !5
  %14 = call ptr @sglib_iListType_it_init(ptr noundef nonnull %2, ptr noundef %13)
  %.not1217 = icmp eq ptr %14, null
  br i1 %.not1217, label %._crit_edge21, label %.lr.ph20

.lr.ph20:                                         ; preds = %._crit_edge, %.lr.ph20
  %.118 = phi ptr [ %15, %.lr.ph20 ], [ %14, %._crit_edge ]
  call void @free_beebs(ptr noundef nonnull %.118)
  %15 = call ptr @sglib_iListType_it_next(ptr noundef nonnull %2)
  %.not12 = icmp eq ptr %15, null
  br i1 %.not12, label %._crit_edge21, label %.lr.ph20, !llvm.loop !16

._crit_edge21:                                    ; preds = %.lr.ph20, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %2) #2
  call void @llvm.lifetime.end.p0(i64 8, 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 = !{!"ilist", !10, i64 0, !6, i64 8}
!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