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

; 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.global_State = type { ptr, i32, i32, ptr, ptr, %struct.GCState, i32, %struct.SBuf, %struct.GCstr, i8, i8, i8, i8, %struct.GCRef, %union.TValue, %union.TValue, %union.TValue, %struct.Node, %struct.GCupval, i32, i32, ptr, ptr, ptr, i32, i32, %struct.GCRef, %struct.MRef, %struct.MRef, [38 x %struct.GCRef] }
%struct.GCState = type { i32, i32, i8, i8, i8, i8, i32, %struct.GCRef, %struct.MRef, %struct.GCRef, %struct.GCRef, %struct.GCRef, %struct.GCRef, i32, i32, i32, i32 }
%struct.SBuf = type { %struct.MRef, %struct.MRef, %struct.MRef, %struct.MRef }
%struct.GCstr = type { %struct.GCRef, i8, i8, i8, i8, i32, i32 }
%union.TValue = type { i64 }
%struct.Node = type { %union.TValue, %union.TValue, %struct.MRef, %struct.MRef }
%struct.GCupval = type { %struct.GCRef, i8, i8, i8, i8, %union.anon.2, %struct.MRef, i32 }
%union.anon.2 = type { %union.TValue }
%struct.GCRef = type { i32 }
%struct.MRef = type { i32 }
%struct.lua_State = type { %union.anon.4 }
%union.anon.4 = type { %struct.anon.5, [320 x i8] }
%struct.anon.5 = type { %struct.GCRef, i8, i8, i8, i8, %struct.MRef, %struct.GCRef, ptr, ptr, %struct.MRef, %struct.MRef, %struct.GCRef, %struct.GCRef, ptr, i32 }
%struct.anon.0 = type { %struct.GCRef, %union.FrameLink }
%union.FrameLink = type { i32 }
%struct.GCfuncL = type { %struct.GCRef, i8, i8, i8, i8, %struct.GCRef, %struct.GCRef, %struct.MRef, [1 x %struct.GCRef] }
%struct.GCproto = type { %struct.GCRef, i8, i8, i8, i8, i32, %struct.GCRef, %struct.MRef, %struct.MRef, i32, i32, i32, i8, i8, i16, %struct.GCRef, i32, i32, %struct.MRef, %struct.MRef, %struct.MRef }

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

; Function Attrs: nounwind uwtable
define hidden i32 @lj_gc_step_jit(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  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 8, ptr %5) #2
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.global_State, ptr %6, i32 0, i32 26
  %8 = getelementptr inbounds %struct.GCRef, ptr %7, i32 0, i32 0
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = zext i32 %9 to i64
  %11 = inttoptr i64 %10 to ptr
  store ptr %11, ptr %5, align 8, !tbaa !5
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.lua_State, ptr %12, i32 0, i32 0
  %14 = getelementptr inbounds %struct.anon.5, ptr %13, i32 0, i32 5
  %15 = getelementptr inbounds %struct.MRef, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !20
  %17 = zext i32 %16 to i64
  %18 = inttoptr i64 %17 to ptr
  %19 = getelementptr inbounds %struct.global_State, ptr %18, i32 0, i32 27
  %20 = getelementptr inbounds %struct.MRef, ptr %19, i32 0, i32 0
  %21 = load i32, ptr %20, align 4, !tbaa !21
  %22 = zext i32 %21 to i64
  %23 = inttoptr i64 %22 to ptr
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.lua_State, ptr %24, i32 0, i32 0
  %26 = getelementptr inbounds %struct.anon.5, ptr %25, i32 0, i32 7
  store ptr %23, ptr %26, align 8, !tbaa !20
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.lua_State, ptr %27, i32 0, i32 0
  %29 = getelementptr inbounds %struct.anon.5, ptr %28, i32 0, i32 7
  %30 = load ptr, ptr %29, align 8, !tbaa !20
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.lua_State, ptr %31, i32 0, i32 0
  %33 = getelementptr inbounds %struct.anon.5, ptr %32, i32 0, i32 7
  %34 = load ptr, ptr %33, align 8, !tbaa !20
  %35 = getelementptr inbounds %union.TValue, ptr %34, i64 -1
  %36 = getelementptr inbounds %struct.anon.0, ptr %35, i32 0, i32 0
  %37 = getelementptr inbounds %struct.GCRef, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !20
  %39 = zext i32 %38 to i64
  %40 = inttoptr i64 %39 to ptr
  %41 = getelementptr inbounds %struct.GCfuncL, ptr %40, i32 0, i32 7
  %42 = getelementptr inbounds %struct.MRef, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 8, !tbaa !20
  %44 = zext i32 %43 to i64
  %45 = inttoptr i64 %44 to ptr
  %46 = getelementptr inbounds i8, ptr %45, i64 -64
  %47 = getelementptr inbounds %struct.GCproto, ptr %46, i32 0, i32 4
  %48 = load i8, ptr %47, align 1, !tbaa !22
  %49 = zext i8 %48 to i32
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds %union.TValue, ptr %30, i64 %50
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.lua_State, ptr %52, i32 0, i32 0
  %54 = getelementptr inbounds %struct.anon.5, ptr %53, i32 0, i32 8
  store ptr %51, ptr %54, align 8, !tbaa !20
  br label %55

55:                                               ; preds = %65, %2
  %56 = load i32, ptr %4, align 4, !tbaa !9
  %57 = add i32 %56, -1
  store i32 %57, ptr %4, align 4, !tbaa !9
  %58 = icmp ugt i32 %56, 0
  br i1 %58, label %59, label %63

59:                                               ; preds = %55
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = call i32 @lj_gc_step(ptr noundef %60)
  %62 = icmp eq i32 %61, 0
  br label %63

63:                                               ; preds = %59, %55
  %64 = phi i1 [ false, %55 ], [ %62, %59 ]
  br i1 %64, label %65, label %66

65:                                               ; preds = %63
  br label %55, !llvm.loop !25

66:                                               ; preds = %63
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.lua_State, ptr %67, i32 0, i32 0
  %69 = getelementptr inbounds %struct.anon.5, ptr %68, i32 0, i32 5
  %70 = getelementptr inbounds %struct.MRef, ptr %69, i32 0, i32 0
  %71 = load i32, ptr %70, align 8, !tbaa !20
  %72 = zext i32 %71 to i64
  %73 = inttoptr i64 %72 to ptr
  %74 = getelementptr inbounds %struct.global_State, ptr %73, i32 0, i32 5
  %75 = getelementptr inbounds %struct.GCState, ptr %74, i32 0, i32 3
  %76 = load i8, ptr %75, align 1, !tbaa !27
  %77 = zext i8 %76 to i32
  %78 = icmp eq i32 %77, 2
  br i1 %78, label %92, label %79

79:                                               ; preds = %66
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.lua_State, ptr %80, i32 0, i32 0
  %82 = getelementptr inbounds %struct.anon.5, ptr %81, i32 0, i32 5
  %83 = getelementptr inbounds %struct.MRef, ptr %82, i32 0, i32 0
  %84 = load i32, ptr %83, align 8, !tbaa !20
  %85 = zext i32 %84 to i64
  %86 = inttoptr i64 %85 to ptr
  %87 = getelementptr inbounds %struct.global_State, ptr %86, i32 0, i32 5
  %88 = getelementptr inbounds %struct.GCState, ptr %87, i32 0, i32 3
  %89 = load i8, ptr %88, align 1, !tbaa !27
  %90 = zext i8 %89 to i32
  %91 = icmp eq i32 %90, 5
  br label %92

92:                                               ; preds = %79, %66
  %93 = phi i1 [ true, %66 ], [ %91, %79 ]
  %94 = zext i1 %93 to i32
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i32 %94
}

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, !10, i64 248}
!12 = !{!"global_State", !6, i64 0, !10, i64 8, !10, i64 12, !6, i64 16, !6, i64 24, !13, i64 32, !10, i64 88, !16, i64 92, !17, i64 108, !7, i64 124, !7, i64 125, !7, i64 126, !7, i64 127, !14, i64 128, !7, i64 136, !7, i64 144, !7, i64 152, !18, i64 160, !19, i64 184, !10, i64 208, !10, i64 212, !6, i64 216, !6, i64 224, !6, i64 232, !10, i64 240, !10, i64 244, !14, i64 248, !15, i64 252, !15, i64 256, !7, i64 260}
!13 = !{!"GCState", !10, i64 0, !10, i64 4, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11, !10, i64 12, !14, i64 16, !15, i64 20, !14, i64 24, !14, i64 28, !14, i64 32, !14, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52}
!14 = !{!"GCRef", !10, i64 0}
!15 = !{!"MRef", !10, i64 0}
!16 = !{!"SBuf", !15, i64 0, !15, i64 4, !15, i64 8, !15, i64 12}
!17 = !{!"GCstr", !14, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !10, i64 8, !10, i64 12}
!18 = !{!"Node", !7, i64 0, !7, i64 8, !15, i64 16, !15, i64 20}
!19 = !{!"GCupval", !14, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !7, i64 8, !15, i64 16, !10, i64 20}
!20 = !{!7, !7, i64 0}
!21 = !{!12, !10, i64 252}
!22 = !{!23, !7, i64 7}
!23 = !{!"GCproto", !14, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !10, i64 8, !14, i64 12, !15, i64 16, !15, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !7, i64 36, !7, i64 37, !24, i64 38, !14, i64 40, !10, i64 44, !10, i64 48, !15, i64 52, !15, i64 56, !15, i64 60}
!24 = !{!"short", !7, i64 0}
!25 = distinct !{!25, !26}
!26 = !{!"llvm.loop.mustprogress"}
!27 = !{!12, !7, i64 41}


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

%union.TValue = type { i64 }

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

; Function Attrs: nounwind uwtable
define hidden range(i32 0, 2) i32 @lj_gc_step_jit(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 248
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = zext i32 %4 to i64
  %6 = inttoptr i64 %5 to ptr
  %7 = getelementptr inbounds i8, ptr %6, i64 8
  %8 = load i32, ptr %7, align 8, !tbaa !18
  %9 = zext i32 %8 to i64
  %10 = inttoptr i64 %9 to ptr
  %11 = getelementptr inbounds i8, ptr %10, i64 252
  %12 = load i32, ptr %11, align 4, !tbaa !19
  %13 = zext i32 %12 to i64
  %14 = inttoptr i64 %13 to ptr
  %15 = getelementptr inbounds i8, ptr %6, i64 16
  store ptr %14, ptr %15, align 8, !tbaa !18
  %16 = getelementptr inbounds i8, ptr %14, i64 -8
  %17 = load i32, ptr %16, align 8, !tbaa !18
  %18 = zext i32 %17 to i64
  %19 = inttoptr i64 %18 to ptr
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  %21 = load i32, ptr %20, align 8, !tbaa !18
  %22 = zext i32 %21 to i64
  %23 = inttoptr i64 %22 to ptr
  %24 = getelementptr inbounds i8, ptr %23, i64 -57
  %25 = load i8, ptr %24, align 1, !tbaa !20
  %26 = zext i8 %25 to i64
  %27 = getelementptr inbounds %union.TValue, ptr %14, i64 %26
  %28 = getelementptr inbounds i8, ptr %6, i64 24
  store ptr %27, ptr %28, align 8, !tbaa !18
  br label %29

29:                                               ; preds = %30, %2
  %.0 = phi i32 [ %1, %2 ], [ %31, %30 ]
  %.not = icmp eq i32 %.0, 0
  br i1 %.not, label %.critedge, label %30

30:                                               ; preds = %29
  %31 = add i32 %.0, -1
  %32 = tail call i32 @lj_gc_step(ptr noundef %6)
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %29, label %.critedge, !llvm.loop !23

.critedge:                                        ; preds = %29, %30
  %34 = load i32, ptr %7, align 8, !tbaa !18
  %35 = zext i32 %34 to i64
  %36 = inttoptr i64 %35 to ptr
  %37 = getelementptr inbounds i8, ptr %36, i64 41
  %38 = load i8, ptr %37, align 1, !tbaa !25
  %39 = icmp eq i8 %38, 2
  %40 = icmp eq i8 %38, 5
  %narrow = or i1 %39, %40
  %41 = zext i1 %narrow to i32
  ret i32 %41
}

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

!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, !10, i64 248}
!6 = !{!"global_State", !7, i64 0, !10, i64 8, !10, i64 12, !7, i64 16, !7, i64 24, !11, i64 32, !10, i64 88, !14, i64 92, !15, i64 108, !8, i64 124, !8, i64 125, !8, i64 126, !8, i64 127, !12, i64 128, !8, i64 136, !8, i64 144, !8, i64 152, !16, i64 160, !17, i64 184, !10, i64 208, !10, i64 212, !7, i64 216, !7, i64 224, !7, i64 232, !10, i64 240, !10, i64 244, !12, i64 248, !13, i64 252, !13, i64 256, !8, i64 260}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"GCState", !10, i64 0, !10, i64 4, !8, i64 8, !8, i64 9, !8, i64 10, !8, i64 11, !10, i64 12, !12, i64 16, !13, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52}
!12 = !{!"GCRef", !10, i64 0}
!13 = !{!"MRef", !10, i64 0}
!14 = !{!"SBuf", !13, i64 0, !13, i64 4, !13, i64 8, !13, i64 12}
!15 = !{!"GCstr", !12, i64 0, !8, i64 4, !8, i64 5, !8, i64 6, !8, i64 7, !10, i64 8, !10, i64 12}
!16 = !{!"Node", !8, i64 0, !8, i64 8, !13, i64 16, !13, i64 20}
!17 = !{!"GCupval", !12, i64 0, !8, i64 4, !8, i64 5, !8, i64 6, !8, i64 7, !8, i64 8, !13, i64 16, !10, i64 20}
!18 = !{!8, !8, i64 0}
!19 = !{!6, !10, i64 252}
!20 = !{!21, !8, i64 7}
!21 = !{!"GCproto", !12, i64 0, !8, i64 4, !8, i64 5, !8, i64 6, !8, i64 7, !10, i64 8, !12, i64 12, !13, i64 16, !13, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !8, i64 36, !8, i64 37, !22, i64 38, !12, i64 40, !10, i64 44, !10, i64 48, !13, i64 52, !13, i64 56, !13, i64 60}
!22 = !{!"short", !8, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = !{!6, !8, i64 41}


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