; 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 }

; 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

declare hidden void @luaC_step(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @lua_gc(ptr noundef %0, i32 noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.lua_State, ptr %10, i32 0, i32 6
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  store ptr %12, ptr %8, align 8, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !9
  switch i32 %13, label %90 [
    i32 0, label %14
    i32 1, label %17
    i32 2, label %23
    i32 3, label %25
    i32 4, label %31
    i32 5, label %37
    i32 6, label %76
    i32 7, label %83
  ]

14:                                               ; preds = %3
  %15 = load ptr, ptr %8, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.global_State, ptr %15, i32 0, i32 13
  store i64 -3, ptr %16, align 8, !tbaa !16
  br label %91

17:                                               ; preds = %3
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.global_State, ptr %18, i32 0, i32 14
  %20 = load i64, ptr %19, align 8, !tbaa !21
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.global_State, ptr %21, i32 0, i32 13
  store i64 %20, ptr %22, align 8, !tbaa !16
  br label %91

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

25:                                               ; preds = %3
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.global_State, ptr %26, i32 0, i32 14
  %28 = load i64, ptr %27, align 8, !tbaa !21
  %29 = lshr i64 %28, 10
  %30 = trunc i64 %29 to i32
  store i32 %30, ptr %7, align 4, !tbaa !9
  br label %91

31:                                               ; preds = %3
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.global_State, ptr %32, i32 0, i32 14
  %34 = load i64, ptr %33, align 8, !tbaa !21
  %35 = and i64 %34, 1023
  %36 = trunc i64 %35 to i32
  store i32 %36, ptr %7, align 4, !tbaa !9
  br label %91

37:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = shl i64 %39, 10
  store i64 %40, ptr %9, align 8, !tbaa !22
  %41 = load i64, ptr %9, align 8, !tbaa !22
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.global_State, ptr %42, i32 0, i32 14
  %44 = load i64, ptr %43, align 8, !tbaa !21
  %45 = icmp ule i64 %41, %44
  br i1 %45, label %46, label %54

46:                                               ; preds = %37
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.global_State, ptr %47, i32 0, i32 14
  %49 = load i64, ptr %48, align 8, !tbaa !21
  %50 = load i64, ptr %9, align 8, !tbaa !22
  %51 = sub i64 %49, %50
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.global_State, ptr %52, i32 0, i32 13
  store i64 %51, ptr %53, align 8, !tbaa !16
  br label %57

54:                                               ; preds = %37
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.global_State, ptr %55, i32 0, i32 13
  store i64 0, ptr %56, align 8, !tbaa !16
  br label %57

57:                                               ; preds = %54, %46
  br label %58

58:                                               ; preds = %74, %57
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.global_State, ptr %59, i32 0, i32 13
  %61 = load i64, ptr %60, align 8, !tbaa !16
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.global_State, ptr %62, i32 0, i32 14
  %64 = load i64, ptr %63, align 8, !tbaa !21
  %65 = icmp ule i64 %61, %64
  br i1 %65, label %66, label %75

66:                                               ; preds = %58
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  call void @luaC_step(ptr noundef %67)
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.global_State, ptr %68, i32 0, i32 4
  %70 = load i8, ptr %69, align 1, !tbaa !23
  %71 = zext i8 %70 to i32
  %72 = icmp eq i32 %71, 0
  br i1 %72, label %73, label %74

73:                                               ; preds = %66
  store i32 1, ptr %7, align 4, !tbaa !9
  br label %75

74:                                               ; preds = %66
  br label %58, !llvm.loop !24

75:                                               ; preds = %73, %58
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %91

76:                                               ; preds = %3
  %77 = load ptr, ptr %8, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.global_State, ptr %77, i32 0, i32 17
  %79 = load i32, ptr %78, align 8, !tbaa !26
  store i32 %79, ptr %7, align 4, !tbaa !9
  %80 = load i32, ptr %6, align 4, !tbaa !9
  %81 = load ptr, ptr %8, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.global_State, ptr %81, i32 0, i32 17
  store i32 %80, ptr %82, align 8, !tbaa !26
  br label %91

83:                                               ; preds = %3
  %84 = load ptr, ptr %8, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.global_State, ptr %84, i32 0, i32 18
  %86 = load i32, ptr %85, align 4, !tbaa !27
  store i32 %86, ptr %7, align 4, !tbaa !9
  %87 = load i32, ptr %6, align 4, !tbaa !9
  %88 = load ptr, ptr %8, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.global_State, ptr %88, i32 0, i32 18
  store i32 %87, ptr %89, align 4, !tbaa !27
  br label %91

90:                                               ; preds = %3
  store i32 -1, ptr %7, align 4, !tbaa !9
  br label %91

91:                                               ; preds = %90, %83, %76, %75, %31, %25, %23, %17, %14
  %92 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret i32 %92
}

declare hidden void @luaC_fullgc(ptr noundef) #1

attributes #0 = { nocallback nofree nosync 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, !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 112}
!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 120}
!22 = !{!15, !15, i64 0}
!23 = !{!17, !7, i64 33}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!17, !10, i64 144}
!27 = !{!17, !10, i64 148}


; 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"

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

; Function Attrs: nounwind uwtable
define dso_local i32 @lua_gc(ptr noundef %0, i32 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
  switch i32 %1, label %.loopexit [
    i32 0, label %6
    i32 1, label %8
    i32 2, label %12
    i32 3, label %13
    i32 4, label %18
    i32 5, label %23
    i32 6, label %36
    i32 7, label %39
  ]

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %5, i64 112
  store i64 -3, ptr %7, align 8, !tbaa !14
  br label %.loopexit

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %5, i64 120
  %10 = load i64, ptr %9, align 8, !tbaa !19
  %11 = getelementptr inbounds i8, ptr %5, i64 112
  store i64 %10, ptr %11, align 8, !tbaa !14
  br label %.loopexit

12:                                               ; preds = %3
  tail call void @luaC_fullgc(ptr noundef nonnull %0) #3
  br label %.loopexit

13:                                               ; preds = %3
  %14 = getelementptr inbounds i8, ptr %5, i64 120
  %15 = load i64, ptr %14, align 8, !tbaa !19
  %16 = lshr i64 %15, 10
  %17 = trunc i64 %16 to i32
  br label %.loopexit

18:                                               ; preds = %3
  %19 = getelementptr inbounds i8, ptr %5, i64 120
  %20 = load i64, ptr %19, align 8, !tbaa !19
  %21 = trunc i64 %20 to i32
  %22 = and i32 %21, 1023
  br label %.loopexit

23:                                               ; preds = %3
  %24 = sext i32 %2 to i64
  %25 = shl nsw i64 %24, 10
  %26 = getelementptr inbounds i8, ptr %5, i64 120
  %27 = load i64, ptr %26, align 8, !tbaa !19
  %spec.select = tail call i64 @llvm.usub.sat.i64(i64 %27, i64 %25)
  %28 = getelementptr inbounds i8, ptr %5, i64 112
  store i64 %spec.select, ptr %28, align 8
  %29 = getelementptr inbounds i8, ptr %5, i64 33
  br label %30

30:                                               ; preds = %33, %23
  %31 = load i64, ptr %28, align 8, !tbaa !14
  %32 = load i64, ptr %26, align 8, !tbaa !19
  %.not27 = icmp ugt i64 %31, %32
  br i1 %.not27, label %.loopexit, label %33

33:                                               ; preds = %30
  tail call void @luaC_step(ptr noundef %0) #3
  %34 = load i8, ptr %29, align 1, !tbaa !20
  %35 = icmp eq i8 %34, 0
  br i1 %35, label %.loopexit, label %30, !llvm.loop !21

36:                                               ; preds = %3
  %37 = getelementptr inbounds i8, ptr %5, i64 144
  %38 = load i32, ptr %37, align 8, !tbaa !23
  store i32 %2, ptr %37, align 8, !tbaa !23
  br label %.loopexit

39:                                               ; preds = %3
  %40 = getelementptr inbounds i8, ptr %5, i64 148
  %41 = load i32, ptr %40, align 4, !tbaa !24
  store i32 %2, ptr %40, align 4, !tbaa !24
  br label %.loopexit

.loopexit:                                        ; preds = %30, %33, %3, %39, %36, %18, %13, %12, %8, %6
  %.1 = phi i32 [ %41, %39 ], [ %38, %36 ], [ %22, %18 ], [ %17, %13 ], [ 0, %12 ], [ 0, %8 ], [ 0, %6 ], [ -1, %3 ], [ 1, %33 ], [ 0, %30 ]
  ret i32 %.1
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.usub.sat.i64(i64, i64) #2

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 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 112}
!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 120}
!20 = !{!15, !8, i64 33}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!15, !10, i64 144}
!24 = !{!15, !10, i64 148}


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