; LLVM IR from ramsdell/lineqpp :: 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, i32, i32, i16, i16, i8, i8, i32, i32, ptr, %struct.lua_TValue, %struct.lua_TValue, ptr, ptr, ptr, i64 }
%struct.lua_TValue = type { %union.Value, i32 }
%union.Value = type { ptr }
%struct.global_State = type { %struct.stringtable, ptr, ptr, i8, i8, i32, ptr, ptr, ptr, ptr, ptr, ptr, %struct.Mbuffer, i64, i64, i64, i64, i32, i32, ptr, %struct.lua_TValue, ptr, %struct.UpVal, [9 x ptr], [17 x ptr] }
%struct.stringtable = type { ptr, i32, i32 }
%struct.Mbuffer = type { ptr, i64, i64 }
%struct.UpVal = type { ptr, i8, i8, ptr, %union.anon }
%union.anon = type { %struct.lua_TValue }
%struct.CClosure = type { ptr, i8, i8, i8, i8, ptr, 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; 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_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
  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
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.lua_State, ptr %11, i32 0, i32 6
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = getelementptr inbounds %struct.global_State, ptr %13, i32 0, i32 14
  %15 = load i64, ptr %14, align 8, !tbaa !16
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.lua_State, ptr %16, i32 0, i32 6
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = getelementptr inbounds %struct.global_State, ptr %18, i32 0, i32 13
  %20 = load i64, ptr %19, align 8, !tbaa !21
  %21 = icmp uge i64 %15, %20
  br i1 %21, label %22, label %24

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

24:                                               ; preds = %22, %3
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = call ptr @getcurrenv(ptr noundef %28)
  %30 = call ptr @luaF_newCclosure(ptr noundef %26, i32 noundef %27, ptr noundef %29)
  store ptr %30, ptr %7, align 8, !tbaa !5
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.CClosure, ptr %32, i32 0, i32 7
  store ptr %31, ptr %33, align 8, !tbaa !22
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.lua_State, ptr %35, i32 0, i32 4
  %37 = load ptr, ptr %36, align 8, !tbaa !23
  %38 = sext i32 %34 to i64
  %39 = sub i64 0, %38
  %40 = getelementptr inbounds %struct.lua_TValue, ptr %37, i64 %39
  store ptr %40, ptr %36, align 8, !tbaa !23
  br label %41

41:                                               ; preds = %45, %24
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = add nsw i32 %42, -1
  store i32 %43, ptr %6, align 4, !tbaa !9
  %44 = icmp ne i32 %42, 0
  br i1 %44, label %45, label %66

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

66:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.lua_State, ptr %67, i32 0, i32 4
  %69 = load ptr, ptr %68, align 8, !tbaa !23
  store ptr %69, ptr %10, align 8, !tbaa !5
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.lua_TValue, ptr %71, i32 0, i32 0
  store ptr %70, ptr %72, align 8, !tbaa !22
  %73 = load ptr, ptr %10, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.lua_TValue, ptr %73, i32 0, i32 1
  store i32 6, ptr %74, align 8, !tbaa !25
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = load ptr, ptr %4, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.lua_State, ptr %76, i32 0, i32 4
  %78 = load ptr, ptr %77, align 8, !tbaa !23
  %79 = getelementptr inbounds %struct.lua_TValue, ptr %78, i32 1
  store ptr %79, ptr %77, align 8, !tbaa !23
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret void
}

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

; Function Attrs: nounwind uwtable
declare hidden ptr @getcurrenv(ptr noundef) #3

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 32}
!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, !10, i64 88, !10, i64 92, !13, i64 96, !13, i64 98, !7, i64 100, !7, i64 101, !10, i64 104, !10, i64 108, !6, i64 112, !14, i64 120, !14, i64 136, !6, i64 152, !6, i64 160, !6, i64 168, !15, i64 176}
!13 = !{!"short", !7, i64 0}
!14 = !{!"lua_TValue", !7, i64 0, !10, i64 8}
!15 = !{!"long", !7, i64 0}
!16 = !{!17, !15, i64 120}
!17 = !{!"global_State", !18, i64 0, !6, i64 16, !6, i64 24, !7, i64 32, !7, i64 33, !10, i64 36, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !19, i64 88, !15, i64 112, !15, i64 120, !15, i64 128, !15, i64 136, !10, i64 144, !10, i64 148, !6, i64 152, !14, i64 160, !6, i64 176, !20, i64 184, !7, i64 224, !7, i64 296}
!18 = !{!"stringtable", !6, i64 0, !10, i64 8, !10, i64 12}
!19 = !{!"Mbuffer", !6, i64 0, !15, i64 8, !15, i64 16}
!20 = !{!"UpVal", !6, i64 0, !7, i64 8, !7, i64 9, !6, i64 16, !7, i64 24}
!21 = !{!17, !15, i64 112}
!22 = !{!7, !7, i64 0}
!23 = !{!12, !6, i64 16}
!24 = !{i64 0, i64 8, !22}
!25 = !{!14, !10, i64 8}
!26 = distinct !{!26, !27}
!27 = !{!"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 }

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

; Function Attrs: nounwind uwtable
define dso_local void @lua_pushcclosure(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = getelementptr inbounds i8, ptr %0, i64 32
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 120
  %7 = load i64, ptr %6, align 8, !tbaa !14
  %8 = getelementptr inbounds i8, ptr %5, i64 112
  %9 = load i64, ptr %8, align 8, !tbaa !19
  %.not = icmp ult i64 %7, %9
  br i1 %.not, label %11, label %10

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

11:                                               ; preds = %10, %3
  %12 = tail call ptr @getcurrenv(ptr noundef nonnull %0)
  %13 = tail call ptr @luaF_newCclosure(ptr noundef nonnull %0, i32 noundef %2, ptr noundef %12) #2
  %14 = getelementptr inbounds i8, ptr %13, i64 32
  store ptr %1, ptr %14, align 8, !tbaa !20
  %15 = getelementptr inbounds i8, ptr %0, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !21
  %17 = sext i32 %2 to i64
  %18 = sub nsw i64 0, %17
  %19 = getelementptr inbounds %struct.lua_TValue, ptr %16, i64 %18
  store ptr %19, ptr %15, align 8, !tbaa !21
  %.not2526 = icmp eq i32 %2, 0
  br i1 %.not2526, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %11
  %20 = getelementptr inbounds i8, ptr %13, i64 40
  %xtraiter = and i64 %17, 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 = %.lr.ph
  %indvars.iv.next.prol = add nsw i64 %17, -1
  %21 = load ptr, ptr %15, align 8, !tbaa !21
  %22 = getelementptr inbounds %struct.lua_TValue, ptr %21, i64 %indvars.iv.next.prol
  %23 = getelementptr inbounds [1 x %struct.lua_TValue], ptr %20, i64 0, i64 %indvars.iv.next.prol
  %24 = load i64, ptr %22, align 8, !tbaa !20
  store i64 %24, ptr %23, align 8, !tbaa !20
  %25 = getelementptr inbounds i8, ptr %22, i64 8
  %26 = load i32, ptr %25, align 8, !tbaa !22
  %27 = getelementptr inbounds i8, ptr %23, i64 8
  store i32 %26, ptr %27, align 8, !tbaa !22
  br label %.prol.loopexit

.prol.loopexit:                                   ; preds = %.prol.loopexit.unr-lcssa, %.lr.ph
  %indvars.iv.unr = phi i64 [ %17, %.lr.ph ], [ %indvars.iv.next.prol, %.prol.loopexit.unr-lcssa ]
  %28 = icmp eq i32 %2, 1
  br i1 %28, label %._crit_edge.loopexit, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.prol.loopexit, %.lr.ph.new
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %.lr.ph.new ], [ %indvars.iv.unr, %.prol.loopexit ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %29 = load ptr, ptr %15, align 8, !tbaa !21
  %30 = getelementptr inbounds %struct.lua_TValue, ptr %29, i64 %indvars.iv.next
  %31 = getelementptr inbounds [1 x %struct.lua_TValue], ptr %20, i64 0, i64 %indvars.iv.next
  %32 = load i64, ptr %30, align 8, !tbaa !20
  store i64 %32, ptr %31, align 8, !tbaa !20
  %33 = getelementptr inbounds i8, ptr %30, i64 8
  %34 = load i32, ptr %33, align 8, !tbaa !22
  %35 = getelementptr inbounds i8, ptr %31, i64 8
  store i32 %34, ptr %35, align 8, !tbaa !22
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, -2
  %36 = load ptr, ptr %15, align 8, !tbaa !21
  %37 = getelementptr inbounds %struct.lua_TValue, ptr %36, i64 %indvars.iv.next.1
  %38 = getelementptr inbounds [1 x %struct.lua_TValue], ptr %20, i64 0, i64 %indvars.iv.next.1
  %39 = load i64, ptr %37, align 8, !tbaa !20
  store i64 %39, ptr %38, align 8, !tbaa !20
  %40 = getelementptr inbounds i8, ptr %37, i64 8
  %41 = load i32, ptr %40, align 8, !tbaa !22
  %42 = getelementptr inbounds i8, ptr %38, i64 8
  store i32 %41, ptr %42, align 8, !tbaa !22
  %.not25.1 = icmp eq i64 %indvars.iv.next.1, 0
  br i1 %.not25.1, label %._crit_edge.loopexit, label %.lr.ph.new, !llvm.loop !23

._crit_edge.loopexit:                             ; preds = %.lr.ph.new, %.prol.loopexit
  %.pre = load ptr, ptr %15, align 8, !tbaa !21
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %11
  %43 = phi ptr [ %.pre, %._crit_edge.loopexit ], [ %19, %11 ]
  store ptr %13, ptr %43, align 8, !tbaa !20
  %44 = getelementptr inbounds i8, ptr %43, i64 8
  store i32 6, ptr %44, align 8, !tbaa !22
  %45 = load ptr, ptr %15, align 8, !tbaa !21
  %46 = getelementptr inbounds i8, ptr %45, i64 16
  store ptr %46, ptr %15, align 8, !tbaa !21
  ret void
}

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

; Function Attrs: nounwind uwtable
declare hidden ptr @getcurrenv(ptr noundef) local_unnamed_addr #1

attributes #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 = { 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 = { 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 32}
!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, !10, i64 88, !10, i64 92, !11, i64 96, !11, i64 98, !8, i64 100, !8, i64 101, !10, i64 104, !10, i64 108, !7, i64 112, !12, i64 120, !12, i64 136, !7, i64 152, !7, i64 160, !7, i64 168, !13, i64 176}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!"lua_TValue", !8, i64 0, !10, i64 8}
!13 = !{!"long", !8, i64 0}
!14 = !{!15, !13, i64 120}
!15 = !{!"global_State", !16, i64 0, !7, i64 16, !7, i64 24, !8, i64 32, !8, i64 33, !10, i64 36, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !17, i64 88, !13, i64 112, !13, i64 120, !13, i64 128, !13, i64 136, !10, i64 144, !10, i64 148, !7, i64 152, !12, i64 160, !7, i64 176, !18, i64 184, !8, i64 224, !8, i64 296}
!16 = !{!"stringtable", !7, i64 0, !10, i64 8, !10, i64 12}
!17 = !{!"Mbuffer", !7, i64 0, !13, i64 8, !13, i64 16}
!18 = !{!"UpVal", !7, i64 0, !8, i64 8, !8, i64 9, !7, i64 16, !8, i64 24}
!19 = !{!15, !13, i64 112}
!20 = !{!8, !8, i64 0}
!21 = !{!6, !7, i64 16}
!22 = !{!12, !10, i64 8}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


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