; LLVM IR from ejoy/ejoy2d :: lua (lapi)

; 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 { ptr, i8, i8, i8, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, %struct.CallInfo, ptr, i64, i32, i32, i32, i16, i16, i8, i8 }
%struct.CallInfo = type { ptr, ptr, ptr, ptr, %union.anon, i64, i16, i8 }
%union.anon = type { %struct.anon.0 }
%struct.anon.0 = type { ptr, i64, i64 }
%struct.lua_TValue = type { %union.Value, i32 }
%union.Value = type { ptr }
%struct.Table = type { ptr, i8, i8, i8, i8, i32, ptr, ptr, ptr, ptr, ptr }
%struct.GCObject = type { ptr, i8, i8 }
%struct.Udata = type { ptr, i8, i8, i8, ptr, i64, %union.Value }
%struct.global_State = type { ptr, ptr, i64, i64, i64, i64, %struct.stringtable, %struct.lua_TValue, i32, i8, i8, i8, i8, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, %struct.Mbuffer, i32, i32, i32, ptr, ptr, ptr, ptr, [24 x ptr], [9 x ptr] }
%struct.stringtable = type { ptr, i32, i32 }
%struct.Mbuffer = type { ptr, i64, i64 }

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

declare hidden void @luaC_barrier_(ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @lua_setmetatable(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = 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) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = load i32, ptr %4, align 4, !tbaa !9
  %9 = call ptr @index2addr(ptr noundef %7, i32 noundef %8)
  store ptr %9, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.lua_State, ptr %10, i32 0, i32 4
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = getelementptr inbounds %struct.lua_TValue, ptr %12, i64 -1
  %14 = getelementptr inbounds %struct.lua_TValue, ptr %13, i32 0, i32 1
  %15 = load i32, ptr %14, align 8, !tbaa !16
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %2
  store ptr null, ptr %6, align 8, !tbaa !5
  br label %25

18:                                               ; preds = %2
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.lua_State, ptr %19, i32 0, i32 4
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %22 = getelementptr inbounds %struct.lua_TValue, ptr %21, i64 -1
  %23 = getelementptr inbounds %struct.lua_TValue, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !18
  store ptr %24, ptr %6, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %18, %17
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.lua_TValue, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 8, !tbaa !16
  %29 = and i32 %28, 15
  switch i32 %29, label %104 [
    i32 5, label %30
    i32 7, label %67
  ]

30:                                               ; preds = %25
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.lua_TValue, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !18
  %35 = getelementptr inbounds %struct.Table, ptr %34, i32 0, i32 9
  store ptr %31, ptr %35, align 8, !tbaa !18
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = icmp ne ptr %36, null
  br i1 %37, label %38, label %66

38:                                               ; preds = %30
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.lua_TValue, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !18
  %42 = getelementptr inbounds %struct.GCObject, ptr %41, i32 0, i32 2
  %43 = load i8, ptr %42, align 1, !tbaa !19
  %44 = zext i8 %43 to i32
  %45 = and i32 %44, 4
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %60

47:                                               ; preds = %38
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Table, ptr %48, i32 0, i32 2
  %50 = load i8, ptr %49, align 1, !tbaa !21
  %51 = zext i8 %50 to i32
  %52 = and i32 %51, 3
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %60

54:                                               ; preds = %47
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.lua_TValue, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !18
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  call void @luaC_barrier_(ptr noundef %55, ptr noundef %58, ptr noundef %59)
  br label %60

60:                                               ; preds = %54, %47, %38
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.lua_TValue, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !18
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  call void @luaC_checkfinalizer(ptr noundef %61, ptr noundef %64, ptr noundef %65)
  br label %66

66:                                               ; preds = %60, %30
  br label %116

67:                                               ; preds = %25
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.lua_TValue, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !18
  %72 = getelementptr inbounds %struct.Udata, ptr %71, i32 0, i32 4
  store ptr %68, ptr %72, align 8, !tbaa !18
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = icmp ne ptr %73, null
  br i1 %74, label %75, label %103

75:                                               ; preds = %67
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.lua_TValue, ptr %76, i32 0, i32 0
  %78 = load ptr, ptr %77, align 8, !tbaa !18
  %79 = getelementptr inbounds %struct.Udata, ptr %78, i32 0, i32 2
  %80 = load i8, ptr %79, align 1, !tbaa !18
  %81 = zext i8 %80 to i32
  %82 = and i32 %81, 4
  %83 = icmp ne i32 %82, 0
  br i1 %83, label %84, label %97

84:                                               ; preds = %75
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.Table, ptr %85, i32 0, i32 2
  %87 = load i8, ptr %86, align 1, !tbaa !21
  %88 = zext i8 %87 to i32
  %89 = and i32 %88, 3
  %90 = icmp ne i32 %89, 0
  br i1 %90, label %91, label %97

91:                                               ; preds = %84
  %92 = load ptr, ptr %3, align 8, !tbaa !5
  %93 = load ptr, ptr %5, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.lua_TValue, ptr %93, i32 0, i32 0
  %95 = load ptr, ptr %94, align 8, !tbaa !18
  %96 = load ptr, ptr %6, align 8, !tbaa !5
  call void @luaC_barrier_(ptr noundef %92, ptr noundef %95, ptr noundef %96)
  br label %97

97:                                               ; preds = %91, %84, %75
  %98 = load ptr, ptr %3, align 8, !tbaa !5
  %99 = load ptr, ptr %5, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.lua_TValue, ptr %99, i32 0, i32 0
  %101 = load ptr, ptr %100, align 8, !tbaa !18
  %102 = load ptr, ptr %6, align 8, !tbaa !5
  call void @luaC_checkfinalizer(ptr noundef %98, ptr noundef %101, ptr noundef %102)
  br label %103

103:                                              ; preds = %97, %67
  br label %116

104:                                              ; preds = %25
  %105 = load ptr, ptr %6, align 8, !tbaa !5
  %106 = load ptr, ptr %3, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.lua_State, ptr %106, i32 0, i32 5
  %108 = load ptr, ptr %107, align 8, !tbaa !23
  %109 = getelementptr inbounds %struct.global_State, ptr %108, i32 0, i32 33
  %110 = load ptr, ptr %5, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.lua_TValue, ptr %110, i32 0, i32 1
  %112 = load i32, ptr %111, align 8, !tbaa !16
  %113 = and i32 %112, 15
  %114 = sext i32 %113 to i64
  %115 = getelementptr inbounds [9 x ptr], ptr %109, i64 0, i64 %114
  store ptr %105, ptr %115, align 8, !tbaa !5
  br label %116

116:                                              ; preds = %104, %103, %66
  %117 = load ptr, ptr %3, align 8, !tbaa !5
  %118 = getelementptr inbounds %struct.lua_State, ptr %117, i32 0, i32 4
  %119 = load ptr, ptr %118, align 8, !tbaa !11
  %120 = getelementptr inbounds %struct.lua_TValue, ptr %119, i32 -1
  store ptr %120, ptr %118, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret i32 1
}

declare hidden void @luaC_checkfinalizer(ptr noundef, ptr noundef, ptr noundef) #2

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 = { 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, !6, i64 16}
!12 = !{!"lua_State", !6, i64 0, !7, i64 8, !7, i64 9, !7, i64 10, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !13, i64 96, !6, i64 168, !14, i64 176, !10, i64 184, !10, i64 188, !10, i64 192, !15, i64 196, !15, i64 198, !7, i64 200, !7, i64 201}
!13 = !{!"CallInfo", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !7, i64 32, !14, i64 56, !15, i64 64, !7, i64 66}
!14 = !{!"long", !7, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = !{!17, !10, i64 8}
!17 = !{!"lua_TValue", !7, i64 0, !10, i64 8}
!18 = !{!7, !7, i64 0}
!19 = !{!20, !7, i64 9}
!20 = !{!"GCObject", !6, i64 0, !7, i64 8, !7, i64 9}
!21 = !{!22, !7, i64 9}
!22 = !{!"Table", !6, i64 0, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11, !10, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!23 = !{!12, !6, i64 24}


; 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 ptr @index2addr(ptr noundef, i32 noundef) local_unnamed_addr #0

declare hidden void @luaC_barrier_(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @lua_setmetatable(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call ptr @index2addr(ptr noundef %0, i32 noundef %1)
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 -8
  %7 = load i32, ptr %6, align 8, !tbaa !14
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %12, label %9

9:                                                ; preds = %2
  %10 = getelementptr inbounds i8, ptr %5, i64 -16
  %11 = load ptr, ptr %10, align 8, !tbaa !16
  br label %12

12:                                               ; preds = %2, %9
  %.0 = phi ptr [ %11, %9 ], [ null, %2 ]
  %13 = getelementptr inbounds i8, ptr %3, i64 8
  %14 = load i32, ptr %13, align 8, !tbaa !14
  %15 = and i32 %14, 15
  switch i32 %15, label %46 [
    i32 5, label %16
    i32 7, label %31
  ]

16:                                               ; preds = %12
  %17 = load ptr, ptr %3, align 8, !tbaa !16
  %18 = getelementptr inbounds i8, ptr %17, i64 40
  store ptr %.0, ptr %18, align 8, !tbaa !16
  %.not32 = icmp eq ptr %.0, null
  br i1 %.not32, label %52, label %19

19:                                               ; preds = %16
  %20 = load ptr, ptr %3, align 8, !tbaa !16
  %21 = getelementptr inbounds i8, ptr %20, i64 9
  %22 = load i8, ptr %21, align 1, !tbaa !17
  %23 = and i8 %22, 4
  %.not33 = icmp eq i8 %23, 0
  br i1 %.not33, label %29, label %24

24:                                               ; preds = %19
  %25 = getelementptr inbounds i8, ptr %.0, i64 9
  %26 = load i8, ptr %25, align 1, !tbaa !19
  %27 = and i8 %26, 3
  %.not34 = icmp eq i8 %27, 0
  br i1 %.not34, label %29, label %28

28:                                               ; preds = %24
  tail call void @luaC_barrier_(ptr noundef nonnull %0, ptr noundef nonnull %20, ptr noundef nonnull %.0) #2
  %.pre35 = load ptr, ptr %3, align 8, !tbaa !16
  br label %29

29:                                               ; preds = %28, %24, %19
  %30 = phi ptr [ %.pre35, %28 ], [ %20, %24 ], [ %20, %19 ]
  tail call void @luaC_checkfinalizer(ptr noundef nonnull %0, ptr noundef %30, ptr noundef nonnull %.0) #2
  br label %52

31:                                               ; preds = %12
  %32 = load ptr, ptr %3, align 8, !tbaa !16
  %33 = getelementptr inbounds i8, ptr %32, i64 16
  store ptr %.0, ptr %33, align 8, !tbaa !16
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %52, label %34

34:                                               ; preds = %31
  %35 = load ptr, ptr %3, align 8, !tbaa !16
  %36 = getelementptr inbounds i8, ptr %35, i64 9
  %37 = load i8, ptr %36, align 1, !tbaa !16
  %38 = and i8 %37, 4
  %.not30 = icmp eq i8 %38, 0
  br i1 %.not30, label %44, label %39

39:                                               ; preds = %34
  %40 = getelementptr inbounds i8, ptr %.0, i64 9
  %41 = load i8, ptr %40, align 1, !tbaa !19
  %42 = and i8 %41, 3
  %.not31 = icmp eq i8 %42, 0
  br i1 %.not31, label %44, label %43

43:                                               ; preds = %39
  tail call void @luaC_barrier_(ptr noundef nonnull %0, ptr noundef nonnull %35, ptr noundef nonnull %.0) #2
  %.pre = load ptr, ptr %3, align 8, !tbaa !16
  br label %44

44:                                               ; preds = %43, %39, %34
  %45 = phi ptr [ %.pre, %43 ], [ %35, %39 ], [ %35, %34 ]
  tail call void @luaC_checkfinalizer(ptr noundef nonnull %0, ptr noundef %45, ptr noundef nonnull %.0) #2
  br label %52

46:                                               ; preds = %12
  %47 = getelementptr inbounds i8, ptr %0, i64 24
  %48 = load ptr, ptr %47, align 8, !tbaa !21
  %49 = getelementptr inbounds i8, ptr %48, i64 440
  %50 = zext nneg i32 %15 to i64
  %51 = getelementptr inbounds [9 x ptr], ptr %49, i64 0, i64 %50
  store ptr %.0, ptr %51, align 8, !tbaa !22
  br label %52

52:                                               ; preds = %31, %44, %16, %29, %46
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds i8, ptr %53, i64 -16
  store ptr %54, ptr %4, align 8, !tbaa !5
  ret i32 1
}

declare hidden void @luaC_checkfinalizer(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

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 = { 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, !7, i64 16}
!6 = !{!"lua_State", !7, i64 0, !8, i64 8, !8, i64 9, !8, i64 10, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !10, i64 96, !7, i64 168, !11, i64 176, !13, i64 184, !13, i64 188, !13, i64 192, !12, i64 196, !12, i64 198, !8, i64 200, !8, i64 201}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"CallInfo", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !8, i64 32, !11, i64 56, !12, i64 64, !8, i64 66}
!11 = !{!"long", !8, i64 0}
!12 = !{!"short", !8, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = !{!15, !13, i64 8}
!15 = !{!"lua_TValue", !8, i64 0, !13, i64 8}
!16 = !{!8, !8, i64 0}
!17 = !{!18, !8, i64 9}
!18 = !{!"GCObject", !7, i64 0, !8, i64 8, !8, i64 9}
!19 = !{!20, !8, i64 9}
!20 = !{!"Table", !7, i64 0, !8, i64 8, !8, i64 9, !8, i64 10, !8, i64 11, !13, i64 12, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48}
!21 = !{!6, !7, i64 24}
!22 = !{!7, !7, i64 0}


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