; LLVM IR from danginsburg/opengles3-book :: Chapter_6/Example_6_6 (Example_6_6)

; 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.ESContext = type { ptr, ptr, i32, i32, ptr, i64, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.UserData = type { i32, [3 x 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: nounwind uwtable
define dso_local void @DrawPrimitiveWithVBOs(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, i32 noundef %4, ptr noundef %5) #1 {
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !9
  store ptr %5, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.ESContext, ptr %14, i32 0, i32 1
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  store ptr %16, ptr %13, align 8, !tbaa !5
  %17 = load ptr, ptr %13, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.UserData, ptr %17, i32 0, i32 1
  %19 = getelementptr inbounds [3 x i32], ptr %18, i64 0, i64 0
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %72

22:                                               ; preds = %6
  %23 = load ptr, ptr %13, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.UserData, ptr %23, i32 0, i32 1
  %25 = getelementptr inbounds [3 x i32], ptr %24, i64 0, i64 1
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %72

28:                                               ; preds = %22
  %29 = load ptr, ptr %13, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.UserData, ptr %29, i32 0, i32 1
  %31 = getelementptr inbounds [3 x i32], ptr %30, i64 0, i64 2
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %34, label %72

34:                                               ; preds = %28
  %35 = load ptr, ptr %13, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.UserData, ptr %35, i32 0, i32 1
  %37 = getelementptr inbounds [3 x i32], ptr %36, i64 0, i64 0
  call void @glGenBuffers(i32 noundef 3, ptr noundef %37)
  %38 = load ptr, ptr %13, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.UserData, ptr %38, i32 0, i32 1
  %40 = getelementptr inbounds [3 x i32], ptr %39, i64 0, i64 0
  %41 = load i32, ptr %40, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34962, i32 noundef %41)
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = getelementptr inbounds i32, ptr %42, i64 0
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = load i32, ptr %8, align 4, !tbaa !9
  %46 = mul nsw i32 %44, %45
  %47 = sext i32 %46 to i64
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  %49 = getelementptr inbounds ptr, ptr %48, i64 0
  %50 = load ptr, ptr %49, align 8, !tbaa !5
  call void @glBufferData(i32 noundef 34962, i64 noundef %47, ptr noundef %50, i32 noundef 35044)
  %51 = load ptr, ptr %13, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.UserData, ptr %51, i32 0, i32 1
  %53 = getelementptr inbounds [3 x i32], ptr %52, i64 0, i64 1
  %54 = load i32, ptr %53, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34962, i32 noundef %54)
  %55 = load ptr, ptr %10, align 8, !tbaa !5
  %56 = getelementptr inbounds i32, ptr %55, i64 1
  %57 = load i32, ptr %56, align 4, !tbaa !9
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = mul nsw i32 %57, %58
  %60 = sext i32 %59 to i64
  %61 = load ptr, ptr %9, align 8, !tbaa !5
  %62 = getelementptr inbounds ptr, ptr %61, i64 1
  %63 = load ptr, ptr %62, align 8, !tbaa !5
  call void @glBufferData(i32 noundef 34962, i64 noundef %60, ptr noundef %63, i32 noundef 35044)
  %64 = load ptr, ptr %13, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.UserData, ptr %64, i32 0, i32 1
  %66 = getelementptr inbounds [3 x i32], ptr %65, i64 0, i64 2
  %67 = load i32, ptr %66, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34963, i32 noundef %67)
  %68 = load i32, ptr %11, align 4, !tbaa !9
  %69 = sext i32 %68 to i64
  %70 = mul i64 2, %69
  %71 = load ptr, ptr %12, align 8, !tbaa !5
  call void @glBufferData(i32 noundef 34963, i64 noundef %70, ptr noundef %71, i32 noundef 35044)
  br label %72

72:                                               ; preds = %34, %28, %22, %6
  %73 = load ptr, ptr %13, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.UserData, ptr %73, i32 0, i32 1
  %75 = getelementptr inbounds [3 x i32], ptr %74, i64 0, i64 0
  %76 = load i32, ptr %75, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34962, i32 noundef %76)
  call void @glEnableVertexAttribArray(i32 noundef 0)
  %77 = load ptr, ptr %10, align 8, !tbaa !5
  %78 = getelementptr inbounds i32, ptr %77, i64 0
  %79 = load i32, ptr %78, align 4, !tbaa !9
  call void @glVertexAttribPointer(i32 noundef 0, i32 noundef 3, i32 noundef 5126, i8 noundef zeroext 0, i32 noundef %79, ptr noundef null)
  %80 = load ptr, ptr %13, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.UserData, ptr %80, i32 0, i32 1
  %82 = getelementptr inbounds [3 x i32], ptr %81, i64 0, i64 1
  %83 = load i32, ptr %82, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34962, i32 noundef %83)
  call void @glEnableVertexAttribArray(i32 noundef 1)
  %84 = load ptr, ptr %10, align 8, !tbaa !5
  %85 = getelementptr inbounds i32, ptr %84, i64 1
  %86 = load i32, ptr %85, align 4, !tbaa !9
  call void @glVertexAttribPointer(i32 noundef 1, i32 noundef 4, i32 noundef 5126, i8 noundef zeroext 0, i32 noundef %86, ptr noundef null)
  %87 = load ptr, ptr %13, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.UserData, ptr %87, i32 0, i32 1
  %89 = getelementptr inbounds [3 x i32], ptr %88, i64 0, i64 2
  %90 = load i32, ptr %89, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34963, i32 noundef %90)
  %91 = load i32, ptr %11, align 4, !tbaa !9
  call void @glDrawElements(i32 noundef 4, i32 noundef %91, i32 noundef 5123, ptr noundef null)
  call void @glDisableVertexAttribArray(i32 noundef 0)
  call void @glDisableVertexAttribArray(i32 noundef 1)
  call void @glBindBuffer(i32 noundef 34962, i32 noundef 0)
  call void @glBindBuffer(i32 noundef 34963, i32 noundef 0)
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  ret void
}

