; 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.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.CClosure = type { ptr, i8, i8, i8, ptr, ptr, [1 x %struct.lua_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: 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) #1

declare hidden void @luaC_step(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @lua_pushcclosure(ptr noundef %0, ptr noundef %1, i32 noundef %2) #3 {
  %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 ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %23

14:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.lua_State, ptr %15, i32 0, i32 4
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  store ptr %17, ptr %7, align 8, !tbaa !5
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.lua_TValue, ptr %19, i32 0, i32 0
  store ptr %18, ptr %20, align 8, !tbaa !16
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.lua_TValue, ptr %21, i32 0, i32 1
  store i32 22, ptr %22, align 8, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %74

23:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.lua_State, ptr %24, i32 0, i32 5
  %26 = load ptr, ptr %25, align 8, !tbaa !19
  %27 = getelementptr inbounds %struct.global_State, ptr %26, i32 0, i32 3
  %28 = load i64, ptr %27, align 8, !tbaa !20
  %29 = icmp sgt i64 %28, 0
  br i1 %29, label %30, label %32

30:                                               ; preds = %23
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  call void @luaC_step(ptr noundef %31)
  br label %32

32:                                               ; preds = %30, %23
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = call ptr @luaF_newCclosure(ptr noundef %33, i32 noundef %34)
  store ptr %35, ptr %8, align 8, !tbaa !5
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.CClosure, ptr %37, i32 0, i32 5
  store ptr %36, ptr %38, align 8, !tbaa !24
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.lua_State, ptr %40, i32 0, i32 4
  %42 = load ptr, ptr %41, align 8, !tbaa !11
  %43 = sext i32 %39 to i64
  %44 = sub i64 0, %43
  %45 = getelementptr inbounds %struct.lua_TValue, ptr %42, i64 %44
  store ptr %45, ptr %41, align 8, !tbaa !11
  br label %46

46:                                               ; preds = %50, %32
  %47 = load i32, ptr %6, align 4, !tbaa !9
  %48 = add nsw i32 %47, -1
  store i32 %48, ptr %6, align 4, !tbaa !9
  %49 = icmp ne i32 %47, 0
  br i1 %49, label %50, label %64

50:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.CClosure, ptr %51, i32 0, i32 6
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [1 x %struct.lua_TValue], ptr %52, i64 0, i64 %54
  store ptr %55, ptr %9, align 8, !tbaa !5
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.lua_State, ptr %57, i32 0, i32 4
  %59 = load ptr, ptr %58, align 8, !tbaa !11
  %60 = load i32, ptr %6, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds %struct.lua_TValue, ptr %59, i64 %61
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %56, ptr align 8 %62, i64 16, i1 false), !tbaa.struct !26
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %46, !llvm.loop !27

64:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.lua_State, ptr %65, i32 0, i32 4
  %67 = load ptr, ptr %66, align 8, !tbaa !11
  store ptr %67, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %68, ptr %11, align 8, !tbaa !5
  %69 = load ptr, ptr %11, align 8, !tbaa !5
  %70 = load ptr, ptr %10, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.lua_TValue, ptr %70, i32 0, i32 0
  store ptr %69, ptr %71, align 8, !tbaa !16
  %72 = load ptr, ptr %10, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.lua_TValue, ptr %72, i32 0, i32 1
  store i32 102, ptr %73, align 8, !tbaa !17
  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 %8) #4
  br label %74

74:                                               ; preds = %64, %14
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.lua_State, ptr %75, i32 0, i32 4
  %77 = load ptr, ptr %76, align 8, !tbaa !11
  %78 = getelementptr inbounds %struct.lua_TValue, ptr %77, i32 1
  store ptr %78, ptr %76, align 8, !tbaa !11
  ret void
}

declare hidden ptr @luaF_newCclosure(ptr noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!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 = !{!7, !7, i64 0}
!17 = !{!18, !10, i64 8}
!18 = !{!"lua_TValue", !7, i64 0, !10, i64 8}
!19 = !{!12, !6, i64 24}
!20 = !{!21, !14, i64 24}
!21 = !{!"global_State", !6, i64 0, !6, i64 8, !14, i64 16, !14, i64 24, !14, i64 32, !14, i64 40, !22, i64 48, !18, 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, !23, 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}
!22 = !{!"stringtable", !6, i64 0, !10, i64 8, !10, i64 12}
!23 = !{!"Mbuffer", !6, i64 0, !14, i64 8, !14, i64 16}
!24 = !{!25, !6, i64 24}
!25 = !{!"CClosure", !6, i64 0, !7, i64 8, !7, i64 9, !7, i64 10, !6, i64 16, !6, i64 24, !7, i64 32}
!26 = !{i64 0, i64 8, !16, i64 8, i64 4, !9}
!27 = distinct !{!27, !28}
!28 = !{!"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"

%struct.lua_TValue = type { %union.Value, i32 }
%union.Value = type { ptr }

; Function Attrs: mustprogress 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) #0

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

; Function Attrs: nounwind uwtable
define dso_local void @lua_pushcclosure(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = icmp eq i32 %2, 0
  br i1 %4, label %5, label %7

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  br label %.loopexit

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

13:                                               ; preds = %7
  tail call void @luaC_step(ptr noundef nonnull %0) #3
  br label %14

14:                                               ; preds = %13, %7
  %15 = tail call ptr @luaF_newCclosure(ptr noundef nonnull %0, i32 noundef %2) #3
  %16 = getelementptr inbounds i8, ptr %15, i64 24
  store ptr %1, ptr %16, align 8, !tbaa !19
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  %18 = load ptr, ptr %17, align 8, !tbaa !21
  %19 = sext i32 %2 to i64
  %20 = sub nsw i64 0, %19
  %21 = getelementptr inbounds %struct.lua_TValue, ptr %18, i64 %20
  store ptr %21, ptr %17, align 8, !tbaa !21
  %22 = getelementptr inbounds i8, ptr %15, i64 32
  %xtraiter = and i64 %19, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.prol.loopexit, label %.prol.loopexit.unr-lcssa

.prol.loopexit.unr-lcssa:                         ; preds = %14
  %indvars.iv.next.prol = add nsw i64 %19, -1
  %23 = getelementptr inbounds [1 x %struct.lua_TValue], ptr %22, i64 0, i64 %indvars.iv.next.prol
  %24 = load ptr, ptr %17, align 8, !tbaa !21
  %25 = getelementptr inbounds %struct.lua_TValue, ptr %24, i64 %indvars.iv.next.prol
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %23, ptr noundef nonnull align 8 dereferenceable(16) %25, i64 16, i1 false), !tbaa.struct !22
  br label %.prol.loopexit

.prol.loopexit:                                   ; preds = %.prol.loopexit.unr-lcssa, %14
  %indvars.iv.unr = phi i64 [ %19, %14 ], [ %indvars.iv.next.prol, %.prol.loopexit.unr-lcssa ]
  %26 = icmp eq i32 %2, 1
  br i1 %26, label %.loopexit, label %.new

.new:                                             ; preds = %.prol.loopexit, %.new
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %.new ], [ %indvars.iv.unr, %.prol.loopexit ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %27 = getelementptr inbounds [1 x %struct.lua_TValue], ptr %22, i64 0, i64 %indvars.iv.next
  %28 = load ptr, ptr %17, align 8, !tbaa !21
  %29 = getelementptr inbounds %struct.lua_TValue, ptr %28, i64 %indvars.iv.next
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %27, ptr noundef nonnull align 8 dereferenceable(16) %29, i64 16, i1 false), !tbaa.struct !22
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, -2
  %30 = getelementptr inbounds [1 x %struct.lua_TValue], ptr %22, i64 0, i64 %indvars.iv.next.1
  %31 = load ptr, ptr %17, align 8, !tbaa !21
  %32 = getelementptr inbounds %struct.lua_TValue, ptr %31, i64 %indvars.iv.next.1
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %30, ptr noundef nonnull align 8 dereferenceable(16) %32, i64 16, i1 false), !tbaa.struct !22
  %.not.1 = icmp eq i64 %indvars.iv.next.1, 0
  br i1 %.not.1, label %.loopexit, label %.new, !llvm.loop !25

.loopexit:                                        ; preds = %.prol.loopexit, %.new, %5
  %.sink32 = phi ptr [ %6, %5 ], [ %17, %.new ], [ %17, %.prol.loopexit ]
  %.sink30 = phi ptr [ %1, %5 ], [ %15, %.new ], [ %15, %.prol.loopexit ]
  %.sink = phi i32 [ 22, %5 ], [ 102, %.new ], [ 102, %.prol.loopexit ]
  %33 = load ptr, ptr %.sink32, align 8, !tbaa !21
  store ptr %.sink30, ptr %33, align 8, !tbaa !23
  %34 = getelementptr inbounds i8, ptr %33, i64 8
  store i32 %.sink, ptr %34, align 8, !tbaa !27
  %35 = getelementptr inbounds i8, ptr %0, i64 16
  %36 = load ptr, ptr %35, align 8, !tbaa !21
  %37 = getelementptr inbounds i8, ptr %36, i64 16
  store ptr %37, ptr %35, align 8, !tbaa !21
  ret void
}

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

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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, !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, !11, i64 24}
!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 = !{!20, !7, i64 24}
!20 = !{!"CClosure", !7, i64 0, !8, i64 8, !8, i64 9, !8, i64 10, !7, i64 16, !7, i64 24, !8, i64 32}
!21 = !{!6, !7, i64 16}
!22 = !{i64 0, i64 8, !23, i64 8, i64 4, !24}
!23 = !{!8, !8, i64 0}
!24 = !{!13, !13, i64 0}
!25 = distinct !{!25, !26}
!26 = !{!"llvm.loop.mustprogress"}
!27 = !{!17, !13, i64 8}


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