; LLVM IR from kazunetakahashi/atcoder :: 2018/0415_s8pc-5/F.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"

@Q = external dso_local global i32, align 4
@a = external dso_local global [50 x i32], align 16
@l = external dso_local global [50 x i32], align 16
@r = external dso_local global [50 x i32], align 16
@used = external dso_local global [50 x i8], align 16

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

6:                                                ; preds = %51, %0
  %7 = load i32, ptr %2, align 4, !tbaa !9
  %8 = load i32, ptr @Q, align 4, !tbaa !9
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %6
  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
  %12 = load i32, ptr %2, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [50 x i32], ptr @l, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !9
  store i32 %15, ptr %5, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %43, %11
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = load i32, ptr %2, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [50 x i32], ptr @r, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = icmp sle i32 %17, %21
  br i1 %22, label %24, label %23

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

24:                                               ; preds = %16
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [50 x i8], ptr @used, i64 0, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !11, !range !13, !noundef !14
  %29 = trunc i8 %28 to i1
  br i1 %29, label %42, label %30

30:                                               ; preds = %24
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [50 x i32], ptr @a, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = load i32, ptr %4, align 4, !tbaa !9
  %36 = icmp sgt i32 %34, %35
  br i1 %36, label %37, label %42

37:                                               ; preds = %30
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [50 x i32], ptr @a, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !9
  store i32 %41, ptr %4, align 4, !tbaa !9
  br label %42

42:                                               ; preds = %37, %30, %24
  br label %43

43:                                               ; preds = %42
  %44 = load i32, ptr %5, align 4, !tbaa !9
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %5, align 4, !tbaa !9
  br label %16, !llvm.loop !15

46:                                               ; preds = %23
  %47 = load i32, ptr %4, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = load i64, ptr %1, align 8, !tbaa !5
  %50 = add nsw i64 %49, %48
  store i64 %50, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %51

51:                                               ; preds = %46
  %52 = load i32, ptr %2, align 4, !tbaa !9
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %2, align 4, !tbaa !9
  br label %6, !llvm.loop !17

54:                                               ; preds = %10
  %55 = load i64, ptr %1, align 8, !tbaa !5
  store i32 1, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret i64 %55
}

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


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

@Q = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local local_unnamed_addr global [50 x i32], align 16
@l = external dso_local local_unnamed_addr global [50 x i32], align 16
@r = external dso_local local_unnamed_addr global [50 x i32], align 16
@used = external dso_local local_unnamed_addr global [50 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z5solvev() local_unnamed_addr #0 {
  %1 = load i32, ptr @Q, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph23.preheader, label %._crit_edge24

.lr.ph23.preheader:                               ; preds = %0
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %.lr.ph23

._crit_edge24:                                    ; preds = %._crit_edge, %0
  %.015.lcssa = phi i64 [ 0, %0 ], [ %18, %._crit_edge ]
  ret i64 %.015.lcssa

.lr.ph23:                                         ; preds = %.lr.ph23.preheader, %._crit_edge
  %indvars.iv27 = phi i64 [ 0, %.lr.ph23.preheader ], [ %indvars.iv.next28, %._crit_edge ]
  %.01520 = phi i64 [ 0, %.lr.ph23.preheader ], [ %18, %._crit_edge ]
  %3 = getelementptr inbounds [50 x i32], ptr @l, i64 0, i64 %indvars.iv27
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = getelementptr inbounds [50 x i32], ptr @r, i64 0, i64 %indvars.iv27
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.not17 = icmp sgt i32 %4, %6
  br i1 %.not17, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.lr.ph23
  %7 = sext i32 %4 to i64
  %8 = add i32 %6, 1
  %9 = sub i32 %8, %4
  %xtraiter = and i32 %9, 1
  %10 = icmp eq i32 %6, %4
  br i1 %10, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i32 %9, -2
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %31, %.lr.ph.preheader
  %.1.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.1.1, %31 ]
  %indvars.iv.unr = phi i64 [ %7, %.lr.ph.preheader ], [ %indvars.iv.next.1, %31 ]
  %.01318.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %.1.1, %31 ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %11 = getelementptr inbounds [50 x i8], ptr @used, i64 0, i64 %indvars.iv.unr
  %12 = load i8, ptr %11, align 1, !tbaa !9, !range !11, !noundef !12
  %13 = trunc nuw i8 %12 to i1
  br i1 %13, label %._crit_edge, label %14

14:                                               ; preds = %.lr.ph.epil
  %15 = getelementptr inbounds [50 x i32], ptr @a, i64 0, i64 %indvars.iv.unr
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %spec.select.epil = tail call i32 @llvm.smax.i32(i32 %16, i32 %.01318.unr)
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %14, %.lr.ph.epil, %.lr.ph23
  %.013.lcssa = phi i32 [ 0, %.lr.ph23 ], [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.01318.unr, %.lr.ph.epil ], [ %spec.select.epil, %14 ]
  %17 = zext nneg i32 %.013.lcssa to i64
  %18 = add nuw nsw i64 %.01520, %17
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %exitcond30.not = icmp eq i64 %indvars.iv.next28, %wide.trip.count
  br i1 %exitcond30.not, label %._crit_edge24, label %.lr.ph23, !llvm.loop !13

.lr.ph:                                           ; preds = %31, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ %7, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %31 ]
  %.01318 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %.1.1, %31 ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %31 ]
  %19 = getelementptr inbounds [50 x i8], ptr @used, i64 0, i64 %indvars.iv
  %20 = load i8, ptr %19, align 1, !tbaa !9, !range !11, !noundef !12
  %21 = trunc nuw i8 %20 to i1
  br i1 %21, label %.lr.ph.1, label %22

22:                                               ; preds = %.lr.ph
  %23 = getelementptr inbounds [50 x i32], ptr @a, i64 0, i64 %indvars.iv
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %spec.select = tail call i32 @llvm.smax.i32(i32 %24, i32 %.01318)
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %22, %.lr.ph
  %.1 = phi i32 [ %.01318, %.lr.ph ], [ %spec.select, %22 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %25 = getelementptr inbounds [50 x i8], ptr @used, i64 0, i64 %indvars.iv.next
  %26 = load i8, ptr %25, align 1, !tbaa !9, !range !11, !noundef !12
  %27 = trunc nuw i8 %26 to i1
  br i1 %27, label %31, label %28

28:                                               ; preds = %.lr.ph.1
  %29 = getelementptr inbounds [50 x i32], ptr @a, i64 0, i64 %indvars.iv.next
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %spec.select.1 = tail call i32 @llvm.smax.i32(i32 %30, i32 %.1)
  br label %31

31:                                               ; preds = %28, %.lr.ph.1
  %.1.1 = phi i32 [ %.1, %.lr.ph.1 ], [ %spec.select.1, %28 ]
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, 2
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !15
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

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


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