declare void @glGenBuffers(i32 noundef, ptr noundef) #2

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

declare void @glBufferData(i32 noundef, i64 noundef, ptr noundef, i32 noundef) #2

declare void @glEnableVertexAttribArray(i32 noundef) #2

declare void @glVertexAttribPointer(i32 noundef, i32 noundef, i32 noundef, i8 noundef zeroext, i32 noundef, ptr noundef) #2

declare void @glDrawElements(i32 noundef, i32 noundef, i32 noundef, ptr noundef) #2

declare void @glDisableVertexAttribArray(i32 noundef) #2

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 = { "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, !6, i64 8}
!12 = !{!"ESContext", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !6, i64 24, !13, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88}
!13 = !{!"long", !7, i64 0}


; 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 dso_local void @DrawPrimitiveWithVBOs(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef readonly %2, ptr nocapture noundef readonly %3, i32 noundef %4, ptr noundef %5) local_unnamed_addr #0 {
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %8, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !12
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %36

12:                                               ; preds = %6
  %13 = getelementptr inbounds i8, ptr %8, i64 8
  %14 = load i32, ptr %13, align 4, !tbaa !12
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %36

16:                                               ; preds = %12
  %17 = getelementptr inbounds i8, ptr %8, i64 12
  %18 = load i32, ptr %17, align 4, !tbaa !12
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %36

20:                                               ; preds = %16
  tail call void @glGenBuffers(i32 noundef 3, ptr noundef nonnull %9) #2
  %21 = load i32, ptr %9, align 4, !tbaa !12
  tail call void @glBindBuffer(i32 noundef 34962, i32 noundef %21) #2
  %22 = load i32, ptr %3, align 4, !tbaa !12
  %23 = mul nsw i32 %22, %1
  %24 = sext i32 %23 to i64
  %25 = load ptr, ptr %2, align 8, !tbaa !13
  tail call void @glBufferData(i32 noundef 34962, i64 noundef %24, ptr noundef %25, i32 noundef 35044) #2
  %26 = load i32, ptr %13, align 4, !tbaa !12
  tail call void @glBindBuffer(i32 noundef 34962, i32 noundef %26) #2
  %27 = getelementptr inbounds i8, ptr %3, i64 4
  %28 = load i32, ptr %27, align 4, !tbaa !12
  %29 = mul nsw i32 %28, %1
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i8, ptr %2, i64 8
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  tail call void @glBufferData(i32 noundef 34962, i64 noundef %30, ptr noundef %32, i32 noundef 35044) #2
  %33 = load i32, ptr %17, align 4, !tbaa !12
  tail call void @glBindBuffer(i32 noundef 34963, i32 noundef %33) #2
  %34 = sext i32 %4 to i64
  %35 = shl nsw i64 %34, 1
  tail call void @glBufferData(i32 noundef 34963, i64 noundef %35, ptr noundef %5, i32 noundef 35044) #2
  %.pre = load i32, ptr %9, align 4, !tbaa !12
  br label %36

36:                                               ; preds = %20, %16, %12, %6
  %37 = phi i32 [ %.pre, %20 ], [ 0, %16 ], [ 0, %12 ], [ %10, %6 ]
  tail call void @glBindBuffer(i32 noundef 34962, i32 noundef %37) #2
  tail call void @glEnableVertexAttribArray(i32 noundef 0) #2
  %38 = load i32, ptr %3, align 4, !tbaa !12
  tail call void @glVertexAttribPointer(i32 noundef 0, i32 noundef 3, i32 noundef 5126, i8 noundef zeroext 0, i32 noundef %38, ptr noundef null) #2
  %39 = getelementptr inbounds i8, ptr %8, i64 8
  %40 = load i32, ptr %39, align 4, !tbaa !12
  tail call void @glBindBuffer(i32 noundef 34962, i32 noundef %40) #2
  tail call void @glEnableVertexAttribArray(i32 noundef 1) #2
  %41 = getelementptr inbounds i8, ptr %3, i64 4
  %42 = load i32, ptr %41, align 4, !tbaa !12
  tail call void @glVertexAttribPointer(i32 noundef 1, i32 noundef 4, i32 noundef 5126, i8 noundef zeroext 0, i32 noundef %42, ptr noundef null) #2
  %43 = getelementptr inbounds i8, ptr %8, i64 12
  %44 = load i32, ptr %43, align 4, !tbaa !12
  tail call void @glBindBuffer(i32 noundef 34963, i32 noundef %44) #2
  tail call void @glDrawElements(i32 noundef 4, i32 noundef %4, i32 noundef 5123, ptr noundef null) #2
  tail call void @glDisableVertexAttribArray(i32 noundef 0) #2
  tail call void @glDisableVertexAttribArray(i32 noundef 1) #2
  tail call void @glBindBuffer(i32 noundef 34962, i32 noundef 0) #2
  tail call void @glBindBuffer(i32 noundef 34963, i32 noundef 0) #2
  ret void
}

declare void @glGenBuffers(i32 noundef, ptr noundef) local_unnamed_addr #1

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

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

declare void @glEnableVertexAttribArray(i32 noundef) local_unnamed_addr #1

declare void @glVertexAttribPointer(i32 noundef, i32 noundef, i32 noundef, i8 noundef zeroext, i32 noundef, ptr noundef) local_unnamed_addr #1

declare void @glDrawElements(i32 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare void @glDisableVertexAttribArray(i32 noundef) local_unnamed_addr #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 = { "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, !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 = !{!7, !7, i64 0}


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