; LLVM IR from ahampriyanshu/algo-ds-101 :: Algorithms/Recursion/Josephus (Josephus)

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

; Function Attrs: nounwind uwtable
define dso_local i32 @findWinner(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %10 = call noalias ptr @malloc(i64 noundef 16) #5
  store ptr %10, ptr %7, align 8, !tbaa !9
  %11 = load ptr, ptr %7, align 8, !tbaa !9
  %12 = getelementptr inbounds %struct.person, ptr %11, i32 0, i32 0
  store i32 1, ptr %12, align 8, !tbaa !11
  %13 = load ptr, ptr %7, align 8, !tbaa !9
  %14 = load ptr, ptr %7, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.person, ptr %14, i32 0, i32 1
  store ptr %13, ptr %15, align 8, !tbaa !13
  %16 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %16, ptr %8, align 8, !tbaa !9
  store i32 2, ptr %5, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %33, %2
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = icmp sle i32 %18, %19
  br i1 %20, label %21, label %36

21:                                               ; preds = %17
  %22 = call noalias ptr @malloc(i64 noundef 16) #5
  store ptr %22, ptr %9, align 8, !tbaa !9
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = load ptr, ptr %9, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.person, ptr %24, i32 0, i32 0
  store i32 %23, ptr %25, align 8, !tbaa !11
  %26 = load ptr, ptr %7, align 8, !tbaa !9
  %27 = load ptr, ptr %9, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.person, ptr %27, i32 0, i32 1
  store ptr %26, ptr %28, align 8, !tbaa !13
  %29 = load ptr, ptr %9, align 8, !tbaa !9
  %30 = load ptr, ptr %8, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.person, ptr %30, i32 0, i32 1
  store ptr %29, ptr %31, align 8, !tbaa !13
  %32 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %32, ptr %8, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %21
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %5, align 4, !tbaa !5
  br label %17, !llvm.loop !14

36:                                               ; preds = %17
  br label %37

37:                                               ; preds = %58, %36
  %38 = load ptr, ptr %7, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.person, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !13
  %41 = load ptr, ptr %7, align 8, !tbaa !9
  %42 = icmp ne ptr %40, %41
  br i1 %42, label %43, label %70

43:                                               ; preds = %37
  %44 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %44, ptr %8, align 8, !tbaa !9
  %45 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %45, ptr %9, align 8, !tbaa !9
  store i32 1, ptr %6, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %55, %43
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = icmp slt i32 %47, %48
  br i1 %49, label %50, label %58

50:                                               ; preds = %46
  %51 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %51, ptr %9, align 8, !tbaa !9
  %52 = load ptr, ptr %8, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.person, ptr %52, i32 0, i32 1
  %54 = load ptr, ptr %53, align 8, !tbaa !13
  store ptr %54, ptr %8, align 8, !tbaa !9
  br label %55

55:                                               ; preds = %50
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %6, align 4, !tbaa !5
  br label %46, !llvm.loop !16

58:                                               ; preds = %46
  %59 = load ptr, ptr %8, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.person, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !13
  %62 = load ptr, ptr %9, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.person, ptr %62, i32 0, i32 1
  store ptr %61, ptr %63, align 8, !tbaa !13
  %64 = load ptr, ptr %9, align 8, !tbaa !9
  %65 = getelementptr inbounds %struct.person, ptr %64, i32 0, i32 1
  %66 = load ptr, ptr %65, align 8, !tbaa !13
  store ptr %66, ptr %7, align 8, !tbaa !9
  %67 = load ptr, ptr %8, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.person, ptr %67, i32 0, i32 1
  store ptr null, ptr %68, align 8, !tbaa !13
  %69 = load ptr, ptr %8, align 8, !tbaa !9
  call void @free(ptr noundef %69) #4
  store ptr null, ptr %8, align 8, !tbaa !9
  br label %37, !llvm.loop !17

70:                                               ; preds = %37
  %71 = load ptr, ptr %7, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.person, ptr %71, i32 0, i32 0
  %73 = load i32, ptr %72, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret i32 %73
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

; 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 = { nounwind allocsize(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 "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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

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


; 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 i32 @findWinner(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #3
  store i32 1, ptr %3, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  store ptr %3, ptr %4, align 8, !tbaa !11
  %.not34 = icmp slt i32 %0, 2
  br i1 %.not34, label %._crit_edge43, label %.lr.ph

.preheader33:                                     ; preds = %.lr.ph
  %.pre = load ptr, ptr %4, align 8, !tbaa !11
  %.not3241 = icmp eq ptr %.pre, %3
  br i1 %.not3241, label %._crit_edge43, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %.preheader33
  %5 = icmp sgt i32 %1, 1
  br i1 %5, label %.preheader.us, label %.preheader

.preheader.us:                                    ; preds = %.preheader.lr.ph, %.preheader.us.backedge
  %.138.us = phi ptr [ %.138.us.be, %.preheader.us.backedge ], [ %3, %.preheader.lr.ph ]
  %.03037.us = phi i32 [ %.03037.us.be, %.preheader.us.backedge ], [ 1, %.preheader.lr.ph ]
  %6 = getelementptr inbounds i8, ptr %.138.us, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %8 = add nuw nsw i32 %.03037.us, 1
  %exitcond48.not = icmp eq i32 %8, %1
  br i1 %exitcond48.not, label %._crit_edge.us, label %.preheader.us.backedge

.preheader.us.backedge:                           ; preds = %.preheader.us, %._crit_edge.us
  %.138.us.be = phi ptr [ %7, %.preheader.us ], [ %11, %._crit_edge.us ]
  %.03037.us.be = phi i32 [ %8, %.preheader.us ], [ 1, %._crit_edge.us ]
  br label %.preheader.us, !llvm.loop !12

._crit_edge.us:                                   ; preds = %.preheader.us
  %9 = getelementptr inbounds i8, ptr %.138.us, i64 8
  %10 = getelementptr inbounds i8, ptr %7, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  store ptr %11, ptr %9, align 8, !tbaa !11
  tail call void @free(ptr noundef %7) #4
  %12 = getelementptr inbounds i8, ptr %11, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %.not32.us = icmp eq ptr %13, %11
  br i1 %.not32.us, label %._crit_edge43, label %.preheader.us.backedge

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.02836 = phi ptr [ %14, %.lr.ph ], [ %3, %2 ]
  %.03135 = phi i32 [ %17, %.lr.ph ], [ 2, %2 ]
  %14 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #3
  store i32 %.03135, ptr %14, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 8
  store ptr %3, ptr %15, align 8, !tbaa !11
  %16 = getelementptr inbounds i8, ptr %.02836, i64 8
  store ptr %14, ptr %16, align 8, !tbaa !11
  %17 = add nuw i32 %.03135, 1
  %exitcond.not = icmp eq i32 %.03135, %0
  br i1 %exitcond.not, label %.preheader33, label %.lr.ph, !llvm.loop !14

.preheader:                                       ; preds = %.preheader.lr.ph, %.preheader
  %18 = phi ptr [ %20, %.preheader ], [ %.pre, %.preheader.lr.ph ]
  %.02942 = phi ptr [ %18, %.preheader ], [ %3, %.preheader.lr.ph ]
  tail call void @free(ptr noundef nonnull %.02942) #4
  %19 = getelementptr inbounds i8, ptr %18, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %.not32 = icmp eq ptr %20, %18
  br i1 %.not32, label %._crit_edge43, label %.preheader, !llvm.loop !12

._crit_edge43:                                    ; preds = %.preheader, %._crit_edge.us, %2, %.preheader33
  %.029.lcssa = phi ptr [ %3, %.preheader33 ], [ %3, %2 ], [ %11, %._crit_edge.us ], [ %18, %.preheader ]
  %21 = load i32, ptr %.029.lcssa, align 8, !tbaa !5
  ret i32 %21
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }
attributes #4 = { 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, !7, i64 0}
!6 = !{!"person", !7, i64 0, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass