; LLVM IR from ADJA/algos :: NumberTheory/PermutationByNumber.ll (Z19permutationByNumberii)

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

@used = external dso_local global [20 x i8], align 16
@f = external dso_local global [20 x i32], align 16
@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z19permutationByNumberii(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 i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  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) #3
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %20, %2
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = icmp sle i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %23

16:                                               ; preds = %11
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [20 x i8], ptr @used, i64 0, i64 %18
  store i8 0, ptr %19, align 1, !tbaa !9
  br label %20

20:                                               ; preds = %16
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %5, align 4, !tbaa !5
  br label %11, !llvm.loop !11

23:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 1, ptr %7, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %72, %23
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = icmp sle i32 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 5, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %75

29:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 1, ptr %9, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %67, %29
  %31 = load i32, ptr %9, align 4, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = icmp sle i32 %31, %32
  br i1 %33, label %35, label %34

34:                                               ; preds = %30
  store i32 8, ptr %8, align 4
  br label %70

35:                                               ; preds = %30
  %36 = load i32, ptr %9, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [20 x i8], ptr @used, i64 0, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !9, !range !13, !noundef !14
  %40 = trunc i8 %39 to i1
  br i1 %40, label %41, label %42

41:                                               ; preds = %35
  br label %67

42:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = sub nsw i32 %43, %44
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [20 x i32], ptr @f, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  store i32 %48, ptr %10, align 4, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = load i32, ptr %10, align 4, !tbaa !5
  %51 = add nsw i32 %49, %50
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = icmp sge i32 %51, %52
  br i1 %53, label %54, label %60

54:                                               ; preds = %42
  %55 = load i32, ptr %9, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [20 x i8], ptr @used, i64 0, i64 %56
  store i8 1, ptr %57, align 1, !tbaa !9
  %58 = load i32, ptr %9, align 4, !tbaa !5
  %59 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %58)
  store i32 8, ptr %8, align 4
  br label %64

60:                                               ; preds = %42
  %61 = load i32, ptr %10, align 4, !tbaa !5
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = add nsw i32 %62, %61
  store i32 %63, ptr %6, align 4, !tbaa !5
  store i32 0, ptr %8, align 4
  br label %64

64:                                               ; preds = %60, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %65 = load i32, ptr %8, align 4
  switch i32 %65, label %70 [
    i32 0, label %66
  ]

66:                                               ; preds = %64
  br label %67

67:                                               ; preds = %66, %41
  %68 = load i32, ptr %9, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %9, align 4, !tbaa !5
  br label %30, !llvm.loop !15

70:                                               ; preds = %64, %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %71

71:                                               ; preds = %70
  br label %72

72:                                               ; preds = %71
  %73 = load i32, ptr %7, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %7, align 4, !tbaa !5
  br label %24, !llvm.loop !16

75:                                               ; preds = %28
  %76 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  ret void
}

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

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

declare i32 @printf(ptr noundef, ...) #2

attributes #0 = { mustprogress 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !12}


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

@used = external dso_local local_unnamed_addr global [20 x i8], align 16
@f = external dso_local local_unnamed_addr global [20 x i32], align 16
@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z19permutationByNumberii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not35 = icmp slt i32 %0, 1
  br i1 %.not35, label %._crit_edge, label %.preheader.preheader

.preheader.preheader:                             ; preds = %2
  %3 = zext nneg i32 %0 to i64
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 getelementptr inbounds (i8, ptr @used, i64 1), i8 0, i64 %3, i1 false), !tbaa !5
  %4 = add nuw i32 %0, 1
  %5 = zext nneg i32 %0 to i64
  %wide.trip.count54 = zext i32 %4 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %..loopexit_crit_edge
  %indvars.iv51 = phi i64 [ 1, %.preheader.preheader ], [ %indvars.iv.next52, %..loopexit_crit_edge ]
  %.02443 = phi i32 [ 0, %.preheader.preheader ], [ %.134, %..loopexit_crit_edge ]
  %6 = sub nsw i64 %5, %indvars.iv51
  %7 = getelementptr inbounds [20 x i32], ptr @f, i64 0, i64 %6
  br label %9

._crit_edge:                                      ; preds = %..loopexit_crit_edge, %2
  %8 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  ret void

9:                                                ; preds = %.preheader, %.thread
  %indvars.iv = phi i64 [ 1, %.preheader ], [ %indvars.iv.next, %.thread ]
  %.139 = phi i32 [ %.02443, %.preheader ], [ %.3, %.thread ]
  %10 = getelementptr inbounds [20 x i8], ptr @used, i64 0, i64 %indvars.iv
  %11 = load i8, ptr %10, align 1, !tbaa !5, !range !9, !noundef !10
  %12 = trunc nuw i8 %11 to i1
  br i1 %12, label %.thread, label %13

13:                                               ; preds = %9
  %14 = load i32, ptr %7, align 4, !tbaa !11
  %15 = add nsw i32 %14, %.139
  %.not29 = icmp slt i32 %15, %1
  br i1 %.not29, label %.thread, label %16

16:                                               ; preds = %13
  %17 = getelementptr inbounds [20 x i8], ptr @used, i64 0, i64 %indvars.iv
  %18 = trunc nuw nsw i64 %indvars.iv to i32
  store i8 1, ptr %17, align 1, !tbaa !5
  %19 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %18)
  br label %..loopexit_crit_edge

.thread:                                          ; preds = %13, %9
  %.3 = phi i32 [ %.139, %9 ], [ %15, %13 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count54
  br i1 %exitcond.not, label %..loopexit_crit_edge, label %9, !llvm.loop !13

..loopexit_crit_edge:                             ; preds = %.thread, %16
  %.134 = phi i32 [ %.139, %16 ], [ %.3, %.thread ]
  %indvars.iv.next52 = add nuw nsw i64 %indvars.iv51, 1
  %exitcond55.not = icmp eq i64 %indvars.iv.next52, %wide.trip.count54
  br i1 %exitcond55.not, label %._crit_edge, label %.preheader, !llvm.loop !15
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { mustprogress nofree 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 = { nofree 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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