; LLVM IR from danginsburg/opengles3-book :: Chapter_7/Instancing (Instancing)

; 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.ESMatrix = type { [4 x [4 x float]] }
%struct.ESContext = type { ptr, ptr, i32, i32, ptr, i64, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.UserData = type { i32, i32, i32, i32, i32, i32, [100 x float] }

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

declare void @glBindBuffer(i32 noundef, i32 noundef) #1

; 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 void @Update(ptr noundef %0, float noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca float, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca %struct.ESMatrix, align 4
  %8 = alloca float, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca %struct.ESMatrix, align 4
  %13 = alloca float, align 4
  %14 = alloca float, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store float %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.ESContext, ptr %15, i32 0, i32 1
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  store ptr %17, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 64, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.ESContext, ptr %18, i32 0, i32 2
  %20 = load i32, ptr %19, align 8, !tbaa !16
  %21 = sitofp i32 %20 to float
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.ESContext, ptr %22, i32 0, i32 3
  %24 = load i32, ptr %23, align 4, !tbaa !17
  %25 = sitofp i32 %24 to float
  %26 = fdiv float %21, %25
  store float %26, ptr %8, align 4, !tbaa !9
  call void @esMatrixLoadIdentity(ptr noundef %7)
  %27 = load float, ptr %8, align 4, !tbaa !9
  call void @esPerspective(ptr noundef %7, float noundef 6.000000e+01, float noundef %27, float noundef 1.000000e+00, float noundef 2.000000e+01)
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.UserData, ptr %28, i32 0, i32 3
  %30 = load i32, ptr %29, align 4, !tbaa !18
  call void @glBindBuffer(i32 noundef 34962, i32 noundef %30)
  %31 = call ptr @glMapBufferRange(i32 noundef 34962, i64 noundef 0, i64 noundef 6400, i32 noundef 2)
  store ptr %31, ptr %6, align 8, !tbaa !5
  %32 = call float @sqrtf(float noundef 1.000000e+02) #5
  %33 = fptosi float %32 to i32
  store i32 %33, ptr %10, align 4, !tbaa !15
  %34 = load i32, ptr %10, align 4, !tbaa !15
  store i32 %34, ptr %11, align 4, !tbaa !15
  store i32 0, ptr %9, align 4, !tbaa !15
  br label %35

35:                                               ; preds = %91, %2
  %36 = load i32, ptr %9, align 4, !tbaa !15
  %37 = icmp slt i32 %36, 100
  br i1 %37, label %38, label %94

38:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 64, ptr %12) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %39 = load i32, ptr %9, align 4, !tbaa !15
  %40 = load i32, ptr %10, align 4, !tbaa !15
  %41 = srem i32 %39, %40
  %42 = sitofp i32 %41 to float
  %43 = load i32, ptr %10, align 4, !tbaa !15
  %44 = sitofp i32 %43 to float
  %45 = fdiv float %42, %44
  %46 = call float @llvm.fmuladd.f32(float %45, float 2.000000e+00, float -1.000000e+00)
  store float %46, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  %47 = load i32, ptr %9, align 4, !tbaa !15
  %48 = load i32, ptr %11, align 4, !tbaa !15
  %49 = sdiv i32 %47, %48
  %50 = sitofp i32 %49 to float
  %51 = load i32, ptr %11, align 4, !tbaa !15
  %52 = sitofp i32 %51 to float
  %53 = fdiv float %50, %52
  %54 = call float @llvm.fmuladd.f32(float %53, float 2.000000e+00, float -1.000000e+00)
  store float %54, ptr %14, align 4, !tbaa !9
  call void @esMatrixLoadIdentity(ptr noundef %12)
  %55 = load float, ptr %13, align 4, !tbaa !9
  %56 = load float, ptr %14, align 4, !tbaa !9
  call void @esTranslate(ptr noundef %12, float noundef %55, float noundef %56, float noundef -2.000000e+00)
  %57 = load float, ptr %4, align 4, !tbaa !9
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.UserData, ptr %58, i32 0, i32 6
  %60 = load i32, ptr %9, align 4, !tbaa !15
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [100 x float], ptr %59, i64 0, i64 %61
  %63 = load float, ptr %62, align 4, !tbaa !9
  %64 = call float @llvm.fmuladd.f32(float %57, float 4.000000e+01, float %63)
  store float %64, ptr %62, align 4, !tbaa !9
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.UserData, ptr %65, i32 0, i32 6
  %67 = load i32, ptr %9, align 4, !tbaa !15
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [100 x float], ptr %66, i64 0, i64 %68
  %70 = load float, ptr %69, align 4, !tbaa !9
  %71 = fcmp oge float %70, 3.600000e+02
  br i1 %71, label %72, label %80

72:                                               ; preds = %38
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.UserData, ptr %73, i32 0, i32 6
  %75 = load i32, ptr %9, align 4, !tbaa !15
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [100 x float], ptr %74, i64 0, i64 %76
  %78 = load float, ptr %77, align 4, !tbaa !9
  %79 = fsub float %78, 3.600000e+02
  store float %79, ptr %77, align 4, !tbaa !9
  br label %80

80:                                               ; preds = %72, %38
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.UserData, ptr %81, i32 0, i32 6
  %83 = load i32, ptr %9, align 4, !tbaa !15
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [100 x float], ptr %82, i64 0, i64 %84
  %86 = load float, ptr %85, align 4, !tbaa !9
  call void @esRotate(ptr noundef %12, float noundef %86, float noundef 1.000000e+00, float noundef 0.000000e+00, float noundef 1.000000e+00)
  %87 = load ptr, ptr %6, align 8, !tbaa !5
  %88 = load i32, ptr %9, align 4, !tbaa !15
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds %struct.ESMatrix, ptr %87, i64 %89
  call void @esMatrixMultiply(ptr noundef %90, ptr noundef %12, ptr noundef %7)
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr %12) #5
  br label %91

