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

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

; Function Attrs: nounwind uwtable
define hidden i32 @lj_cconv_tv_bf(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.CType, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 4, !tbaa !9
  store i32 %16, ptr %9, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %17 = load i32, ptr %9, align 4, !tbaa !14
  %18 = lshr i32 %17, 16
  %19 = and i32 %18, 127
  switch i32 %19, label %31 [
    i32 4, label %20
    i32 2, label %23
    i32 1, label %27
  ]

20:                                               ; preds = %4
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = load i32, ptr %21, align 4, !tbaa !14
  store i32 %22, ptr %12, align 4, !tbaa !14
  br label %32

23:                                               ; preds = %4
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = load i16, ptr %24, align 2, !tbaa !15
  %26 = zext i16 %25 to i32
  store i32 %26, ptr %12, align 4, !tbaa !14
  br label %32

27:                                               ; preds = %4
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = load i8, ptr %28, align 1, !tbaa !16
  %30 = zext i8 %29 to i32
  store i32 %30, ptr %12, align 4, !tbaa !14
  br label %32

31:                                               ; preds = %4
  store i32 0, ptr %12, align 4, !tbaa !14
  br label %32

32:                                               ; preds = %31, %27, %23, %20
  %33 = load i32, ptr %9, align 4, !tbaa !14
  %34 = lshr i32 %33, 0
  %35 = and i32 %34, 127
  store i32 %35, ptr %10, align 4, !tbaa !14
  %36 = load i32, ptr %9, align 4, !tbaa !14
  %37 = lshr i32 %36, 8
  %38 = and i32 %37, 127
  store i32 %38, ptr %11, align 4, !tbaa !14
  %39 = load i32, ptr %10, align 4, !tbaa !14
  %40 = load i32, ptr %11, align 4, !tbaa !14
  %41 = add i32 %39, %40
  %42 = load i32, ptr %9, align 4, !tbaa !14
  %43 = lshr i32 %42, 16
  %44 = and i32 %43, 127
  %45 = mul i32 8, %44
  %46 = icmp ugt i32 %41, %45
  br i1 %46, label %47, label %51

47:                                               ; preds = %32
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.CTState, ptr %48, i32 0, i32 3
  %50 = load ptr, ptr %49, align 8, !tbaa !17
  call void @lj_err_caller(ptr noundef %50, i32 noundef 3687) #5
  unreachable

51:                                               ; preds = %32
  %52 = load i32, ptr %9, align 4, !tbaa !14
  %53 = and i32 %52, 134217728
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %82, label %55

55:                                               ; preds = %51
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %56 = load i32, ptr %11, align 4, !tbaa !14
  %57 = sub i32 32, %56
  store i32 %57, ptr %13, align 4, !tbaa !14
  %58 = load i32, ptr %9, align 4, !tbaa !14
  %59 = and i32 %58, 8388608
  %60 = icmp ne i32 %59, 0
  br i1 %60, label %70, label %61

61:                                               ; preds = %55
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = load i32, ptr %12, align 4, !tbaa !14
  %64 = load i32, ptr %13, align 4, !tbaa !14
  %65 = load i32, ptr %10, align 4, !tbaa !14
  %66 = sub i32 %64, %65
  %67 = shl i32 %63, %66
  %68 = load i32, ptr %13, align 4, !tbaa !14
  %69 = ashr i32 %67, %68
  call void @setintV(ptr noundef %62, i32 noundef %69)
  br label %81

70:                                               ; preds = %55
  %71 = load i32, ptr %12, align 4, !tbaa !14
  %72 = load i32, ptr %13, align 4, !tbaa !14
  %73 = load i32, ptr %10, align 4, !tbaa !14
  %74 = sub i32 %72, %73
  %75 = shl i32 %71, %74
  %76 = load i32, ptr %13, align 4, !tbaa !14
  %77 = lshr i32 %75, %76
  store i32 %77, ptr %12, align 4, !tbaa !14
  %78 = load i32, ptr %12, align 4, !tbaa !14
  %79 = uitofp i32 %78 to double
  %80 = load ptr, ptr %7, align 8, !tbaa !5
  store double %79, ptr %80, align 8, !tbaa !16
  br label %81

81:                                               ; preds = %70, %61
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  br label %90

82:                                               ; preds = %51
  %83 = load i32, ptr %12, align 4, !tbaa !14
  %84 = load i32, ptr %10, align 4, !tbaa !14
  %85 = lshr i32 %83, %84
  %86 = and i32 %85, 1
  %87 = sub i32 -2, %86
  %88 = load ptr, ptr %7, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.anon, ptr %88, i32 0, i32 1
  store i32 %87, ptr %89, align 4, !tbaa !16
  br label %90

90:                                               ; preds = %82, %81
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret i32 0
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @setintV(ptr noundef, i32 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { 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 #4 = { nounwind }
attributes #5 = { 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 0}
!10 = !{!"CType", !11, i64 0, !11, i64 4, !12, i64 8, !12, i64 10, !13, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!"GCRef", !11, i64 0}
!14 = !{!11, !11, i64 0}
!15 = !{!12, !12, i64 0}
!16 = !{!7, !7, i64 0}
!17 = !{!18, !6, i64 16}
!18 = !{!"CTState", !6, i64 0, !11, i64 8, !11, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !19, i64 48, !7, i64 216}
!19 = !{!"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}


; 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: nounwind uwtable
define hidden noundef i32 @lj_cconv_tv_bf(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, ptr noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = lshr i32 %5, 16
  %7 = and i32 %6, 127
  switch i32 %7, label %16 [
    i32 4, label %8
    i32 2, label %10
    i32 1, label %13
  ]

8:                                                ; preds = %4
  %9 = load i32, ptr %3, align 4, !tbaa !12
  br label %16

10:                                               ; preds = %4
  %11 = load i16, ptr %3, align 2, !tbaa !13
  %12 = zext i16 %11 to i32
  br label %16

13:                                               ; preds = %4
  %14 = load i8, ptr %3, align 1, !tbaa !14
  %15 = zext i8 %14 to i32
  br label %16

16:                                               ; preds = %4, %13, %10, %8
  %.0 = phi i32 [ %15, %13 ], [ %12, %10 ], [ %9, %8 ], [ 0, %4 ]
  %17 = and i32 %5, 127
  %18 = lshr i32 %5, 8
  %19 = and i32 %18, 127
  %20 = add nuw nsw i32 %19, %17
  %21 = shl nuw nsw i32 %7, 3
  %22 = icmp ugt i32 %20, %21
  br i1 %22, label %23, label %26

23:                                               ; preds = %16
  %24 = getelementptr inbounds i8, ptr %0, i64 16
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  tail call void @lj_err_caller(ptr noundef %25, i32 noundef 3687) #3
  unreachable

26:                                               ; preds = %16
  %27 = and i32 %5, 134217728
  %.not = icmp eq i32 %27, 0
  br i1 %.not, label %28, label %38

28:                                               ; preds = %26
  %29 = sub nsw i32 32, %19
  %30 = and i32 %5, 8388608
  %.not27 = icmp eq i32 %30, 0
  %31 = sub nsw i32 %29, %17
  %32 = shl i32 %.0, %31
  br i1 %.not27, label %33, label %35

33:                                               ; preds = %28
  %34 = ashr i32 %32, %29
  tail call void @setintV(ptr noundef %2, i32 noundef %34)
  br label %43

35:                                               ; preds = %28
  %36 = lshr i32 %32, %29
  %37 = uitofp i32 %36 to double
  store double %37, ptr %2, align 8, !tbaa !14
  br label %43

38:                                               ; preds = %26
  %39 = lshr i32 %.0, %17
  %40 = and i32 %39, 1
  %41 = sub nuw nsw i32 -2, %40
  %42 = getelementptr inbounds i8, ptr %2, i64 4
  store i32 %41, ptr %42, align 4, !tbaa !14
  br label %43

43:                                               ; preds = %33, %35, %38
  ret i32 0
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @setintV(ptr noundef, i32 noundef) local_unnamed_addr #2

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 = { 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 #3 = { noreturn 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, !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 = !{!7, !7, i64 0}
!13 = !{!10, !10, i64 0}
!14 = !{!8, !8, i64 0}
!15 = !{!16, !17, i64 16}
!16 = !{!"CTState", !17, i64 0, !7, i64 8, !7, i64 12, !17, i64 16, !17, i64 24, !17, i64 32, !17, i64 40, !18, i64 48, !8, i64 216}
!17 = !{!"any pointer", !8, i64 0}
!18 = !{!"CCallback", !8, i64 0, !8, i64 64, !17, i64 128, !17, i64 136, !17, i64 144, !7, i64 152, !7, i64 156, !7, i64 160}


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