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

; 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.0, %struct.MRef, i32 }
%union.anon.0 = type { %union.TValue }

@.str = external hidden unnamed_addr constant [135 x i8], align 1

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

17:                                               ; preds = %59, %1
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = load i8, ptr %18, align 1, !tbaa !9
  %20 = icmp ne i8 %19, 0
  br i1 %20, label %21, label %63

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i64 2
  store ptr %23, ptr %6, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %37, %21
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %27 = sext i8 %26 to i32
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %34

29:                                               ; preds = %24
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = load i8, ptr %30, align 1, !tbaa !9
  %32 = sext i8 %31 to i32
  %33 = icmp ne i32 %32, 95
  br label %34

34:                                               ; preds = %29, %24
  %35 = phi i1 [ false, %24 ], [ %33, %29 ]
  br i1 %35, label %36, label %40

36:                                               ; preds = %34
  br label %37

37:                                               ; preds = %36
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i32 1
  store ptr %39, ptr %6, align 8, !tbaa !5
  br label %24, !llvm.loop !12

40:                                               ; preds = %34
  %41 = load ptr, ptr %2, align 8, !tbaa !5
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = ptrtoint ptr %43 to i64
  %46 = ptrtoint ptr %44 to i64
  %47 = sub i64 %45, %46
  %48 = call ptr @lj_str_new(ptr noundef %41, ptr noundef %42, i64 noundef %47)
  store ptr %48, ptr %8, align 8, !tbaa !5
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = ptrtoint ptr %49 to i64
  %51 = trunc i64 %50 to i32
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.global_State, ptr %52, i32 0, i32 29
  %54 = load i32, ptr %7, align 4, !tbaa !10
  %55 = add i32 0, %54
  %56 = zext i32 %55 to i64
  %57 = getelementptr inbounds [38 x %struct.GCRef], ptr %53, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.GCRef, ptr %57, i32 0, i32 0
  store i32 %51, ptr %58, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %59

59:                                               ; preds = %40
  %60 = load i32, ptr %7, align 4, !tbaa !10
  %61 = add i32 %60, 1
  store i32 %61, ptr %7, align 4, !tbaa !10
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %62, ptr %5, align 8, !tbaa !5
  br label %17, !llvm.loop !16

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

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

declare hidden ptr @lj_str_new(ptr noundef, ptr noundef, i64 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, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !11, i64 0}
!15 = !{!"GCRef", !11, i64 0}
!16 = distinct !{!16, !13}


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

%struct.GCRef = type { i32 }

@.str = external hidden unnamed_addr constant [135 x i8], align 1

; Function Attrs: nounwind uwtable
define hidden void @lj_meta_init(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i8, ptr @.str, align 1, !tbaa !5
  %.not20 = icmp eq i8 %2, 0
  br i1 %.not20, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %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 %6, i64 260
  br label %8

8:                                                ; preds = %.lr.ph, %.critedge
  %.022 = phi ptr [ @.str, %.lr.ph ], [ %.016, %.critedge ]
  %.01521 = phi i32 [ 0, %.lr.ph ], [ %22, %.critedge ]
  %9 = getelementptr inbounds i8, ptr %.022, i64 2
  br label %10

10:                                               ; preds = %12, %8
  %.016 = phi ptr [ %9, %8 ], [ %13, %12 ]
  %11 = load i8, ptr %.016, align 1, !tbaa !5
  switch i8 %11, label %12 [
    i8 0, label %.critedge
    i8 95, label %.critedge
  ]

12:                                               ; preds = %10
  %13 = getelementptr inbounds i8, ptr %.016, i64 1
  br label %10, !llvm.loop !8

.critedge:                                        ; preds = %10, %10
  %14 = ptrtoint ptr %.016 to i64
  %15 = ptrtoint ptr %.022 to i64
  %16 = sub i64 %14, %15
  %17 = tail call ptr @lj_str_new(ptr noundef %0, ptr noundef nonnull %.022, i64 noundef %16) #2
  %18 = ptrtoint ptr %17 to i64
  %19 = trunc i64 %18 to i32
  %20 = zext i32 %.01521 to i64
  %21 = getelementptr inbounds [38 x %struct.GCRef], ptr %7, i64 0, i64 %20
  store i32 %19, ptr %21, align 4, !tbaa !10
  %22 = add i32 %.01521, 1
  %.not = icmp eq i8 %11, 0
  br i1 %.not, label %._crit_edge, label %8, !llvm.loop !13

._crit_edge:                                      ; preds = %.critedge, %1
  ret void
}

declare hidden ptr @lj_str_new(ptr noundef, ptr 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 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !12, i64 0}
!11 = !{!"GCRef", !12, i64 0}
!12 = !{!"int", !6, i64 0}
!13 = distinct !{!13, !9}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass