; LLVM IR from viveakrt/Competitive-programing :: SPOJ/xorx.ll (Z6updatei)

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

@cnt = external dso_local global i32, align 4
@tree = external dso_local global [262144 x [2 x i32]], align 16
@val = external dso_local global [262144 x i32], align 16

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

6:                                                ; preds = %52, %1
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = icmp sge i32 %7, 0
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %55

10:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = shl i32 1, %12
  %14 = and i32 %11, %13
  store i32 %14, ptr %5, align 4, !tbaa !5
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %10
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %17, %10
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [262144 x [2 x i32]], ptr @tree, i64 0, i64 %20
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [2 x i32], ptr %21, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %35

27:                                               ; preds = %18
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [262144 x [2 x i32]], ptr @tree, i64 0, i64 %29
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [2 x i32], ptr %30, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  store i32 %34, ptr %3, align 4, !tbaa !5
  br label %51

35:                                               ; preds = %18
  %36 = load i32, ptr @cnt, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr @cnt, align 4, !tbaa !5
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [262144 x [2 x i32]], ptr @tree, i64 0, i64 %39
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [2 x i32], ptr %40, i64 0, i64 %42
  store i32 %37, ptr %43, align 4, !tbaa !5
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [262144 x [2 x i32]], ptr @tree, i64 0, i64 %45
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [2 x i32], ptr %46, i64 0, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !5
  store i32 %50, ptr %3, align 4, !tbaa !5
  br label %51

51:                                               ; preds = %35, %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %52

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

55:                                               ; preds = %9
  %56 = load i32, ptr %2, align 4, !tbaa !5
  %57 = load i32, ptr %3, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [262144 x i32], ptr @val, i64 0, i64 %58
  store i32 %56, ptr %59, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret void
}

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

@cnt = external dso_local local_unnamed_addr global i32, align 4
@tree = external dso_local local_unnamed_addr global [262144 x [2 x i32]], align 16
@val = external dso_local local_unnamed_addr global [262144 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z6updatei(i32 noundef %0) local_unnamed_addr #0 {
  %cnt.promoted = load i32, ptr @cnt, align 4, !tbaa !5
  br label %5

2:                                                ; preds = %14
  %3 = sext i32 %.1 to i64
  %4 = getelementptr inbounds [262144 x i32], ptr @val, i64 0, i64 %3
  store i32 %0, ptr %4, align 4, !tbaa !5
  ret void

5:                                                ; preds = %25, %1
  %.018 = phi i32 [ 30, %1 ], [ %27, %25 ]
  %.01517 = phi i32 [ 0, %1 ], [ %.1.1, %25 ]
  %6 = phi i32 [ %cnt.promoted, %1 ], [ %26, %25 ]
  %7 = lshr i32 %0, %.018
  %spec.store.select = and i32 %7, 1
  %8 = sext i32 %.01517 to i64
  %9 = zext nneg i32 %spec.store.select to i64
  %10 = getelementptr inbounds [262144 x [2 x i32]], ptr @tree, i64 0, i64 %8, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %12, label %14

12:                                               ; preds = %5
  %13 = add nsw i32 %6, 1
  store i32 %13, ptr @cnt, align 4, !tbaa !5
  store i32 %13, ptr %10, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %5, %12
  %15 = phi i32 [ %13, %12 ], [ %6, %5 ]
  %.1 = phi i32 [ %13, %12 ], [ %11, %5 ]
  %.not19 = icmp eq i32 %.018, 0
  br i1 %.not19, label %2, label %16, !llvm.loop !9

16:                                               ; preds = %14
  %17 = add nsw i32 %.018, -1
  %18 = lshr i32 %0, %17
  %spec.store.select.1 = and i32 %18, 1
  %19 = sext i32 %.1 to i64
  %20 = zext nneg i32 %spec.store.select.1 to i64
  %21 = getelementptr inbounds [262144 x [2 x i32]], ptr @tree, i64 0, i64 %19, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %.not.1 = icmp eq i32 %22, 0
  br i1 %.not.1, label %23, label %25

23:                                               ; preds = %16
  %24 = add nsw i32 %15, 1
  store i32 %24, ptr @cnt, align 4, !tbaa !5
  store i32 %24, ptr %21, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %23, %16
  %26 = phi i32 [ %24, %23 ], [ %15, %16 ]
  %.1.1 = phi i32 [ %24, %23 ], [ %22, %16 ]
  %27 = add nsw i32 %.018, -2
  br label %5
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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