; LLVM IR from viveakrt/Competitive-programing :: SPOJ/qtree3.ll (Z7queryupi)

; 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.node = type { i32 }

@n = external dso_local global i32, align 4
@in = external dso_local global [100001 x i32], align 16
@nxt = external dso_local global [100001 x i32], align 16
@dp = external dso_local global [20 x [100001 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 uwtable
declare dso_local i32 @_Z4readiiiii(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z7queryupi(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca %struct.node, align 4
  %7 = alloca i32, align 4
  %8 = alloca %struct.node, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 1, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 -1, ptr %4, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %60, %1
  br label %10

10:                                               ; preds = %9
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100001 x i32], ptr @nxt, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [100001 x i32], ptr @nxt, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp eq i32 %14, %18
  br i1 %19, label %20, label %39

20:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %21 = load i32, ptr @n, align 4, !tbaa !5
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [100001 x i32], ptr @in, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [100001 x i32], ptr @in, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = call i32 @_Z4readiiiii(i32 noundef 1, i32 noundef %21, i32 noundef 1, i32 noundef %25, i32 noundef %29)
  %31 = getelementptr inbounds %struct.node, ptr %6, i32 0, i32 0
  store i32 %30, ptr %31, align 4
  %32 = getelementptr inbounds %struct.node, ptr %6, i32 0, i32 0
  %33 = load i32, ptr %32, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  store i32 %33, ptr %5, align 4, !tbaa !5
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %38

36:                                               ; preds = %20
  %37 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %37, ptr %4, align 4, !tbaa !5
  br label %38

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

39:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %40 = load i32, ptr @n, align 4, !tbaa !5
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [100001 x i32], ptr @nxt, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [100001 x i32], ptr @in, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = load i32, ptr %2, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [100001 x i32], ptr @in, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = call i32 @_Z4readiiiii(i32 noundef 1, i32 noundef %40, i32 noundef 1, i32 noundef %47, i32 noundef %51)
  %53 = getelementptr inbounds %struct.node, ptr %8, i32 0, i32 0
  store i32 %52, ptr %53, align 4
  %54 = getelementptr inbounds %struct.node, ptr %8, i32 0, i32 0
  %55 = load i32, ptr %54, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  store i32 %55, ptr %7, align 4, !tbaa !5
  %56 = load i32, ptr %7, align 4, !tbaa !5
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %60

58:                                               ; preds = %39
  %59 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %59, ptr %4, align 4, !tbaa !5
  br label %60

60:                                               ; preds = %58, %39
  %61 = load i32, ptr %2, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [100001 x i32], ptr @nxt, i64 0, i64 %62
  %64 = load i32, ptr %63, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [100001 x i32], ptr @dp, i64 0, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !5
  store i32 %67, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %9, !llvm.loop !11

68:                                               ; preds = %38
  %69 = load i32, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %69
}

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

@n = external dso_local local_unnamed_addr global i32, align 4
@in = external dso_local local_unnamed_addr global [100001 x i32], align 16
@nxt = external dso_local local_unnamed_addr global [100001 x i32], align 16
@dp = external dso_local local_unnamed_addr global [20 x [100001 x i32]], align 16

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

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 1, 0) i32 @_Z7queryupi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [100001 x i32], ptr @nxt, i64 0, i64 %2
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = load i32, ptr getelementptr inbounds (i8, ptr @nxt, i64 4), align 4, !tbaa !5
  %6 = icmp eq i32 %4, %5
  br i1 %6, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %1
  %.013.lcssa = phi i32 [ -1, %1 ], [ %spec.select18, %.lr.ph ]
  %.lcssa = phi i64 [ %2, %1 ], [ %26, %.lr.ph ]
  %7 = load i32, ptr @n, align 4, !tbaa !5
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @in, i64 4), align 4, !tbaa !5
  %9 = getelementptr inbounds [100001 x i32], ptr @in, i64 0, i64 %.lcssa
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = tail call i32 @_Z4readiiiii(i32 noundef 1, i32 noundef %7, i32 noundef 1, i32 noundef %8, i32 noundef %10)
  %.not17 = icmp eq i32 %11, 0
  %spec.select = select i1 %.not17, i32 %.013.lcssa, i32 %11
  ret i32 %spec.select

.lr.ph:                                           ; preds = %1, %.lr.ph
  %12 = phi i32 [ %28, %.lr.ph ], [ %4, %1 ]
  %13 = phi ptr [ %27, %.lr.ph ], [ %3, %1 ]
  %14 = phi i64 [ %26, %.lr.ph ], [ %2, %1 ]
  %.01319 = phi i32 [ %spec.select18, %.lr.ph ], [ -1, %1 ]
  %15 = load i32, ptr @n, align 4, !tbaa !5
  %16 = sext i32 %12 to i64
  %17 = getelementptr inbounds [100001 x i32], ptr @in, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = getelementptr inbounds [100001 x i32], ptr @in, i64 0, i64 %14
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = tail call i32 @_Z4readiiiii(i32 noundef 1, i32 noundef %15, i32 noundef 1, i32 noundef %18, i32 noundef %20)
  %.not = icmp eq i32 %21, 0
  %spec.select18 = select i1 %.not, i32 %.01319, i32 %21
  %22 = load i32, ptr %13, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [100001 x i32], ptr @dp, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100001 x i32], ptr @nxt, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = load i32, ptr getelementptr inbounds (i8, ptr @nxt, i64 4), align 4, !tbaa !5
  %30 = icmp eq i32 %28, %29
  br i1 %30, label %._crit_edge, label %.lr.ph, !llvm.loop !9
}

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


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