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

; 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.GCupval = type { %struct.GCRef, i8, i8, i8, i8, %union.anon.0, %struct.MRef, i32 }
%union.anon.0 = type { %union.TValue }
%union.TValue = type { i64 }
%struct.GChead = type { %struct.GCRef, i8, i8, i8, i8, %struct.GCRef, %struct.GCRef, %struct.GCRef }
%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 }
%struct.Node = type { %union.TValue, %union.TValue, %struct.MRef, %struct.MRef }

; Function Attrs: nounwind uwtable
define hidden void @lj_func_closeuv(ptr noundef %0, ptr noundef %1) #0 {
  %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
  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) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.lua_State, ptr %8, i32 0, i32 0
  %10 = getelementptr inbounds %struct.anon, ptr %9, i32 0, i32 5
  %11 = getelementptr inbounds %struct.MRef, ptr %10, i32 0, i32 0
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = zext i32 %12 to i64
  %14 = inttoptr i64 %13 to ptr
  store ptr %14, ptr %6, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %71, %2
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.lua_State, ptr %16, i32 0, i32 0
  %18 = getelementptr inbounds %struct.anon, ptr %17, i32 0, i32 11
  %19 = getelementptr inbounds %struct.GCRef, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %19, align 8, !tbaa !9
  %21 = zext i32 %20 to i64
  %22 = inttoptr i64 %21 to ptr
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %39

24:                                               ; preds = %15
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.lua_State, ptr %25, i32 0, i32 0
  %27 = getelementptr inbounds %struct.anon, ptr %26, i32 0, i32 11
  %28 = getelementptr inbounds %struct.GCRef, ptr %27, i32 0, i32 0
  %29 = load i32, ptr %28, align 8, !tbaa !9
  %30 = zext i32 %29 to i64
  %31 = inttoptr i64 %30 to ptr
  store ptr %31, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.GCupval, ptr %31, i32 0, i32 6
  %33 = getelementptr inbounds %struct.MRef, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 8, !tbaa !10
  %35 = zext i32 %34 to i64
  %36 = inttoptr i64 %35 to ptr
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = icmp uge ptr %36, %37
  br label %39

39:                                               ; preds = %24, %15
  %40 = phi i1 [ false, %15 ], [ %38, %24 ]
  br i1 %40, label %41, label %72

41:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %42, ptr %7, align 8, !tbaa !5
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.GCupval, ptr %43, i32 0, i32 0
  %45 = getelementptr inbounds %struct.GCRef, ptr %44, i32 0, i32 0
  %46 = load i32, ptr %45, align 8, !tbaa !15
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.lua_State, ptr %47, i32 0, i32 0
  %49 = getelementptr inbounds %struct.anon, ptr %48, i32 0, i32 11
  %50 = getelementptr inbounds %struct.GCRef, ptr %49, i32 0, i32 0
  store i32 %46, ptr %50, align 8, !tbaa !9
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.GChead, ptr %51, i32 0, i32 1
  %53 = load i8, ptr %52, align 4, !tbaa !9
  %54 = zext i8 %53 to i32
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.global_State, ptr %55, i32 0, i32 5
  %57 = getelementptr inbounds %struct.GCState, ptr %56, i32 0, i32 2
  %58 = load i8, ptr %57, align 8, !tbaa !16
  %59 = zext i8 %58 to i32
  %60 = xor i32 %59, 3
  %61 = and i32 %54, %60
  %62 = and i32 %61, 3
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %67

64:                                               ; preds = %41
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  call void @lj_func_freeuv(ptr noundef %65, ptr noundef %66)
  br label %71

67:                                               ; preds = %41
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  call void @unlinkuv(ptr noundef %68)
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  call void @lj_gc_closeuv(ptr noundef %69, ptr noundef %70)
  br label %71

71:                                               ; preds = %67, %64
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %15, !llvm.loop !22

72:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare hidden void @lj_func_freeuv(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @unlinkuv(ptr noundef) #0

declare hidden void @lj_gc_closeuv(ptr noundef, ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!7, !7, i64 0}
!10 = !{!11, !13, i64 16}
!11 = !{!"GCupval", !12, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !7, i64 8, !14, i64 16, !13, i64 20}
!12 = !{!"GCRef", !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!"MRef", !13, i64 0}
!15 = !{!11, !13, i64 0}
!16 = !{!17, !7, i64 40}
!17 = !{!"global_State", !6, i64 0, !13, i64 8, !13, i64 12, !6, i64 16, !6, i64 24, !18, i64 32, !13, i64 88, !19, i64 92, !20, i64 108, !7, i64 124, !7, i64 125, !7, i64 126, !7, i64 127, !12, i64 128, !7, i64 136, !7, i64 144, !7, i64 152, !21, i64 160, !11, i64 184, !13, i64 208, !13, i64 212, !6, i64 216, !6, i64 224, !6, i64 232, !13, i64 240, !13, i64 244, !12, i64 248, !14, i64 252, !14, i64 256, !7, i64 260}
!18 = !{!"GCState", !13, i64 0, !13, i64 4, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11, !13, i64 12, !12, i64 16, !14, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !13, i64 40, !13, i64 44, !13, i64 48, !13, i64 52}
!19 = !{!"SBuf", !14, i64 0, !14, i64 4, !14, i64 8, !14, i64 12}
!20 = !{!"GCstr", !12, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !13, i64 8, !13, i64 12}
!21 = !{!"Node", !7, i64 0, !7, i64 8, !14, i64 16, !14, i64 20}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


; 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_func_closeuv(ptr nocapture noundef %0, ptr noundef readnone %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = zext i32 %4 to i64
  %6 = inttoptr i64 %5 to ptr
  %7 = getelementptr inbounds i8, ptr %0, i64 40
  %8 = load i32, ptr %7, align 8, !tbaa !5
  %.not16 = icmp eq i32 %8, 0
  br i1 %.not16, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %9 = getelementptr inbounds i8, ptr %6, i64 40
  br label %10

10:                                               ; preds = %.lr.ph, %26
  %.in.in = phi i32 [ %8, %.lr.ph ], [ %27, %26 ]
  %.in = zext i32 %.in.in to i64
  %11 = inttoptr i64 %.in to ptr
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  %13 = load i32, ptr %12, align 8, !tbaa !8
  %14 = zext i32 %13 to i64
  %15 = inttoptr i64 %14 to ptr
  %.not14 = icmp ult ptr %15, %1
  br i1 %.not14, label %.critedge, label %16

16:                                               ; preds = %10
  %17 = load i32, ptr %11, align 8, !tbaa !13
  store i32 %17, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %11, i64 4
  %19 = load i8, ptr %18, align 4, !tbaa !5
  %20 = load i8, ptr %9, align 8, !tbaa !14
  %21 = xor i8 %20, -1
  %22 = and i8 %19, 3
  %23 = and i8 %22, %21
  %.not15 = icmp eq i8 %23, 0
  br i1 %.not15, label %25, label %24

24:                                               ; preds = %16
  tail call void @lj_func_freeuv(ptr noundef nonnull %6, ptr noundef nonnull %11)
  br label %26

25:                                               ; preds = %16
  tail call void @unlinkuv(ptr noundef nonnull %11)
  tail call void @lj_gc_closeuv(ptr noundef nonnull %6, ptr noundef nonnull %11) #2
  br label %26

26:                                               ; preds = %25, %24
  %27 = load i32, ptr %7, align 8, !tbaa !5
  %.not = icmp eq i32 %27, 0
  br i1 %.not, label %.critedge, label %10, !llvm.loop !21

.critedge:                                        ; preds = %10, %26, %2
  ret void
}

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

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

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass