; LLVM IR from Izaron/ACM-ICPC :: Russian Olympiad in Informatics/Final Stage 2016/fisher.ll (Z9getMinPosiiix)

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

@tree = external dso_local global [1280020 x i64], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z9getMinPosiiix(i32 noundef %0, i32 noundef %1, i32 noundef %2, i64 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  %10 = load i32, ptr %7, align 4, !tbaa !5
  %11 = load i32, ptr %8, align 4, !tbaa !5
  %12 = icmp eq i32 %10, %11
  br i1 %12, label %13, label %15

13:                                               ; preds = %4
  %14 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %14, ptr %5, align 4
  br label %51

15:                                               ; preds = %4
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = mul nsw i32 %16, 2
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [1280020 x i64], ptr @tree, i64 0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !9
  %21 = load i64, ptr %9, align 8, !tbaa !9
  %22 = icmp sge i64 %20, %21
  br i1 %22, label %23, label %33

23:                                               ; preds = %15
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = mul nsw i32 %24, 2
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = load i32, ptr %7, align 4, !tbaa !5
  %28 = load i32, ptr %8, align 4, !tbaa !5
  %29 = add nsw i32 %27, %28
  %30 = sdiv i32 %29, 2
  %31 = load i64, ptr %9, align 8, !tbaa !9
  %32 = call noundef i32 @_Z9getMinPosiiix(i32 noundef %25, i32 noundef %26, i32 noundef %30, i64 noundef %31)
  store i32 %32, ptr %5, align 4
  br label %51

33:                                               ; preds = %15
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = mul nsw i32 %34, 2
  %36 = add nsw i32 %35, 1
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = load i32, ptr %8, align 4, !tbaa !5
  %39 = add nsw i32 %37, %38
  %40 = sdiv i32 %39, 2
  %41 = add nsw i32 %40, 1
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = load i64, ptr %9, align 8, !tbaa !9
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = mul nsw i32 %44, 2
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1280020 x i64], ptr @tree, i64 0, i64 %46
  %48 = load i64, ptr %47, align 8, !tbaa !9
  %49 = sub nsw i64 %43, %48
  %50 = call noundef i32 @_Z9getMinPosiiix(i32 noundef %36, i32 noundef %41, i32 noundef %42, i64 noundef %49)
  store i32 %50, ptr %5, align 4
  br label %51

51:                                               ; preds = %33, %23, %13
  %52 = load i32, ptr %5, align 4
  ret i32 %52
}

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

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

@tree = external dso_local local_unnamed_addr global [1280020 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z9getMinPosiiix(i32 noundef %0, i32 noundef %1, i32 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq i32 %1, %2
  br i1 %5, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %tailrecurse.outer
  %.tr24.ph33 = phi i64 [ %18, %tailrecurse.outer ], [ %3, %4 ]
  %.tr23.ph32 = phi i32 [ %.tr2328, %tailrecurse.outer ], [ %2, %4 ]
  %.tr22.ph31 = phi i32 [ %17, %tailrecurse.outer ], [ %1, %4 ]
  %.tr.ph30 = phi i32 [ %14, %tailrecurse.outer ], [ %0, %4 ]
  br label %6

6:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr2328 = phi i32 [ %.tr23.ph32, %.lr.ph ], [ %12, %tailrecurse ]
  %.tr27 = phi i32 [ %.tr.ph30, %.lr.ph ], [ %7, %tailrecurse ]
  %7 = shl nsw i32 %.tr27, 1
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [1280020 x i64], ptr @tree, i64 0, i64 %8
  %10 = load i64, ptr %9, align 16, !tbaa !5
  %.not = icmp slt i64 %10, %.tr24.ph33
  br i1 %.not, label %tailrecurse.outer, label %tailrecurse

tailrecurse:                                      ; preds = %6
  %11 = add nsw i32 %.tr2328, %.tr22.ph31
  %12 = sdiv i32 %11, 2
  %13 = icmp eq i32 %.tr22.ph31, %12
  br i1 %13, label %tailrecurse.outer._crit_edge, label %6

tailrecurse.outer:                                ; preds = %6
  %14 = or disjoint i32 %7, 1
  %15 = add nsw i32 %.tr2328, %.tr22.ph31
  %16 = sdiv i32 %15, 2
  %17 = add nsw i32 %16, 1
  %18 = sub nsw i64 %.tr24.ph33, %10
  %19 = icmp eq i32 %17, %.tr2328
  br i1 %19, label %tailrecurse.outer._crit_edge, label %.lr.ph

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse, %4
  %.tr22.ph.lcssa = phi i32 [ %1, %4 ], [ %.tr22.ph31, %tailrecurse ], [ %.tr2328, %tailrecurse.outer ]
  ret i32 %.tr22.ph.lcssa
}

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,GVNPass,GlobalOptPass,InferAlignmentPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass