; LLVM IR from danginsburg/opengles3-book :: Chapter_14/Noise3D (Noise3D)

; 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"

@gradientTable = external hidden global [768 x float], align 16
@permTable = external hidden global [256 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @initNoiseTable() #0 {
  %1 = alloca i32, align 4
  %2 = alloca float, align 4
  %3 = alloca float, align 4
  %4 = alloca float, align 4
  %5 = alloca float, align 4
  %6 = alloca float, align 4
  %7 = alloca float, align 4
  %8 = alloca [768 x float], align 16
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 3072, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  call void @srandom(i32 noundef 0) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %58, %0
  %13 = load i32, ptr %1, align 4, !tbaa !5
  %14 = icmp slt i32 %13, 256
  br i1 %14, label %15, label %61

15:                                               ; preds = %12
  %16 = call i64 @random() #4
  %17 = srem i64 %16, 32768
  %18 = sitofp i64 %17 to float
  %19 = fdiv float %18, 3.276800e+04
  store float %19, ptr %2, align 4, !tbaa !9
  %20 = load float, ptr %2, align 4, !tbaa !9
  %21 = call float @llvm.fmuladd.f32(float -2.000000e+00, float %20, float 1.000000e+00)
  store float %21, ptr %5, align 4, !tbaa !9
  %22 = load float, ptr %5, align 4, !tbaa !9
  %23 = load float, ptr %5, align 4, !tbaa !9
  %24 = fneg float %22
  %25 = call float @llvm.fmuladd.f32(float %24, float %23, float 1.000000e+00)
  %26 = call float @sqrtf(float noundef %25) #4
  store float %26, ptr %6, align 4, !tbaa !9
  %27 = call i64 @random() #4
  %28 = srem i64 %27, 32768
  %29 = sitofp i64 %28 to float
  %30 = fdiv float %29, 3.276800e+04
  store float %30, ptr %2, align 4, !tbaa !9
  %31 = load float, ptr %2, align 4, !tbaa !9
  %32 = fmul float 0x401921FB60000000, %31
  store float %32, ptr %7, align 4, !tbaa !9
  %33 = load float, ptr %6, align 4, !tbaa !9
  %34 = load float, ptr %2, align 4, !tbaa !9
  %35 = call float @cosf(float noundef %34) #4
  %36 = fmul float %33, %35
  store float %36, ptr %3, align 4, !tbaa !9
  %37 = load float, ptr %6, align 4, !tbaa !9
  %38 = load float, ptr %2, align 4, !tbaa !9
  %39 = call float @sinf(float noundef %38) #4
  %40 = fmul float %37, %39
  store float %40, ptr %4, align 4, !tbaa !9
  %41 = load float, ptr %3, align 4, !tbaa !9
  %42 = load i32, ptr %1, align 4, !tbaa !5
  %43 = mul nsw i32 %42, 3
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [768 x float], ptr %8, i64 0, i64 %44
  store float %41, ptr %45, align 4, !tbaa !9
  %46 = load float, ptr %4, align 4, !tbaa !9
  %47 = load i32, ptr %1, align 4, !tbaa !5
  %48 = mul nsw i32 %47, 3
  %49 = add nsw i32 %48, 1
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [768 x float], ptr %8, i64 0, i64 %50
  store float %46, ptr %51, align 4, !tbaa !9
  %52 = load float, ptr %5, align 4, !tbaa !9
  %53 = load i32, ptr %1, align 4, !tbaa !5
  %54 = mul nsw i32 %53, 3
  %55 = add nsw i32 %54, 2
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [768 x float], ptr %8, i64 0, i64 %56
  store float %52, ptr %57, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %15
  %59 = load i32, ptr %1, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %1, align 4, !tbaa !5
  br label %12, !llvm.loop !11

61:                                               ; preds = %12
  store ptr @gradientTable, ptr %9, align 8, !tbaa !13
  %62 = getelementptr inbounds [768 x float], ptr %8, i64 0, i64 0
  store ptr %62, ptr %10, align 8, !tbaa !13
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %63

63:                                               ; preds = %109, %61
  %64 = load i32, ptr %1, align 4, !tbaa !5
  %65 = icmp slt i32 %64, 256
  br i1 %65, label %66, label %112

66:                                               ; preds = %63
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %67 = load i32, ptr %1, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [256 x i8], ptr @permTable, i64 0, i64 %68
  %70 = load i8, ptr %69, align 1, !tbaa !15
  %71 = zext i8 %70 to i32
  store i32 %71, ptr %11, align 4, !tbaa !5
  %72 = load ptr, ptr %10, align 8, !tbaa !13
  %73 = load i32, ptr %11, align 4, !tbaa !5
  %74 = mul nsw i32 %73, 3
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds i32, ptr %72, i64 %75
  %77 = load i32, ptr %76, align 4, !tbaa !5
  %78 = load ptr, ptr %9, align 8, !tbaa !13
  %79 = load i32, ptr %1, align 4, !tbaa !5
  %80 = mul nsw i32 %79, 3
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds i32, ptr %78, i64 %81
  store i32 %77, ptr %82, align 4, !tbaa !5
  %83 = load ptr, ptr %10, align 8, !tbaa !13
  %84 = load i32, ptr %11, align 4, !tbaa !5
  %85 = mul nsw i32 %84, 3
  %86 = add nsw i32 %85, 1
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds i32, ptr %83, i64 %87
  %89 = load i32, ptr %88, align 4, !tbaa !5
  %90 = load ptr, ptr %9, align 8, !tbaa !13
  %91 = load i32, ptr %1, align 4, !tbaa !5
  %92 = mul nsw i32 %91, 3
  %93 = add nsw i32 %92, 1
  %94 = sext i32 %93 to i64
  %95 = getelementptr inbounds i32, ptr %90, i64 %94
  store i32 %89, ptr %95, align 4, !tbaa !5
  %96 = load ptr, ptr %10, align 8, !tbaa !13
  %97 = load i32, ptr %11, align 4, !tbaa !5
  %98 = mul nsw i32 %97, 3
  %99 = add nsw i32 %98, 2
  %100 = sext i32 %99 to i64
  %101 = getelementptr inbounds i32, ptr %96, i64 %100
  %102 = load i32, ptr %101, align 4, !tbaa !5
  %103 = load ptr, ptr %9, align 8, !tbaa !13
  %104 = load i32, ptr %1, align 4, !tbaa !5
  %105 = mul nsw i32 %104, 3
  %106 = add nsw i32 %105, 2
  %107 = sext i32 %106 to i64
  %108 = getelementptr inbounds i32, ptr %103, i64 %107
  store i32 %102, ptr %108, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %109

109:                                              ; preds = %66
  %110 = load i32, ptr %1, align 4, !tbaa !5
  %111 = add nsw i32 %110, 1
  store i32 %111, ptr %1, align 4, !tbaa !5
  br label %63, !llvm.loop !16

112:                                              ; preds = %63
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 3072, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  ret void
}

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

; Function Attrs: nounwind
declare void @srandom(i32 noundef) #2

; Function Attrs: nounwind
declare i64 @random() #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fmuladd.f32(float, float, float) #3

; Function Attrs: nounwind
declare float @sqrtf(float noundef) #2

; Function Attrs: nounwind
declare float @cosf(float noundef) #2

; Function Attrs: nounwind
declare float @sinf(float noundef) #2

; 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 = { nounwind "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 nosync nounwind speculatable willreturn memory(none) }
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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !7, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !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"

@gradientTable = external hidden local_unnamed_addr global [768 x float], align 16
@permTable = external hidden local_unnamed_addr global [256 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @initNoiseTable() local_unnamed_addr #0 {
  %1 = alloca [768 x float], align 16
  call void @llvm.lifetime.start.p0(i64 3072, ptr nonnull %1) #5
  tail call void @srandom(i32 noundef 0) #5
  br label %2

2:                                                ; preds = %0, %2
  %indvars.iv = phi i64 [ 0, %0 ], [ %indvars.iv.next, %2 ]
  %3 = tail call i64 @random() #5
  %4 = srem i64 %3, 32768
  %5 = sitofp i64 %4 to float
  %6 = fmul float %5, 0x3F00000000000000
  %7 = tail call float @llvm.fmuladd.f32(float %6, float -2.000000e+00, float 1.000000e+00)
  %8 = fneg float %7
  %9 = tail call float @llvm.fmuladd.f32(float %8, float %7, float 1.000000e+00)
  %10 = tail call float @sqrtf(float noundef %9) #5
  %11 = tail call i64 @random() #5
  %12 = srem i64 %11, 32768
  %13 = sitofp i64 %12 to float
  %14 = fmul float %13, 0x3F00000000000000
  %15 = tail call float @cosf(float noundef %14) #5
  %16 = tail call float @sinf(float noundef %14) #5
  %17 = mul nuw nsw i64 %indvars.iv, 3
  %18 = getelementptr inbounds [768 x float], ptr %1, i64 0, i64 %17
  %19 = insertelement <2 x float> poison, float %10, i64 0
  %20 = shufflevector <2 x float> %19, <2 x float> poison, <2 x i32> zeroinitializer
  %21 = insertelement <2 x float> poison, float %15, i64 0
  %22 = insertelement <2 x float> %21, float %16, i64 1
  %23 = fmul <2 x float> %20, %22
  store <2 x float> %23, ptr %18, align 4, !tbaa !5
  %24 = add nuw nsw i64 %17, 2
  %25 = getelementptr inbounds [768 x float], ptr %1, i64 0, i64 %24
  store float %7, ptr %25, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 256
  br i1 %exitcond.not, label %.preheader, label %2, !llvm.loop !9

.preheader:                                       ; preds = %2, %.preheader
  %indvars.iv33 = phi i64 [ %indvars.iv.next34.1, %.preheader ], [ 0, %2 ]
  %26 = getelementptr inbounds [256 x i8], ptr @permTable, i64 0, i64 %indvars.iv33
  %27 = load i8, ptr %26, align 2, !tbaa !11
  %28 = zext i8 %27 to i64
  %.idx = mul nuw nsw i64 %28, 12
  %29 = getelementptr inbounds i8, ptr %1, i64 %.idx
  %.idx37 = mul nuw nsw i64 %indvars.iv33, 12
  %30 = getelementptr inbounds i8, ptr @gradientTable, i64 %.idx37
  %31 = load <2 x i32>, ptr %29, align 4, !tbaa !12
  store <2 x i32> %31, ptr %30, align 8, !tbaa !12
  %32 = getelementptr inbounds i8, ptr %29, i64 8
  %33 = load i32, ptr %32, align 4, !tbaa !12
  %34 = getelementptr inbounds i8, ptr %30, i64 8
  store i32 %33, ptr %34, align 8, !tbaa !12
  %indvars.iv.next34 = or disjoint i64 %indvars.iv33, 1
  %35 = getelementptr inbounds [256 x i8], ptr @permTable, i64 0, i64 %indvars.iv.next34
  %36 = load i8, ptr %35, align 1, !tbaa !11
  %37 = zext i8 %36 to i64
  %.idx.1 = mul nuw nsw i64 %37, 12
  %38 = getelementptr inbounds i8, ptr %1, i64 %.idx.1
  %.idx37.1 = mul nuw nsw i64 %indvars.iv.next34, 12
  %39 = getelementptr inbounds i8, ptr @gradientTable, i64 %.idx37.1
  %40 = load <2 x i32>, ptr %38, align 4, !tbaa !12
  store <2 x i32> %40, ptr %39, align 4, !tbaa !12
  %41 = getelementptr inbounds i8, ptr %38, i64 8
  %42 = load i32, ptr %41, align 4, !tbaa !12
  %43 = getelementptr inbounds i8, ptr %39, i64 8
  store i32 %42, ptr %43, align 4, !tbaa !12
  %indvars.iv.next34.1 = add nuw nsw i64 %indvars.iv33, 2
  %exitcond36.not.1 = icmp eq i64 %indvars.iv.next34.1, 256
  br i1 %exitcond36.not.1, label %44, label %.preheader, !llvm.loop !14

44:                                               ; preds = %.preheader
  call void @llvm.lifetime.end.p0(i64 3072, ptr nonnull %1) #5
  ret void
}

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

; Function Attrs: nounwind
declare void @srandom(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i64 @random() local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fmuladd.f32(float, float, float) #3

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare float @sqrtf(float noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare float @cosf(float noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare float @sinf(float noundef) local_unnamed_addr #4

; Function Attrs: mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind "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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { mustprogress nofree nounwind willreturn memory(write) "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 }

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass