; LLVM IR from ramsdell/lineqpp :: lua (lgc)

; 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

; Function Attrs: nounwind uwtable
define hidden void @luaC_step(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.lua_State, ptr %5, i32 0, i32 6
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %7, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.global_State, ptr %8, i32 0, i32 18
  %10 = load i32, ptr %9, align 4, !tbaa !15
  %11 = mul i32 10, %10
  %12 = zext i32 %11 to i64
  store i64 %12, ptr %4, align 8, !tbaa !20
  %13 = load i64, ptr %4, align 8, !tbaa !20
  %14 = icmp eq i64 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %1
  store i64 9223372036854775806, ptr %4, align 8, !tbaa !20
  br label %16

16:                                               ; preds = %15, %1
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.global_State, ptr %17, i32 0, i32 14
  %19 = load i64, ptr %18, align 8, !tbaa !21
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.global_State, ptr %20, i32 0, i32 13
  %22 = load i64, ptr %21, align 8, !tbaa !22
  %23 = sub i64 %19, %22
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.global_State, ptr %24, i32 0, i32 16
  %26 = load i64, ptr %25, align 8, !tbaa !23
  %27 = add i64 %26, %23
  store i64 %27, ptr %25, align 8, !tbaa !23
  br label %28

28:                                               ; preds = %40, %16
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = call i64 @singlestep(ptr noundef %29)
  %31 = load i64, ptr %4, align 8, !tbaa !20
  %32 = sub nsw i64 %31, %30
  store i64 %32, ptr %4, align 8, !tbaa !20
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.global_State, ptr %33, i32 0, i32 4
  %35 = load i8, ptr %34, align 1, !tbaa !24
  %36 = zext i8 %35 to i32
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %39

38:                                               ; preds = %28
  br label %43

39:                                               ; preds = %28
  br label %40

40:                                               ; preds = %39
  %41 = load i64, ptr %4, align 8, !tbaa !20
  %42 = icmp sgt i64 %41, 0
  br i1 %42, label %28, label %43, !llvm.loop !25

43:                                               ; preds = %40, %38
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.global_State, ptr %44, i32 0, i32 4
  %46 = load i8, ptr %45, align 1, !tbaa !24
  %47 = zext i8 %46 to i32
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %72

49:                                               ; preds = %43
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.global_State, ptr %50, i32 0, i32 16
  %52 = load i64, ptr %51, align 8, !tbaa !23
  %53 = icmp ult i64 %52, 1024
  br i1 %53, label %54, label %61

54:                                               ; preds = %49
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.global_State, ptr %55, i32 0, i32 14
  %57 = load i64, ptr %56, align 8, !tbaa !21
  %58 = add i64 %57, 1024
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.global_State, ptr %59, i32 0, i32 13
  store i64 %58, ptr %60, align 8, !tbaa !22
  br label %71

61:                                               ; preds = %49
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.global_State, ptr %62, i32 0, i32 16
  %64 = load i64, ptr %63, align 8, !tbaa !23
  %65 = sub i64 %64, 1024
  store i64 %65, ptr %63, align 8, !tbaa !23
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.global_State, ptr %66, i32 0, i32 14
  %68 = load i64, ptr %67, align 8, !tbaa !21
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.global_State, ptr %69, i32 0, i32 13
  store i64 %68, ptr %70, align 8, !tbaa !22
  br label %71

71:                                               ; preds = %61, %54
  br label %84

72:                                               ; preds = %43
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.global_State, ptr %73, i32 0, i32 15
  %75 = load i64, ptr %74, align 8, !tbaa !27
  %76 = udiv i64 %75, 100
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.global_State, ptr %77, i32 0, i32 17
  %79 = load i32, ptr %78, align 8, !tbaa !28
  %80 = sext i32 %79 to i64
  %81 = mul i64 %76, %80
  %82 = load ptr, ptr %3, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.global_State, ptr %82, i32 0, i32 13
  store i64 %81, ptr %83, align 8, !tbaa !22
  br label %84

84:                                               ; preds = %72, %71
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden i64 @singlestep(ptr noundef) #1

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


; 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 void @luaC_step(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 32
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 148
  %5 = load i32, ptr %4, align 4, !tbaa !14
  %6 = mul i32 %5, 10
  %7 = zext i32 %6 to i64
  %8 = icmp eq i32 %6, 0
  %spec.store.select = select i1 %8, i64 9223372036854775806, i64 %7
  %9 = getelementptr inbounds i8, ptr %3, i64 120
  %10 = load i64, ptr %9, align 8, !tbaa !19
  %11 = getelementptr inbounds i8, ptr %3, i64 112
  %12 = load i64, ptr %11, align 8, !tbaa !20
  %13 = sub i64 %10, %12
  %14 = getelementptr inbounds i8, ptr %3, i64 136
  %15 = load i64, ptr %14, align 8, !tbaa !21
  %16 = add i64 %13, %15
  store i64 %16, ptr %14, align 8, !tbaa !21
  %17 = getelementptr inbounds i8, ptr %3, i64 33
  br label %18

18:                                               ; preds = %18, %1
  %.0 = phi i64 [ %spec.store.select, %1 ], [ %20, %18 ]
  %19 = tail call i64 @singlestep(ptr noundef %0)
  %20 = sub nsw i64 %.0, %19
  %21 = load i8, ptr %17, align 1, !tbaa !22
  %22 = icmp ne i8 %21, 0
  %23 = icmp sgt i64 %20, 0
  %or.cond = select i1 %22, i1 %23, i1 false
  br i1 %or.cond, label %18, label %24, !llvm.loop !23

24:                                               ; preds = %18
  br i1 %22, label %25, label %34

25:                                               ; preds = %24
  %26 = load i64, ptr %14, align 8, !tbaa !21
  %27 = icmp ult i64 %26, 1024
  br i1 %27, label %28, label %31

28:                                               ; preds = %25
  %29 = load i64, ptr %9, align 8, !tbaa !19
  %30 = add i64 %29, 1024
  br label %42

31:                                               ; preds = %25
  %32 = add i64 %26, -1024
  store i64 %32, ptr %14, align 8, !tbaa !21
  %33 = load i64, ptr %9, align 8, !tbaa !19
  br label %42

34:                                               ; preds = %24
  %35 = getelementptr inbounds i8, ptr %3, i64 128
  %36 = load i64, ptr %35, align 8, !tbaa !25
  %37 = udiv i64 %36, 100
  %38 = getelementptr inbounds i8, ptr %3, i64 144
  %39 = load i32, ptr %38, align 8, !tbaa !26
  %40 = sext i32 %39 to i64
  %41 = mul i64 %37, %40
  br label %42

42:                                               ; preds = %28, %31, %34
  %.sink = phi i64 [ %30, %28 ], [ %33, %31 ], [ %41, %34 ]
  store i64 %.sink, ptr %11, align 8, !tbaa !20
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden i64 @singlestep(ptr noundef) local_unnamed_addr #0

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

!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, !10, i64 148}
!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, !13, i64 112}
!21 = !{!15, !13, i64 136}
!22 = !{!15, !8, i64 33}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = !{!15, !13, i64 128}
!26 = !{!15, !10, i64 144}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass