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

; 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.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.lua_TValue = type { %union.Value, i32 }
%union.Value = type { ptr }
%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: nounwind uwtable
define hidden ptr @luaM_realloc_(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  store i64 %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.lua_State, ptr %12, i32 0, i32 5
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  store ptr %14, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %19

17:                                               ; preds = %4
  %18 = load i64, ptr %7, align 8, !tbaa !9
  br label %20

19:                                               ; preds = %4
  br label %20

20:                                               ; preds = %19, %17
  %21 = phi i64 [ %18, %17 ], [ 0, %19 ]
  store i64 %21, ptr %11, align 8, !tbaa !9
  %22 = load ptr, ptr %10, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.global_State, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = load ptr, ptr %10, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.global_State, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !21
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = load i64, ptr %8, align 8, !tbaa !9
  %31 = call ptr %24(ptr noundef %27, ptr noundef %28, i64 noundef %29, i64 noundef %30)
  store ptr %31, ptr %9, align 8, !tbaa !5
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = icmp eq ptr %32, null
  br i1 %33, label %34, label %54

34:                                               ; preds = %20
  %35 = load i64, ptr %8, align 8, !tbaa !9
  %36 = icmp ugt i64 %35, 0
  br i1 %36, label %37, label %54

37:                                               ; preds = %34
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  call void @luaC_fullgc(ptr noundef %38, i32 noundef 1)
  %39 = load ptr, ptr %10, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.global_State, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !16
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.global_State, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !21
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load i64, ptr %7, align 8, !tbaa !9
  %47 = load i64, ptr %8, align 8, !tbaa !9
  %48 = call ptr %41(ptr noundef %44, ptr noundef %45, i64 noundef %46, i64 noundef %47)
  store ptr %48, ptr %9, align 8, !tbaa !5
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  %50 = icmp eq ptr %49, null
  br i1 %50, label %51, label %53

51:                                               ; preds = %37
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  call void @luaD_throw(ptr noundef %52, i32 noundef 4) #5
  unreachable

53:                                               ; preds = %37
  br label %54

54:                                               ; preds = %53, %34, %20
  %55 = load ptr, ptr %10, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.global_State, ptr %55, i32 0, i32 3
  %57 = load i64, ptr %56, align 8, !tbaa !22
  %58 = load i64, ptr %8, align 8, !tbaa !9
  %59 = add i64 %57, %58
  %60 = load i64, ptr %11, align 8, !tbaa !9
  %61 = sub i64 %59, %60
  %62 = load ptr, ptr %10, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.global_State, ptr %62, i32 0, i32 3
  store i64 %61, ptr %63, align 8, !tbaa !22
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  ret ptr %64
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare hidden void @luaC_fullgc(ptr noundef, i32 noundef) #2

; Function Attrs: noreturn
declare hidden void @luaD_throw(ptr noundef, i32 noundef) #3

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 = { noreturn "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 }
attributes #5 = { noreturn }

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 24}
!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, !10, i64 176, !15, i64 184, !15, i64 188, !15, i64 192, !14, i64 196, !14, 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, !10, i64 56, !14, i64 64, !7, i64 66}
!14 = !{!"short", !7, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!17, !6, i64 0}
!17 = !{!"global_State", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !18, i64 48, !19, i64 64, !15, 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, !20, i64 176, !15, i64 200, !15, i64 204, !15, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !7, i64 248, !7, i64 440}
!18 = !{!"stringtable", !6, i64 0, !15, i64 8, !15, i64 12}
!19 = !{!"lua_TValue", !7, i64 0, !15, i64 8}
!20 = !{!"Mbuffer", !6, i64 0, !10, i64 8, !10, i64 16}
!21 = !{!17, !6, i64 8}
!22 = !{!17, !10, 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
define hidden ptr @luaM_realloc_(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = load ptr, ptr %6, align 8, !tbaa !14
  %8 = getelementptr inbounds i8, ptr %6, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !19
  %10 = tail call ptr %7(ptr noundef %9, ptr noundef %1, i64 noundef %2, i64 noundef %3) #3
  %11 = icmp eq ptr %10, null
  %12 = icmp ne i64 %3, 0
  %or.cond = and i1 %12, %11
  br i1 %or.cond, label %13, label %19

13:                                               ; preds = %4
  tail call void @luaC_fullgc(ptr noundef nonnull %0, i32 noundef 1) #3
  %14 = load ptr, ptr %6, align 8, !tbaa !14
  %15 = load ptr, ptr %8, align 8, !tbaa !19
  %16 = tail call ptr %14(ptr noundef %15, ptr noundef %1, i64 noundef %2, i64 noundef %3) #3
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %19

18:                                               ; preds = %13
  tail call void @luaD_throw(ptr noundef nonnull %0, i32 noundef 4) #4
  unreachable

19:                                               ; preds = %13, %4
  %.0 = phi ptr [ %16, %13 ], [ %10, %4 ]
  %.not = icmp eq ptr %1, null
  %20 = select i1 %.not, i64 0, i64 %2
  %21 = getelementptr inbounds i8, ptr %6, i64 24
  %22 = load i64, ptr %21, align 8, !tbaa !20
  %23 = sub i64 %3, %20
  %24 = add i64 %23, %22
  store i64 %24, ptr %21, align 8, !tbaa !20
  ret ptr %.0
}

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

; Function Attrs: noreturn
declare hidden void @luaD_throw(ptr noundef, i32 noundef) local_unnamed_addr #2

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 = { noreturn "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 }
attributes #4 = { noreturn 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 24}
!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, !7, i64 0}
!15 = !{!"global_State", !7, i64 0, !7, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !16, i64 48, !17, i64 64, !13, i64 80, !8, i64 84, !8, i64 85, !8, i64 86, !8, i64 87, !7, i64 88, !7, i64 96, !7, i64 104, !7, i64 112, !7, i64 120, !7, i64 128, !7, i64 136, !7, i64 144, !7, i64 152, !7, i64 160, !7, i64 168, !18, i64 176, !13, i64 200, !13, i64 204, !13, i64 208, !7, i64 216, !7, i64 224, !7, i64 232, !7, i64 240, !8, i64 248, !8, i64 440}
!16 = !{!"stringtable", !7, i64 0, !13, i64 8, !13, i64 12}
!17 = !{!"lua_TValue", !8, i64 0, !13, i64 8}
!18 = !{!"Mbuffer", !7, i64 0, !11, i64 8, !11, i64 16}
!19 = !{!15, !7, i64 8}
!20 = !{!15, !11, i64 24}


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