; LLVM IR from AdarshGaur/Competitive-Programming :: Codeforces/Codeforces Round #779 (Div. 2)/C. Shinju and the Lost Permutation.ll (Z5solvev)

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

%"class.std::basic_istream" = type { ptr, i64, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }

@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5solvev() #0 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i8, align 1
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  %9 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %1)
  %10 = load i64, ptr %1, align 8, !tbaa !5
  %11 = call ptr @llvm.stacksave.p0()
  store ptr %11, ptr %2, align 8
  %12 = alloca i64, i64 %10, align 16
  store i64 %10, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store i64 0, ptr %4, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %22, %0
  %14 = load i64, ptr %4, align 8, !tbaa !5
  %15 = load i64, ptr %1, align 8, !tbaa !5
  %16 = icmp slt i64 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %25

18:                                               ; preds = %13
  %19 = load i64, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds i64, ptr %12, i64 %19
  %21 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %20)
  br label %22

22:                                               ; preds = %18
  %23 = load i64, ptr %4, align 8, !tbaa !5
  %24 = add nsw i64 %23, 1
  store i64 %24, ptr %4, align 8, !tbaa !5
  br label %13, !llvm.loop !9

25:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store i64 0, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #4
  store i8 1, ptr %6, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store i64 0, ptr %7, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %61, %25
  %27 = load i64, ptr %7, align 8, !tbaa !5
  %28 = load i64, ptr %1, align 8, !tbaa !5
  %29 = icmp slt i64 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %64

31:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %32 = load i64, ptr %7, align 8, !tbaa !5
  %33 = add nsw i64 %32, 1
  %34 = load i64, ptr %1, align 8, !tbaa !5
  %35 = srem i64 %33, %34
  store i64 %35, ptr %8, align 8, !tbaa !5
  %36 = load i64, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds i64, ptr %12, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !5
  %39 = load i64, ptr %7, align 8, !tbaa !5
  %40 = getelementptr inbounds i64, ptr %12, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = icmp sgt i64 %38, %41
  br i1 %42, label %43, label %53

43:                                               ; preds = %31
  %44 = load i64, ptr %8, align 8, !tbaa !5
  %45 = getelementptr inbounds i64, ptr %12, i64 %44
  %46 = load i64, ptr %45, align 8, !tbaa !5
  %47 = load i64, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds i64, ptr %12, i64 %47
  %49 = load i64, ptr %48, align 8, !tbaa !5
  %50 = add nsw i64 %49, 1
  %51 = icmp ne i64 %46, %50
  br i1 %51, label %52, label %53

52:                                               ; preds = %43
  store i8 0, ptr %6, align 1, !tbaa !11
  br label %53

53:                                               ; preds = %52, %43, %31
  %54 = load i64, ptr %7, align 8, !tbaa !5
  %55 = getelementptr inbounds i64, ptr %12, i64 %54
  %56 = load i64, ptr %55, align 8, !tbaa !5
  %57 = icmp eq i64 %56, 1
  %58 = zext i1 %57 to i64
  %59 = load i64, ptr %5, align 8, !tbaa !5
  %60 = add nsw i64 %59, %58
  store i64 %60, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %61

61:                                               ; preds = %53
  %62 = load i64, ptr %7, align 8, !tbaa !5
  %63 = add nsw i64 %62, 1
  store i64 %63, ptr %7, align 8, !tbaa !5
  br label %26, !llvm.loop !13

64:                                               ; preds = %30
  %65 = load i8, ptr %6, align 1, !tbaa !11, !range !14, !noundef !15
  %66 = trunc i8 %65 to i1
  br i1 %66, label %67, label %70

67:                                               ; preds = %64
  %68 = load i64, ptr %5, align 8, !tbaa !5
  %69 = icmp eq i64 %68, 1
  br label %70

70:                                               ; preds = %67, %64
  %71 = phi i1 [ false, %64 ], [ %69, %67 ]
  %72 = zext i1 %71 to i8
  store i8 %72, ptr %6, align 1, !tbaa !11
  %73 = load i8, ptr %6, align 1, !tbaa !11, !range !14, !noundef !15
  %74 = trunc i8 %73 to i1
  %75 = zext i1 %74 to i64
  %76 = select i1 %74, ptr @.str, ptr @.str.1
  %77 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef %76)
  %78 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) %77, ptr noundef @.str.2)
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  %79 = load ptr, ptr %2, align 8
  call void @llvm.stackrestore.p0(ptr %79)
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  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: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(8)) #0 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

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

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { inlinehint 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 #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, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = distinct !{!13, !10}
!14 = !{i8 0, i8 2}
!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"

%"class.std::basic_istream" = type { ptr, i64, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }

@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5solvev() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #4
  %2 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %1)
  %3 = load i64, ptr %1, align 8, !tbaa !5
  %4 = call ptr @llvm.stacksave.p0()
  %5 = alloca i64, i64 %3, align 16
  %6 = load i64, ptr %1, align 8, !tbaa !5
  %7 = icmp sgt i64 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

.preheader:                                       ; preds = %.lr.ph
  %8 = icmp sgt i64 %13, 0
  br i1 %8, label %.lr.ph25.preheader, label %._crit_edge

.lr.ph25.preheader:                               ; preds = %.preheader
  %xtraiter = and i64 %13, 1
  %9 = icmp eq i64 %13, 1
  br i1 %9, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph25.preheader.new

.lr.ph25.preheader.new:                           ; preds = %.lr.ph25.preheader
  %unroll_iter = and i64 %13, 9223372036854775806
  br label %.lr.ph25

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.021 = phi i64 [ %12, %.lr.ph ], [ 0, %0 ]
  %10 = getelementptr inbounds i64, ptr %5, i64 %.021
  %11 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %10)
  %12 = add nuw nsw i64 %.021, 1
  %13 = load i64, ptr %1, align 8, !tbaa !5
  %14 = icmp slt i64 %12, %13
  br i1 %14, label %.lr.ph, label %.preheader, !llvm.loop !9

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph25, %.lr.ph25.preheader
  %.1.lcssa.ph = phi i1 [ poison, %.lr.ph25.preheader ], [ %.1.1, %.lr.ph25 ]
  %.lcssa.ph = phi i64 [ poison, %.lr.ph25.preheader ], [ %53, %.lr.ph25 ]
  %.01524.unr = phi i64 [ 0, %.lr.ph25.preheader ], [ %42, %.lr.ph25 ]
  %.01623.unr = phi i1 [ true, %.lr.ph25.preheader ], [ %.1.1, %.lr.ph25 ]
  %.01722.unr = phi i64 [ 0, %.lr.ph25.preheader ], [ %53, %.lr.ph25 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge.loopexit, label %.lr.ph25.epil

.lr.ph25.epil:                                    ; preds = %._crit_edge.loopexit.unr-lcssa
  %15 = add nuw nsw i64 %.01524.unr, 1
  %16 = icmp eq i64 %15, %13
  %iv.rem.epil = select i1 %16, i64 0, i64 %15
  %17 = getelementptr inbounds i64, ptr %5, i64 %iv.rem.epil
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = getelementptr inbounds i64, ptr %5, i64 %.01524.unr
  %20 = load i64, ptr %19, align 8, !tbaa !5
  %21 = icmp sle i64 %18, %20
  %22 = add nsw i64 %20, 1
  %.not.epil = icmp eq i64 %18, %22
  %or.cond.epil = select i1 %21, i1 true, i1 %.not.epil
  %.1.epil = select i1 %or.cond.epil, i1 %.01623.unr, i1 false
  %23 = icmp eq i64 %20, 1
  %24 = zext i1 %23 to i64
  %25 = add nuw nsw i64 %.01722.unr, %24
  br label %._crit_edge.loopexit

._crit_edge.loopexit:                             ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph25.epil
  %.1.lcssa = phi i1 [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.1.epil, %.lr.ph25.epil ]
  %.lcssa = phi i64 [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %25, %.lr.ph25.epil ]
  %26 = icmp eq i64 %.lcssa, 1
  %27 = select i1 %.1.lcssa, i1 %26, i1 false
  %28 = select i1 %27, ptr @.str, ptr @.str.1
  br label %._crit_edge

._crit_edge:                                      ; preds = %0, %._crit_edge.loopexit, %.preheader
  %.016.lcssa = phi ptr [ @.str.1, %.preheader ], [ %28, %._crit_edge.loopexit ], [ @.str.1, %0 ]
  %29 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef nonnull %.016.lcssa)
  %30 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) %29, ptr noundef nonnull @.str.2)
  call void @llvm.stackrestore.p0(ptr %4)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #4
  ret void

.lr.ph25:                                         ; preds = %.lr.ph25, %.lr.ph25.preheader.new
  %.01524 = phi i64 [ 0, %.lr.ph25.preheader.new ], [ %42, %.lr.ph25 ]
  %.01623 = phi i1 [ true, %.lr.ph25.preheader.new ], [ %.1.1, %.lr.ph25 ]
  %.01722 = phi i64 [ 0, %.lr.ph25.preheader.new ], [ %53, %.lr.ph25 ]
  %niter = phi i64 [ 0, %.lr.ph25.preheader.new ], [ %niter.next.1, %.lr.ph25 ]
  %31 = or disjoint i64 %.01524, 1
  %32 = icmp eq i64 %31, %13
  %iv.rem = select i1 %32, i64 0, i64 %31
  %33 = getelementptr inbounds i64, ptr %5, i64 %iv.rem
  %34 = load i64, ptr %33, align 8, !tbaa !5
  %35 = getelementptr inbounds i64, ptr %5, i64 %.01524
  %36 = load i64, ptr %35, align 16, !tbaa !5
  %37 = icmp sle i64 %34, %36
  %38 = add nsw i64 %36, 1
  %.not = icmp eq i64 %34, %38
  %or.cond = select i1 %37, i1 true, i1 %.not
  %39 = icmp eq i64 %36, 1
  %40 = zext i1 %39 to i64
  %41 = add nuw nsw i64 %.01722, %40
  %42 = add nuw nsw i64 %.01524, 2
  %43 = icmp eq i64 %42, %13
  %iv.rem.1 = select i1 %43, i64 0, i64 %42
  %44 = getelementptr inbounds i64, ptr %5, i64 %iv.rem.1
  %45 = load i64, ptr %44, align 16, !tbaa !5
  %46 = getelementptr inbounds i64, ptr %5, i64 %31
  %47 = load i64, ptr %46, align 8, !tbaa !5
  %48 = icmp sle i64 %45, %47
  %49 = add nsw i64 %47, 1
  %.not.1 = icmp eq i64 %45, %49
  %or.cond.1 = select i1 %48, i1 true, i1 %.not.1
  %50 = select i1 %or.cond.1, i1 %or.cond, i1 false
  %.1.1 = select i1 %50, i1 %.01623, i1 false
  %51 = icmp eq i64 %47, 1
  %52 = zext i1 %51 to i64
  %53 = add nuw nsw i64 %41, %52
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph25, !llvm.loop !11
}

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

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

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

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #3 = { inlinehint 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 #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, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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