; 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.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 }
%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: alwaysinline nounwind uwtable
declare hidden void @copyTV(ptr noundef, ptr noundef, ptr noundef) #1

; 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_state_growstack1(ptr noundef) #2

declare hidden void @lj_vm_call(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @setstrV(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @lua_concat(ptr noundef %0, i32 noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = load i32, ptr %4, align 4, !tbaa !9
  %9 = icmp sge i32 %8, 2
  br i1 %9, label %10, label %78

10:                                               ; preds = %2
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = add nsw i32 %11, -1
  store i32 %12, ptr %4, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %73, %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = load ptr, ptr %3, 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 = load i32, ptr %4, align 4, !tbaa !9
  %21 = sub nsw i32 0, %20
  %22 = call ptr @lj_meta_cat(ptr noundef %14, ptr noundef %19, i32 noundef %21)
  store ptr %22, ptr %5, align 8, !tbaa !5
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = icmp eq ptr %23, null
  br i1 %24, label %25, label %34

25:                                               ; preds = %13
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.lua_State, ptr %27, i32 0, i32 0
  %29 = getelementptr inbounds %struct.anon, ptr %28, i32 0, i32 8
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  %31 = sext i32 %26 to i64
  %32 = sub i64 0, %31
  %33 = getelementptr inbounds %union.TValue, ptr %30, i64 %32
  store ptr %33, ptr %29, align 8, !tbaa !11
  store i32 2, ptr %6, align 4
  br label %70

34:                                               ; preds = %13
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.lua_State, ptr %35, i32 0, i32 0
  %37 = getelementptr inbounds %struct.anon, ptr %36, i32 0, i32 8
  %38 = load ptr, ptr %37, align 8, !tbaa !11
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = ptrtoint ptr %38 to i64
  %41 = ptrtoint ptr %39 to i64
  %42 = sub i64 %40, %41
  %43 = sdiv exact i64 %42, 8
  %44 = trunc i64 %43 to i32
  %45 = load i32, ptr %4, align 4, !tbaa !9
  %46 = sub nsw i32 %45, %44
  store i32 %46, ptr %4, align 4, !tbaa !9
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %union.TValue, ptr %47, i64 2
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.lua_State, ptr %49, i32 0, i32 0
  %51 = getelementptr inbounds %struct.anon, ptr %50, i32 0, i32 8
  store ptr %48, ptr %51, align 8, !tbaa !11
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  call void @lj_vm_call(ptr noundef %52, ptr noundef %53, i32 noundef 2)
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.lua_State, ptr %54, i32 0, i32 0
  %56 = getelementptr inbounds %struct.anon, ptr %55, i32 0, i32 8
  %57 = load ptr, ptr %56, align 8, !tbaa !11
  %58 = getelementptr inbounds %union.TValue, ptr %57, i64 -1
  store ptr %58, ptr %56, align 8, !tbaa !11
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.lua_State, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds %struct.anon, ptr %61, i32 0, i32 8
  %63 = load ptr, ptr %62, align 8, !tbaa !11
  %64 = getelementptr inbounds %union.TValue, ptr %63, i64 -1
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.lua_State, ptr %65, i32 0, i32 0
  %67 = getelementptr inbounds %struct.anon, ptr %66, i32 0, i32 8
  %68 = load ptr, ptr %67, align 8, !tbaa !11
  %69 = getelementptr inbounds %union.TValue, ptr %68, i64 0
  call void @copyTV(ptr noundef %59, ptr noundef %64, ptr noundef %69)
  store i32 0, ptr %6, align 4
  br label %70

70:                                               ; preds = %34, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  %71 = load i32, ptr %6, align 4
  switch i32 %71, label %115 [
    i32 0, label %72
    i32 2, label %77
  ]

72:                                               ; preds = %70
  br label %73

73:                                               ; preds = %72
  %74 = load i32, ptr %4, align 4, !tbaa !9
  %75 = add nsw i32 %74, -1
  store i32 %75, ptr %4, align 4, !tbaa !9
  %76 = icmp sgt i32 %75, 0
  br i1 %76, label %13, label %77, !llvm.loop !12

77:                                               ; preds = %73, %70
  br label %114

78:                                               ; preds = %2
  %79 = load i32, ptr %4, align 4, !tbaa !9
  %80 = icmp eq i32 %79, 0
  br i1 %80, label %81, label %113

81:                                               ; preds = %78
  %82 = load ptr, ptr %3, align 8, !tbaa !5
  %83 = load ptr, ptr %3, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.lua_State, ptr %83, i32 0, i32 0
  %85 = getelementptr inbounds %struct.anon, ptr %84, i32 0, i32 8
  %86 = load ptr, ptr %85, align 8, !tbaa !11
  %87 = load ptr, ptr %3, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.lua_State, ptr %87, i32 0, i32 0
  %89 = getelementptr inbounds %struct.anon, ptr %88, i32 0, i32 5
  %90 = getelementptr inbounds %struct.MRef, ptr %89, i32 0, i32 0
  %91 = load i32, ptr %90, align 8, !tbaa !11
  %92 = zext i32 %91 to i64
  %93 = inttoptr i64 %92 to ptr
  %94 = getelementptr inbounds %struct.global_State, ptr %93, i32 0, i32 8
  call void @setstrV(ptr noundef %82, ptr noundef %86, ptr noundef %94)
  %95 = load ptr, ptr %3, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.lua_State, ptr %95, i32 0, i32 0
  %97 = getelementptr inbounds %struct.anon, ptr %96, i32 0, i32 8
  %98 = load ptr, ptr %97, align 8, !tbaa !11
  %99 = getelementptr inbounds %union.TValue, ptr %98, i32 1
  store ptr %99, ptr %97, align 8, !tbaa !11
  %100 = load ptr, ptr %3, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.lua_State, ptr %100, i32 0, i32 0
  %102 = getelementptr inbounds %struct.anon, ptr %101, i32 0, i32 9
  %103 = getelementptr inbounds %struct.MRef, ptr %102, i32 0, i32 0
  %104 = load i32, ptr %103, align 8, !tbaa !11
  %105 = zext i32 %104 to i64
  %106 = inttoptr i64 %105 to ptr
  %107 = icmp uge ptr %99, %106
  br i1 %107, label %108, label %110

108:                                              ; preds = %81
  %109 = load ptr, ptr %3, align 8, !tbaa !5
  call void @lj_state_growstack1(ptr noundef %109)
  br label %110

110:                                              ; preds = %108, %81
  %111 = phi i1 [ false, %81 ], [ false, %108 ]
  %112 = zext i1 %111 to i32
  br label %113

113:                                              ; preds = %110, %78
  br label %114

114:                                              ; preds = %113, %77
  ret void

115:                                              ; preds = %70
  unreachable
}

declare hidden ptr @lj_meta_cat(ptr noundef, ptr noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { alwaysinline 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 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 #4 = { 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 = distinct !{!12, !13}
!13 = !{!"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"

%union.TValue = type { i64 }

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

declare hidden void @lj_state_growstack1(ptr noundef) local_unnamed_addr #1

declare hidden void @lj_vm_call(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local void @lua_concat(ptr noundef %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = icmp sgt i32 %1, 1
  br i1 %3, label %.preheader, label %27

.preheader:                                       ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  br label %5

5:                                                ; preds = %.preheader, %15
  %.028.in = phi i32 [ %21, %15 ], [ %1, %.preheader ]
  %.028 = add nsw i32 %.028.in, -1
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 -8
  %8 = sub nsw i32 1, %.028.in
  %9 = tail call ptr @lj_meta_cat(ptr noundef %0, ptr noundef nonnull %7, i32 noundef %8) #3
  %10 = icmp eq ptr %9, null
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  br i1 %10, label %.thread, label %15

.thread:                                          ; preds = %5
  %12 = zext nneg i32 %.028 to i64
  %13 = sub nsw i64 0, %12
  %14 = getelementptr inbounds %union.TValue, ptr %11, i64 %13
  store ptr %14, ptr %4, align 8, !tbaa !5
  br label %.loopexit

15:                                               ; preds = %5
  %16 = ptrtoint ptr %11 to i64
  %17 = ptrtoint ptr %9 to i64
  %18 = sub i64 %16, %17
  %19 = lshr exact i64 %18, 3
  %20 = trunc i64 %19 to i32
  %21 = sub nsw i32 %.028, %20
  %22 = getelementptr inbounds i8, ptr %9, i64 16
  store ptr %22, ptr %4, align 8, !tbaa !5
  tail call void @lj_vm_call(ptr noundef nonnull %0, ptr noundef nonnull %9, i32 noundef 2) #3
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %23, i64 -8
  store ptr %24, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %23, i64 -16
  tail call void @copyTV(ptr noundef nonnull %0, ptr noundef nonnull %25, ptr noundef nonnull %24)
  %26 = icmp sgt i32 %21, 1
  br i1 %26, label %5, label %.loopexit, !llvm.loop !8

27:                                               ; preds = %2
  %28 = icmp eq i32 %1, 0
  br i1 %28, label %29, label %.loopexit

29:                                               ; preds = %27
  %30 = getelementptr inbounds i8, ptr %0, i64 24
  %31 = load ptr, ptr %30, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %0, i64 8
  %33 = load i32, ptr %32, align 8, !tbaa !5
  %34 = zext i32 %33 to i64
  %35 = inttoptr i64 %34 to ptr
  %36 = getelementptr inbounds i8, ptr %35, i64 108
  tail call void @setstrV(ptr noundef %0, ptr noundef %31, ptr noundef nonnull %36)
  %37 = load ptr, ptr %30, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i64 8
  store ptr %38, ptr %30, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %0, i64 32
  %40 = load i32, ptr %39, align 8, !tbaa !5
  %41 = zext i32 %40 to i64
  %42 = inttoptr i64 %41 to ptr
  %.not = icmp ult ptr %38, %42
  br i1 %.not, label %.loopexit, label %43

43:                                               ; preds = %29
  tail call void @lj_state_growstack1(ptr noundef nonnull %0) #3
  br label %.loopexit

.loopexit:                                        ; preds = %15, %.thread, %29, %43, %27
  ret void
}

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

attributes #0 = { alwaysinline 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 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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