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

; 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.anon.0 = type { %union.anon.1, i32 }
%union.anon.1 = type { %struct.GCRef }
%struct.GCRef = type { i32 }
%struct.GCtab = type { %struct.GCRef, i8, i8, i8, i8, %struct.MRef, %struct.GCRef, %struct.GCRef, ptr, %struct.MRef, i32, i32 }
%struct.MRef = type { i32 }
%struct.GCudata = type { %struct.GCRef, i8, i8, i8, i8, %struct.GCRef, i32, %struct.GCRef, i32 }
%struct.lua_State = type { %union.anon }
%union.anon = type { %struct.anon, [320 x i8] }
%struct.anon = type { %struct.GCRef, i8, i8, i8, i8, %struct.MRef, %struct.GCRef, ptr, ptr, %struct.MRef, %struct.MRef, %struct.GCRef, %struct.GCRef, ptr, 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.4, %struct.MRef, i32 }
%union.anon.4 = 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 ptr @index2adr(ptr noundef, i32 noundef) #1

declare hidden void @lj_state_growstack1(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @lua_getmetatable(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = load i32, ptr %5, align 4, !tbaa !9
  %11 = call ptr @index2adr(ptr noundef %9, i32 noundef %10)
  store ptr %11, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store ptr null, ptr %7, align 8, !tbaa !5
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.anon.0, ptr %12, i32 0, i32 1
  %14 = load i32, ptr %13, align 4, !tbaa !11
  %15 = icmp eq i32 %14, -12
  br i1 %15, label %16, label %28

16:                                               ; preds = %2
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.anon.0, ptr %17, i32 0, i32 0
  %19 = getelementptr inbounds %struct.GCRef, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %19, align 8, !tbaa !11
  %21 = zext i32 %20 to i64
  %22 = inttoptr i64 %21 to ptr
  %23 = getelementptr inbounds %struct.GCtab, ptr %22, i32 0, i32 7
  %24 = getelementptr inbounds %struct.GCRef, ptr %23, i32 0, i32 0
  %25 = load i32, ptr %24, align 8, !tbaa !11
  %26 = zext i32 %25 to i64
  %27 = inttoptr i64 %26 to ptr
  store ptr %27, ptr %7, align 8, !tbaa !5
  br label %83

28:                                               ; preds = %2
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.anon.0, ptr %29, i32 0, i32 1
  %31 = load i32, ptr %30, align 4, !tbaa !11
  %32 = icmp eq i32 %31, -13
  br i1 %32, label %33, label %45

33:                                               ; preds = %28
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.anon.0, ptr %34, i32 0, i32 0
  %36 = getelementptr inbounds %struct.GCRef, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 8, !tbaa !11
  %38 = zext i32 %37 to i64
  %39 = inttoptr i64 %38 to ptr
  %40 = getelementptr inbounds %struct.GCudata, ptr %39, i32 0, i32 7
  %41 = getelementptr inbounds %struct.GCRef, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !11
  %43 = zext i32 %42 to i64
  %44 = inttoptr i64 %43 to ptr
  store ptr %44, ptr %7, align 8, !tbaa !5
  br label %82

45:                                               ; preds = %28
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.lua_State, ptr %46, i32 0, i32 0
  %48 = getelementptr inbounds %struct.anon, ptr %47, i32 0, i32 5
  %49 = getelementptr inbounds %struct.MRef, ptr %48, i32 0, i32 0
  %50 = load i32, ptr %49, align 8, !tbaa !11
  %51 = zext i32 %50 to i64
  %52 = inttoptr i64 %51 to ptr
  %53 = getelementptr inbounds %struct.global_State, ptr %52, i32 0, i32 29
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.anon.0, ptr %54, i32 0, i32 1
  %56 = load i32, ptr %55, align 4, !tbaa !11
  %57 = icmp ule i32 %56, -65537
  br i1 %57, label %58, label %59

58:                                               ; preds = %45
  br label %73

59:                                               ; preds = %45
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.anon.0, ptr %60, i32 0, i32 1
  %62 = load i32, ptr %61, align 4, !tbaa !11
  %63 = ashr i32 %62, 15
  %64 = icmp eq i32 %63, -2
  br i1 %64, label %65, label %66

65:                                               ; preds = %59
  br label %71

66:                                               ; preds = %59
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.anon.0, ptr %67, i32 0, i32 1
  %69 = load i32, ptr %68, align 4, !tbaa !11
  %70 = xor i32 %69, -1
  br label %71

71:                                               ; preds = %66, %65
  %72 = phi i32 [ 3, %65 ], [ %70, %66 ]
  br label %73

73:                                               ; preds = %71, %58
  %74 = phi i32 [ 13, %58 ], [ %72, %71 ]
  %75 = add i32 22, %74
  %76 = zext i32 %75 to i64
  %77 = getelementptr inbounds [38 x %struct.GCRef], ptr %53, i64 0, i64 %76
  %78 = getelementptr inbounds %struct.GCRef, ptr %77, i32 0, i32 0
  %79 = load i32, ptr %78, align 4, !tbaa !12
  %80 = zext i32 %79 to i64
  %81 = inttoptr i64 %80 to ptr
  store ptr %81, ptr %7, align 8, !tbaa !5
  br label %82

82:                                               ; preds = %73, %33
  br label %83

83:                                               ; preds = %82, %16
  %84 = load ptr, ptr %7, align 8, !tbaa !5
  %85 = icmp eq ptr %84, null
  br i1 %85, label %86, label %87

86:                                               ; preds = %83
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %112

87:                                               ; preds = %83
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  %89 = load ptr, ptr %4, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.lua_State, ptr %89, i32 0, i32 0
  %91 = getelementptr inbounds %struct.anon, ptr %90, i32 0, i32 8
  %92 = load ptr, ptr %91, align 8, !tbaa !11
  %93 = load ptr, ptr %7, align 8, !tbaa !5
  call void @settabV(ptr noundef %88, ptr noundef %92, ptr noundef %93)
  %94 = load ptr, ptr %4, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.lua_State, ptr %94, i32 0, i32 0
  %96 = getelementptr inbounds %struct.anon, ptr %95, i32 0, i32 8
  %97 = load ptr, ptr %96, align 8, !tbaa !11
  %98 = getelementptr inbounds %union.TValue, ptr %97, i32 1
  store ptr %98, ptr %96, align 8, !tbaa !11
  %99 = load ptr, ptr %4, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.lua_State, ptr %99, i32 0, i32 0
  %101 = getelementptr inbounds %struct.anon, ptr %100, i32 0, i32 9
  %102 = getelementptr inbounds %struct.MRef, ptr %101, i32 0, i32 0
  %103 = load i32, ptr %102, align 8, !tbaa !11
  %104 = zext i32 %103 to i64
  %105 = inttoptr i64 %104 to ptr
  %106 = icmp uge ptr %98, %105
  br i1 %106, label %107, label %109

107:                                              ; preds = %87
  %108 = load ptr, ptr %4, align 8, !tbaa !5
  call void @lj_state_growstack1(ptr noundef %108)
  br label %109

109:                                              ; preds = %107, %87
  %110 = phi i1 [ false, %87 ], [ false, %107 ]
  %111 = zext i1 %110 to i32
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %112

112:                                              ; preds = %109, %86
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %113 = load i32, ptr %3, align 4
  ret i32 %113
}

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

!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 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 0}
!13 = !{!"GCRef", !10, i64 0}


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

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

declare hidden void @lj_state_growstack1(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @lua_getmetatable(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call ptr @index2adr(ptr noundef %0, i32 noundef %1)
  %4 = getelementptr inbounds i8, ptr %3, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !5
  switch i32 %5, label %16 [
    i32 -12, label %6
    i32 -13, label %11
  ]

6:                                                ; preds = %2
  %7 = load i32, ptr %3, align 8, !tbaa !5
  %8 = zext i32 %7 to i64
  %9 = inttoptr i64 %8 to ptr
  %10 = getelementptr inbounds i8, ptr %9, i64 16
  br label %31

11:                                               ; preds = %2
  %12 = load i32, ptr %3, align 8, !tbaa !5
  %13 = zext i32 %12 to i64
  %14 = inttoptr i64 %13 to ptr
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  br label %31

16:                                               ; preds = %2
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  %18 = load i32, ptr %17, align 8, !tbaa !5
  %19 = zext i32 %18 to i64
  %20 = inttoptr i64 %19 to ptr
  %21 = getelementptr inbounds i8, ptr %20, i64 260
  %22 = icmp ult i32 %5, -65536
  br i1 %22, label %28, label %23

23:                                               ; preds = %16
  %.mask = and i32 %5, -32768
  %24 = icmp eq i32 %.mask, -65536
  br i1 %24, label %28, label %25

25:                                               ; preds = %23
  %26 = sub nsw i32 21, %5
  %27 = zext nneg i32 %26 to i64
  br label %28

28:                                               ; preds = %25, %23, %16
  %29 = phi i64 [ 35, %16 ], [ %27, %25 ], [ 25, %23 ]
  %30 = getelementptr inbounds [38 x %struct.GCRef], ptr %21, i64 0, i64 %29
  br label %31

31:                                               ; preds = %11, %28, %6
  %.0.in.in.in = phi ptr [ %10, %6 ], [ %15, %11 ], [ %30, %28 ]
  %.0.in.in = load i32, ptr %.0.in.in.in, align 4, !tbaa !5
  %32 = icmp eq i32 %.0.in.in, 0
  br i1 %32, label %43, label %33

33:                                               ; preds = %31
  %.0.in = zext i32 %.0.in.in to i64
  %.0 = inttoptr i64 %.0.in to ptr
  %34 = getelementptr inbounds i8, ptr %0, i64 24
  %35 = load ptr, ptr %34, align 8, !tbaa !5
  tail call void @settabV(ptr noundef %0, ptr noundef %35, ptr noundef nonnull %.0)
  %36 = load ptr, ptr %34, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %36, i64 8
  store ptr %37, ptr %34, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %0, i64 32
  %39 = load i32, ptr %38, align 8, !tbaa !5
  %40 = zext i32 %39 to i64
  %41 = inttoptr i64 %40 to ptr
  %.not = icmp ult ptr %37, %41
  br i1 %.not, label %43, label %42

42:                                               ; preds = %33
  tail call void @lj_state_growstack1(ptr noundef nonnull %0) #3
  br label %43

43:                                               ; preds = %33, %42, %31
  %.017 = phi i32 [ 0, %31 ], [ 1, %42 ], [ 1, %33 ]
  ret i32 %.017
}

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" }
attributes #1 = { "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 = { 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 #3 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


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