; LLVM IR from simon-zerisenay/42_Push_Swap :: src (calculate_cost)

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

; Function Attrs: nounwind uwtable
define dso_local void @find_cost(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %10, ptr %6, align 8, !tbaa !5
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = call i32 @find_len(ptr noundef %12)
  store i32 %13, ptr %5, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %60, %2
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %64

17:                                               ; preds = %14
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.t_list, ptr %18, i32 0, i32 4
  %20 = load i32, ptr %19, align 8, !tbaa !11
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.t_list, ptr %21, i32 0, i32 3
  store i32 %20, ptr %22, align 4, !tbaa !13
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.t_list, ptr %23, i32 0, i32 3
  %25 = load i32, ptr %24, align 4, !tbaa !13
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = sdiv i32 %26, 2
  %28 = icmp sgt i32 %25, %27
  br i1 %28, label %29, label %37

29:                                               ; preds = %17
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.t_list, ptr %30, i32 0, i32 4
  %32 = load i32, ptr %31, align 8, !tbaa !11
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = sub nsw i32 %32, %33
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.t_list, ptr %35, i32 0, i32 3
  store i32 %34, ptr %36, align 4, !tbaa !13
  br label %37

37:                                               ; preds = %29, %17
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = call i32 @get_target_index(ptr noundef %38, ptr noundef %6)
  store i32 %39, ptr %8, align 4, !tbaa !9
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = load ptr, ptr %40, align 8, !tbaa !5
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = call i32 @count_backward_moves(ptr noundef %41, i32 noundef %42)
  store i32 %43, ptr %7, align 4, !tbaa !9
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = load ptr, ptr %44, align 8, !tbaa !5
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = call i32 @count_forward_moves(ptr noundef %45, i32 noundef %46)
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.t_list, ptr %48, i32 0, i32 2
  store i32 %47, ptr %49, align 8, !tbaa !14
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.t_list, ptr %51, i32 0, i32 2
  %53 = load i32, ptr %52, align 8, !tbaa !14
  %54 = icmp slt i32 %50, %53
  br i1 %54, label %55, label %60

55:                                               ; preds = %37
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = mul nsw i32 %56, -1
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.t_list, ptr %58, i32 0, i32 2
  store i32 %57, ptr %59, align 8, !tbaa !14
  br label %60

60:                                               ; preds = %55, %37
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.t_list, ptr %61, i32 0, i32 5
  %63 = load ptr, ptr %62, align 8, !tbaa !15
  store ptr %63, ptr %6, align 8, !tbaa !5
  br label %14, !llvm.loop !16

64:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

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

declare i32 @find_len(ptr noundef) #2

declare i32 @get_target_index(ptr noundef, ptr noundef) #2

declare i32 @count_backward_moves(ptr noundef, i32 noundef) #2

declare i32 @count_forward_moves(ptr noundef, i32 noundef) #2

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

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


; 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: nounwind uwtable
define dso_local void @find_cost(ptr noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %4 = load ptr, ptr %1, align 8, !tbaa !5
  store ptr %4, ptr %3, align 8, !tbaa !5
  %5 = tail call i32 @find_len(ptr noundef %4) #3
  %.not12 = icmp eq ptr %4, null
  br i1 %.not12, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %6 = sdiv i32 %5, 2
  br label %7

7:                                                ; preds = %.lr.ph, %7
  %8 = phi ptr [ %4, %.lr.ph ], [ %24, %7 ]
  %9 = getelementptr inbounds i8, ptr %8, i64 16
  %10 = load i32, ptr %9, align 8, !tbaa !9
  %11 = getelementptr inbounds i8, ptr %8, i64 12
  %12 = icmp sgt i32 %10, %6
  %13 = select i1 %12, i32 %5, i32 0
  %spec.select = sub nsw i32 %10, %13
  store i32 %spec.select, ptr %11, align 4, !tbaa !12
  %14 = call i32 @get_target_index(ptr noundef %0, ptr noundef nonnull %3) #3
  %15 = load ptr, ptr %0, align 8, !tbaa !5
  %16 = call i32 @count_backward_moves(ptr noundef %15, i32 noundef %14) #3
  %17 = load ptr, ptr %0, align 8, !tbaa !5
  %18 = call i32 @count_forward_moves(ptr noundef %17, i32 noundef %14) #3
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %19, i64 8
  %21 = icmp slt i32 %16, %18
  %22 = sub nsw i32 0, %16
  %storemerge11 = select i1 %21, i32 %22, i32 %18
  store i32 %storemerge11, ptr %20, align 8, !tbaa !13
  %23 = getelementptr inbounds i8, ptr %19, i64 24
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  store ptr %24, ptr %3, align 8, !tbaa !5
  %.not = icmp eq ptr %24, null
  br i1 %.not, label %._crit_edge, label %7, !llvm.loop !15

._crit_edge:                                      ; preds = %7, %2
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  ret void
}

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

declare i32 @find_len(ptr noundef) local_unnamed_addr #2

declare i32 @get_target_index(ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @count_backward_moves(ptr noundef, i32 noundef) local_unnamed_addr #2

declare i32 @count_forward_moves(ptr noundef, i32 noundef) local_unnamed_addr #2

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

attributes #0 = { 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = !{!10, !11, i64 16}
!10 = !{!"t_list", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !6, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 12}
!13 = !{!10, !11, i64 8}
!14 = !{!10, !6, i64 24}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass