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

; 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.GCtab = type { %struct.GCRef, i8, i8, i8, i8, %struct.MRef, %struct.GCRef, %struct.GCRef, ptr, %struct.MRef, i32, i32 }
%struct.GCRef = type { i32 }
%struct.MRef = type { i32 }
%struct.Cache = type { %struct.Node, %struct.MRef, i32 }
%struct.Node = type { %union.TValue, %union.TValue, %struct.MRef, %struct.MRef }
%union.TValue = type { i64 }
%struct.lua_State = type { %union.anon.2 }
%union.anon.2 = type { %struct.anon.3, [320 x i8] }
%struct.anon.3 = type { %struct.GCRef, i8, i8, i8, i8, %struct.MRef, %struct.GCRef, ptr, ptr, %struct.MRef, %struct.MRef, %struct.GCRef, %struct.GCRef, ptr, i32 }

; 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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctlz.i32(i32, i1 immarg) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @lj_mem_free(ptr noundef, ptr noundef, i64 noundef) #3

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

; Function Attrs: nounwind uwtable
define hidden void @lj_tab_commit_cache(ptr noundef %0, ptr noundef %1) #4 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.GCtab, ptr %9, i32 0, i32 8
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  store ptr %11, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.GCtab, ptr %12, i32 0, i32 8
  store ptr null, ptr %13, align 8, !tbaa !9
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.GCtab, ptr %16, i32 0, i32 10
  %18 = load i32, ptr %17, align 4, !tbaa !14
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.Cache, ptr %19, i32 0, i32 2
  %21 = load i32, ptr %20, align 4, !tbaa !15
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.GCtab, ptr %22, i32 0, i32 11
  %24 = load i32, ptr %23, align 8, !tbaa !18
  %25 = add i32 %24, 1
  %26 = icmp ugt i32 %21, %25
  br i1 %26, label %27, label %32

27:                                               ; preds = %2
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.Cache, ptr %28, i32 0, i32 2
  %30 = load i32, ptr %29, align 4, !tbaa !15
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %38, label %86

32:                                               ; preds = %2
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.GCtab, ptr %33, i32 0, i32 11
  %35 = load i32, ptr %34, align 8, !tbaa !18
  %36 = add i32 %35, 1
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %86

38:                                               ; preds = %32, %27
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.Cache, ptr %39, i32 0, i32 2
  %41 = load i32, ptr %40, align 4, !tbaa !15
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.GCtab, ptr %42, i32 0, i32 11
  %44 = load i32, ptr %43, align 8, !tbaa !18
  %45 = add i32 %44, 1
  %46 = icmp ugt i32 %41, %45
  br i1 %46, label %47, label %51

47:                                               ; preds = %38
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Cache, ptr %48, i32 0, i32 2
  %50 = load i32, ptr %49, align 4, !tbaa !15
  br label %56

51:                                               ; preds = %38
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.GCtab, ptr %52, i32 0, i32 11
  %54 = load i32, ptr %53, align 8, !tbaa !18
  %55 = add i32 %54, 1
  br label %56

56:                                               ; preds = %51, %47
  %57 = phi i32 [ %50, %47 ], [ %55, %51 ]
  %58 = icmp eq i32 %57, 1
  br i1 %58, label %59, label %60

59:                                               ; preds = %56
  br label %84

60:                                               ; preds = %56
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.Cache, ptr %61, i32 0, i32 2
  %63 = load i32, ptr %62, align 4, !tbaa !15
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.GCtab, ptr %64, i32 0, i32 11
  %66 = load i32, ptr %65, align 8, !tbaa !18
  %67 = add i32 %66, 1
  %68 = icmp ugt i32 %63, %67
  br i1 %68, label %69, label %73

69:                                               ; preds = %60
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.Cache, ptr %70, i32 0, i32 2
  %72 = load i32, ptr %71, align 4, !tbaa !15
  br label %78

73:                                               ; preds = %60
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.GCtab, ptr %74, i32 0, i32 11
  %76 = load i32, ptr %75, align 8, !tbaa !18
  %77 = add i32 %76, 1
  br label %78

78:                                               ; preds = %73, %69
  %79 = phi i32 [ %72, %69 ], [ %77, %73 ]
  %80 = sub i32 %79, 1
  %81 = call i32 @llvm.ctlz.i32(i32 %80, i1 true)
  %82 = xor i32 %81, 31
  %83 = add i32 1, %82
  br label %84

84:                                               ; preds = %78, %59
  %85 = phi i32 [ 1, %59 ], [ %83, %78 ]
  br label %87

86:                                               ; preds = %32, %27
  br label %87

87:                                               ; preds = %86, %84
  %88 = phi i32 [ %85, %84 ], [ 0, %86 ]
  call void @lj_tab_resize(ptr noundef %14, ptr noundef %15, i32 noundef %18, i32 noundef %88)
  %89 = load ptr, ptr %5, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.Cache, ptr %89, i32 0, i32 0
  %91 = getelementptr inbounds %struct.Node, ptr %90, i32 0, i32 2
  %92 = getelementptr inbounds %struct.MRef, ptr %91, i32 0, i32 0
  %93 = load i32, ptr %92, align 8, !tbaa !19
  %94 = zext i32 %93 to i64
  %95 = inttoptr i64 %94 to ptr
  store ptr %95, ptr %6, align 8, !tbaa !5
  br label %96

96:                                               ; preds = %99, %87
  %97 = load ptr, ptr %6, align 8, !tbaa !5
  %98 = icmp ne ptr %97, null
  br i1 %98, label %99, label %123

99:                                               ; preds = %96
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %100 = load ptr, ptr %6, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.Node, ptr %100, i32 0, i32 2
  %102 = getelementptr inbounds %struct.MRef, ptr %101, i32 0, i32 0
  %103 = load i32, ptr %102, align 8, !tbaa !20
  %104 = zext i32 %103 to i64
  %105 = inttoptr i64 %104 to ptr
  store ptr %105, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  store ptr null, ptr %8, align 8, !tbaa !5
  %106 = load ptr, ptr %3, align 8, !tbaa !5
  %107 = load ptr, ptr %4, align 8, !tbaa !5
  %108 = load ptr, ptr %6, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct.Node, ptr %108, i32 0, i32 1
  %110 = call ptr @lj_tab_newkey(ptr noundef %106, ptr noundef %107, ptr noundef %109)
  store ptr %110, ptr %8, align 8, !tbaa !5
  %111 = load ptr, ptr %8, align 8, !tbaa !5
  %112 = load ptr, ptr %6, align 8, !tbaa !5
  %113 = getelementptr inbounds %struct.Node, ptr %112, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %111, ptr align 8 %113, i64 8, i1 false), !tbaa.struct !21
  %114 = load ptr, ptr %3, align 8, !tbaa !5
  %115 = getelementptr inbounds %struct.lua_State, ptr %114, i32 0, i32 0
  %116 = getelementptr inbounds %struct.anon.3, ptr %115, i32 0, i32 5
  %117 = getelementptr inbounds %struct.MRef, ptr %116, i32 0, i32 0
  %118 = load i32, ptr %117, align 8, !tbaa !22
  %119 = zext i32 %118 to i64
  %120 = inttoptr i64 %119 to ptr
  %121 = load ptr, ptr %6, align 8, !tbaa !5
  call void @lj_mem_free(ptr noundef %120, ptr noundef %121, i64 noundef 24)
  %122 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %122, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %96, !llvm.loop !23

123:                                              ; preds = %96
  %124 = load ptr, ptr %3, align 8, !tbaa !5
  %125 = getelementptr inbounds %struct.lua_State, ptr %124, i32 0, i32 0
  %126 = getelementptr inbounds %struct.anon.3, ptr %125, i32 0, i32 5
  %127 = getelementptr inbounds %struct.MRef, ptr %126, i32 0, i32 0
  %128 = load i32, ptr %127, align 8, !tbaa !22
  %129 = zext i32 %128 to i64
  %130 = inttoptr i64 %129 to ptr
  %131 = load ptr, ptr %5, align 8, !tbaa !5
  call void @lj_mem_free(ptr noundef %130, ptr noundef %131, i64 noundef 32)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden ptr @lj_tab_newkey(ptr noundef, ptr noundef, ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { alwaysinline 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 #4 = { 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 #5 = { 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, !6, i64 24}
!10 = !{!"GCtab", !11, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !13, i64 8, !11, i64 12, !11, i64 16, !6, i64 24, !13, i64 32, !12, i64 36, !12, i64 40}
!11 = !{!"GCRef", !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"MRef", !12, i64 0}
!14 = !{!10, !12, i64 36}
!15 = !{!16, !12, i64 28}
!16 = !{!"", !17, i64 0, !13, i64 24, !12, i64 28}
!17 = !{!"Node", !7, i64 0, !7, i64 8, !13, i64 16, !13, i64 20}
!18 = !{!10, !12, i64 40}
!19 = !{!16, !12, i64 16}
!20 = !{!17, !12, i64 16}
!21 = !{i64 0, i64 8, !22}
!22 = !{!7, !7, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"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: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctlz.i32(i32, i1 immarg) #0

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @lj_mem_free(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @lj_tab_resize(ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define hidden void @lj_tab_commit_cache(ptr noundef %0, ptr noundef %1) local_unnamed_addr #2 {
  %3 = getelementptr inbounds i8, ptr %1, i64 24
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr null, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %1, i64 36
  %6 = load i32, ptr %5, align 4, !tbaa !13
  %7 = getelementptr inbounds i8, ptr %4, i64 28
  %8 = load i32, ptr %7, align 4, !tbaa !14
  %9 = getelementptr inbounds i8, ptr %1, i64 40
  %10 = load i32, ptr %9, align 8, !tbaa !17
  %11 = add i32 %10, 1
  %12 = or i32 %11, %8
  %or.cond = icmp eq i32 %12, 0
  br i1 %or.cond, label %21, label %13

13:                                               ; preds = %2
  %. = tail call i32 @llvm.umax.i32(i32 %8, i32 %11)
  %14 = icmp eq i32 %., 1
  br i1 %14, label %21, label %15

15:                                               ; preds = %13
  %16 = icmp ugt i32 %8, %11
  %17 = add i32 %8, -1
  %18 = select i1 %16, i32 %17, i32 %10
  %19 = tail call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 %18, i1 true)
  %20 = sub nuw nsw i32 32, %19
  br label %21

21:                                               ; preds = %2, %15, %13
  %22 = phi i32 [ %20, %15 ], [ 1, %13 ], [ 0, %2 ]
  tail call void @lj_tab_resize(ptr noundef %0, ptr noundef nonnull %1, i32 noundef %6, i32 noundef %22)
  %23 = getelementptr inbounds i8, ptr %4, i64 16
  %24 = load i32, ptr %23, align 8, !tbaa !18
  %.not3536 = icmp eq i32 %24, 0
  br i1 %.not3536, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %21
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  br label %26

26:                                               ; preds = %.lr.ph, %26
  %.0.in.in37 = phi i32 [ %24, %.lr.ph ], [ %28, %26 ]
  %.0.in = zext i32 %.0.in.in37 to i64
  %.0 = inttoptr i64 %.0.in to ptr
  %27 = getelementptr inbounds i8, ptr %.0, i64 16
  %28 = load i32, ptr %27, align 8, !tbaa !19
  %29 = getelementptr inbounds i8, ptr %.0, i64 8
  %30 = tail call ptr @lj_tab_newkey(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %29)
  %31 = load i64, ptr %.0, align 8, !tbaa !20
  store i64 %31, ptr %30, align 8, !tbaa !20
  %32 = load i32, ptr %25, align 8, !tbaa !20
  %33 = zext i32 %32 to i64
  %34 = inttoptr i64 %33 to ptr
  tail call void @lj_mem_free(ptr noundef %34, ptr noundef nonnull %.0, i64 noundef 24)
  %.not35 = icmp eq i32 %28, 0
  br i1 %.not35, label %._crit_edge, label %26, !llvm.loop !21

._crit_edge:                                      ; preds = %26, %21
  %35 = getelementptr inbounds i8, ptr %0, i64 8
  %36 = load i32, ptr %35, align 8, !tbaa !20
  %37 = zext i32 %36 to i64
  %38 = inttoptr i64 %37 to ptr
  tail call void @lj_mem_free(ptr noundef %38, ptr noundef %4, i64 noundef 32)
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden ptr @lj_tab_newkey(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umax.i32(i32, i32) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { alwaysinline 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 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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 24}
!6 = !{!"GCtab", !7, i64 0, !9, i64 4, !9, i64 5, !9, i64 6, !9, i64 7, !11, i64 8, !7, i64 12, !7, i64 16, !12, i64 24, !11, i64 32, !8, i64 36, !8, i64 40}
!7 = !{!"GCRef", !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"MRef", !8, i64 0}
!12 = !{!"any pointer", !9, i64 0}
!13 = !{!6, !8, i64 36}
!14 = !{!15, !8, i64 28}
!15 = !{!"", !16, i64 0, !11, i64 24, !8, i64 28}
!16 = !{!"Node", !9, i64 0, !9, i64 8, !11, i64 16, !11, i64 20}
!17 = !{!6, !8, i64 40}
!18 = !{!15, !8, i64 16}
!19 = !{!16, !8, i64 16}
!20 = !{!9, !9, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


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