; 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 }
%union.TValue = type { i64 }
%struct.anon.0 = type { %union.anon.1, i32 }
%union.anon.1 = type { %struct.GCRef }
%struct.GCfuncC = type { %struct.GCRef, i8, i8, i8, i8, %struct.GCRef, %struct.GCRef, %struct.MRef, ptr, [1 x %union.TValue] }
%struct.GCudata = type { %struct.GCRef, i8, i8, i8, i8, %struct.GCRef, i32, %struct.GCRef, i32 }
%struct.GChead = type { %struct.GCRef, i8, i8, i8, i8, %struct.GCRef, %struct.GCRef, %struct.GCRef }

; 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 @lj_gc_barrierf(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden ptr @index2adr(ptr noundef, i32 noundef) #2

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

29:                                               ; preds = %2
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = ptrtoint ptr %30 to i64
  %32 = trunc i64 %31 to i32
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.anon.0, ptr %33, i32 0, i32 0
  %35 = getelementptr inbounds %struct.GCRef, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %35, align 8, !tbaa !11
  %37 = zext i32 %36 to i64
  %38 = inttoptr i64 %37 to ptr
  %39 = getelementptr inbounds %struct.GCfuncC, ptr %38, i32 0, i32 5
  %40 = getelementptr inbounds %struct.GCRef, ptr %39, i32 0, i32 0
  store i32 %32, ptr %40, align 8, !tbaa !11
  br label %84

41:                                               ; preds = %2
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.anon.0, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !11
  %45 = icmp eq i32 %44, -13
  br i1 %45, label %46, label %58

46:                                               ; preds = %41
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = ptrtoint ptr %47 to i64
  %49 = trunc i64 %48 to i32
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.anon.0, ptr %50, i32 0, i32 0
  %52 = getelementptr inbounds %struct.GCRef, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 8, !tbaa !11
  %54 = zext i32 %53 to i64
  %55 = inttoptr i64 %54 to ptr
  %56 = getelementptr inbounds %struct.GCudata, ptr %55, i32 0, i32 5
  %57 = getelementptr inbounds %struct.GCRef, ptr %56, i32 0, i32 0
  store i32 %49, ptr %57, align 8, !tbaa !11
  br label %83

58:                                               ; preds = %41
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.anon.0, ptr %59, i32 0, i32 1
  %61 = load i32, ptr %60, align 4, !tbaa !11
  %62 = icmp eq i32 %61, -7
  br i1 %62, label %63, label %76

63:                                               ; preds = %58
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  %65 = ptrtoint ptr %64 to i64
  %66 = trunc i64 %65 to i32
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.anon.0, ptr %67, i32 0, i32 0
  %69 = getelementptr inbounds %struct.GCRef, ptr %68, i32 0, i32 0
  %70 = load i32, ptr %69, align 8, !tbaa !11
  %71 = zext i32 %70 to i64
  %72 = inttoptr i64 %71 to ptr
  %73 = getelementptr inbounds %struct.lua_State, ptr %72, i32 0, i32 0
  %74 = getelementptr inbounds %struct.anon, ptr %73, i32 0, i32 12
  %75 = getelementptr inbounds %struct.GCRef, ptr %74, i32 0, i32 0
  store i32 %66, ptr %75, align 4, !tbaa !11
  br label %82

76:                                               ; preds = %58
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.lua_State, ptr %77, i32 0, i32 0
  %79 = getelementptr inbounds %struct.anon, ptr %78, i32 0, i32 8
  %80 = load ptr, ptr %79, align 8, !tbaa !11
  %81 = getelementptr inbounds %union.TValue, ptr %80, i32 -1
  store ptr %81, ptr %79, align 8, !tbaa !11
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %124

82:                                               ; preds = %63
  br label %83

83:                                               ; preds = %82, %46
  br label %84

84:                                               ; preds = %83, %29
  %85 = load ptr, ptr %7, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.GChead, ptr %85, i32 0, i32 1
  %87 = load i8, ptr %86, align 4, !tbaa !11
  %88 = zext i8 %87 to i32
  %89 = and i32 %88, 3
  %90 = icmp ne i32 %89, 0
  br i1 %90, label %91, label %118

91:                                               ; preds = %84
  %92 = load ptr, ptr %6, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.anon.0, ptr %92, i32 0, i32 0
  %94 = getelementptr inbounds %struct.GCRef, ptr %93, i32 0, i32 0
  %95 = load i32, ptr %94, align 8, !tbaa !11
  %96 = zext i32 %95 to i64
  %97 = inttoptr i64 %96 to ptr
  %98 = getelementptr inbounds %struct.GChead, ptr %97, i32 0, i32 1
  %99 = load i8, ptr %98, align 4, !tbaa !11
  %100 = zext i8 %99 to i32
  %101 = and i32 %100, 4
  %102 = icmp ne i32 %101, 0
  br i1 %102, label %103, label %118

103:                                              ; preds = %91
  %104 = load ptr, ptr %4, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct.lua_State, ptr %104, i32 0, i32 0
  %106 = getelementptr inbounds %struct.anon, ptr %105, i32 0, i32 5
  %107 = getelementptr inbounds %struct.MRef, ptr %106, i32 0, i32 0
  %108 = load i32, ptr %107, align 8, !tbaa !11
  %109 = zext i32 %108 to i64
  %110 = inttoptr i64 %109 to ptr
  %111 = load ptr, ptr %6, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.anon.0, ptr %111, i32 0, i32 0
  %113 = getelementptr inbounds %struct.GCRef, ptr %112, i32 0, i32 0
  %114 = load i32, ptr %113, align 8, !tbaa !11
  %115 = zext i32 %114 to i64
  %116 = inttoptr i64 %115 to ptr
  %117 = load ptr, ptr %7, align 8, !tbaa !5
  call void @lj_gc_barrierf(ptr noundef %110, ptr noundef %116, ptr noundef %117)
  br label %118

118:                                              ; preds = %103, %91, %84
  %119 = load ptr, ptr %4, align 8, !tbaa !5
  %120 = getelementptr inbounds %struct.lua_State, ptr %119, i32 0, i32 0
  %121 = getelementptr inbounds %struct.anon, ptr %120, i32 0, i32 8
  %122 = load ptr, ptr %121, align 8, !tbaa !11
  %123 = getelementptr inbounds %union.TValue, ptr %122, i32 -1
  store ptr %123, ptr %121, align 8, !tbaa !11
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %124

124:                                              ; preds = %118, %76
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %125 = load i32, ptr %3, align 4
  ret i32 %125
}

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 = !{!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 @lj_gc_barrierf(ptr noundef, ptr noundef, 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 range(i32 0, 2) i32 @lua_setfenv(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = tail call ptr @index2adr(ptr noundef %0, i32 noundef %1)
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 -8
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %8 = zext i32 %7 to i64
  %9 = inttoptr i64 %8 to ptr
  %10 = getelementptr inbounds i8, ptr %3, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !5
  switch i32 %11, label %46 [
    i32 -9, label %12
    i32 -13, label %17
    i32 -7, label %22
  ]

12:                                               ; preds = %2
  %13 = load i32, ptr %3, align 8, !tbaa !5
  %14 = zext i32 %13 to i64
  %15 = inttoptr i64 %14 to ptr
  %16 = getelementptr inbounds i8, ptr %15, i64 8
  store i32 %7, ptr %16, align 8, !tbaa !5
  br label %27

17:                                               ; preds = %2
  %18 = load i32, ptr %3, align 8, !tbaa !5
  %19 = zext i32 %18 to i64
  %20 = inttoptr i64 %19 to ptr
  %21 = getelementptr inbounds i8, ptr %20, i64 8
  store i32 %7, ptr %21, align 8, !tbaa !5
  br label %27

22:                                               ; preds = %2
  %23 = load i32, ptr %3, align 8, !tbaa !5
  %24 = zext i32 %23 to i64
  %25 = inttoptr i64 %24 to ptr
  %26 = getelementptr inbounds i8, ptr %25, i64 44
  store i32 %7, ptr %26, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %17, %22, %12
  %28 = getelementptr inbounds i8, ptr %9, i64 4
  %29 = load i8, ptr %28, align 4, !tbaa !5
  %30 = and i8 %29, 3
  %.not = icmp eq i8 %30, 0
  br i1 %.not, label %43, label %31

31:                                               ; preds = %27
  %32 = load i32, ptr %3, align 8, !tbaa !5
  %33 = zext i32 %32 to i64
  %34 = inttoptr i64 %33 to ptr
  %35 = getelementptr inbounds i8, ptr %34, i64 4
  %36 = load i8, ptr %35, align 4, !tbaa !5
  %37 = and i8 %36, 4
  %.not23 = icmp eq i8 %37, 0
  br i1 %.not23, label %43, label %38

38:                                               ; preds = %31
  %39 = getelementptr inbounds i8, ptr %0, i64 8
  %40 = load i32, ptr %39, align 8, !tbaa !5
  %41 = zext i32 %40 to i64
  %42 = inttoptr i64 %41 to ptr
  tail call void @lj_gc_barrierf(ptr noundef %42, ptr noundef nonnull %34, ptr noundef nonnull %9) #2
  br label %43

43:                                               ; preds = %38, %31, %27
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds i8, ptr %44, i64 -8
  br label %46

46:                                               ; preds = %2, %43
  %storemerge = phi ptr [ %45, %43 ], [ %6, %2 ]
  %.0 = phi i32 [ 1, %43 ], [ 0, %2 ]
  store ptr %storemerge, ptr %4, align 8, !tbaa !5
  ret i32 %.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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


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