; LLVM IR from tangjz/acm-icpc :: 51nod/1648.ll (Z4lasti)

; 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, [2 x i32], i32 }

@t = external dso_local global [100009 x %struct.Node], 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 @_Z4lasti(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 %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 2, ptr %4, align 4, !tbaa !5
  br label %6

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

9:                                                ; preds = %6
  store i32 2, ptr %5, align 4
  br label %68

10:                                               ; preds = %6
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100009 x %struct.Node], ptr @t, i64 0, i64 %12
  %14 = getelementptr inbounds %struct.Node, ptr %13, i32 0, i32 1
  %15 = getelementptr inbounds [2 x i32], ptr %14, i64 0, i64 0
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [100009 x %struct.Node], ptr @t, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.Node, ptr %18, i32 0, i32 2
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = add nsw i32 %20, 1
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = icmp eq i32 %21, %22
  br i1 %23, label %24, label %26

24:                                               ; preds = %10
  %25 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %25, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %68

26:                                               ; preds = %10
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [100009 x %struct.Node], ptr @t, i64 0, i64 %29
  %31 = getelementptr inbounds %struct.Node, ptr %30, i32 0, i32 1
  %32 = getelementptr inbounds [2 x i32], ptr %31, i64 0, i64 0
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [100009 x %struct.Node], ptr @t, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.Node, ptr %35, i32 0, i32 2
  %37 = load i32, ptr %36, align 4, !tbaa !9
  %38 = icmp sle i32 %27, %37
  br i1 %38, label %39, label %46

39:                                               ; preds = %26
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100009 x %struct.Node], ptr @t, i64 0, i64 %41
  %43 = getelementptr inbounds %struct.Node, ptr %42, i32 0, i32 1
  %44 = getelementptr inbounds [2 x i32], ptr %43, i64 0, i64 0
  %45 = load i32, ptr %44, align 4, !tbaa !5
  store i32 %45, ptr %3, align 4, !tbaa !5
  br label %66

46:                                               ; preds = %26
  %47 = load i32, ptr %3, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [100009 x %struct.Node], ptr @t, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.Node, ptr %49, i32 0, i32 1
  %51 = getelementptr inbounds [2 x i32], ptr %50, i64 0, i64 0
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [100009 x %struct.Node], ptr @t, i64 0, i64 %53
  %55 = getelementptr inbounds %struct.Node, ptr %54, i32 0, i32 2
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  %58 = load i32, ptr %4, align 4, !tbaa !5
  %59 = sub nsw i32 %58, %57
  store i32 %59, ptr %4, align 4, !tbaa !5
  %60 = load i32, ptr %3, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [100009 x %struct.Node], ptr @t, i64 0, i64 %61
  %63 = getelementptr inbounds %struct.Node, ptr %62, i32 0, i32 1
  %64 = getelementptr inbounds [2 x i32], ptr %63, i64 0, i64 1
  %65 = load i32, ptr %64, align 4, !tbaa !5
  store i32 %65, ptr %3, align 4, !tbaa !5
  br label %66

66:                                               ; preds = %46, %39
  br label %67

67:                                               ; preds = %66
  br label %6, !llvm.loop !11

68:                                               ; preds = %24, %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %69 = load i32, ptr %5, align 4
  switch i32 %69, label %73 [
    i32 2, label %70
    i32 1, label %71
  ]

70:                                               ; preds = %68
  unreachable

71:                                               ; preds = %68
  %72 = load i32, ptr %2, align 4
  ret i32 %72

73:                                               ; preds = %68
  unreachable
}

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 = !{!10, !6, i64 12}
!10 = !{!"_ZTS4Node", !6, i64 0, !7, i64 4, !6, i64 12}
!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"

%struct.Node = type { i32, [2 x i32], i32 }

@t = external dso_local local_unnamed_addr global [100009 x %struct.Node], align 16

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

2:                                                ; preds = %1, %16
  %.017 = phi i32 [ 2, %1 ], [ %.1, %16 ]
  %.01116 = phi i32 [ %0, %1 ], [ %.112, %16 ]
  %3 = sext i32 %.01116 to i64
  %4 = getelementptr inbounds [100009 x %struct.Node], ptr @t, i64 0, i64 %3, i32 1
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [100009 x %struct.Node], ptr @t, i64 0, i64 %6, i32 2
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = add nsw i32 %8, 1
  %10 = icmp eq i32 %9, %.017
  br i1 %10, label %17, label %11

11:                                               ; preds = %2
  %.not15 = icmp sgt i32 %.017, %8
  br i1 %.not15, label %12, label %16

12:                                               ; preds = %11
  %13 = sub nsw i32 %.017, %9
  %14 = getelementptr inbounds i8, ptr %4, i64 4
  %15 = load i32, ptr %14, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %11, %12
  %.112 = phi i32 [ %15, %12 ], [ %5, %11 ]
  %.1 = phi i32 [ %13, %12 ], [ %.017, %11 ]
  %.not = icmp eq i32 %.1, 0
  br i1 %.not, label %17, label %2, !llvm.loop !11

17:                                               ; preds = %2, %16
  %.011.lcssa = phi i32 [ %.01116, %2 ], [ %.112, %16 ]
  ret i32 %.011.lcssa
}

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 = !{!10, !6, i64 12}
!10 = !{!"_ZTS4Node", !6, i64 0, !7, i64 4, !6, i64 12}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass