; LLVM IR from Dev-XYS/Algorithms :: Sprague-Grundy.ll (Z7init_sgv)

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

@k = external dso_local global i32, align 4
@s = external dso_local global [100 x i32], align 16
@sg = external dso_local global [10001 x i32], align 16
@has = external dso_local global [10001 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z7init_sgv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 0, ptr @sg, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 1, ptr %1, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %58, %0
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = icmp sle i32 %6, 10000
  br i1 %7, label %9, label %8

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

9:                                                ; preds = %5
  call void @llvm.memset.p0.i64(ptr align 16 @has, i8 0, i64 10001, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %36, %9
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = load i32, ptr @k, align 4, !tbaa !5
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  store i32 5, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %39

15:                                               ; preds = %10
  %16 = load i32, ptr %1, align 4, !tbaa !5
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [100 x i32], ptr @s, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sub nsw i32 %16, %20
  %22 = icmp sge i32 %21, 0
  br i1 %22, label %23, label %35

23:                                               ; preds = %15
  %24 = load i32, ptr %1, align 4, !tbaa !5
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100 x i32], ptr @s, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = sub nsw i32 %24, %28
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [10001 x i32], ptr @sg, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [10001 x i8], ptr @has, i64 0, i64 %33
  store i8 1, ptr %34, align 1, !tbaa !9
  br label %35

35:                                               ; preds = %23, %15
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %3, align 4, !tbaa !5
  br label %10, !llvm.loop !11

39:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %54, %39
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [10001 x i8], ptr @has, i64 0, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !9, !range !13, !noundef !14
  %45 = trunc i8 %44 to i1
  %46 = zext i1 %45 to i32
  %47 = icmp eq i32 %46, 0
  br i1 %47, label %48, label %53

48:                                               ; preds = %40
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = load i32, ptr %1, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [10001 x i32], ptr @sg, i64 0, i64 %51
  store i32 %49, ptr %52, align 4, !tbaa !5
  store i32 8, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %57

53:                                               ; preds = %40
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %4, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %4, align 4, !tbaa !5
  br label %40, !llvm.loop !15

57:                                               ; preds = %48
  br label %58

58:                                               ; preds = %57
  %59 = load i32, ptr %1, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %1, align 4, !tbaa !5
  br label %5, !llvm.loop !16

61:                                               ; preds = %8
  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 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.end.p0(i64 immarg, ptr nocapture) #1

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

@k = external dso_local local_unnamed_addr global i32, align 4
@s = external dso_local local_unnamed_addr global [100 x i32], align 16
@sg = external dso_local local_unnamed_addr global [10001 x i32], align 16
@has = external dso_local local_unnamed_addr global [10001 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z7init_sgv() local_unnamed_addr #0 {
  store i32 0, ptr @sg, align 16, !tbaa !5
  %1 = load i32, ptr @k, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph.us.preheader, label %.preheader

.lr.ph.us.preheader:                              ; preds = %0
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %3 = icmp eq i32 %1, 1
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %8
  %indvars.iv31 = phi i64 [ 1, %.lr.ph.us.preheader ], [ %indvars.iv.next32, %8 ]
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(10001) @has, i8 0, i64 10001, i1 false)
  %4 = trunc nuw nsw i64 %indvars.iv31 to i32
  br i1 %3, label %..preheader_crit_edge.us.preheader.unr-lcssa, label %.lr.ph.us.new

..preheader_crit_edge.us:                         ; preds = %..preheader_crit_edge.us.preheader, %..preheader_crit_edge.us
  %indvars.iv28 = phi i64 [ %indvars.iv.next29, %..preheader_crit_edge.us ], [ 0, %..preheader_crit_edge.us.preheader ]
  %5 = getelementptr inbounds [10001 x i8], ptr @has, i64 0, i64 %indvars.iv28
  %6 = load i8, ptr %5, align 1, !tbaa !9, !range !11, !noundef !12
  %7 = icmp eq i8 %6, 0
  %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1
  br i1 %7, label %8, label %..preheader_crit_edge.us, !llvm.loop !13

8:                                                ; preds = %..preheader_crit_edge.us
  %9 = trunc nuw nsw i64 %indvars.iv28 to i32
  %10 = getelementptr inbounds [10001 x i32], ptr @sg, i64 0, i64 %indvars.iv31
  store i32 %9, ptr %10, align 4, !tbaa !5
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %exitcond34.not = icmp eq i64 %indvars.iv.next32, 10001
  br i1 %exitcond34.not, label %.split18.us, label %.lr.ph.us, !llvm.loop !15

.lr.ph.us.new:                                    ; preds = %.lr.ph.us, %32
  %indvars.iv24 = phi i64 [ %indvars.iv.next25.1, %32 ], [ 0, %.lr.ph.us ]
  %niter = phi i64 [ %niter.next.1, %32 ], [ 0, %.lr.ph.us ]
  %11 = getelementptr inbounds [100 x i32], ptr @s, i64 0, i64 %indvars.iv24
  %12 = load i32, ptr %11, align 8, !tbaa !5
  %13 = sub nsw i32 %4, %12
  %14 = icmp sgt i32 %13, -1
  br i1 %14, label %15, label %21

15:                                               ; preds = %.lr.ph.us.new
  %16 = zext nneg i32 %13 to i64
  %17 = getelementptr inbounds [10001 x i32], ptr @sg, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [10001 x i8], ptr @has, i64 0, i64 %19
  store i8 1, ptr %20, align 1, !tbaa !9
  br label %21

21:                                               ; preds = %15, %.lr.ph.us.new
  %indvars.iv.next25 = or disjoint i64 %indvars.iv24, 1
  %22 = getelementptr inbounds [100 x i32], ptr @s, i64 0, i64 %indvars.iv.next25
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = sub nsw i32 %4, %23
  %25 = icmp sgt i32 %24, -1
  br i1 %25, label %26, label %32

26:                                               ; preds = %21
  %27 = zext nneg i32 %24 to i64
  %28 = getelementptr inbounds [10001 x i32], ptr @sg, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [10001 x i8], ptr @has, i64 0, i64 %30
  store i8 1, ptr %31, align 1, !tbaa !9
  br label %32

32:                                               ; preds = %26, %21
  %indvars.iv.next25.1 = add nuw nsw i64 %indvars.iv24, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %..preheader_crit_edge.us.preheader.unr-lcssa, label %.lr.ph.us.new, !llvm.loop !16

..preheader_crit_edge.us.preheader.unr-lcssa:     ; preds = %32, %.lr.ph.us
  %indvars.iv24.unr = phi i64 [ 0, %.lr.ph.us ], [ %indvars.iv.next25.1, %32 ]
  br i1 %lcmp.mod.not, label %..preheader_crit_edge.us.preheader, label %.epil.preheader

.epil.preheader:                                  ; preds = %..preheader_crit_edge.us.preheader.unr-lcssa
  %33 = getelementptr inbounds [100 x i32], ptr @s, i64 0, i64 %indvars.iv24.unr
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = sub nsw i32 %4, %34
  %36 = icmp sgt i32 %35, -1
  br i1 %36, label %37, label %..preheader_crit_edge.us.preheader

37:                                               ; preds = %.epil.preheader
  %38 = zext nneg i32 %35 to i64
  %39 = getelementptr inbounds [10001 x i32], ptr @sg, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [10001 x i8], ptr @has, i64 0, i64 %41
  store i8 1, ptr %42, align 1, !tbaa !9
  br label %..preheader_crit_edge.us.preheader

..preheader_crit_edge.us.preheader:               ; preds = %.epil.preheader, %37, %..preheader_crit_edge.us.preheader.unr-lcssa
  br label %..preheader_crit_edge.us

.split18.us:                                      ; preds = %53, %8
  ret void

.preheader:                                       ; preds = %0, %53
  %indvars.iv21 = phi i64 [ %indvars.iv.next22.1, %53 ], [ 1, %0 ]
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(10001) @has, i8 0, i64 10001, i1 false)
  br label %43

43:                                               ; preds = %43, %.preheader
  %indvars.iv = phi i64 [ %indvars.iv.next, %43 ], [ 0, %.preheader ]
  %44 = getelementptr inbounds [10001 x i8], ptr @has, i64 0, i64 %indvars.iv
  %45 = load i8, ptr %44, align 1, !tbaa !9, !range !11, !noundef !12
  %46 = icmp eq i8 %45, 0
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  br i1 %46, label %.preheader.1, label %43, !llvm.loop !13

.preheader.1:                                     ; preds = %43
  %47 = trunc nuw nsw i64 %indvars.iv to i32
  %48 = getelementptr inbounds [10001 x i32], ptr @sg, i64 0, i64 %indvars.iv21
  store i32 %47, ptr %48, align 4, !tbaa !5
  %indvars.iv.next22 = add nuw nsw i64 %indvars.iv21, 1
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(10001) @has, i8 0, i64 10001, i1 false)
  br label %49

49:                                               ; preds = %49, %.preheader.1
  %indvars.iv.1 = phi i64 [ %indvars.iv.next.1, %49 ], [ 0, %.preheader.1 ]
  %50 = getelementptr inbounds [10001 x i8], ptr @has, i64 0, i64 %indvars.iv.1
  %51 = load i8, ptr %50, align 1, !tbaa !9, !range !11, !noundef !12
  %52 = icmp eq i8 %51, 0
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv.1, 1
  br i1 %52, label %53, label %49, !llvm.loop !13

53:                                               ; preds = %49
  %54 = trunc nuw nsw i64 %indvars.iv.1 to i32
  %55 = getelementptr inbounds [10001 x i32], ptr @sg, i64 0, i64 %indvars.iv.next22
  store i32 %54, ptr %55, align 4, !tbaa !5
  %indvars.iv.next22.1 = add nuw nsw i64 %indvars.iv21, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next22.1, 10001
  br i1 %exitcond.not.1, label %.split18.us, label %.preheader, !llvm.loop !15
}

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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) }

!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 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


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