; LLVM IR from ejoy/ejoy2d :: lib (texture)

; 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.texture_pool = type { i32, [128 x %struct.texture] }
%struct.texture = type { i32, i32, float, float, i32, i32 }

@POOL = external hidden global %struct.texture_pool, align 4

; 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 dso_local i32 @texture_coord(i32 noundef %0, float noundef %1, float noundef %2, ptr noundef %3, ptr noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca float, align 4
  %9 = alloca float, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  store i32 %0, ptr %7, align 4, !tbaa !5
  store float %1, ptr %8, align 4, !tbaa !9
  store float %2, ptr %9, align 4, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !11
  store ptr %4, ptr %11, align 8, !tbaa !11
  %14 = load i32, ptr %7, align 4, !tbaa !5
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %20, label %16

16:                                               ; preds = %5
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = load i32, ptr @POOL, align 4, !tbaa !13
  %19 = icmp sge i32 %17, %18
  br i1 %19, label %20, label %27

20:                                               ; preds = %16, %5
  %21 = load float, ptr %8, align 4, !tbaa !9
  %22 = fptoui float %21 to i16
  %23 = load ptr, ptr %10, align 8, !tbaa !11
  store i16 %22, ptr %23, align 2, !tbaa !15
  %24 = load float, ptr %9, align 4, !tbaa !9
  %25 = fptoui float %24 to i16
  %26 = load ptr, ptr %11, align 8, !tbaa !11
  store i16 %25, ptr %26, align 2, !tbaa !15
  store i32 1, ptr %6, align 4
  br label %72

27:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %28 = load i32, ptr %7, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [128 x %struct.texture], ptr getelementptr inbounds (%struct.texture_pool, ptr @POOL, i32 0, i32 1), i64 0, i64 %29
  store ptr %30, ptr %12, align 8, !tbaa !11
  %31 = load ptr, ptr %12, align 8, !tbaa !11
  %32 = getelementptr inbounds %struct.texture, ptr %31, i32 0, i32 2
  %33 = load float, ptr %32, align 4, !tbaa !17
  %34 = fcmp oeq float %33, 0.000000e+00
  br i1 %34, label %35, label %42

35:                                               ; preds = %27
  %36 = load float, ptr %8, align 4, !tbaa !9
  %37 = fptoui float %36 to i16
  %38 = load ptr, ptr %10, align 8, !tbaa !11
  store i16 %37, ptr %38, align 2, !tbaa !15
  %39 = load float, ptr %9, align 4, !tbaa !9
  %40 = fptoui float %39 to i16
  %41 = load ptr, ptr %11, align 8, !tbaa !11
  store i16 %40, ptr %41, align 2, !tbaa !15
  store i32 1, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %71

42:                                               ; preds = %27
  %43 = load ptr, ptr %12, align 8, !tbaa !11
  %44 = getelementptr inbounds %struct.texture, ptr %43, i32 0, i32 2
  %45 = load float, ptr %44, align 4, !tbaa !17
  %46 = load float, ptr %8, align 4, !tbaa !9
  %47 = fmul float %46, %45
  store float %47, ptr %8, align 4, !tbaa !9
  %48 = load ptr, ptr %12, align 8, !tbaa !11
  %49 = getelementptr inbounds %struct.texture, ptr %48, i32 0, i32 3
  %50 = load float, ptr %49, align 4, !tbaa !19
  %51 = load float, ptr %9, align 4, !tbaa !9
  %52 = fmul float %51, %50
  store float %52, ptr %9, align 4, !tbaa !9
  %53 = load float, ptr %8, align 4, !tbaa !9
  %54 = fcmp ogt float %53, 1.000000e+00
  br i1 %54, label %55, label %56

55:                                               ; preds = %42
  store float 1.000000e+00, ptr %8, align 4, !tbaa !9
  br label %56

56:                                               ; preds = %55, %42
  %57 = load float, ptr %9, align 4, !tbaa !9
  %58 = fcmp ogt float %57, 1.000000e+00
  br i1 %58, label %59, label %60

59:                                               ; preds = %56
  store float 1.000000e+00, ptr %9, align 4, !tbaa !9
  br label %60

60:                                               ; preds = %59, %56
  %61 = load float, ptr %8, align 4, !tbaa !9
  %62 = fmul float %61, 6.553500e+04
  store float %62, ptr %8, align 4, !tbaa !9
  %63 = load float, ptr %9, align 4, !tbaa !9
  %64 = fmul float %63, 6.553500e+04
  store float %64, ptr %9, align 4, !tbaa !9
  %65 = load float, ptr %8, align 4, !tbaa !9
  %66 = fptoui float %65 to i16
  %67 = load ptr, ptr %10, align 8, !tbaa !11
  store i16 %66, ptr %67, align 2, !tbaa !15
  %68 = load float, ptr %9, align 4, !tbaa !9
  %69 = fptoui float %68 to i16
  %70 = load ptr, ptr %11, align 8, !tbaa !11
  store i16 %69, ptr %70, align 2, !tbaa !15
  store i32 0, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %71

71:                                               ; preds = %60, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  br label %72

72:                                               ; preds = %71, %20
  %73 = load i32, ptr %6, align 4
  ret i32 %73
}

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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"float", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"texture_pool", !6, i64 0, !7, i64 4}
!15 = !{!16, !16, i64 0}
!16 = !{!"short", !7, i64 0}
!17 = !{!18, !10, i64 8}
!18 = !{!"texture", !6, i64 0, !6, i64 4, !10, i64 8, !10, i64 12, !6, i64 16, !6, i64 20}
!19 = !{!18, !10, 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.texture_pool = type { i32, [128 x %struct.texture] }
%struct.texture = type { i32, i32, float, float, i32, i32 }

@POOL = external hidden local_unnamed_addr global %struct.texture_pool, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: write, inaccessiblemem: none) uwtable
define dso_local range(i32 0, 2) i32 @texture_coord(i32 noundef %0, float noundef %1, float noundef %2, ptr nocapture noundef writeonly %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #0 {
  %6 = icmp sgt i32 %0, -1
  %7 = load i32, ptr @POOL, align 4
  %.not = icmp sgt i32 %7, %0
  %or.cond = select i1 %6, i1 %.not, i1 false
  %8 = insertelement <2 x float> poison, float %1, i64 0
  %9 = insertelement <2 x float> %8, float %2, i64 1
  br i1 %or.cond, label %10, label %25

10:                                               ; preds = %5
  %11 = zext nneg i32 %0 to i64
  %12 = getelementptr inbounds [128 x %struct.texture], ptr getelementptr inbounds (i8, ptr @POOL, i64 4), i64 0, i64 %11
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  %14 = load float, ptr %13, align 4, !tbaa !5
  %15 = fcmp oeq float %14, 0.000000e+00
  br i1 %15, label %25, label %16

16:                                               ; preds = %10
  %17 = getelementptr inbounds i8, ptr %12, i64 12
  %18 = load float, ptr %17, align 4, !tbaa !11
  %19 = insertelement <2 x float> poison, float %14, i64 0
  %20 = insertelement <2 x float> %19, float %18, i64 1
  %21 = fmul <2 x float> %20, %9
  %22 = fcmp ogt <2 x float> %21, <float 1.000000e+00, float 1.000000e+00>
  %23 = select <2 x i1> %22, <2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x float> %21
  %24 = fmul <2 x float> %23, <float 6.553500e+04, float 6.553500e+04>
  br label %25

25:                                               ; preds = %16, %10, %5
  %.1 = phi i32 [ 1, %5 ], [ 0, %16 ], [ 1, %10 ]
  %26 = phi <2 x float> [ %9, %5 ], [ %24, %16 ], [ %9, %10 ]
  %27 = extractelement <2 x float> %26, i64 0
  %storemerge = fptoui float %27 to i16
  store i16 %storemerge, ptr %3, align 2, !tbaa !12
  %28 = extractelement <2 x float> %26, i64 1
  %storemerge31 = fptoui float %28 to i16
  store i16 %storemerge31, ptr %4, align 2, !tbaa !12
  ret i32 %.1
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: write, inaccessiblemem: none) 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, !10, i64 8}
!6 = !{!"texture", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 12, !7, i64 16, !7, i64 20}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"float", !8, i64 0}
!11 = !{!6, !10, i64 12}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !8, i64 0}


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