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

; 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, [2 x i32], 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

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

declare void @glEnableVertexAttribArray(i32 noundef) #1

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

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

declare void @glDisableVertexAttribArray(i32 noundef) #1

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

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

29:                                               ; preds = %23
  %30 = load ptr, ptr %13, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.UserData, ptr %30, i32 0, i32 1
  %32 = getelementptr inbounds [2 x i32], ptr %31, i64 0, i64 0
  call void @glGenBuffers(i32 noundef 2, ptr noundef %32)
  %33 = load ptr, ptr %13, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.UserData, ptr %33, i32 0, i32 1
  %35 = getelementptr inbounds [2 x i32], ptr %34, i64 0, i64 0
  %36 = load i32, ptr %35, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34962, i32 noundef %36)
  %37 = load i32, ptr %10, align 4, !tbaa !9
  %38 = load i32, ptr %8, align 4, !tbaa !9
  %39 = mul nsw i32 %37, %38
  %40 = sext i32 %39 to i64
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  call void @glBufferData(i32 noundef 34962, i64 noundef %40, ptr noundef %41, i32 noundef 35044)
  %42 = load ptr, ptr %13, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.UserData, ptr %42, i32 0, i32 1
  %44 = getelementptr inbounds [2 x i32], ptr %43, i64 0, i64 1
  %45 = load i32, ptr %44, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34963, i32 noundef %45)
  %46 = load i32, ptr %11, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = mul i64 2, %47
  %49 = load ptr, ptr %12, align 8, !tbaa !5
  call void @glBufferData(i32 noundef 34963, i64 noundef %48, ptr noundef %49, i32 noundef 35044)
  br label %50

50:                                               ; preds = %29, %23, %6
  %51 = load ptr, ptr %13, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.UserData, ptr %51, i32 0, i32 1
  %53 = getelementptr inbounds [2 x i32], ptr %52, i64 0, i64 0
  %54 = load i32, ptr %53, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34962, i32 noundef %54)
  %55 = load ptr, ptr %13, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.UserData, ptr %55, i32 0, i32 1
  %57 = getelementptr inbounds [2 x i32], ptr %56, i64 0, i64 1
  %58 = load i32, ptr %57, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34963, i32 noundef %58)
  call void @glEnableVertexAttribArray(i32 noundef 0)
  call void @glEnableVertexAttribArray(i32 noundef 1)
  %59 = load i32, ptr %10, align 4, !tbaa !9
  %60 = load i32, ptr %14, align 4, !tbaa !9
  %61 = zext i32 %60 to i64
  %62 = inttoptr i64 %61 to ptr
  call void @glVertexAttribPointer(i32 noundef 0, i32 noundef 3, i32 noundef 5126, i8 noundef zeroext 0, i32 noundef %59, ptr noundef %62)
  %63 = load i32, ptr %14, align 4, !tbaa !9
  %64 = zext i32 %63 to i64
  %65 = add i64 %64, 12
  %66 = trunc i64 %65 to i32
  store i32 %66, ptr %14, align 4, !tbaa !9
  %67 = load i32, ptr %10, align 4, !tbaa !9
  %68 = load i32, ptr %14, align 4, !tbaa !9
  %69 = zext i32 %68 to i64
  %70 = inttoptr i64 %69 to ptr
  call void @glVertexAttribPointer(i32 noundef 1, i32 noundef 4, i32 noundef 5126, i8 noundef zeroext 0, i32 noundef %67, ptr noundef %70)
  %71 = load i32, ptr %11, align 4, !tbaa !9
  call void @glDrawElements(i32 noundef 4, i32 noundef %71, 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 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  ret void
}

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

declare void @glBufferData(i32 noundef, i64 noundef, ptr noundef, 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 }

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

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

declare void @glEnableVertexAttribArray(i32 noundef) local_unnamed_addr #0

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

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

declare void @glDisableVertexAttribArray(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @DrawPrimitiveWithVBOs(ptr nocapture noundef readonly %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5) local_unnamed_addr #1 {
  %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 %23

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 %23

16:                                               ; preds = %12
  tail call void @glGenBuffers(i32 noundef 2, ptr noundef nonnull %9) #2
  %17 = load i32, ptr %9, align 4, !tbaa !12
  tail call void @glBindBuffer(i32 noundef 34962, i32 noundef %17) #2
  %18 = mul nsw i32 %3, %1
  %19 = sext i32 %18 to i64
  tail call void @glBufferData(i32 noundef 34962, i64 noundef %19, ptr noundef %2, i32 noundef 35044) #2
  %20 = load i32, ptr %13, align 4, !tbaa !12
  tail call void @glBindBuffer(i32 noundef 34963, i32 noundef %20) #2
  %21 = sext i32 %4 to i64
  %22 = shl nsw i64 %21, 1
  tail call void @glBufferData(i32 noundef 34963, i64 noundef %22, ptr noundef %5, i32 noundef 35044) #2
  %.pre = load i32, ptr %9, align 4, !tbaa !12
  br label %23

23:                                               ; preds = %16, %12, %6
  %24 = phi i32 [ %.pre, %16 ], [ 0, %12 ], [ %10, %6 ]
  tail call void @glBindBuffer(i32 noundef 34962, i32 noundef %24) #2
  %25 = getelementptr inbounds i8, ptr %8, i64 8
  %26 = load i32, ptr %25, align 4, !tbaa !12
  tail call void @glBindBuffer(i32 noundef 34963, i32 noundef %26) #2
  tail call void @glEnableVertexAttribArray(i32 noundef 0) #2
  tail call void @glEnableVertexAttribArray(i32 noundef 1) #2
  tail call void @glVertexAttribPointer(i32 noundef 0, i32 noundef 3, i32 noundef 5126, i8 noundef zeroext 0, i32 noundef %3, ptr noundef null) #2
  tail call void @glVertexAttribPointer(i32 noundef 1, i32 noundef 4, i32 noundef 5126, i8 noundef zeroext 0, i32 noundef %3, ptr noundef nonnull inttoptr (i64 12 to ptr)) #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 #0

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

attributes #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 = { 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, !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}


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