; LLVM IR from andreimaximov/algorithms :: codeforces/mister-b-and-pr-shifts/main.ll (Z9referencev)

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

%struct.Shift = type { i64, i32 }

@N = external dso_local global i32, align 4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z3devi(i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local { i64, i32 } @_Z9referencev() #1 {
  %1 = alloca %struct.Shift, align 8
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = getelementptr inbounds %struct.Shift, ptr %1, i32 0, i32 0
  %5 = call noundef i64 @_Z3devi(i32 noundef 0)
  store i64 %5, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.Shift, ptr %1, i32 0, i32 1
  store i32 0, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 1, ptr %2, align 4, !tbaa !12
  br label %7

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

11:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  br label %28

12:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %13 = load i32, ptr %2, align 4, !tbaa !12
  %14 = call noundef i64 @_Z3devi(i32 noundef %13)
  store i64 %14, ptr %3, align 8, !tbaa !13
  %15 = load i64, ptr %3, align 8, !tbaa !13
  %16 = getelementptr inbounds %struct.Shift, ptr %1, i32 0, i32 0
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = icmp slt i64 %15, %17
  br i1 %18, label %19, label %24

19:                                               ; preds = %12
  %20 = load i64, ptr %3, align 8, !tbaa !13
  %21 = getelementptr inbounds %struct.Shift, ptr %1, i32 0, i32 0
  store i64 %20, ptr %21, align 8, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !12
  %23 = getelementptr inbounds %struct.Shift, ptr %1, i32 0, i32 1
  store i32 %22, ptr %23, align 8, !tbaa !11
  br label %24

24:                                               ; preds = %19, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %25

25:                                               ; preds = %24
  %26 = load i32, ptr %2, align 4, !tbaa !12
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %2, align 4, !tbaa !12
  br label %7, !llvm.loop !14

28:                                               ; preds = %11
  %29 = load { i64, i32 }, ptr %1, align 8
  ret { i64, i32 } %29
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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" }
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, !7, i64 0}
!6 = !{!"_ZTS5Shift", !7, i64 0, !10, i64 8}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!10, !10, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z3devi(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local { i64, i32 } @_Z9referencev() local_unnamed_addr #0 {
  %1 = tail call noundef i64 @_Z3devi(i32 noundef 0)
  %2 = load i32, ptr @N, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 1
  br i1 %3, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %0
  %.sroa.0.0.lcssa = phi i64 [ %1, %0 ], [ %spec.select, %.lr.ph ]
  %.sroa.4.0.lcssa = phi i32 [ 0, %0 ], [ %spec.select8, %.lr.ph ]
  %.fca.0.insert = insertvalue { i64, i32 } poison, i64 %.sroa.0.0.lcssa, 0
  %.fca.1.insert = insertvalue { i64, i32 } %.fca.0.insert, i32 %.sroa.4.0.lcssa, 1
  ret { i64, i32 } %.fca.1.insert

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.sroa.4.011 = phi i32 [ %spec.select8, %.lr.ph ], [ 0, %0 ]
  %.010 = phi i32 [ %6, %.lr.ph ], [ 1, %0 ]
  %.sroa.0.09 = phi i64 [ %spec.select, %.lr.ph ], [ %1, %0 ]
  %4 = tail call noundef i64 @_Z3devi(i32 noundef %.010)
  %5 = icmp slt i64 %4, %.sroa.0.09
  %spec.select = tail call i64 @llvm.smin.i64(i64 %4, i64 %.sroa.0.09)
  %spec.select8 = select i1 %5, i32 %.010, i32 %.sroa.4.011
  %6 = add nuw nsw i32 %.010, 1
  %7 = load i32, ptr @N, align 4, !tbaa !5
  %8 = icmp slt i32 %6, %7
  br i1 %8, label %.lr.ph, label %._crit_edge, !llvm.loop !9
}

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

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


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass