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

; 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.CTState = type { ptr, i32, i32, ptr, ptr, ptr, ptr, %struct.CCallback, [128 x i16] }
%struct.CCallback = type { [8 x %union.FPRCBArg], [8 x i64], ptr, ptr, ptr, i32, i32, i32 }
%union.FPRCBArg = type { double }
%struct.lua_State = type { %union.anon.4 }
%union.anon.4 = type { %struct.anon.5, [320 x i8] }
%struct.anon.5 = 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 }
%union.TValue = type { i64 }
%struct.anon.0 = type { %struct.GCRef, %union.FrameLink }
%union.FrameLink = type { i32 }
%struct.GCfuncC = type { %struct.GCRef, i8, i8, i8, i8, %struct.GCRef, %struct.GCRef, %struct.MRef, ptr, [1 x %union.TValue] }
%struct.GCfuncL = type { %struct.GCRef, i8, i8, i8, i8, %struct.GCRef, %struct.GCRef, %struct.MRef, [1 x %struct.GCRef] }
%struct.GCproto = type { %struct.GCRef, i8, i8, i8, i8, i32, %struct.GCRef, %struct.MRef, %struct.MRef, i32, i32, i32, i8, i8, i16, %struct.GCRef, i32, i32, %struct.MRef, %struct.MRef, %struct.MRef }

; 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 @lj_ccallback_leave(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.CTState, ptr %9, i32 0, i32 3
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  store ptr %11, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.lua_State, ptr %12, i32 0, i32 0
  %14 = getelementptr inbounds %struct.anon.5, ptr %13, i32 0, i32 7
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  store ptr %15, ptr %7, align 8, !tbaa !5
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.lua_State, ptr %16, i32 0, i32 0
  %18 = getelementptr inbounds %struct.anon.5, ptr %17, i32 0, i32 8
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.lua_State, ptr %20, i32 0, i32 0
  %22 = getelementptr inbounds %struct.anon.5, ptr %21, i32 0, i32 7
  store ptr %19, ptr %22, align 8, !tbaa !13
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.lua_State, ptr %24, i32 0, i32 0
  %26 = getelementptr inbounds %struct.anon.5, ptr %25, i32 0, i32 7
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  %28 = icmp uge ptr %23, %27
  br i1 %28, label %29, label %70

29:                                               ; preds = %2
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.lua_State, ptr %30, i32 0, i32 0
  %32 = getelementptr inbounds %struct.anon.5, ptr %31, i32 0, i32 7
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  %34 = getelementptr inbounds %union.TValue, ptr %33, i64 -1
  %35 = getelementptr inbounds %struct.anon.0, ptr %34, i32 0, i32 0
  %36 = getelementptr inbounds %struct.GCRef, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 8, !tbaa !13
  %38 = zext i32 %37 to i64
  %39 = inttoptr i64 %38 to ptr
  store ptr %39, ptr %6, align 8, !tbaa !5
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.GCfuncC, ptr %40, i32 0, i32 3
  %42 = load i8, ptr %41, align 2, !tbaa !13
  %43 = zext i8 %42 to i32
  %44 = icmp eq i32 %43, 0
  br i1 %44, label %45, label %69

45:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.GCfuncL, ptr %46, i32 0, i32 7
  %48 = getelementptr inbounds %struct.MRef, ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 8, !tbaa !13
  %50 = zext i32 %49 to i64
  %51 = inttoptr i64 %50 to ptr
  %52 = getelementptr inbounds i8, ptr %51, i64 -64
  store ptr %52, ptr %8, align 8, !tbaa !5
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = getelementptr inbounds i8, ptr %53, i64 64
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.GCproto, ptr %55, i32 0, i32 5
  %57 = load i32, ptr %56, align 4, !tbaa !14
  %58 = zext i32 %57 to i64
  %59 = getelementptr inbounds i32, ptr %54, i64 %58
  %60 = getelementptr inbounds i32, ptr %59, i64 1
  %61 = ptrtoint ptr %60 to i64
  %62 = trunc i64 %61 to i32
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.lua_State, ptr %63, i32 0, i32 0
  %65 = getelementptr inbounds %struct.anon.5, ptr %64, i32 0, i32 13
  %66 = load ptr, ptr %65, align 8, !tbaa !13
  %67 = getelementptr inbounds i8, ptr %66, i64 28
  %68 = getelementptr inbounds %struct.MRef, ptr %67, i32 0, i32 0
  store i32 %62, ptr %68, align 4, !tbaa !19
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %69

69:                                               ; preds = %45, %29
  br label %70

70:                                               ; preds = %69, %2
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  call void @callback_conv_result(ptr noundef %71, ptr noundef %72, ptr noundef %73)
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.lua_State, ptr %74, i32 0, i32 0
  %76 = getelementptr inbounds %struct.anon.5, ptr %75, i32 0, i32 8
  %77 = load ptr, ptr %76, align 8, !tbaa !13
  %78 = getelementptr inbounds %union.TValue, ptr %77, i64 -2
  store ptr %78, ptr %76, align 8, !tbaa !13
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.lua_State, ptr %80, i32 0, i32 0
  %82 = getelementptr inbounds %struct.anon.5, ptr %81, i32 0, i32 7
  store ptr %79, ptr %82, align 8, !tbaa !13
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.lua_State, ptr %83, i32 0, i32 0
  %85 = getelementptr inbounds %struct.anon.5, ptr %84, i32 0, i32 13
  %86 = load ptr, ptr %85, align 8, !tbaa !13
  %87 = getelementptr inbounds i8, ptr %86, i64 32
  %88 = load ptr, ptr %87, align 8, !tbaa !5
  %89 = load ptr, ptr %5, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.lua_State, ptr %89, i32 0, i32 0
  %91 = getelementptr inbounds %struct.anon.5, ptr %90, i32 0, i32 13
  store ptr %88, ptr %91, align 8, !tbaa !13
  %92 = load ptr, ptr %3, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.CTState, ptr %92, i32 0, i32 7
  %94 = getelementptr inbounds %struct.CCallback, ptr %93, i32 0, i32 7
  store i32 0, ptr %94, align 8, !tbaa !20
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @callback_conv_result(ptr noundef, ptr noundef, 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 16}
!10 = !{!"CTState", !6, i64 0, !11, i64 8, !11, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !12, i64 48, !7, i64 216}
!11 = !{!"int", !7, i64 0}
!12 = !{!"CCallback", !7, i64 0, !7, i64 64, !6, i64 128, !6, i64 136, !6, i64 144, !11, i64 152, !11, i64 156, !11, i64 160}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !11, i64 8}
!15 = !{!"GCproto", !16, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !11, i64 8, !16, i64 12, !17, i64 16, !17, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !7, i64 36, !7, i64 37, !18, i64 38, !16, i64 40, !11, i64 44, !11, i64 48, !17, i64 52, !17, i64 56, !17, i64 60}
!16 = !{!"GCRef", !11, i64 0}
!17 = !{!"MRef", !11, i64 0}
!18 = !{!"short", !7, i64 0}
!19 = !{!17, !11, i64 0}
!20 = !{!10, !11, i64 208}


; 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 @lj_ccallback_leave(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !12
  %7 = getelementptr inbounds i8, ptr %4, i64 24
  %8 = load ptr, ptr %7, align 8, !tbaa !12
  store ptr %8, ptr %5, align 8, !tbaa !12
  %.not = icmp ugt ptr %8, %1
  br i1 %.not, label %32, label %9

9:                                                ; preds = %2
  %10 = getelementptr inbounds i8, ptr %8, i64 -8
  %11 = load i32, ptr %10, align 8, !tbaa !12
  %12 = zext i32 %11 to i64
  %13 = inttoptr i64 %12 to ptr
  %14 = getelementptr inbounds i8, ptr %13, i64 6
  %15 = load i8, ptr %14, align 2, !tbaa !12
  %16 = icmp eq i8 %15, 0
  br i1 %16, label %17, label %32

17:                                               ; preds = %9
  %18 = getelementptr inbounds i8, ptr %13, i64 16
  %19 = load i32, ptr %18, align 8, !tbaa !12
  %20 = zext i32 %19 to i64
  %21 = inttoptr i64 %20 to ptr
  %22 = getelementptr inbounds i8, ptr %21, i64 -56
  %23 = load i32, ptr %22, align 4, !tbaa !13
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds i32, ptr %21, i64 %24
  %26 = getelementptr inbounds i8, ptr %25, i64 4
  %27 = ptrtoint ptr %26 to i64
  %28 = trunc i64 %27 to i32
  %29 = getelementptr inbounds i8, ptr %4, i64 48
  %30 = load ptr, ptr %29, align 8, !tbaa !12
  %31 = getelementptr inbounds i8, ptr %30, i64 28
  store i32 %28, ptr %31, align 4, !tbaa !18
  br label %32

32:                                               ; preds = %9, %17, %2
  tail call void @callback_conv_result(ptr noundef nonnull %0, ptr noundef nonnull %4, ptr noundef %1)
  %33 = load ptr, ptr %7, align 8, !tbaa !12
  %34 = getelementptr inbounds i8, ptr %33, i64 -16
  store ptr %34, ptr %7, align 8, !tbaa !12
  store ptr %6, ptr %5, align 8, !tbaa !12
  %35 = getelementptr inbounds i8, ptr %4, i64 48
  %36 = load ptr, ptr %35, align 8, !tbaa !12
  %37 = getelementptr inbounds i8, ptr %36, i64 32
  %38 = load ptr, ptr %37, align 8, !tbaa !19
  store ptr %38, ptr %35, align 8, !tbaa !12
  %39 = getelementptr inbounds i8, ptr %0, i64 208
  store i32 0, ptr %39, align 8, !tbaa !20
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @callback_conv_result(ptr noundef, ptr noundef, 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 16}
!6 = !{!"CTState", !7, i64 0, !10, i64 8, !10, i64 12, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !11, i64 48, !8, i64 216}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"CCallback", !8, i64 0, !8, i64 64, !7, i64 128, !7, i64 136, !7, i64 144, !10, i64 152, !10, i64 156, !10, i64 160}
!12 = !{!8, !8, i64 0}
!13 = !{!14, !10, i64 8}
!14 = !{!"GCproto", !15, i64 0, !8, i64 4, !8, i64 5, !8, i64 6, !8, i64 7, !10, i64 8, !15, i64 12, !16, i64 16, !16, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !8, i64 36, !8, i64 37, !17, i64 38, !15, i64 40, !10, i64 44, !10, i64 48, !16, i64 52, !16, i64 56, !16, i64 60}
!15 = !{!"GCRef", !10, i64 0}
!16 = !{!"MRef", !10, i64 0}
!17 = !{!"short", !8, i64 0}
!18 = !{!16, !10, i64 0}
!19 = !{!7, !7, i64 0}
!20 = !{!6, !10, i64 208}


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