; LLVM IR from ejoy/ejoy2d :: lua (ldo)

; 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, ptr, %struct.CallInfo, ptr, i64, i32, i32, i32, i16, i16, i8, i8 }
%struct.CallInfo = type { ptr, ptr, ptr, ptr, %union.anon, i64, i16, i8 }
%union.anon = type { %struct.anon.0 }
%struct.anon.0 = type { ptr, i64, i64 }

; 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: nounwind uwtable
declare hidden void @luaD_reallocstack(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define hidden void @luaD_shrinkstack(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = call i32 @stackinuse(ptr noundef %5)
  store i32 %6, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %7 = load i32, ptr %3, align 4, !tbaa !9
  %8 = load i32, ptr %3, align 4, !tbaa !9
  %9 = sdiv i32 %8, 8
  %10 = add nsw i32 %7, %9
  %11 = add nsw i32 %10, 10
  store i32 %11, ptr %4, align 4, !tbaa !9
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = icmp sgt i32 %12, 1000000
  br i1 %13, label %14, label %15

14:                                               ; preds = %1
  store i32 1000000, ptr %4, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %14, %1
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.lua_State, ptr %16, i32 0, i32 17
  %18 = load i32, ptr %17, align 8, !tbaa !11
  %19 = icmp sgt i32 %18, 1000000
  br i1 %19, label %20, label %22

20:                                               ; preds = %15
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  call void @luaE_freeCI(ptr noundef %21)
  br label %24

22:                                               ; preds = %15
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  call void @luaE_shrinkCI(ptr noundef %23)
  br label %24

24:                                               ; preds = %22, %20
  %25 = load i32, ptr %3, align 4, !tbaa !9
  %26 = icmp sgt i32 %25, 1000000
  br i1 %26, label %33, label %27

27:                                               ; preds = %24
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.lua_State, ptr %29, i32 0, i32 17
  %31 = load i32, ptr %30, align 8, !tbaa !11
  %32 = icmp sge i32 %28, %31
  br i1 %32, label %33, label %34

33:                                               ; preds = %27, %24
  br label %37

34:                                               ; preds = %27
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = load i32, ptr %4, align 4, !tbaa !9
  call void @luaD_reallocstack(ptr noundef %35, i32 noundef %36)
  br label %37

37:                                               ; preds = %34, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden i32 @stackinuse(ptr noundef) #1

declare hidden void @luaE_freeCI(ptr noundef) #2

declare hidden void @luaE_shrinkCI(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { "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 #3 = { 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, !10, i64 184}
!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, !6, i64 88, !13, i64 96, !6, i64 168, !14, i64 176, !10, i64 184, !10, i64 188, !10, i64 192, !15, i64 196, !15, i64 198, !7, i64 200, !7, i64 201}
!13 = !{!"CallInfo", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !7, i64 32, !14, i64 56, !15, i64 64, !7, i64 66}
!14 = !{!"long", !7, i64 0}
!15 = !{!"short", !7, i64 0}


; 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: nounwind uwtable
declare hidden void @luaD_reallocstack(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define hidden void @luaD_shrinkstack(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @stackinuse(ptr noundef %0)
  %3 = sdiv i32 %2, 8
  %4 = add nsw i32 %3, %2
  %5 = tail call i32 @llvm.smin.i32(i32 %4, i32 999990)
  %spec.store.select = add nsw i32 %5, 10
  %6 = getelementptr inbounds i8, ptr %0, i64 184
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %8 = icmp sgt i32 %7, 1000000
  br i1 %8, label %9, label %10

9:                                                ; preds = %1
  tail call void @luaE_freeCI(ptr noundef nonnull %0) #3
  br label %11

10:                                               ; preds = %1
  tail call void @luaE_shrinkCI(ptr noundef nonnull %0) #3
  br label %11

11:                                               ; preds = %10, %9
  %12 = icmp sgt i32 %2, 1000000
  br i1 %12, label %16, label %13

13:                                               ; preds = %11
  %14 = load i32, ptr %6, align 8, !tbaa !5
  %.not = icmp slt i32 %spec.store.select, %14
  br i1 %.not, label %15, label %16

15:                                               ; preds = %13
  tail call void @luaD_reallocstack(ptr noundef nonnull %0, i32 noundef %spec.store.select)
  br label %16

16:                                               ; preds = %11, %13, %15
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden i32 @stackinuse(ptr noundef) local_unnamed_addr #0

declare hidden void @luaE_freeCI(ptr noundef) local_unnamed_addr #1

declare hidden void @luaE_shrinkCI(ptr noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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, !13, i64 184}
!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, !7, i64 88, !10, i64 96, !7, i64 168, !11, i64 176, !13, i64 184, !13, i64 188, !13, i64 192, !12, i64 196, !12, i64 198, !8, i64 200, !8, i64 201}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"CallInfo", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !8, i64 32, !11, i64 56, !12, i64 64, !8, i64 66}
!11 = !{!"long", !8, i64 0}
!12 = !{!"short", !8, i64 0}
!13 = !{!"int", !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass