; LLVM IR from ravireddy07/cpp-book :: src/Code-Chef/CHFNSWAP_Chefina-and-Swap.ll (Z5Combixxx)

; 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: mustprogress uwtable
define dso_local noundef i64 @_Z5Combixxx(i64 noundef %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !5
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = add nsw i64 %12, 1
  %14 = call ptr @llvm.stacksave.p0()
  store ptr %14, ptr %7, align 8
  %15 = alloca i64, i64 %13, align 16
  store i64 %13, ptr %8, align 8
  %16 = mul nuw i64 8, %13
  call void @llvm.memset.p0.i64(ptr align 16 %15, i8 0, i64 %16, i1 false)
  %17 = getelementptr inbounds i64, ptr %15, i64 0
  store i64 1, ptr %17, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  store i64 0, ptr %9, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %47, %3
  %19 = load i64, ptr %9, align 8, !tbaa !5
  %20 = load i64, ptr %4, align 8, !tbaa !5
  %21 = icmp sle i64 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  br label %50

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %24 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %9, ptr noundef nonnull align 8 dereferenceable(8) %5)
  %25 = load i64, ptr %24, align 8, !tbaa !5
  store i64 %25, ptr %11, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %43, %23
  %27 = load i64, ptr %11, align 8, !tbaa !5
  %28 = icmp sgt i64 %27, 0
  br i1 %28, label %30, label %29

29:                                               ; preds = %26
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  br label %46

30:                                               ; preds = %26
  %31 = load i64, ptr %11, align 8, !tbaa !5
  %32 = getelementptr inbounds i64, ptr %15, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = load i64, ptr %11, align 8, !tbaa !5
  %35 = sub nsw i64 %34, 1
  %36 = getelementptr inbounds i64, ptr %15, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = add nsw i64 %33, %37
  %39 = load i64, ptr %6, align 8, !tbaa !5
  %40 = srem i64 %38, %39
  %41 = load i64, ptr %11, align 8, !tbaa !5
  %42 = getelementptr inbounds i64, ptr %15, i64 %41
  store i64 %40, ptr %42, align 8, !tbaa !5
  br label %43

43:                                               ; preds = %30
  %44 = load i64, ptr %11, align 8, !tbaa !5
  %45 = add nsw i64 %44, -1
  store i64 %45, ptr %11, align 8, !tbaa !5
  br label %26, !llvm.loop !9

46:                                               ; preds = %29
  br label %47

47:                                               ; preds = %46
  %48 = load i64, ptr %9, align 8, !tbaa !5
  %49 = add nsw i64 %48, 1
  store i64 %49, ptr %9, align 8, !tbaa !5
  br label %18, !llvm.loop !11

50:                                               ; preds = %22
  %51 = load i64, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds i64, ptr %15, i64 %51
  %53 = load i64, ptr %52, align 8, !tbaa !5
  store i32 1, ptr %10, align 4
  %54 = load ptr, ptr %7, align 8
  call void @llvm.stackrestore.p0(ptr %54)
  ret i64 %53
}

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

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

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #4

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #1

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 }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { inlinehint mustprogress 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 #5 = { 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 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}


; 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: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z5Combixxx(i64 noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  store i64 %1, ptr %4, align 8, !tbaa !5
  %6 = add nsw i64 %1, 1
  %7 = alloca i64, i64 %6, align 16
  %8 = shl nuw i64 %6, 3
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %7, i8 0, i64 %8, i1 false)
  store i64 1, ptr %7, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #4
  store i64 0, ptr %5, align 8, !tbaa !5
  %.not8 = icmp slt i64 %0, 0
  br i1 %.not8, label %._crit_edge11, label %.lr.ph10

._crit_edge11.loopexit:                           ; preds = %._crit_edge
  %.pre = load i64, ptr %4, align 8, !tbaa !5
  br label %._crit_edge11

._crit_edge11:                                    ; preds = %._crit_edge11.loopexit, %3
  %9 = phi i64 [ %.pre, %._crit_edge11.loopexit ], [ %1, %3 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #4
  %10 = getelementptr inbounds i64, ptr %7, i64 %9
  %11 = load i64, ptr %10, align 8, !tbaa !5
  ret i64 %11

.lr.ph10:                                         ; preds = %3, %._crit_edge
  %12 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %5, ptr noundef nonnull align 8 dereferenceable(8) %4)
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = icmp sgt i64 %13, 0
  br i1 %14, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.lr.ph10
  %xtraiter = and i64 %13, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader
  %15 = getelementptr inbounds i64, ptr %7, i64 %13
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = getelementptr i8, ptr %15, i64 -8
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = add nsw i64 %18, %16
  %20 = srem i64 %19, %2
  store i64 %20, ptr %15, align 8, !tbaa !5
  %21 = add nsw i64 %13, -1
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.07.unr = phi i64 [ %13, %.lr.ph.preheader ], [ %21, %.lr.ph.prol ]
  %22 = icmp eq i64 %13, 1
  br i1 %22, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %.lr.ph10
  %23 = load i64, ptr %5, align 8, !tbaa !5
  %24 = add nsw i64 %23, 1
  store i64 %24, ptr %5, align 8, !tbaa !5
  %.not.not = icmp slt i64 %23, %0
  br i1 %.not.not, label %.lr.ph10, label %._crit_edge11.loopexit, !llvm.loop !9

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.07 = phi i64 [ %37, %.lr.ph ], [ %.07.unr, %.lr.ph.prol.loopexit ]
  %25 = getelementptr inbounds i64, ptr %7, i64 %.07
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = getelementptr i8, ptr %25, i64 -8
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = add nsw i64 %28, %26
  %30 = srem i64 %29, %2
  store i64 %30, ptr %25, align 8, !tbaa !5
  %31 = add nsw i64 %.07, -1
  %32 = getelementptr inbounds i64, ptr %7, i64 %31
  %33 = getelementptr i8, ptr %32, i64 -8
  %34 = load i64, ptr %33, align 8, !tbaa !5
  %35 = add nsw i64 %34, %28
  %36 = srem i64 %35, %2
  store i64 %36, ptr %32, align 8, !tbaa !5
  %37 = add nsw i64 %.07, -2
  %38 = icmp ugt i64 %31, 1
  br i1 %38, label %.lr.ph, label %._crit_edge, !llvm.loop !11
}

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

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

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

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

attributes #0 = { mustprogress 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 nounwind willreturn memory(argmem: write) }
attributes #2 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { inlinehint mustprogress 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 #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 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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass