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

; 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.CType = type { i32, i32, i16, i16, %struct.GCRef }
%struct.GCRef = type { i32 }

; Function Attrs: nounwind uwtable
define hidden i32 @lj_ctype_new(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = 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 4, ptr %5) #5
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.CTState, ptr %7, i32 0, i32 1
  %9 = load i32, ptr %8, align 8, !tbaa !9
  store i32 %9, ptr %5, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %10 = load i32, ptr %5, align 4, !tbaa !13
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.CTState, ptr %11, i32 0, i32 2
  %13 = load i32, ptr %12, align 4, !tbaa !14
  %14 = icmp uge i32 %10, %13
  %15 = xor i1 %14, true
  %16 = xor i1 %15, true
  %17 = zext i1 %16 to i32
  %18 = sext i32 %17 to i64
  %19 = call i64 @llvm.expect.i64(i64 %18, i64 0)
  %20 = icmp ne i64 %19, 0
  br i1 %20, label %21, label %40

21:                                               ; preds = %2
  %22 = load i32, ptr %5, align 4, !tbaa !13
  %23 = icmp uge i32 %22, 8388607
  br i1 %23, label %24, label %28

24:                                               ; preds = %21
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.CTState, ptr %25, i32 0, i32 3
  %27 = load ptr, ptr %26, align 8, !tbaa !15
  call void @lj_err_msg(ptr noundef %27, i32 noundef 139) #6
  unreachable

28:                                               ; preds = %21
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.CTState, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !15
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.CTState, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !16
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.CTState, ptr %35, i32 0, i32 2
  %37 = call ptr @lj_mem_grow(ptr noundef %31, ptr noundef %34, ptr noundef %36, i32 noundef 8388607, i32 noundef 16)
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.CTState, ptr %38, i32 0, i32 0
  store ptr %37, ptr %39, align 8, !tbaa !16
  br label %40

40:                                               ; preds = %28, %2
  %41 = load i32, ptr %5, align 4, !tbaa !13
  %42 = add i32 %41, 1
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.CTState, ptr %43, i32 0, i32 1
  store i32 %42, ptr %44, align 8, !tbaa !9
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.CTState, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !16
  %48 = load i32, ptr %5, align 4, !tbaa !13
  %49 = zext i32 %48 to i64
  %50 = getelementptr inbounds %struct.CType, ptr %47, i64 %49
  store ptr %50, ptr %6, align 8, !tbaa !5
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %50, ptr %51, align 8, !tbaa !5
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.CType, ptr %52, i32 0, i32 0
  store i32 0, ptr %53, align 4, !tbaa !17
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.CType, ptr %54, i32 0, i32 1
  store i32 0, ptr %55, align 4, !tbaa !21
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.CType, ptr %56, i32 0, i32 2
  store i16 0, ptr %57, align 4, !tbaa !22
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.CType, ptr %58, i32 0, i32 3
  store i16 0, ptr %59, align 2, !tbaa !23
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.CType, ptr %60, i32 0, i32 4
  %62 = getelementptr inbounds %struct.GCRef, ptr %61, i32 0, i32 0
  store i32 0, ptr %62, align 4, !tbaa !24
  %63 = load i32, ptr %5, align 4, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  ret i32 %63
}

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #2

; Function Attrs: noreturn
declare hidden void @lj_err_msg(ptr noundef, i32 noundef) #3

declare hidden ptr @lj_mem_grow(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef) #4

; 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 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #3 = { noreturn "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 = { "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 #5 = { nounwind }
attributes #6 = { noreturn }

!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, !11, i64 8}
!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 = !{!11, !11, i64 0}
!14 = !{!10, !11, i64 12}
!15 = !{!10, !6, i64 16}
!16 = !{!10, !6, i64 0}
!17 = !{!18, !11, i64 0}
!18 = !{!"CType", !11, i64 0, !11, i64 4, !19, i64 8, !19, i64 10, !20, i64 12}
!19 = !{!"short", !7, i64 0}
!20 = !{!"GCRef", !11, i64 0}
!21 = !{!18, !11, i64 4}
!22 = !{!18, !19, i64 8}
!23 = !{!18, !19, i64 10}
!24 = !{!18, !11, i64 12}


; 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.CType = type { i32, i32, i16, i16, %struct.GCRef }
%struct.GCRef = type { i32 }

; Function Attrs: nounwind uwtable
define hidden i32 @lj_ctype_new(ptr noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 12
  %6 = load i32, ptr %5, align 4, !tbaa !12
  %.not = icmp ult i32 %4, %6
  br i1 %.not, label %._crit_edge, label %7, !prof !13

._crit_edge:                                      ; preds = %2
  %.pre = load ptr, ptr %0, align 8, !tbaa !14
  br label %15

7:                                                ; preds = %2
  %8 = icmp ugt i32 %4, 8388606
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load ptr, ptr %9, align 8, !tbaa !15
  br i1 %8, label %11, label %12

11:                                               ; preds = %7
  tail call void @lj_err_msg(ptr noundef %10, i32 noundef 139) #4
  unreachable

12:                                               ; preds = %7
  %13 = load ptr, ptr %0, align 8, !tbaa !14
  %14 = tail call ptr @lj_mem_grow(ptr noundef %10, ptr noundef %13, ptr noundef nonnull %5, i32 noundef 8388607, i32 noundef 16) #5
  store ptr %14, ptr %0, align 8, !tbaa !14
  br label %15

15:                                               ; preds = %._crit_edge, %12
  %16 = phi ptr [ %.pre, %._crit_edge ], [ %14, %12 ]
  %17 = add nuw i32 %4, 1
  store i32 %17, ptr %3, align 8, !tbaa !5
  %18 = zext i32 %4 to i64
  %19 = getelementptr inbounds %struct.CType, ptr %16, i64 %18
  store ptr %19, ptr %1, align 8, !tbaa !16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %19, i8 0, i64 16, i1 false)
  ret i32 %4
}

; Function Attrs: noreturn
declare hidden void @lj_err_msg(ptr noundef, i32 noundef) local_unnamed_addr #1

declare hidden ptr @lj_mem_grow(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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 = { noreturn "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { noreturn nounwind }
attributes #5 = { 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, !10, i64 8}
!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 = !{!6, !10, i64 12}
!13 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!14 = !{!6, !7, i64 0}
!15 = !{!6, !7, i64 16}
!16 = !{!7, !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LowerExpectIntrinsicPass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass