; LLVM IR from paintdream/DontStarveLuaJIT :: src/luajit/src (lj_api)

; 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 { %union.anon }
%union.anon = type { %struct.anon, [320 x i8] }
%struct.anon = type { %struct.GCRef, i8, i8, i8, i8, %struct.MRef, %struct.GCRef, ptr, ptr, %struct.MRef, %struct.MRef, %struct.GCRef, %struct.GCRef, ptr, i32 }
%struct.MRef = type { i32 }
%struct.GCRef = type { i32 }
%struct.global_State = type { ptr, i32, i32, ptr, ptr, %struct.GCState, i32, %struct.SBuf, %struct.GCstr, i8, i8, i8, i8, %struct.GCRef, %union.TValue, %union.TValue, %union.TValue, %struct.Node, %struct.GCupval, i32, i32, ptr, ptr, ptr, i32, i32, %struct.GCRef, %struct.MRef, %struct.MRef, [38 x %struct.GCRef] }
%struct.GCState = type { i32, i32, i8, i8, i8, i8, i32, %struct.GCRef, %struct.MRef, %struct.GCRef, %struct.GCRef, %struct.GCRef, %struct.GCRef, i32, i32, i32, i32 }
%struct.SBuf = type { %struct.MRef, %struct.MRef, %struct.MRef, %struct.MRef }
%struct.GCstr = type { %struct.GCRef, i8, i8, i8, i8, i32, i32 }
%union.TValue = type { i64 }
%struct.Node = type { %union.TValue, %union.TValue, %struct.MRef, %struct.MRef }
%struct.GCupval = type { %struct.GCRef, i8, i8, i8, i8, %union.anon.4, %struct.MRef, i32 }
%union.anon.4 = type { %union.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
declare hidden ptr @stkindex2adr(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @lua_pcall(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i8, align 1
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.lua_State, ptr %14, i32 0, i32 0
  %16 = getelementptr inbounds %struct.anon, ptr %15, i32 0, i32 5
  %17 = getelementptr inbounds %struct.MRef, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8, !tbaa !11
  %19 = zext i32 %18 to i64
  %20 = inttoptr i64 %19 to ptr
  store ptr %20, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #3
  %21 = load ptr, ptr %9, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.global_State, ptr %21, i32 0, i32 10
  %23 = load i8, ptr %22, align 1, !tbaa !12
  %24 = zext i8 %23 to i32
  %25 = and i32 %24, -16
  %26 = trunc i32 %25 to i8
  store i8 %26, ptr %10, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load i32, ptr %8, align 4, !tbaa !9
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %4
  store i64 0, ptr %11, align 8, !tbaa !21
  br label %48

32:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = call ptr @stkindex2adr(ptr noundef %33, i32 noundef %34)
  store ptr %35, ptr %13, align 8, !tbaa !5
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load ptr, ptr %13, align 8, !tbaa !5
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.lua_State, ptr %38, i32 0, i32 0
  %40 = getelementptr inbounds %struct.anon, ptr %39, i32 0, i32 10
  %41 = getelementptr inbounds %struct.MRef, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 4, !tbaa !11
  %43 = zext i32 %42 to i64
  %44 = inttoptr i64 %43 to ptr
  %45 = ptrtoint ptr %37 to i64
  %46 = ptrtoint ptr %44 to i64
  %47 = sub i64 %45, %46
  store i64 %47, ptr %11, align 8, !tbaa !21
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  br label %48

48:                                               ; preds = %32, %31
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.lua_State, ptr %50, i32 0, i32 0
  %52 = getelementptr inbounds %struct.anon, ptr %51, i32 0, i32 8
  %53 = load ptr, ptr %52, align 8, !tbaa !11
  %54 = load i32, ptr %6, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = sub i64 0, %55
  %57 = getelementptr inbounds %union.TValue, ptr %53, i64 %56
  %58 = load i32, ptr %7, align 4, !tbaa !9
  %59 = add nsw i32 %58, 1
  %60 = load i64, ptr %11, align 8, !tbaa !21
  %61 = call i32 @lj_vm_pcall(ptr noundef %49, ptr noundef %57, i32 noundef %59, i64 noundef %60)
  store i32 %61, ptr %12, align 4, !tbaa !9
  %62 = load i32, ptr %12, align 4, !tbaa !9
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %76

64:                                               ; preds = %48
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.global_State, ptr %65, i32 0, i32 10
  %67 = load i8, ptr %66, align 1, !tbaa !12
  %68 = zext i8 %67 to i32
  %69 = and i32 %68, 15
  %70 = load i8, ptr %10, align 1, !tbaa !11
  %71 = zext i8 %70 to i32
  %72 = or i32 %69, %71
  %73 = trunc i32 %72 to i8
  %74 = load ptr, ptr %9, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.global_State, ptr %74, i32 0, i32 10
  store i8 %73, ptr %75, align 1, !tbaa !12
  br label %76

76:                                               ; preds = %64, %48
  %77 = load i32, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  ret i32 %77
}

declare hidden i32 @lj_vm_pcall(ptr noundef, ptr noundef, i32 noundef, i64 noundef) #2

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 = { "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 = !{!7, !7, i64 0}
!12 = !{!13, !7, i64 125}
!13 = !{!"global_State", !6, i64 0, !10, i64 8, !10, i64 12, !6, i64 16, !6, i64 24, !14, i64 32, !10, i64 88, !17, i64 92, !18, i64 108, !7, i64 124, !7, i64 125, !7, i64 126, !7, i64 127, !15, i64 128, !7, i64 136, !7, i64 144, !7, i64 152, !19, i64 160, !20, i64 184, !10, i64 208, !10, i64 212, !6, i64 216, !6, i64 224, !6, i64 232, !10, i64 240, !10, i64 244, !15, i64 248, !16, i64 252, !16, i64 256, !7, i64 260}
!14 = !{!"GCState", !10, i64 0, !10, i64 4, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11, !10, i64 12, !15, i64 16, !16, i64 20, !15, i64 24, !15, i64 28, !15, i64 32, !15, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52}
!15 = !{!"GCRef", !10, i64 0}
!16 = !{!"MRef", !10, i64 0}
!17 = !{!"SBuf", !16, i64 0, !16, i64 4, !16, i64 8, !16, i64 12}
!18 = !{!"GCstr", !15, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !10, i64 8, !10, i64 12}
!19 = !{!"Node", !7, i64 0, !7, i64 8, !16, i64 16, !16, i64 20}
!20 = !{!"GCupval", !15, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !7, i64 8, !16, i64 16, !10, i64 20}
!21 = !{!22, !22, i64 0}
!22 = !{!"long", !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"

%union.TValue = type { i64 }

; Function Attrs: nounwind uwtable
declare hidden ptr @stkindex2adr(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @lua_pcall(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = zext i32 %6 to i64
  %8 = inttoptr i64 %7 to ptr
  %9 = getelementptr inbounds i8, ptr %8, i64 125
  %10 = load i8, ptr %9, align 1, !tbaa !8
  %11 = and i8 %10, -16
  %12 = icmp eq i32 %3, 0
  br i1 %12, label %20, label %13

13:                                               ; preds = %4
  %14 = tail call ptr @stkindex2adr(ptr noundef nonnull %0, i32 noundef %3)
  %15 = getelementptr inbounds i8, ptr %0, i64 36
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = zext i32 %16 to i64
  %18 = ptrtoint ptr %14 to i64
  %19 = sub i64 %18, %17
  br label %20

20:                                               ; preds = %4, %13
  %.0 = phi i64 [ %19, %13 ], [ 0, %4 ]
  %21 = getelementptr inbounds i8, ptr %0, i64 24
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  %23 = sext i32 %1 to i64
  %24 = sub nsw i64 0, %23
  %25 = getelementptr inbounds %union.TValue, ptr %22, i64 %24
  %26 = add nsw i32 %2, 1
  %27 = tail call i32 @lj_vm_pcall(ptr noundef nonnull %0, ptr noundef %25, i32 noundef %26, i64 noundef %.0) #2
  %.not = icmp eq i32 %27, 0
  br i1 %.not, label %32, label %28

28:                                               ; preds = %20
  %29 = load i8, ptr %9, align 1, !tbaa !8
  %30 = and i8 %29, 15
  %31 = or disjoint i8 %30, %11
  store i8 %31, ptr %9, align 1, !tbaa !8
  br label %32

32:                                               ; preds = %28, %20
  ret i32 %27
}

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

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

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !6, i64 125}
!9 = !{!"global_State", !10, i64 0, !11, i64 8, !11, i64 12, !10, i64 16, !10, i64 24, !12, i64 32, !11, i64 88, !15, i64 92, !16, i64 108, !6, i64 124, !6, i64 125, !6, i64 126, !6, i64 127, !13, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !17, i64 160, !18, i64 184, !11, i64 208, !11, i64 212, !10, i64 216, !10, i64 224, !10, i64 232, !11, i64 240, !11, i64 244, !13, i64 248, !14, i64 252, !14, i64 256, !6, i64 260}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = !{!"GCState", !11, i64 0, !11, i64 4, !6, i64 8, !6, i64 9, !6, i64 10, !6, i64 11, !11, i64 12, !13, i64 16, !14, i64 20, !13, i64 24, !13, i64 28, !13, i64 32, !13, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52}
!13 = !{!"GCRef", !11, i64 0}
!14 = !{!"MRef", !11, i64 0}
!15 = !{!"SBuf", !14, i64 0, !14, i64 4, !14, i64 8, !14, i64 12}
!16 = !{!"GCstr", !13, i64 0, !6, i64 4, !6, i64 5, !6, i64 6, !6, i64 7, !11, i64 8, !11, i64 12}
!17 = !{!"Node", !6, i64 0, !6, i64 8, !14, i64 16, !14, i64 20}
!18 = !{!"GCupval", !13, i64 0, !6, i64 4, !6, i64 5, !6, i64 6, !6, i64 7, !6, i64 8, !14, i64 16, !11, i64 20}


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