; LLVM IR from paintdream/DontStarveLuaJIT :: src/luajit/src (lj_state)

; 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 { %union.anon }
%union.anon = type { %struct.anon, [320 x i8] }
%struct.anon = type { %struct.GCRef, i8, i8, i8, i8, %struct.MRef, %struct.GCRef, ptr, ptr, %struct.MRef, %struct.MRef, %struct.GCRef, %struct.GCRef, ptr, i32 }
%struct.MRef = type { i32 }
%struct.GCRef = type { i32 }

; Function Attrs: nounwind uwtable
declare hidden void @resizestack(ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define hidden void @lj_state_growstack(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.lua_State, ptr %6, i32 0, i32 0
  %8 = getelementptr inbounds %struct.anon, ptr %7, i32 0, i32 14
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = icmp ugt i32 %9, 65506
  br i1 %10, label %11, label %13

11:                                               ; preds = %2
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  call void @lj_err_throw(ptr noundef %12, i32 noundef 5) #4
  unreachable

13:                                               ; preds = %2
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.lua_State, ptr %14, i32 0, i32 0
  %16 = getelementptr inbounds %struct.anon, ptr %15, i32 0, i32 14
  %17 = load i32, ptr %16, align 8, !tbaa !11
  %18 = load i32, ptr %4, align 4, !tbaa !9
  %19 = add i32 %17, %18
  store i32 %19, ptr %5, align 4, !tbaa !9
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = icmp ugt i32 %20, 65500
  br i1 %21, label %22, label %25

22:                                               ; preds = %13
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = add i32 %23, 40
  store i32 %24, ptr %5, align 4, !tbaa !9
  br label %44

25:                                               ; preds = %13
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.lua_State, ptr %27, i32 0, i32 0
  %29 = getelementptr inbounds %struct.anon, ptr %28, i32 0, i32 14
  %30 = load i32, ptr %29, align 8, !tbaa !11
  %31 = mul i32 2, %30
  %32 = icmp ult i32 %26, %31
  br i1 %32, label %33, label %43

33:                                               ; preds = %25
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.lua_State, ptr %34, i32 0, i32 0
  %36 = getelementptr inbounds %struct.anon, ptr %35, i32 0, i32 14
  %37 = load i32, ptr %36, align 8, !tbaa !11
  %38 = mul i32 2, %37
  store i32 %38, ptr %5, align 4, !tbaa !9
  %39 = load i32, ptr %5, align 4, !tbaa !9
  %40 = icmp uge i32 %39, 65500
  br i1 %40, label %41, label %42

41:                                               ; preds = %33
  store i32 65500, ptr %5, align 4, !tbaa !9
  br label %42

42:                                               ; preds = %41, %33
  br label %43

43:                                               ; preds = %42, %25
  br label %44

44:                                               ; preds = %43, %22
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = load i32, ptr %5, align 4, !tbaa !9
  call void @resizestack(ptr noundef %45, i32 noundef %46)
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.lua_State, ptr %47, i32 0, i32 0
  %49 = getelementptr inbounds %struct.anon, ptr %48, i32 0, i32 14
  %50 = load i32, ptr %49, align 8, !tbaa !11
  %51 = icmp ugt i32 %50, 65506
  br i1 %51, label %52, label %54

52:                                               ; preds = %44
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  call void @lj_err_msg(ptr noundef %53, i32 noundef 104) #4
  unreachable

54:                                               ; preds = %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: noreturn
declare hidden void @lj_err_throw(ptr noundef, i32 noundef) #2

; Function Attrs: noreturn
declare hidden void @lj_err_msg(ptr noundef, i32 noundef) #2

; 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 = { noreturn "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 }
attributes #4 = { noreturn }

!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 = !{!7, !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 @resizestack(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define hidden void @lj_state_growstack(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 56
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp ugt i32 %4, 65506
  br i1 %5, label %6, label %7

6:                                                ; preds = %2
  tail call void @lj_err_throw(ptr noundef nonnull %0, i32 noundef 5) #2
  unreachable

7:                                                ; preds = %2
  %8 = add i32 %4, %1
  %9 = icmp ugt i32 %8, 65500
  br i1 %9, label %10, label %12

10:                                               ; preds = %7
  %11 = add i32 %8, 40
  br label %17

12:                                               ; preds = %7
  %13 = shl nuw nsw i32 %4, 1
  %14 = icmp ult i32 %8, %13
  br i1 %14, label %15, label %17

15:                                               ; preds = %12
  %16 = icmp ugt i32 %4, 32749
  %spec.store.select = select i1 %16, i32 65500, i32 %13
  br label %17

17:                                               ; preds = %12, %15, %10
  %.0 = phi i32 [ %11, %10 ], [ %spec.store.select, %15 ], [ %8, %12 ]
  tail call void @resizestack(ptr noundef nonnull %0, i32 noundef %.0)
  %18 = load i32, ptr %3, align 8, !tbaa !5
  %19 = icmp ugt i32 %18, 65506
  br i1 %19, label %20, label %21

20:                                               ; preds = %17
  tail call void @lj_err_msg(ptr noundef nonnull %0, i32 noundef 104) #2
  unreachable

21:                                               ; preds = %17
  ret void
}

; Function Attrs: noreturn
declare hidden void @lj_err_throw(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: noreturn
declare hidden void @lj_err_msg(ptr noundef, i32 noundef) local_unnamed_addr #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 = { noreturn "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 = { noreturn 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


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