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

; 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

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

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

; Function Attrs: nounwind uwtable
define hidden i32 @lj_ctype_vlsize(ptr noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store i64 0, ptr %7, align 8, !tbaa !11
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.CType, ptr %11, i32 0, i32 0
  %13 = load i32, ptr %12, align 4, !tbaa !13
  %14 = lshr i32 %13, 28
  %15 = icmp eq i32 %14, 1
  br i1 %15, label %16, label %51

16:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.CType, ptr %17, i32 0, i32 2
  %19 = load i16, ptr %18, align 4, !tbaa !17
  %20 = zext i16 %19 to i32
  store i32 %20, ptr %9, align 4, !tbaa !9
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.CType, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 4, !tbaa !18
  %24 = zext i32 %23 to i64
  store i64 %24, ptr %7, align 8, !tbaa !11
  br label %25

25:                                               ; preds = %42, %16
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %47

28:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = call ptr @ctype_get(ptr noundef %29, i32 noundef %30)
  store ptr %31, ptr %10, align 8, !tbaa !5
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.CType, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 4, !tbaa !13
  %35 = lshr i32 %34, 28
  %36 = icmp eq i32 %35, 9
  br i1 %36, label %37, label %42

37:                                               ; preds = %28
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.CType, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 4, !tbaa !13
  %41 = and i32 %40, 65535
  store i32 %41, ptr %8, align 4, !tbaa !9
  br label %42

42:                                               ; preds = %37, %28
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.CType, ptr %43, i32 0, i32 2
  %45 = load i16, ptr %44, align 4, !tbaa !17
  %46 = zext i16 %45 to i32
  store i32 %46, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %25, !llvm.loop !19

47:                                               ; preds = %25
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load i32, ptr %8, align 4, !tbaa !9
  %50 = call ptr @ctype_raw(ptr noundef %48, i32 noundef %49)
  store ptr %50, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %51

51:                                               ; preds = %47, %3
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = call ptr @ctype_rawchild(ptr noundef %52, ptr noundef %53)
  store ptr %54, ptr %5, align 8, !tbaa !5
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.CType, ptr %55, i32 0, i32 1
  %57 = load i32, ptr %56, align 4, !tbaa !18
  %58 = zext i32 %57 to i64
  %59 = load i32, ptr %6, align 4, !tbaa !9
  %60 = zext i32 %59 to i64
  %61 = mul i64 %58, %60
  %62 = load i64, ptr %7, align 8, !tbaa !11
  %63 = add i64 %62, %61
  store i64 %63, ptr %7, align 8, !tbaa !11
  %64 = load i64, ptr %7, align 8, !tbaa !11
  %65 = icmp ult i64 %64, 2147483648
  br i1 %65, label %66, label %69

66:                                               ; preds = %51
  %67 = load i64, ptr %7, align 8, !tbaa !11
  %68 = trunc i64 %67 to i32
  br label %70

69:                                               ; preds = %51
  br label %70

70:                                               ; preds = %69, %66
  %71 = phi i32 [ %68, %66 ], [ -1, %69 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret i32 %71
}

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

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 = { 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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"CType", !10, i64 0, !10, i64 4, !15, i64 8, !15, i64 10, !16, i64 12}
!15 = !{!"short", !7, i64 0}
!16 = !{!"GCRef", !10, i64 0}
!17 = !{!14, !15, i64 8}
!18 = !{!14, !10, i64 4}
!19 = distinct !{!19, !20}
!20 = !{!"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: alwaysinline nounwind uwtable
declare hidden ptr @ctype_get(ptr noundef, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define hidden i32 @lj_ctype_vlsize(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = load i32, ptr %1, align 4, !tbaa !5
  %.mask = and i32 %4, -268435456
  %5 = icmp eq i32 %.mask, 268435456
  br i1 %5, label %6, label %15

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %1, i64 4
  %8 = load i32, ptr %7, align 4, !tbaa !12
  %9 = zext i32 %8 to i64
  %.017.in.in21 = getelementptr inbounds i8, ptr %1, i64 8
  %.017.in22 = load i16, ptr %.017.in.in21, align 4, !tbaa !13
  %.not23 = icmp eq i16 %.017.in22, 0
  br i1 %.not23, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6, %.lr.ph
  %.017.in25 = phi i16 [ %.017.in, %.lr.ph ], [ %.017.in22, %6 ]
  %.01824 = phi i32 [ %spec.select, %.lr.ph ], [ 0, %6 ]
  %.017 = zext i16 %.017.in25 to i32
  %10 = tail call ptr @ctype_get(ptr noundef %0, i32 noundef %.017)
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %.mask20 = and i32 %11, -268435456
  %12 = icmp eq i32 %.mask20, -1879048192
  %13 = and i32 %11, 65535
  %spec.select = select i1 %12, i32 %13, i32 %.01824
  %.017.in.in = getelementptr inbounds i8, ptr %10, i64 8
  %.017.in = load i16, ptr %.017.in.in, align 4, !tbaa !13
  %.not = icmp eq i16 %.017.in, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph, %6
  %.018.lcssa = phi i32 [ 0, %6 ], [ %spec.select, %.lr.ph ]
  %14 = tail call ptr @ctype_raw(ptr noundef %0, i32 noundef %.018.lcssa)
  br label %15

15:                                               ; preds = %._crit_edge, %3
  %.019 = phi i64 [ %9, %._crit_edge ], [ 0, %3 ]
  %.0 = phi ptr [ %14, %._crit_edge ], [ %1, %3 ]
  %16 = tail call ptr @ctype_rawchild(ptr noundef %0, ptr noundef %.0)
  %17 = getelementptr inbounds i8, ptr %16, i64 4
  %18 = load i32, ptr %17, align 4, !tbaa !12
  %19 = zext i32 %18 to i64
  %20 = zext i32 %2 to i64
  %21 = mul nuw i64 %19, %20
  %22 = add nuw i64 %21, %.019
  %23 = icmp ult i64 %22, 2147483648
  %24 = trunc i64 %22 to i32
  %25 = select i1 %23, i32 %24, i32 -1
  ret i32 %25
}

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

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 = { 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" }

!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, !7, i64 0}
!6 = !{!"CType", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 10, !11, i64 12}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!"GCRef", !7, i64 0}
!12 = !{!6, !7, i64 4}
!13 = !{!6, !10, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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