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

; 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 [262144 x [2 x i32]], align 16
@val = external dso_local global [262144 x i32], 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 @_Z5queryi(i32 noundef %0) #1 {
  %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 = %46, %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 %49

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 0, ptr %5, align 4, !tbaa !5
  br label %19

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

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

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

36:                                               ; preds = %19
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [262144 x [2 x i32]], ptr @tree, i64 0, i64 %38
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = xor i32 %40, 1
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [2 x i32], ptr %39, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  store i32 %44, ptr %3, align 4, !tbaa !5
  br label %45

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

46:                                               ; preds = %45
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = add nsw i32 %47, -1
  store i32 %48, ptr %4, align 4, !tbaa !5
  br label %6, !llvm.loop !9

49:                                               ; preds = %9
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [262144 x i32], ptr @val, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  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 = !{!"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"

@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 willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = xor i32 %0, -1
  br label %7

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

7:                                                ; preds = %31, %1
  %.01217 = phi i32 [ 30, %1 ], [ %32, %31 ]
  %.01316 = phi i32 [ 0, %1 ], [ %.1.1, %31 ]
  %8 = lshr i32 %2, %.01217
  %. = and i32 %8, 1
  %9 = sext i32 %.01316 to i64
  %10 = zext nneg i32 %. to i64
  %11 = getelementptr inbounds [262144 x [2 x i32]], ptr @tree, i64 0, i64 %9, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %.not15 = icmp eq i32 %12, 0
  br i1 %.not15, label %13, label %18

13:                                               ; preds = %7
  %14 = xor i32 %., 1
  %15 = zext nneg i32 %14 to i64
  %16 = getelementptr inbounds [262144 x [2 x i32]], ptr @tree, i64 0, i64 %9, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %7, %13
  %.1 = phi i32 [ %17, %13 ], [ %12, %7 ]
  %.not = icmp eq i32 %.01217, 0
  br i1 %.not, label %3, label %19, !llvm.loop !9

19:                                               ; preds = %18
  %20 = add nsw i32 %.01217, -1
  %21 = lshr i32 %2, %20
  %..1 = and i32 %21, 1
  %22 = sext i32 %.1 to i64
  %23 = zext nneg i32 %..1 to i64
  %24 = getelementptr inbounds [262144 x [2 x i32]], ptr @tree, i64 0, i64 %22, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %.not15.1 = icmp eq i32 %25, 0
  br i1 %.not15.1, label %26, label %31

26:                                               ; preds = %19
  %27 = xor i32 %..1, 1
  %28 = zext nneg i32 %27 to i64
  %29 = getelementptr inbounds [262144 x [2 x i32]], ptr @tree, i64 0, i64 %22, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %26, %19
  %.1.1 = phi i32 [ %30, %26 ], [ %25, %19 ]
  %32 = add nsw i32 %.01217, -2
  br label %7
}

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 = !{!"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,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass