; 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 }
%struct.lua_TValue = type { %union.Value, i32 }
%union.Value = type { ptr }

; 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 @seterrorobj(ptr noundef, i32 noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @lua_resume(ptr noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.lua_State, ptr %9, i32 0, i32 20
  %11 = load i16, ptr %10, align 4, !tbaa !11
  %12 = zext i16 %11 to i32
  store i32 %12, ptr %8, align 4, !tbaa !9
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %22

16:                                               ; preds = %3
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.lua_State, ptr %17, i32 0, i32 21
  %19 = load i16, ptr %18, align 2, !tbaa !16
  %20 = zext i16 %19 to i32
  %21 = add nsw i32 %20, 1
  br label %23

22:                                               ; preds = %3
  br label %23

23:                                               ; preds = %22, %16
  %24 = phi i32 [ %21, %16 ], [ 1, %22 ]
  %25 = trunc i32 %24 to i16
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.lua_State, ptr %26, i32 0, i32 21
  store i16 %25, ptr %27, align 2, !tbaa !16
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.lua_State, ptr %28, i32 0, i32 20
  store i16 0, ptr %29, align 4, !tbaa !11
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.lua_State, ptr %31, i32 0, i32 4
  %33 = load ptr, ptr %32, align 8, !tbaa !17
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = sub i64 0, %35
  %37 = getelementptr inbounds %struct.lua_TValue, ptr %33, i64 %36
  %38 = call i32 @luaD_rawrunprotected(ptr noundef %30, ptr noundef @resume, ptr noundef %37)
  store i32 %38, ptr %7, align 4, !tbaa !9
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = icmp eq i32 %39, -1
  br i1 %40, label %41, label %42

41:                                               ; preds = %23
  store i32 2, ptr %7, align 4, !tbaa !9
  br label %78

42:                                               ; preds = %23
  br label %43

43:                                               ; preds = %53, %42
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = icmp sgt i32 %44, 1
  br i1 %45, label %46, label %51

46:                                               ; preds = %43
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = call i32 @recover(ptr noundef %47, i32 noundef %48)
  %50 = icmp ne i32 %49, 0
  br label %51

51:                                               ; preds = %46, %43
  %52 = phi i1 [ false, %43 ], [ %50, %46 ]
  br i1 %52, label %53, label %56

53:                                               ; preds = %51
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = call i32 @luaD_rawrunprotected(ptr noundef %54, ptr noundef @unroll, ptr noundef %7)
  store i32 %55, ptr %7, align 4, !tbaa !9
  br label %43, !llvm.loop !18

56:                                               ; preds = %51
  %57 = load i32, ptr %7, align 4, !tbaa !9
  %58 = icmp sgt i32 %57, 1
  br i1 %58, label %59, label %76

59:                                               ; preds = %56
  %60 = load i32, ptr %7, align 4, !tbaa !9
  %61 = trunc i32 %60 to i8
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.lua_State, ptr %62, i32 0, i32 3
  store i8 %61, ptr %63, align 2, !tbaa !20
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = load i32, ptr %7, align 4, !tbaa !9
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.lua_State, ptr %66, i32 0, i32 4
  %68 = load ptr, ptr %67, align 8, !tbaa !17
  call void @seterrorobj(ptr noundef %64, i32 noundef %65, ptr noundef %68)
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.lua_State, ptr %69, i32 0, i32 4
  %71 = load ptr, ptr %70, align 8, !tbaa !17
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.lua_State, ptr %72, i32 0, i32 6
  %74 = load ptr, ptr %73, align 8, !tbaa !21
  %75 = getelementptr inbounds %struct.CallInfo, ptr %74, i32 0, i32 1
  store ptr %71, ptr %75, align 8, !tbaa !22
  br label %77

76:                                               ; preds = %56
  br label %77

77:                                               ; preds = %76, %59
  br label %78

78:                                               ; preds = %77, %41
  %79 = load i32, ptr %8, align 4, !tbaa !9
  %80 = trunc i32 %79 to i16
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.lua_State, ptr %81, i32 0, i32 20
  store i16 %80, ptr %82, align 4, !tbaa !11
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.lua_State, ptr %83, i32 0, i32 21
  %85 = load i16, ptr %84, align 2, !tbaa !16
  %86 = add i16 %85, -1
  store i16 %86, ptr %84, align 2, !tbaa !16
  %87 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i32 %87
}

; Function Attrs: nounwind uwtable
declare hidden void @resume(ptr noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
declare hidden void @unroll(ptr noundef, ptr noundef) #1

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 = { 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, !15, i64 196}
!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}
!16 = !{!12, !15, i64 198}
!17 = !{!12, !6, i64 16}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!12, !7, i64 10}
!21 = !{!12, !6, i64 32}
!22 = !{!13, !6, i64 8}


; 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.lua_TValue = type { %union.Value, i32 }
%union.Value = type { ptr }

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

; Function Attrs: mustprogress 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 @seterrorobj(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @lua_resume(ptr noundef %0, ptr noundef readonly %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #2
  %5 = getelementptr inbounds i8, ptr %0, i64 196
  %6 = load i16, ptr %5, align 4, !tbaa !5
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %11, label %7

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %1, i64 198
  %9 = load i16, ptr %8, align 2, !tbaa !14
  %10 = add i16 %9, 1
  br label %11

11:                                               ; preds = %3, %7
  %12 = phi i16 [ %10, %7 ], [ 1, %3 ]
  %13 = getelementptr inbounds i8, ptr %0, i64 198
  store i16 %12, ptr %13, align 2, !tbaa !14
  store i16 0, ptr %5, align 4, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  %15 = load ptr, ptr %14, align 8, !tbaa !15
  %16 = sext i32 %2 to i64
  %17 = sub nsw i64 0, %16
  %18 = getelementptr inbounds %struct.lua_TValue, ptr %15, i64 %17
  %19 = tail call i32 @luaD_rawrunprotected(ptr noundef nonnull %0, ptr noundef nonnull @resume, ptr noundef %18)
  store i32 %19, ptr %4, align 4, !tbaa !16
  %20 = icmp eq i32 %19, -1
  br i1 %20, label %.critedge.thread, label %.preheader

.preheader:                                       ; preds = %11
  %21 = icmp sgt i32 %19, 1
  br i1 %21, label %.lr.ph, label %.critedge.thread

.lr.ph:                                           ; preds = %.preheader, %24
  %22 = phi i32 [ %25, %24 ], [ %19, %.preheader ]
  %23 = call i32 @recover(ptr noundef %0, i32 noundef %22)
  %.not20 = icmp eq i32 %23, 0
  br i1 %.not20, label %.critedge, label %24

24:                                               ; preds = %.lr.ph
  %25 = call i32 @luaD_rawrunprotected(ptr noundef %0, ptr noundef nonnull @unroll, ptr noundef nonnull %4)
  store i32 %25, ptr %4, align 4, !tbaa !16
  %26 = icmp sgt i32 %25, 1
  br i1 %26, label %.lr.ph, label %.critedge.thread, !llvm.loop !17

.critedge:                                        ; preds = %.lr.ph
  %.pre.pre = load i32, ptr %4, align 4, !tbaa !16
  %27 = icmp sgt i32 %.pre.pre, 1
  br i1 %27, label %28, label %.critedge.thread

28:                                               ; preds = %.critedge
  %29 = trunc i32 %.pre.pre to i8
  %30 = getelementptr inbounds i8, ptr %0, i64 10
  store i8 %29, ptr %30, align 2, !tbaa !19
  %31 = load ptr, ptr %14, align 8, !tbaa !15
  call void @seterrorobj(ptr noundef %0, i32 noundef %.pre.pre, ptr noundef %31)
  %32 = load ptr, ptr %14, align 8, !tbaa !15
  %33 = getelementptr inbounds i8, ptr %0, i64 32
  %34 = load ptr, ptr %33, align 8, !tbaa !20
  %35 = getelementptr inbounds i8, ptr %34, i64 8
  store ptr %32, ptr %35, align 8, !tbaa !21
  %.pre22 = load i32, ptr %4, align 4, !tbaa !16
  br label %.critedge.thread

.critedge.thread:                                 ; preds = %24, %11, %.preheader, %28, %.critedge
  %36 = phi i32 [ %.pre22, %28 ], [ %.pre.pre, %.critedge ], [ %19, %.preheader ], [ 2, %11 ], [ %25, %24 ]
  store i16 %6, ptr %5, align 4, !tbaa !5
  %37 = load i16, ptr %13, align 2, !tbaa !14
  %38 = add i16 %37, -1
  store i16 %38, ptr %13, align 2, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #2
  ret i32 %36
}

; Function Attrs: nounwind uwtable
declare hidden void @resume(ptr noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
declare hidden void @unroll(ptr noundef, ptr noundef) #1

attributes #0 = { mustprogress 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 = { 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, !12, i64 196}
!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}
!14 = !{!6, !12, i64 198}
!15 = !{!6, !7, i64 16}
!16 = !{!13, !13, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!6, !8, i64 10}
!20 = !{!6, !7, i64 32}
!21 = !{!10, !7, i64 8}


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