; LLVM IR from ramsdell/lineqpp :: lua (ldebug)

; 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.lua_State = type { ptr, i8, i8, i8, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i16, i16, i8, i8, i32, i32, ptr, %struct.lua_TValue, %struct.lua_TValue, ptr, ptr, ptr, i64 }
%struct.lua_TValue = type { %union.Value, i32 }
%union.Value = type { ptr }
%struct.CallInfo = type { ptr, ptr, ptr, ptr, i32, i32 }
%struct.CClosure = type { ptr, i8, i8, i8, i8, ptr, ptr, ptr, [1 x %struct.lua_TValue] }
%struct.lua_Debug = type { i32, ptr, ptr, ptr, ptr, i32, i32, i32, i32, [60 x i8], i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @lua_getstack(ptr noundef %0, i32 noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.lua_State, ptr %9, i32 0, i32 7
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  store ptr %11, ptr %8, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %41, %3
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %15, label %21

15:                                               ; preds = %12
  %16 = load ptr, ptr %8, align 8, !tbaa !5
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.lua_State, ptr %17, i32 0, i32 12
  %19 = load ptr, ptr %18, align 8, !tbaa !16
  %20 = icmp ugt ptr %16, %19
  br label %21

21:                                               ; preds = %15, %12
  %22 = phi i1 [ false, %12 ], [ %20, %15 ]
  br i1 %22, label %23, label %44

23:                                               ; preds = %21
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = add nsw i32 %24, -1
  store i32 %25, ptr %5, align 4, !tbaa !9
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.CallInfo, ptr %26, i32 0, i32 1
  %28 = load ptr, ptr %27, align 8, !tbaa !17
  %29 = getelementptr inbounds %struct.lua_TValue, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !19
  %31 = getelementptr inbounds %struct.CClosure, ptr %30, i32 0, i32 3
  %32 = load i8, ptr %31, align 2, !tbaa !19
  %33 = icmp ne i8 %32, 0
  br i1 %33, label %40, label %34

34:                                               ; preds = %23
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.CallInfo, ptr %35, i32 0, i32 5
  %37 = load i32, ptr %36, align 4, !tbaa !20
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = sub nsw i32 %38, %37
  store i32 %39, ptr %5, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %34, %23
  br label %41

41:                                               ; preds = %40
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.CallInfo, ptr %42, i32 -1
  store ptr %43, ptr %8, align 8, !tbaa !5
  br label %12, !llvm.loop !21

44:                                               ; preds = %21
  %45 = load i32, ptr %5, align 4, !tbaa !9
  %46 = icmp eq i32 %45, 0
  br i1 %46, label %47, label %65

47:                                               ; preds = %44
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.lua_State, ptr %49, i32 0, i32 12
  %51 = load ptr, ptr %50, align 8, !tbaa !16
  %52 = icmp ugt ptr %48, %51
  br i1 %52, label %53, label %65

53:                                               ; preds = %47
  store i32 1, ptr %7, align 4, !tbaa !9
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.lua_State, ptr %55, i32 0, i32 12
  %57 = load ptr, ptr %56, align 8, !tbaa !16
  %58 = ptrtoint ptr %54 to i64
  %59 = ptrtoint ptr %57 to i64
  %60 = sub i64 %58, %59
  %61 = sdiv exact i64 %60, 40
  %62 = trunc i64 %61 to i32
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.lua_Debug, ptr %63, i32 0, i32 10
  store i32 %62, ptr %64, align 4, !tbaa !23
  br label %73

65:                                               ; preds = %47, %44
  %66 = load i32, ptr %5, align 4, !tbaa !9
  %67 = icmp slt i32 %66, 0
  br i1 %67, label %68, label %71

68:                                               ; preds = %65
  store i32 1, ptr %7, align 4, !tbaa !9
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.lua_Debug, ptr %69, i32 0, i32 10
  store i32 0, ptr %70, align 4, !tbaa !23
  br label %72

71:                                               ; preds = %65
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %72

72:                                               ; preds = %71, %68
  br label %73

73:                                               ; preds = %72, %53
  %74 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i32 %74
}

; 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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 40}
!12 = !{!"lua_State", !6, i64 0, !7, i64 8, !7, i64 9, !7, i64 10, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !10, i64 88, !10, i64 92, !13, i64 96, !13, i64 98, !7, i64 100, !7, i64 101, !10, i64 104, !10, i64 108, !6, i64 112, !14, i64 120, !14, i64 136, !6, i64 152, !6, i64 160, !6, i64 168, !15, i64 176}
!13 = !{!"short", !7, i64 0}
!14 = !{!"lua_TValue", !7, i64 0, !10, i64 8}
!15 = !{!"long", !7, i64 0}
!16 = !{!12, !6, i64 80}
!17 = !{!18, !6, i64 8}
!18 = !{!"CallInfo", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 36}
!19 = !{!7, !7, i64 0}
!20 = !{!18, !10, i64 36}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!24, !10, i64 116}
!24 = !{!"lua_Debug", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !7, i64 56, !10, i64 116}


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

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 0, 2) i32 @lua_getstack(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 40
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = icmp sgt i32 %1, 0
  br i1 %6, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %3
  %7 = getelementptr inbounds i8, ptr %0, i64 80
  %8 = load ptr, ptr %7, align 8, !tbaa !14
  br label %9

9:                                                ; preds = %.lr.ph, %22
  %.023 = phi ptr [ %5, %.lr.ph ], [ %23, %22 ]
  %.01822 = phi i32 [ %1, %.lr.ph ], [ %.1, %22 ]
  %10 = icmp ugt ptr %.023, %8
  br i1 %10, label %11, label %.thread

11:                                               ; preds = %9
  %12 = add nsw i32 %.01822, -1
  %13 = getelementptr inbounds i8, ptr %.023, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  %15 = load ptr, ptr %14, align 8, !tbaa !17
  %16 = getelementptr inbounds i8, ptr %15, i64 10
  %17 = load i8, ptr %16, align 2, !tbaa !17
  %.not = icmp eq i8 %17, 0
  br i1 %.not, label %18, label %22

18:                                               ; preds = %11
  %19 = getelementptr inbounds i8, ptr %.023, i64 36
  %20 = load i32, ptr %19, align 4, !tbaa !18
  %21 = sub nsw i32 %12, %20
  br label %22

22:                                               ; preds = %11, %18
  %.1 = phi i32 [ %12, %11 ], [ %21, %18 ]
  %23 = getelementptr inbounds i8, ptr %.023, i64 -40
  %24 = icmp sgt i32 %.1, 0
  br i1 %24, label %9, label %.critedge, !llvm.loop !19

.critedge:                                        ; preds = %22, %3
  %.018.lcssa = phi i32 [ %1, %3 ], [ %.1, %22 ]
  %.0.lcssa = phi ptr [ %5, %3 ], [ %23, %22 ]
  %25 = icmp eq i32 %.018.lcssa, 0
  br i1 %25, label %26, label %.thread.sink.split

26:                                               ; preds = %.critedge
  %27 = getelementptr inbounds i8, ptr %0, i64 80
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  %29 = icmp ugt ptr %.0.lcssa, %28
  br i1 %29, label %30, label %.thread

30:                                               ; preds = %26
  %31 = ptrtoint ptr %.0.lcssa to i64
  %32 = ptrtoint ptr %28 to i64
  %33 = sub i64 %31, %32
  %34 = sdiv exact i64 %33, 40
  %35 = trunc i64 %34 to i32
  br label %.thread.sink.split

.thread.sink.split:                               ; preds = %.critedge, %30
  %.sink = phi i32 [ %35, %30 ], [ 0, %.critedge ]
  %36 = getelementptr inbounds i8, ptr %2, i64 116
  store i32 %.sink, ptr %36, align 4, !tbaa !21
  br label %.thread

.thread:                                          ; preds = %9, %.thread.sink.split, %26
  %.017 = phi i32 [ 0, %26 ], [ 1, %.thread.sink.split ], [ 0, %9 ]
  ret i32 %.017
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, 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, !7, i64 40}
!6 = !{!"lua_State", !7, i64 0, !8, i64 8, !8, i64 9, !8, i64 10, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !10, i64 88, !10, i64 92, !11, i64 96, !11, i64 98, !8, i64 100, !8, i64 101, !10, i64 104, !10, i64 108, !7, i64 112, !12, i64 120, !12, i64 136, !7, i64 152, !7, i64 160, !7, i64 168, !13, i64 176}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!"lua_TValue", !8, i64 0, !10, i64 8}
!13 = !{!"long", !8, i64 0}
!14 = !{!6, !7, i64 80}
!15 = !{!16, !7, i64 8}
!16 = !{!"CallInfo", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !10, i64 36}
!17 = !{!8, !8, i64 0}
!18 = !{!16, !10, i64 36}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!22, !10, i64 116}
!22 = !{!"lua_Debug", !10, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !8, i64 56, !10, i64 116}


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