; 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_TValue = type { %union.Value, i32 }
%union.Value = type { ptr }
%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.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.anon.0 = type { ptr, i8, i8, i8, i32, i64 }
%union.TString = type { %struct.anon.0 }

; 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
declare hidden ptr @index2adr(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @lua_tolstring(ptr noundef %0, i32 noundef %1, ptr noundef %2) #2 {
  %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 i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = call ptr @index2adr(ptr noundef %10, i32 noundef %11)
  store ptr %12, ptr %8, align 8, !tbaa !5
  %13 = load ptr, ptr %8, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.lua_TValue, ptr %13, i32 0, i32 1
  %15 = load i32, ptr %14, align 8, !tbaa !11
  %16 = icmp eq i32 %15, 4
  br i1 %16, label %46, label %17

17:                                               ; preds = %3
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = call i32 @luaV_tostring(ptr noundef %18, ptr noundef %19)
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %28, label %22

22:                                               ; preds = %17
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %27

25:                                               ; preds = %22
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 0, ptr %26, align 8, !tbaa !13
  br label %27

27:                                               ; preds = %25, %22
  store ptr null, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %61

28:                                               ; preds = %17
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.lua_State, ptr %29, i32 0, i32 6
  %31 = load ptr, ptr %30, align 8, !tbaa !15
  %32 = getelementptr inbounds %struct.global_State, ptr %31, i32 0, i32 14
  %33 = load i64, ptr %32, align 8, !tbaa !18
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.lua_State, ptr %34, i32 0, i32 6
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  %37 = getelementptr inbounds %struct.global_State, ptr %36, i32 0, i32 13
  %38 = load i64, ptr %37, align 8, !tbaa !23
  %39 = icmp uge i64 %33, %38
  br i1 %39, label %40, label %42

40:                                               ; preds = %28
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  call void @luaC_step(ptr noundef %41)
  br label %42

42:                                               ; preds = %40, %28
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = load i32, ptr %6, align 4, !tbaa !9
  %45 = call ptr @index2adr(ptr noundef %43, i32 noundef %44)
  store ptr %45, ptr %8, align 8, !tbaa !5
  br label %46

46:                                               ; preds = %42, %3
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = icmp ne ptr %47, null
  br i1 %48, label %49, label %56

49:                                               ; preds = %46
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.lua_TValue, ptr %50, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !24
  %53 = getelementptr inbounds %struct.anon.0, ptr %52, i32 0, i32 5
  %54 = load i64, ptr %53, align 8, !tbaa !24
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %54, ptr %55, align 8, !tbaa !13
  br label %56

56:                                               ; preds = %49, %46
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.lua_TValue, ptr %57, i32 0, i32 0
  %59 = load ptr, ptr %58, align 8, !tbaa !24
  %60 = getelementptr inbounds %union.TString, ptr %59, i64 1
  store ptr %60, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %61

61:                                               ; preds = %56, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %62 = load ptr, ptr %4, align 8
  ret ptr %62
}

declare hidden i32 @luaV_tostring(ptr noundef, 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, !10, i64 8}
!12 = !{!"lua_TValue", !7, i64 0, !10, i64 8}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!16, !6, i64 32}
!16 = !{!"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, !17, i64 96, !17, i64 98, !7, i64 100, !7, i64 101, !10, i64 104, !10, i64 108, !6, i64 112, !12, i64 120, !12, i64 136, !6, i64 152, !6, i64 160, !6, i64 168, !14, i64 176}
!17 = !{!"short", !7, i64 0}
!18 = !{!19, !14, i64 120}
!19 = !{!"global_State", !20, 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, !21, i64 88, !14, i64 112, !14, i64 120, !14, i64 128, !14, i64 136, !10, i64 144, !10, i64 148, !6, i64 152, !12, i64 160, !6, i64 176, !22, i64 184, !7, i64 224, !7, i64 296}
!20 = !{!"stringtable", !6, i64 0, !10, i64 8, !10, i64 12}
!21 = !{!"Mbuffer", !6, i64 0, !14, i64 8, !14, i64 16}
!22 = !{!"UpVal", !6, i64 0, !7, i64 8, !7, i64 9, !6, i64 16, !7, i64 24}
!23 = !{!19, !14, i64 112}
!24 = !{!7, !7, i64 0}


; 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
declare hidden ptr @index2adr(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @lua_tolstring(ptr noundef %0, i32 noundef %1, ptr noundef writeonly %2) local_unnamed_addr #1 {
  %4 = tail call ptr @index2adr(ptr noundef %0, i32 noundef %1)
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = icmp eq i32 %6, 4
  br i1 %7, label %22, label %8

8:                                                ; preds = %3
  %9 = tail call i32 @luaV_tostring(ptr noundef %0, ptr noundef nonnull %4) #2
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %10, label %12

10:                                               ; preds = %8
  %.not18 = icmp eq ptr %2, null
  br i1 %.not18, label %30, label %11

11:                                               ; preds = %10
  store i64 0, ptr %2, align 8, !tbaa !10
  br label %30

12:                                               ; preds = %8
  %13 = getelementptr inbounds i8, ptr %0, i64 32
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  %15 = getelementptr inbounds i8, ptr %14, i64 120
  %16 = load i64, ptr %15, align 8, !tbaa !16
  %17 = getelementptr inbounds i8, ptr %14, i64 112
  %18 = load i64, ptr %17, align 8, !tbaa !21
  %.not19 = icmp ult i64 %16, %18
  br i1 %.not19, label %20, label %19

19:                                               ; preds = %12
  tail call void @luaC_step(ptr noundef nonnull %0) #2
  br label %20

20:                                               ; preds = %19, %12
  %21 = tail call ptr @index2adr(ptr noundef nonnull %0, i32 noundef %1)
  br label %22

22:                                               ; preds = %20, %3
  %.0 = phi ptr [ %4, %3 ], [ %21, %20 ]
  %.not20 = icmp eq ptr %2, null
  br i1 %.not20, label %27, label %23

23:                                               ; preds = %22
  %24 = load ptr, ptr %.0, align 8, !tbaa !22
  %25 = getelementptr inbounds i8, ptr %24, i64 16
  %26 = load i64, ptr %25, align 8, !tbaa !22
  store i64 %26, ptr %2, align 8, !tbaa !10
  br label %27

27:                                               ; preds = %23, %22
  %28 = load ptr, ptr %.0, align 8, !tbaa !22
  %29 = getelementptr inbounds i8, ptr %28, i64 24
  br label %30

30:                                               ; preds = %10, %11, %27
  %.016 = phi ptr [ %29, %27 ], [ null, %11 ], [ null, %10 ]
  ret ptr %.016
}

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

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


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