; LLVM IR from kazunetakahashi/atcoder :: 2018/0701_ARC100/D.ll (Z5indexx)

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

@N = external dso_local global i32, align 4
@imos = external dso_local global [200010 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z5indexx(i64 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %8 = load i32, ptr @N, align 4, !tbaa !9
  %9 = sub nsw i32 %8, 1
  store i32 %9, ptr %5, align 4, !tbaa !9
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [200010 x i64], ptr @imos, i64 0, i64 %12
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = icmp slt i64 %10, %14
  br i1 %15, label %16, label %18

16:                                               ; preds = %1
  %17 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %17, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %42

18:                                               ; preds = %1
  br label %19

19:                                               ; preds = %39, %18
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = sub nsw i32 %20, %21
  %23 = icmp sgt i32 %22, 1
  br i1 %23, label %24, label %40

24:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = add nsw i32 %25, %26
  %28 = sdiv i32 %27, 2
  store i32 %28, ptr %7, align 4, !tbaa !9
  %29 = load i64, ptr %3, align 8, !tbaa !5
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [200010 x i64], ptr @imos, i64 0, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = icmp slt i64 %29, %33
  br i1 %34, label %35, label %37

35:                                               ; preds = %24
  %36 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %36, ptr %5, align 4, !tbaa !9
  br label %39

37:                                               ; preds = %24
  %38 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %38, ptr %4, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %37, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %19, !llvm.loop !11

40:                                               ; preds = %19
  %41 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %41, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %42

42:                                               ; preds = %40, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %43 = load i32, ptr %2, align 4
  ret i32 %43
}

; 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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

@N = external dso_local local_unnamed_addr global i32, align 4
@imos = external dso_local local_unnamed_addr global [200010 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef range(i32 -1073741824, 1073741824) i32 @_Z5indexx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @N, align 4, !tbaa !5
  %3 = load i64, ptr @imos, align 16, !tbaa !9
  %4 = icmp sle i64 %3, %0
  %5 = icmp sgt i32 %2, 2
  %or.cond = select i1 %4, i1 %5, i1 false
  br i1 %or.cond, label %.lr.ph.preheader, label %.loopexit

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

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.01317 = phi i32 [ %..013, %.lr.ph ], [ %6, %.lr.ph.preheader ]
  %.01416 = phi i32 [ %.014., %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %7 = add nsw i32 %.01317, %.01416
  %8 = sdiv i32 %7, 2
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [200010 x i64], ptr @imos, i64 0, i64 %9
  %11 = load i64, ptr %10, align 8, !tbaa !9
  %12 = icmp sgt i64 %11, %0
  %.014. = select i1 %12, i32 %.01416, i32 %8
  %..013 = select i1 %12, i32 %8, i32 %.01317
  %13 = sub nsw i32 %..013, %.014.
  %14 = icmp sgt i32 %13, 1
  br i1 %14, label %.lr.ph, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph, %1
  %.0 = phi i32 [ 0, %1 ], [ %.014., %.lr.ph ]
  ret i32 %.0
}

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


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