; 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
define hidden i32 @lj_gc_step(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.lua_State, ptr %8, i32 0, i32 0
  %10 = getelementptr inbounds %struct.anon.5, ptr %9, i32 0, i32 5
  %11 = getelementptr inbounds %struct.MRef, ptr %10, i32 0, i32 0
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = zext i32 %12 to i64
  %14 = inttoptr i64 %13 to ptr
  store ptr %14, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.global_State, ptr %15, i32 0, i32 6
  %17 = load volatile i32, ptr %16, align 8, !tbaa !10
  store i32 %17, ptr %6, align 4, !tbaa !20
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.global_State, ptr %18, i32 0, i32 6
  store volatile i32 -3, ptr %19, align 8, !tbaa !10
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.global_State, ptr %20, i32 0, i32 5
  %22 = getelementptr inbounds %struct.GCState, ptr %21, i32 0, i32 15
  %23 = load i32, ptr %22, align 8, !tbaa !21
  %24 = mul i32 10, %23
  store i32 %24, ptr %5, align 4, !tbaa !20
  %25 = load i32, ptr %5, align 4, !tbaa !20
  %26 = icmp eq i32 %25, 0
  br i1 %26, label %27, label %28

27:                                               ; preds = %1
  store i32 2147483392, ptr %5, align 4, !tbaa !20
  br label %28

28:                                               ; preds = %27, %1
  %29 = load ptr, ptr %4, 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 0
  %32 = load i32, ptr %31, align 8, !tbaa !22
  %33 = load ptr, ptr %4, 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 1
  %36 = load i32, ptr %35, align 4, !tbaa !23
  %37 = icmp ugt i32 %32, %36
  br i1 %37, label %38, label %53

38:                                               ; preds = %28
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.global_State, ptr %39, i32 0, i32 5
  %41 = getelementptr inbounds %struct.GCState, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !22
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.global_State, ptr %43, i32 0, i32 5
  %45 = getelementptr inbounds %struct.GCState, ptr %44, i32 0, i32 1
  %46 = load i32, ptr %45, align 4, !tbaa !23
  %47 = sub i32 %42, %46
  %48 = load ptr, ptr %4, 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 13
  %51 = load i32, ptr %50, align 8, !tbaa !24
  %52 = add i32 %51, %47
  store i32 %52, ptr %50, align 8, !tbaa !24
  br label %53

53:                                               ; preds = %38, %28
  br label %54

54:                                               ; preds = %84, %53
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = call i64 @gc_onestep(ptr noundef %55)
  %57 = trunc i64 %56 to i32
  %58 = load i32, ptr %5, align 4, !tbaa !20
  %59 = sub i32 %58, %57
  store i32 %59, ptr %5, align 4, !tbaa !20
  %60 = load ptr, ptr %4, 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 !25
  %64 = zext i8 %63 to i32
  %65 = icmp eq i32 %64, 0
  br i1 %65, label %66, label %83

66:                                               ; preds = %54
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.global_State, ptr %67, i32 0, i32 5
  %69 = getelementptr inbounds %struct.GCState, ptr %68, i32 0, i32 14
  %70 = load i32, ptr %69, align 4, !tbaa !26
  %71 = udiv i32 %70, 100
  %72 = load ptr, ptr %4, 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 16
  %75 = load i32, ptr %74, align 4, !tbaa !27
  %76 = mul i32 %71, %75
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.global_State, ptr %77, i32 0, i32 5
  %79 = getelementptr inbounds %struct.GCState, ptr %78, i32 0, i32 1
  store i32 %76, ptr %79, align 4, !tbaa !23
  %80 = load i32, ptr %6, align 4, !tbaa !20
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.global_State, ptr %81, i32 0, i32 6
  store volatile i32 %80, ptr %82, align 8, !tbaa !10
  store i32 1, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %121

83:                                               ; preds = %54
  br label %84

84:                                               ; preds = %83
  %85 = load i32, ptr %5, align 4, !tbaa !20
  %86 = icmp sgt i32 %85, 0
  br i1 %86, label %54, label %87, !llvm.loop !28

87:                                               ; preds = %84
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.global_State, ptr %88, i32 0, i32 5
  %90 = getelementptr inbounds %struct.GCState, ptr %89, i32 0, i32 13
  %91 = load i32, ptr %90, align 8, !tbaa !24
  %92 = icmp ult i32 %91, 1024
  br i1 %92, label %93, label %105

93:                                               ; preds = %87
  %94 = load ptr, ptr %4, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.global_State, ptr %94, i32 0, i32 5
  %96 = getelementptr inbounds %struct.GCState, ptr %95, i32 0, i32 0
  %97 = load i32, ptr %96, align 8, !tbaa !22
  %98 = add i32 %97, 1024
  %99 = load ptr, ptr %4, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.global_State, ptr %99, i32 0, i32 5
  %101 = getelementptr inbounds %struct.GCState, ptr %100, i32 0, i32 1
  store i32 %98, ptr %101, align 4, !tbaa !23
  %102 = load i32, ptr %6, align 4, !tbaa !20
  %103 = load ptr, ptr %4, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.global_State, ptr %103, i32 0, i32 6
  store volatile i32 %102, ptr %104, align 8, !tbaa !10
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %121

105:                                              ; preds = %87
  %106 = load ptr, ptr %4, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.global_State, ptr %106, i32 0, i32 5
  %108 = getelementptr inbounds %struct.GCState, ptr %107, i32 0, i32 13
  %109 = load i32, ptr %108, align 8, !tbaa !24
  %110 = sub i32 %109, 1024
  store i32 %110, ptr %108, align 8, !tbaa !24
  %111 = load ptr, ptr %4, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.global_State, ptr %111, i32 0, i32 5
  %113 = getelementptr inbounds %struct.GCState, ptr %112, i32 0, i32 0
  %114 = load i32, ptr %113, align 8, !tbaa !22
  %115 = load ptr, ptr %4, align 8, !tbaa !5
  %116 = getelementptr inbounds %struct.global_State, ptr %115, i32 0, i32 5
  %117 = getelementptr inbounds %struct.GCState, ptr %116, i32 0, i32 1
  store i32 %114, ptr %117, align 4, !tbaa !23
  %118 = load i32, ptr %6, align 4, !tbaa !20
  %119 = load ptr, ptr %4, align 8, !tbaa !5
  %120 = getelementptr inbounds %struct.global_State, ptr %119, i32 0, i32 6
  store volatile i32 %118, ptr %120, align 8, !tbaa !10
  store i32 0, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %121

121:                                              ; preds = %105, %93, %66
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %122 = load i32, ptr %2, align 4
  ret i32 %122
}

; Function Attrs: nounwind uwtable
declare hidden i64 @gc_onestep(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 = !{!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, !12, i64 80}
!22 = !{!11, !12, i64 32}
!23 = !{!11, !12, i64 36}
!24 = !{!11, !12, i64 72}
!25 = !{!11, !7, i64 41}
!26 = !{!11, !12, i64 76}
!27 = !{!11, !12, i64 84}
!28 = distinct !{!28, !29}
!29 = !{!"llvm.loop.mustprogress"}


; 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
define hidden range(i32 -1, 2) i32 @lj_gc_step(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 32
  %9 = getelementptr inbounds i8, ptr %5, i64 80
  %10 = load i32, ptr %9, align 8, !tbaa !19
  %11 = mul i32 %10, 10
  %12 = icmp eq i32 %11, 0
  %spec.store.select = select i1 %12, i32 2147483392, i32 %11
  %13 = load i32, ptr %8, align 8, !tbaa !20
  %14 = getelementptr inbounds i8, ptr %5, i64 36
  %15 = load i32, ptr %14, align 4, !tbaa !21
  %16 = icmp ugt i32 %13, %15
  br i1 %16, label %17, label %22

17:                                               ; preds = %1
  %18 = sub i32 %13, %15
  %19 = getelementptr inbounds i8, ptr %5, i64 72
  %20 = load i32, ptr %19, align 8, !tbaa !22
  %21 = add i32 %18, %20
  store i32 %21, ptr %19, align 8, !tbaa !22
  br label %22

22:                                               ; preds = %17, %1
  %23 = getelementptr inbounds i8, ptr %5, i64 41
  br label %24

24:                                               ; preds = %35, %22
  %.029 = phi i32 [ %spec.store.select, %22 ], [ %37, %35 ]
  %25 = tail call i64 @gc_onestep(ptr noundef %0)
  %26 = load i8, ptr %23, align 1, !tbaa !23
  %27 = icmp eq i8 %26, 0
  br i1 %27, label %28, label %35

28:                                               ; preds = %24
  %29 = getelementptr inbounds i8, ptr %5, i64 76
  %30 = load i32, ptr %29, align 4, !tbaa !24
  %31 = udiv i32 %30, 100
  %32 = getelementptr inbounds i8, ptr %5, i64 84
  %33 = load i32, ptr %32, align 4, !tbaa !25
  %34 = mul i32 %31, %33
  br label %49

35:                                               ; preds = %24
  %36 = trunc i64 %25 to i32
  %37 = sub i32 %.029, %36
  %38 = icmp sgt i32 %37, 0
  br i1 %38, label %24, label %39, !llvm.loop !26

39:                                               ; preds = %35
  %40 = getelementptr inbounds i8, ptr %5, i64 72
  %41 = load i32, ptr %40, align 8, !tbaa !22
  %42 = icmp ult i32 %41, 1024
  br i1 %42, label %43, label %46

43:                                               ; preds = %39
  %44 = load i32, ptr %8, align 8, !tbaa !20
  %45 = add i32 %44, 1024
  br label %49

46:                                               ; preds = %39
  %47 = add i32 %41, -1024
  store i32 %47, ptr %40, align 8, !tbaa !22
  %48 = load i32, ptr %8, align 8, !tbaa !20
  br label %49

49:                                               ; preds = %46, %43, %28
  %.sink = phi i32 [ %48, %46 ], [ %45, %43 ], [ %34, %28 ]
  %.0 = phi i32 [ 0, %46 ], [ -1, %43 ], [ 1, %28 ]
  store i32 %.sink, ptr %14, align 4, !tbaa !21
  store volatile i32 %7, ptr %6, align 8, !tbaa !8
  ret i32 %.0
}

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

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, !11, i64 80}
!20 = !{!9, !11, i64 32}
!21 = !{!9, !11, i64 36}
!22 = !{!9, !11, i64 72}
!23 = !{!9, !6, i64 41}
!24 = !{!9, !11, i64 76}
!25 = !{!9, !11, i64 84}
!26 = distinct !{!26, !27}
!27 = !{!"llvm.loop.mustprogress"}


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