; 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.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.MRef = type { i32 }
%struct.GCRef = type { i32 }
%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 }

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

; Function Attrs: nounwind uwtable
define hidden void @lj_gc_fullgc(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.lua_State, ptr %5, i32 0, i32 0
  %7 = getelementptr inbounds %struct.anon.5, ptr %6, i32 0, i32 5
  %8 = getelementptr inbounds %struct.MRef, ptr %7, i32 0, i32 0
  %9 = load i32, ptr %8, align 8, !tbaa !9
  %10 = zext i32 %9 to i64
  %11 = inttoptr i64 %10 to ptr
  store ptr %11, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.global_State, ptr %12, i32 0, i32 6
  %14 = load volatile i32, ptr %13, align 8, !tbaa !10
  store i32 %14, ptr %4, align 4, !tbaa !20
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.global_State, ptr %15, i32 0, i32 6
  store volatile i32 -3, ptr %16, align 8, !tbaa !10
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.global_State, ptr %17, i32 0, i32 5
  %19 = getelementptr inbounds %struct.GCState, ptr %18, i32 0, i32 3
  %20 = load i8, ptr %19, align 1, !tbaa !21
  %21 = zext i8 %20 to i32
  %22 = icmp sle i32 %21, 2
  br i1 %22, label %23, label %51

23:                                               ; preds = %1
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.global_State, ptr %24, i32 0, i32 5
  %26 = getelementptr inbounds %struct.GCState, ptr %25, i32 0, i32 7
  %27 = ptrtoint ptr %26 to i64
  %28 = trunc i64 %27 to i32
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.global_State, ptr %29, i32 0, i32 5
  %31 = getelementptr inbounds %struct.GCState, ptr %30, i32 0, i32 8
  %32 = getelementptr inbounds %struct.MRef, ptr %31, i32 0, i32 0
  store i32 %28, ptr %32, align 4, !tbaa !22
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.global_State, ptr %33, i32 0, i32 5
  %35 = getelementptr inbounds %struct.GCState, ptr %34, i32 0, i32 9
  %36 = getelementptr inbounds %struct.GCRef, ptr %35, i32 0, i32 0
  store i32 0, ptr %36, align 8, !tbaa !23
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.global_State, ptr %37, i32 0, i32 5
  %39 = getelementptr inbounds %struct.GCState, ptr %38, i32 0, i32 10
  %40 = getelementptr inbounds %struct.GCRef, ptr %39, i32 0, i32 0
  store i32 0, ptr %40, align 4, !tbaa !24
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.global_State, ptr %41, i32 0, i32 5
  %43 = getelementptr inbounds %struct.GCState, ptr %42, i32 0, i32 11
  %44 = getelementptr inbounds %struct.GCRef, ptr %43, i32 0, i32 0
  store i32 0, ptr %44, align 8, !tbaa !25
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.global_State, ptr %45, i32 0, i32 5
  %47 = getelementptr inbounds %struct.GCState, ptr %46, i32 0, i32 3
  store i8 3, ptr %47, align 1, !tbaa !21
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.global_State, ptr %48, i32 0, i32 5
  %50 = getelementptr inbounds %struct.GCState, ptr %49, i32 0, i32 6
  store i32 0, ptr %50, align 4, !tbaa !26
  br label %51

51:                                               ; preds = %23, %1
  br label %52

52:                                               ; preds = %68, %51
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.global_State, ptr %53, i32 0, i32 5
  %55 = getelementptr inbounds %struct.GCState, ptr %54, i32 0, i32 3
  %56 = load i8, ptr %55, align 1, !tbaa !21
  %57 = zext i8 %56 to i32
  %58 = icmp eq i32 %57, 3
  br i1 %58, label %66, label %59

59:                                               ; preds = %52
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.global_State, ptr %60, i32 0, i32 5
  %62 = getelementptr inbounds %struct.GCState, ptr %61, i32 0, i32 3
  %63 = load i8, ptr %62, align 1, !tbaa !21
  %64 = zext i8 %63 to i32
  %65 = icmp eq i32 %64, 4
  br label %66

66:                                               ; preds = %59, %52
  %67 = phi i1 [ true, %52 ], [ %65, %59 ]
  br i1 %67, label %68, label %71

68:                                               ; preds = %66
  %69 = load ptr, ptr %2, align 8, !tbaa !5
  %70 = call i64 @gc_onestep(ptr noundef %69)
  br label %52, !llvm.loop !27

71:                                               ; preds = %66
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.global_State, ptr %72, i32 0, i32 5
  %74 = getelementptr inbounds %struct.GCState, ptr %73, i32 0, i32 3
  store i8 0, ptr %74, align 1, !tbaa !21
  br label %75

75:                                               ; preds = %78, %71
  %76 = load ptr, ptr %2, align 8, !tbaa !5
  %77 = call i64 @gc_onestep(ptr noundef %76)
  br label %78

78:                                               ; preds = %75
  %79 = load ptr, ptr %3, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.global_State, ptr %79, i32 0, i32 5
  %81 = getelementptr inbounds %struct.GCState, ptr %80, i32 0, i32 3
  %82 = load i8, ptr %81, align 1, !tbaa !21
  %83 = zext i8 %82 to i32
  %84 = icmp ne i32 %83, 0
  br i1 %84, label %75, label %85, !llvm.loop !29

85:                                               ; preds = %78
  %86 = load ptr, ptr %3, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.global_State, ptr %86, i32 0, i32 5
  %88 = getelementptr inbounds %struct.GCState, ptr %87, i32 0, i32 14
  %89 = load i32, ptr %88, align 4, !tbaa !30
  %90 = udiv i32 %89, 100
  %91 = load ptr, ptr %3, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.global_State, ptr %91, i32 0, i32 5
  %93 = getelementptr inbounds %struct.GCState, ptr %92, i32 0, i32 16
  %94 = load i32, ptr %93, align 4, !tbaa !31
  %95 = mul i32 %90, %94
  %96 = load ptr, ptr %3, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.global_State, ptr %96, i32 0, i32 5
  %98 = getelementptr inbounds %struct.GCState, ptr %97, i32 0, i32 1
  store i32 %95, ptr %98, align 4, !tbaa !32
  %99 = load i32, ptr %4, align 4, !tbaa !20
  %100 = load ptr, ptr %3, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.global_State, ptr %100, i32 0, i32 6
  store volatile i32 %99, ptr %101, align 8, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret void
}

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 = !{!7, !7, i64 0}
!10 = !{!11, !12, i64 88}
!11 = !{!"global_State", !6, i64 0, !12, i64 8, !12, i64 12, !6, i64 16, !6, i64 24, !13, i64 32, !12, 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, !12, i64 208, !12, i64 212, !6, i64 216, !6, i64 224, !6, i64 232, !12, i64 240, !12, i64 244, !14, i64 248, !15, i64 252, !15, i64 256, !7, i64 260}
!12 = !{!"int", !7, i64 0}
!13 = !{!"GCState", !12, i64 0, !12, i64 4, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11, !12, i64 12, !14, i64 16, !15, i64 20, !14, i64 24, !14, i64 28, !14, i64 32, !14, i64 36, !12, i64 40, !12, i64 44, !12, i64 48, !12, i64 52}
!14 = !{!"GCRef", !12, i64 0}
!15 = !{!"MRef", !12, 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, !12, i64 8, !12, 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, !12, i64 20}
!20 = !{!12, !12, i64 0}
!21 = !{!11, !7, i64 41}
!22 = !{!11, !12, i64 52}
!23 = !{!11, !12, i64 56}
!24 = !{!11, !12, i64 60}
!25 = !{!11, !12, i64 64}
!26 = !{!11, !12, i64 44}
!27 = distinct !{!27, !28}
!28 = !{!"llvm.loop.mustprogress"}
!29 = distinct !{!29, !28}
!30 = !{!11, !12, i64 76}
!31 = !{!11, !12, i64 84}
!32 = !{!11, !12, i64 36}


; 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 i64 @gc_onestep(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define hidden void @lj_gc_fullgc(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = zext i32 %3 to i64
  %5 = inttoptr i64 %4 to ptr
  %6 = getelementptr inbounds i8, ptr %5, i64 88
  %7 = load volatile i32, ptr %6, align 8, !tbaa !8
  store volatile i32 -3, ptr %6, align 8, !tbaa !8
  %8 = getelementptr inbounds i8, ptr %5, i64 41
  %9 = load i8, ptr %8, align 1, !tbaa !19
  %10 = icmp ult i8 %9, 3
  br i1 %10, label %.thread, label %19

.thread:                                          ; preds = %1
  %11 = getelementptr inbounds i8, ptr %5, i64 48
  %12 = ptrtoint ptr %11 to i64
  %13 = trunc i64 %12 to i32
  %14 = getelementptr inbounds i8, ptr %5, i64 52
  store i32 %13, ptr %14, align 4, !tbaa !20
  %15 = getelementptr inbounds i8, ptr %5, i64 56
  store i32 0, ptr %15, align 8, !tbaa !21
  %16 = getelementptr inbounds i8, ptr %5, i64 60
  store i32 0, ptr %16, align 4, !tbaa !22
  %17 = getelementptr inbounds i8, ptr %5, i64 64
  store i32 0, ptr %17, align 8, !tbaa !23
  store i8 3, ptr %8, align 1, !tbaa !19
  %18 = getelementptr inbounds i8, ptr %5, i64 44
  store i32 0, ptr %18, align 4, !tbaa !24
  br label %.critedge.preheader

19:                                               ; preds = %1
  %.off21 = add i8 %9, -3
  %switch22 = icmp ult i8 %.off21, 2
  br i1 %switch22, label %.critedge.preheader, label %._crit_edge

.critedge.preheader:                              ; preds = %.thread, %19
  br label %.critedge

.critedge:                                        ; preds = %.critedge.preheader, %.critedge
  %20 = tail call i64 @gc_onestep(ptr noundef %0)
  %21 = load i8, ptr %8, align 1, !tbaa !19
  %.off = add i8 %21, -3
  %switch = icmp ult i8 %.off, 2
  br i1 %switch, label %.critedge, label %._crit_edge, !llvm.loop !25

._crit_edge:                                      ; preds = %.critedge, %19
  store i8 0, ptr %8, align 1, !tbaa !19
  br label %22

22:                                               ; preds = %22, %._crit_edge
  %23 = tail call i64 @gc_onestep(ptr noundef %0)
  %24 = load i8, ptr %8, align 1, !tbaa !19
  %.not = icmp eq i8 %24, 0
  br i1 %.not, label %25, label %22, !llvm.loop !27

25:                                               ; preds = %22
  %26 = getelementptr inbounds i8, ptr %5, i64 76
  %27 = load i32, ptr %26, align 4, !tbaa !28
  %28 = udiv i32 %27, 100
  %29 = getelementptr inbounds i8, ptr %5, i64 84
  %30 = load i32, ptr %29, align 4, !tbaa !29
  %31 = mul i32 %28, %30
  %32 = getelementptr inbounds i8, ptr %5, i64 36
  store i32 %31, ptr %32, align 4, !tbaa !30
  store volatile i32 %7, ptr %6, align 8, !tbaa !8
  ret void
}

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass