; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/WF/Medium Hard/FNCS/dreamoon.ll (Z4get2i)

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

@seg_sum2 = external dso_local global [201 x i64], align 16
@b = external dso_local global [100500 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4get2i(i32 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  %5 = load i32, ptr %3, align 4, !tbaa !5
  %6 = icmp slt i32 %5, 0
  br i1 %6, label %7, label %8

7:                                                ; preds = %1
  store i64 0, ptr %2, align 8
  br label %37

8:                                                ; preds = %1
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 0, ptr %4, align 8, !tbaa !9
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = sdiv i32 %11, 500
  %13 = icmp sgt i32 %12, 0
  br i1 %13, label %14, label %23

14:                                               ; preds = %8
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sdiv i32 %15, 500
  %17 = sub nsw i32 %16, 1
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [201 x i64], ptr @seg_sum2, i64 0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !9
  %21 = load i64, ptr %4, align 8, !tbaa !9
  %22 = add nsw i64 %21, %20
  store i64 %22, ptr %4, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %14, %8
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = srem i32 %24, 500
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %35

27:                                               ; preds = %23
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = sub nsw i32 %28, 1
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [100500 x i64], ptr @b, i64 0, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !9
  %33 = load i64, ptr %4, align 8, !tbaa !9
  %34 = add nsw i64 %33, %32
  store i64 %34, ptr %4, align 8, !tbaa !9
  br label %35

35:                                               ; preds = %27, %23
  %36 = load i64, ptr %4, align 8, !tbaa !9
  store i64 %36, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %37

37:                                               ; preds = %35, %7
  %38 = load i64, ptr %2, align 8
  ret i64 %38
}

; 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

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 = { 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 = !{!"long long", !7, i64 0}


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

@seg_sum2 = external dso_local local_unnamed_addr global [201 x i64], align 16
@b = external dso_local local_unnamed_addr global [100500 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z4get2i(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i32 %0, 0
  br i1 %2, label %18, label %3

3:                                                ; preds = %1
  %4 = add nuw nsw i32 %0, 1
  %.not = icmp ult i32 %0, 499
  br i1 %.not, label %11, label %5

5:                                                ; preds = %3
  %6 = udiv i32 %4, 500
  %7 = add nsw i32 %6, -1
  %8 = zext nneg i32 %7 to i64
  %9 = getelementptr inbounds [201 x i64], ptr @seg_sum2, i64 0, i64 %8
  %10 = load i64, ptr %9, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %5, %3
  %.0 = phi i64 [ %10, %5 ], [ 0, %3 ]
  %12 = urem i32 %4, 500
  %.not13 = icmp eq i32 %12, 0
  br i1 %.not13, label %18, label %13

13:                                               ; preds = %11
  %14 = zext nneg i32 %0 to i64
  %15 = getelementptr inbounds [100500 x i64], ptr @b, i64 0, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = add nsw i64 %16, %.0
  br label %18

18:                                               ; preds = %11, %13, %1
  %.010 = phi i64 [ 0, %1 ], [ %17, %13 ], [ %.0, %11 ]
  ret i64 %.010
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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" }

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass