; LLVM IR from UtkarshPathrabe/Competitive-Coding :: SPOJ Solutions/NBIN.ll (Z4findx)

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

@pre_com = external dso_local global [100 x i64], align 16

; 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 nounwind uwtable
define dso_local noundef i32 @_Z4findx(i64 noundef %0) #1 {
  %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
  store i32 72, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %8 = load i32, ptr %4, align 4, !tbaa !9
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = add nsw i32 %8, %9
  %11 = ashr i32 %10, 1
  store i32 %11, ptr %6, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %51, %1
  br label %13

13:                                               ; preds = %12
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = add nsw i32 %14, %15
  %17 = ashr i32 %16, 1
  store i32 %17, ptr %6, align 4, !tbaa !9
  %18 = load i32, ptr %6, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [100 x i64], ptr @pre_com, i64 0, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = icmp slt i64 %21, %22
  br i1 %23, label %24, label %37

24:                                               ; preds = %13
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = add nsw i32 %25, 1
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [100 x i64], ptr @pre_com, i64 0, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = load i64, ptr %3, align 8, !tbaa !5
  %31 = icmp sgt i64 %29, %30
  br i1 %31, label %32, label %34

32:                                               ; preds = %24
  %33 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %33, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %52

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

36:                                               ; preds = %34
  br label %51

37:                                               ; preds = %13
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = sub nsw i32 %38, 1
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [100 x i64], ptr @pre_com, i64 0, i64 %40
  %42 = load i64, ptr %41, align 8, !tbaa !5
  %43 = load i64, ptr %3, align 8, !tbaa !5
  %44 = icmp slt i64 %42, %43
  br i1 %44, label %45, label %48

45:                                               ; preds = %37
  %46 = load i32, ptr %6, align 4, !tbaa !9
  %47 = sub nsw i32 %46, 1
  store i32 %47, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %52

48:                                               ; preds = %37
  %49 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %49, ptr %5, align 4, !tbaa !9
  br label %50

50:                                               ; preds = %48
  br label %51

51:                                               ; preds = %50, %36
  br label %12, !llvm.loop !11

52:                                               ; preds = %45, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %53 = load i32, ptr %2, align 4
  ret i32 %53
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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"

@pre_com = external dso_local local_unnamed_addr global [100 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z4findx(i64 noundef %0) local_unnamed_addr #0 {
  br label %.outer

.outer:                                           ; preds = %9, %1
  %.015.ph = phi i32 [ %4, %9 ], [ 0, %1 ]
  %.0.ph = phi i32 [ %.0, %9 ], [ 72, %1 ]
  br label %2

2:                                                ; preds = %.outer, %15
  %.0 = phi i32 [ %4, %15 ], [ %.0.ph, %.outer ]
  %3 = add nsw i32 %.0, %.015.ph
  %4 = ashr i32 %3, 1
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [100 x i64], ptr @pre_com, i64 0, i64 %5
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %8 = icmp slt i64 %7, %0
  br i1 %8, label %9, label %15

9:                                                ; preds = %2
  %10 = add nsw i32 %4, 1
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [100 x i64], ptr @pre_com, i64 0, i64 %11
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = icmp sgt i64 %13, %0
  br i1 %14, label %.loopexit, label %.outer, !llvm.loop !9

15:                                               ; preds = %2
  %16 = add nsw i32 %4, -1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [100 x i64], ptr @pre_com, i64 0, i64 %17
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %20 = icmp slt i64 %19, %0
  br i1 %20, label %.loopexit, label %2, !llvm.loop !9

.loopexit:                                        ; preds = %9, %15
  %.017 = phi i32 [ %16, %15 ], [ %4, %9 ]
  ret i32 %.017
}

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass