; 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_TValue = type { %union.Value, i32 }
%union.Value = type { ptr }
%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.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 }
%struct.TString = type { ptr, i8, i8, i8, i32, i64, ptr }

; 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

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

18:                                               ; preds = %3
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.lua_TValue, ptr %19, i32 0, i32 1
  %21 = load i32, ptr %20, align 8, !tbaa !11
  %22 = and i32 %21, 15
  %23 = icmp eq i32 %22, 3
  br i1 %23, label %30, label %24

24:                                               ; preds = %18
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %29

27:                                               ; preds = %24
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 0, ptr %28, align 8, !tbaa !13
  br label %29

29:                                               ; preds = %27, %24
  store ptr null, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %60

30:                                               ; preds = %18
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.lua_State, ptr %31, i32 0, i32 5
  %33 = load ptr, ptr %32, align 8, !tbaa !15
  %34 = getelementptr inbounds %struct.global_State, ptr %33, i32 0, i32 3
  %35 = load i64, ptr %34, align 8, !tbaa !19
  %36 = icmp sgt i64 %35, 0
  br i1 %36, label %37, label %39

37:                                               ; preds = %30
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  call void @luaC_step(ptr noundef %38)
  br label %39

39:                                               ; preds = %37, %30
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = call ptr @index2addr(ptr noundef %40, i32 noundef %41)
  store ptr %42, ptr %8, align 8, !tbaa !5
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  call void @luaO_tostring(ptr noundef %43, ptr noundef %44)
  br label %45

45:                                               ; preds = %39, %3
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = icmp ne ptr %46, null
  br i1 %47, label %48, label %55

48:                                               ; preds = %45
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.lua_TValue, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !23
  %52 = getelementptr inbounds %struct.TString, ptr %51, i32 0, i32 5
  %53 = load i64, ptr %52, align 8, !tbaa !23
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %53, ptr %54, align 8, !tbaa !13
  br label %55

55:                                               ; preds = %48, %45
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.lua_TValue, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !23
  %59 = getelementptr inbounds i8, ptr %58, i64 32
  store ptr %59, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %60

60:                                               ; preds = %55, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %61 = load ptr, ptr %4, align 8
  ret ptr %61
}

declare hidden void @luaC_step(ptr noundef) #2

declare hidden void @luaO_tostring(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, !10, i64 8}
!12 = !{!"lua_TValue", !7, i64 0, !10, i64 8}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!16, !6, i64 24}
!16 = !{!"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, !17, i64 96, !6, i64 168, !14, i64 176, !10, i64 184, !10, i64 188, !10, i64 192, !18, i64 196, !18, i64 198, !7, i64 200, !7, i64 201}
!17 = !{!"CallInfo", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !7, i64 32, !14, i64 56, !18, i64 64, !7, i64 66}
!18 = !{!"short", !7, i64 0}
!19 = !{!20, !14, i64 24}
!20 = !{!"global_State", !6, i64 0, !6, i64 8, !14, i64 16, !14, i64 24, !14, i64 32, !14, i64 40, !21, i64 48, !12, i64 64, !10, i64 80, !7, i64 84, !7, i64 85, !7, i64 86, !7, i64 87, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !22, i64 176, !10, i64 200, !10, i64 204, !10, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !7, i64 248, !7, i64 440}
!21 = !{!"stringtable", !6, i64 0, !10, i64 8, !10, i64 12}
!22 = !{!"Mbuffer", !6, i64 0, !14, i64 8, !14, i64 16}
!23 = !{!7, !7, 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"

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

; Function Attrs: nounwind uwtable
define dso_local ptr @lua_tolstring(ptr noundef %0, i32 noundef %1, ptr noundef writeonly %2) local_unnamed_addr #0 {
  %4 = tail call ptr @index2addr(ptr noundef %0, i32 noundef %1)
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = and i32 %6, 15
  switch i32 %7, label %8 [
    i32 4, label %19
    i32 3, label %10
  ]

8:                                                ; preds = %3
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %27, label %9

9:                                                ; preds = %8
  store i64 0, ptr %2, align 8, !tbaa !10
  br label %27

10:                                               ; preds = %3
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %13 = getelementptr inbounds i8, ptr %12, i64 24
  %14 = load i64, ptr %13, align 8, !tbaa !17
  %15 = icmp sgt i64 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %10
  tail call void @luaC_step(ptr noundef nonnull %0) #2
  br label %17

17:                                               ; preds = %16, %10
  %18 = tail call ptr @index2addr(ptr noundef nonnull %0, i32 noundef %1)
  tail call void @luaO_tostring(ptr noundef nonnull %0, ptr noundef %18) #2
  br label %19

19:                                               ; preds = %3, %17
  %.0 = phi ptr [ %4, %3 ], [ %18, %17 ]
  %.not19 = icmp eq ptr %2, null
  br i1 %.not19, label %24, label %20

20:                                               ; preds = %19
  %21 = load ptr, ptr %.0, align 8, !tbaa !21
  %22 = getelementptr inbounds i8, ptr %21, i64 16
  %23 = load i64, ptr %22, align 8, !tbaa !21
  store i64 %23, ptr %2, align 8, !tbaa !10
  br label %24

24:                                               ; preds = %20, %19
  %25 = load ptr, ptr %.0, align 8, !tbaa !21
  %26 = getelementptr inbounds i8, ptr %25, i64 32
  br label %27

27:                                               ; preds = %8, %9, %24
  %.016 = phi ptr [ %26, %24 ], [ null, %9 ], [ null, %8 ]
  ret ptr %.016
}

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

declare hidden void @luaO_tostring(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, !9, i64 8}
!6 = !{!"lua_TValue", !7, i64 0, !9, i64 8}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !14, i64 24}
!13 = !{!"lua_State", !14, i64 0, !7, i64 8, !7, i64 9, !7, i64 10, !14, i64 16, !14, i64 24, !14, i64 32, !14, i64 40, !14, i64 48, !14, i64 56, !14, i64 64, !14, i64 72, !14, i64 80, !14, i64 88, !15, i64 96, !14, i64 168, !11, i64 176, !9, i64 184, !9, i64 188, !9, i64 192, !16, i64 196, !16, i64 198, !7, i64 200, !7, i64 201}
!14 = !{!"any pointer", !7, i64 0}
!15 = !{!"CallInfo", !14, i64 0, !14, i64 8, !14, i64 16, !14, i64 24, !7, i64 32, !11, i64 56, !16, i64 64, !7, i64 66}
!16 = !{!"short", !7, i64 0}
!17 = !{!18, !11, i64 24}
!18 = !{!"global_State", !14, i64 0, !14, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !19, i64 48, !6, i64 64, !9, i64 80, !7, i64 84, !7, i64 85, !7, i64 86, !7, i64 87, !14, i64 88, !14, i64 96, !14, i64 104, !14, i64 112, !14, i64 120, !14, i64 128, !14, i64 136, !14, i64 144, !14, i64 152, !14, i64 160, !14, i64 168, !20, i64 176, !9, i64 200, !9, i64 204, !9, i64 208, !14, i64 216, !14, i64 224, !14, i64 232, !14, i64 240, !7, i64 248, !7, i64 440}
!19 = !{!"stringtable", !14, i64 0, !9, i64 8, !9, i64 12}
!20 = !{!"Mbuffer", !14, i64 0, !11, i64 8, !11, i64 16}
!21 = !{!7, !7, i64 0}


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