; LLVM IR from LeetCode-in-C/LeetCode-in-C :: src/main/c/g0001_0100/s0031_next_permutation (Solution)

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

; Function Attrs: nounwind uwtable
declare dso_local void @swap(ptr noundef, i32 noundef, i32 noundef) #0

; 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

; Function Attrs: nounwind uwtable
declare dso_local void @reverse(ptr noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @nextPermutation(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = icmp eq ptr %7, null
  br i1 %8, label %12, label %9

9:                                                ; preds = %2
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = icmp sle i32 %10, 1
  br i1 %11, label %12, label %13

12:                                               ; preds = %9, %2
  br label %68

13:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = sub nsw i32 %14, 2
  store i32 %15, ptr %5, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %34, %13
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = icmp sge i32 %17, 0
  br i1 %18, label %19, label %32

19:                                               ; preds = %16
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i32, ptr %20, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = add nsw i32 %26, 1
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i32, ptr %25, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = icmp sge i32 %24, %30
  br label %32

32:                                               ; preds = %19, %16
  %33 = phi i1 [ false, %16 ], [ %31, %19 ]
  br i1 %33, label %34, label %37

34:                                               ; preds = %32
  %35 = load i32, ptr %5, align 4, !tbaa !9
  %36 = add nsw i32 %35, -1
  store i32 %36, ptr %5, align 4, !tbaa !9
  br label %16, !llvm.loop !11

37:                                               ; preds = %32
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = icmp sge i32 %38, 0
  br i1 %39, label %40, label %62

40:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = sub nsw i32 %41, 1
  store i32 %42, ptr %6, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %55, %40
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %44, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !9
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = load i32, ptr %5, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i32, ptr %49, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !9
  %54 = icmp sle i32 %48, %53
  br i1 %54, label %55, label %58

55:                                               ; preds = %43
  %56 = load i32, ptr %6, align 4, !tbaa !9
  %57 = add nsw i32 %56, -1
  store i32 %57, ptr %6, align 4, !tbaa !9
  br label %43, !llvm.loop !13

58:                                               ; preds = %43
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = load i32, ptr %5, align 4, !tbaa !9
  %61 = load i32, ptr %6, align 4, !tbaa !9
  call void @swap(ptr noundef %59, i32 noundef %60, i32 noundef %61)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %62

62:                                               ; preds = %58, %37
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = load i32, ptr %5, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  %66 = load i32, ptr %4, align 4, !tbaa !9
  %67 = sub nsw i32 %66, 1
  call void @reverse(ptr noundef %63, i32 noundef %65, i32 noundef %67)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %68

68:                                               ; preds = %62, %12
  ret void
}

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 }

!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !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"

; Function Attrs: nounwind uwtable
declare dso_local void @swap(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @reverse(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @nextPermutation(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp slt i32 %1, 2
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %21, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %5 = add nsw i32 %1, -2
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %12
  %.02229 = phi i32 [ %13, %12 ], [ %5, %.lr.ph.preheader ]
  %6 = zext nneg i32 %.02229 to i64
  %7 = getelementptr inbounds i32, ptr %0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %7, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %.not = icmp slt i32 %8, %10
  br i1 %.not, label %.critedge.preheader, label %12

.critedge.preheader:                              ; preds = %.lr.ph
  %11 = zext nneg i32 %1 to i64
  br label %.critedge

12:                                               ; preds = %.lr.ph
  %13 = add nsw i32 %.02229, -1
  %14 = icmp sgt i32 %.02229, 0
  br i1 %14, label %.lr.ph, label %.critedge26, !llvm.loop !9

.critedge:                                        ; preds = %.critedge.preheader, %.critedge
  %indvars.iv = phi i64 [ %11, %.critedge.preheader ], [ %indvars.iv.next, %.critedge ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %15 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %.not25 = icmp sgt i32 %16, %8
  br i1 %.not25, label %17, label %.critedge, !llvm.loop !11

17:                                               ; preds = %.critedge
  %18 = trunc nsw i64 %indvars.iv.next to i32
  tail call void @swap(ptr noundef nonnull %0, i32 noundef %.02229, i32 noundef %18)
  %19 = add nsw i32 %.02229, 1
  br label %.critedge26

.critedge26:                                      ; preds = %12, %17
  %.02228 = phi i32 [ %19, %17 ], [ 0, %12 ]
  %20 = add nsw i32 %1, -1
  tail call void @reverse(ptr noundef nonnull %0, i32 noundef %.02228, i32 noundef %20)
  br label %21

21:                                               ; preds = %2, %.critedge26
  ret void
}

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

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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