91:                                               ; preds = %80
  %92 = load i32, ptr %9, align 4, !tbaa !15
  %93 = add nsw i32 %92, 1
  store i32 %93, ptr %9, align 4, !tbaa !15
  br label %35, !llvm.loop !20

94:                                               ; preds = %35
  %95 = call zeroext i8 @glUnmapBuffer(i32 noundef 34962)
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret void
}

declare void @esMatrixLoadIdentity(ptr noundef) #1

declare void @esPerspective(ptr noundef, float noundef, float noundef, float noundef, float noundef) #1

declare ptr @glMapBufferRange(i32 noundef, i64 noundef, i64 noundef, i32 noundef) #1

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

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

declare void @esTranslate(ptr noundef, float noundef, float noundef, float noundef) #1

declare void @esRotate(ptr noundef, float noundef, float noundef, float noundef, float noundef) #1

declare void @esMatrixMultiply(ptr noundef, ptr noundef, ptr noundef) #1

declare zeroext i8 @glUnmapBuffer(i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"any pointer", !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, !6, i64 8}
!12 = !{!"ESContext", !6, i64 0, !6, i64 8, !13, i64 16, !13, i64 20, !6, i64 24, !14, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88}
!13 = !{!"int", !7, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !13, i64 16}
!17 = !{!12, !13, i64 20}
!18 = !{!19, !13, i64 12}
!19 = !{!"", !13, i64 0, !13, i64 4, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20, !7, i64 24}
!20 = distinct !{!20, !21}
!21 = !{!"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"

%struct.ESMatrix = type { [4 x [4 x float]] }

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

declare void @glBindBuffer(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress 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 void @Update(ptr nocapture noundef readonly %0, float noundef %1) local_unnamed_addr #2 {
  %3 = alloca %struct.ESMatrix, align 4
  %4 = alloca %struct.ESMatrix, align 4
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %3) #4
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = load <2 x i32>, ptr %7, align 8, !tbaa !12
  %9 = sitofp <2 x i32> %8 to <2 x float>
  %10 = extractelement <2 x float> %9, i64 0
  %11 = extractelement <2 x float> %9, i64 1
  %12 = fdiv float %10, %11
  call void @esMatrixLoadIdentity(ptr noundef nonnull %3) #4
  call void @esPerspective(ptr noundef nonnull %3, float noundef 6.000000e+01, float noundef %12, float noundef 1.000000e+00, float noundef 2.000000e+01) #4
  %13 = getelementptr inbounds i8, ptr %6, i64 12
  %14 = load i32, ptr %13, align 4, !tbaa !13
  call void @glBindBuffer(i32 noundef 34962, i32 noundef %14) #4
  %15 = call ptr @glMapBufferRange(i32 noundef 34962, i64 noundef 0, i64 noundef 6400, i32 noundef 2) #4
  %16 = getelementptr inbounds i8, ptr %6, i64 24
  br label %17

17:                                               ; preds = %2, %17
  %indvars.iv = phi i64 [ 0, %2 ], [ %indvars.iv.next, %17 ]
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %4) #4
  %.lhs.trunc = trunc i64 %indvars.iv to i8
  %18 = urem i8 %.lhs.trunc, 10
  %19 = uitofp nneg i8 %18 to float
  %20 = fdiv float %19, 1.000000e+01
  %21 = call float @llvm.fmuladd.f32(float %20, float 2.000000e+00, float -1.000000e+00)
  %22 = udiv i8 %.lhs.trunc, 10
  %23 = uitofp nneg i8 %22 to float
  %24 = fdiv float %23, 1.000000e+01
  %25 = call float @llvm.fmuladd.f32(float %24, float 2.000000e+00, float -1.000000e+00)
  call void @esMatrixLoadIdentity(ptr noundef nonnull %4) #4
  call void @esTranslate(ptr noundef nonnull %4, float noundef %21, float noundef %25, float noundef -2.000000e+00) #4
  %26 = getelementptr inbounds [100 x float], ptr %16, i64 0, i64 %indvars.iv
  %27 = load float, ptr %26, align 4, !tbaa !15
  %28 = call float @llvm.fmuladd.f32(float %1, float 4.000000e+01, float %27)
  %29 = fcmp ult float %28, 3.600000e+02
  %30 = fadd float %28, -3.600000e+02
  %storemerge = select i1 %29, float %28, float %30
  store float %storemerge, ptr %26, align 4, !tbaa !15
  call void @esRotate(ptr noundef nonnull %4, float noundef %storemerge, float noundef 1.000000e+00, float noundef 0.000000e+00, float noundef 1.000000e+00) #4
  %31 = getelementptr inbounds %struct.ESMatrix, ptr %15, i64 %indvars.iv
  call void @esMatrixMultiply(ptr noundef %31, ptr noundef nonnull %4, ptr noundef nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %4) #4
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 100
  br i1 %exitcond.not, label %32, label %17, !llvm.loop !17

32:                                               ; preds = %17
  %33 = call zeroext i8 @glUnmapBuffer(i32 noundef 34962) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %3) #4
  ret void
}

declare void @esMatrixLoadIdentity(ptr noundef) local_unnamed_addr #1

declare void @esPerspective(ptr noundef, float noundef, float noundef, float noundef, float noundef) local_unnamed_addr #1

declare ptr @glMapBufferRange(i32 noundef, i64 noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

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

declare void @esTranslate(ptr noundef, float noundef, float noundef, float noundef) local_unnamed_addr #1

declare void @esRotate(ptr noundef, float noundef, float noundef, float noundef, float noundef) local_unnamed_addr #1

declare void @esMatrixMultiply(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare zeroext i8 @glUnmapBuffer(i32 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { mustprogress 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, !7, i64 8}
!6 = !{!"ESContext", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !7, i64 24, !11, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!10, !10, i64 0}
!13 = !{!14, !10, i64 12}
!14 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !8, i64 24}
!15 = !{!16, !16, i64 0}
!16 = !{!"float", !8, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass