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

; 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] }

@.str = external hidden unnamed_addr constant [36 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [45 x i8], align 1

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 @DrawPrimitiveWithVBOsMapBuffers(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
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  %17 = 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) #4
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.ESContext, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  store ptr %20, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  store i32 0, ptr %14, align 4, !tbaa !9
  %21 = load ptr, ptr %13, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.UserData, ptr %21, i32 0, i32 1
  %23 = getelementptr inbounds [2 x i32], ptr %22, i64 0, i64 0
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %26, label %92

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

32:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %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
  call void @glGenBuffers(i32 noundef 2, ptr noundef %35)
  %36 = load ptr, ptr %13, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.UserData, ptr %36, i32 0, i32 1
  %38 = getelementptr inbounds [2 x i32], ptr %37, i64 0, i64 0
  %39 = load i32, ptr %38, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34962, i32 noundef %39)
  %40 = load i32, ptr %10, align 4, !tbaa !9
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = mul nsw i32 %40, %41
  %43 = sext i32 %42 to i64
  call void @glBufferData(i32 noundef 34962, i64 noundef %43, ptr noundef null, i32 noundef 35044)
  %44 = load i32, ptr %10, 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 = call ptr @glMapBufferRange(i32 noundef 34962, i64 noundef 0, i64 noundef %47, i32 noundef 10)
  store ptr %48, ptr %15, align 8, !tbaa !5
  %49 = load ptr, ptr %15, align 8, !tbaa !5
  %50 = icmp eq ptr %49, null
  br i1 %50, label %51, label %52

51:                                               ; preds = %32
  call void (ptr, ...) @esLogMessage(ptr noundef @.str)
  store i32 1, ptr %17, align 4
  br label %89

52:                                               ; preds = %32
  %53 = load ptr, ptr %15, align 8, !tbaa !5
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  %55 = load i32, ptr %10, align 4, !tbaa !9
  %56 = load i32, ptr %8, align 4, !tbaa !9
  %57 = mul nsw i32 %55, %56
  %58 = sext i32 %57 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %53, ptr align 4 %54, i64 %58, i1 false)
  %59 = call zeroext i8 @glUnmapBuffer(i32 noundef 34962)
  %60 = zext i8 %59 to i32
  %61 = icmp eq i32 %60, 0
  br i1 %61, label %62, label %63

62:                                               ; preds = %52
  call void (ptr, ...) @esLogMessage(ptr noundef @.str.1)
  store i32 1, ptr %17, align 4
  br label %89

63:                                               ; preds = %52
  %64 = load ptr, ptr %13, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.UserData, ptr %64, i32 0, i32 1
  %66 = getelementptr inbounds [2 x i32], ptr %65, i64 0, i64 1
  %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
  call void @glBufferData(i32 noundef 34963, i64 noundef %70, ptr noundef null, i32 noundef 35044)
  %71 = load i32, ptr %11, align 4, !tbaa !9
  %72 = sext i32 %71 to i64
  %73 = mul i64 2, %72
  %74 = call ptr @glMapBufferRange(i32 noundef 34963, i64 noundef 0, i64 noundef %73, i32 noundef 10)
  store ptr %74, ptr %16, align 8, !tbaa !5
  %75 = load ptr, ptr %16, align 8, !tbaa !5
  %76 = icmp eq ptr %75, null
  br i1 %76, label %77, label %78

77:                                               ; preds = %63
  call void (ptr, ...) @esLogMessage(ptr noundef @.str.2)
  store i32 1, ptr %17, align 4
  br label %89

78:                                               ; preds = %63
  %79 = load ptr, ptr %16, align 8, !tbaa !5
  %80 = load ptr, ptr %12, align 8, !tbaa !5
  %81 = load i32, ptr %11, align 4, !tbaa !9
  %82 = sext i32 %81 to i64
  %83 = mul i64 2, %82
  call void @llvm.memcpy.p0.p0.i64(ptr align 2 %79, ptr align 2 %80, i64 %83, i1 false)
  %84 = call zeroext i8 @glUnmapBuffer(i32 noundef 34963)
  %85 = zext i8 %84 to i32
  %86 = icmp eq i32 %85, 0
  br i1 %86, label %87, label %88

87:                                               ; preds = %78
  call void (ptr, ...) @esLogMessage(ptr noundef @.str.3)
  store i32 1, ptr %17, align 4
  br label %89

88:                                               ; preds = %78
  store i32 0, ptr %17, align 4
  br label %89

89:                                               ; preds = %88, %87, %77, %62, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  %90 = load i32, ptr %17, align 4
  switch i32 %90, label %114 [
    i32 0, label %91
  ]

91:                                               ; preds = %89
  br label %92

92:                                               ; preds = %91, %26, %6
  %93 = load ptr, ptr %13, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.UserData, ptr %93, i32 0, i32 1
  %95 = getelementptr inbounds [2 x i32], ptr %94, i64 0, i64 0
  %96 = load i32, ptr %95, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34962, i32 noundef %96)
  %97 = load ptr, ptr %13, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.UserData, ptr %97, i32 0, i32 1
  %99 = getelementptr inbounds [2 x i32], ptr %98, i64 0, i64 1
  %100 = load i32, ptr %99, align 4, !tbaa !9
  call void @glBindBuffer(i32 noundef 34963, i32 noundef %100)
  call void @glEnableVertexAttribArray(i32 noundef 0)
  call void @glEnableVertexAttribArray(i32 noundef 1)
  %101 = load i32, ptr %10, align 4, !tbaa !9
  %102 = load i32, ptr %14, align 4, !tbaa !9
  %103 = zext i32 %102 to i64
  %104 = inttoptr i64 %103 to ptr
  call void @glVertexAttribPointer(i32 noundef 0, i32 noundef 3, i32 noundef 5126, i8 noundef zeroext 0, i32 noundef %101, ptr noundef %104)
  %105 = load i32, ptr %14, align 4, !tbaa !9
  %106 = zext i32 %105 to i64
  %107 = add i64 %106, 12
  %108 = trunc i64 %107 to i32
  store i32 %108, ptr %14, align 4, !tbaa !9
  %109 = load i32, ptr %10, align 4, !tbaa !9
  %110 = load i32, ptr %14, align 4, !tbaa !9
  %111 = zext i32 %110 to i64
  %112 = inttoptr i64 %111 to ptr
  call void @glVertexAttribPointer(i32 noundef 1, i32 noundef 4, i32 noundef 5126, i8 noundef zeroext 0, i32 noundef %109, ptr noundef %112)
  %113 = load i32, ptr %11, align 4, !tbaa !9
  call void @glDrawElements(i32 noundef 4, i32 noundef %113, 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)
  store i32 0, ptr %17, align 4
  br label %114

114:                                              ; preds = %92, %89
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  %115 = load i32, ptr %17, align 4
  switch i32 %115, label %117 [
    i32 0, label %116
    i32 1, label %116
  ]

116:                                              ; preds = %114, %114
  ret void

117:                                              ; preds = %114
  unreachable
}

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

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

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

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

declare void @esLogMessage(ptr noundef, ...) #3

declare zeroext i8 @glUnmapBuffer(i32 noundef) #3

declare void @glEnableVertexAttribArray(i32 noundef) #3

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

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

declare void @glDisableVertexAttribArray(i32 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = { 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"

@.str = external hidden unnamed_addr constant [36 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [45 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: nounwind uwtable
define dso_local void @DrawPrimitiveWithVBOsMapBuffers(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef readonly %2, i32 noundef %3, i32 noundef %4, ptr nocapture noundef readonly %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 %38

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

16:                                               ; preds = %12
  tail call void @glGenBuffers(i32 noundef 2, ptr noundef nonnull %9) #3
  %17 = load i32, ptr %9, align 4, !tbaa !12
  tail call void @glBindBuffer(i32 noundef 34962, i32 noundef %17) #3
  %18 = mul nsw i32 %3, %1
  %19 = sext i32 %18 to i64
  tail call void @glBufferData(i32 noundef 34962, i64 noundef %19, ptr noundef null, i32 noundef 35044) #3
  %20 = tail call ptr @glMapBufferRange(i32 noundef 34962, i64 noundef 0, i64 noundef %19, i32 noundef 10) #3
  %21 = icmp eq ptr %20, null
  br i1 %21, label %22, label %23

22:                                               ; preds = %16
  tail call void (ptr, ...) @esLogMessage(ptr noundef nonnull @.str) #3
  br label %.critedge

23:                                               ; preds = %16
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 4 %20, ptr align 4 %2, i64 %19, i1 false)
  %24 = tail call zeroext i8 @glUnmapBuffer(i32 noundef 34962) #3
  %25 = icmp eq i8 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %23
  tail call void (ptr, ...) @esLogMessage(ptr noundef nonnull @.str.1) #3
  br label %.critedge

27:                                               ; preds = %23
  %28 = load i32, ptr %13, align 4, !tbaa !12
  tail call void @glBindBuffer(i32 noundef 34963, i32 noundef %28) #3
  %29 = sext i32 %4 to i64
  %30 = shl nsw i64 %29, 1
  tail call void @glBufferData(i32 noundef 34963, i64 noundef %30, ptr noundef null, i32 noundef 35044) #3
  %31 = tail call ptr @glMapBufferRange(i32 noundef 34963, i64 noundef 0, i64 noundef %30, i32 noundef 10) #3
  %32 = icmp eq ptr %31, null
  br i1 %32, label %33, label %34

33:                                               ; preds = %27
  tail call void (ptr, ...) @esLogMessage(ptr noundef nonnull @.str.2) #3
  br label %.critedge

34:                                               ; preds = %27
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 2 %31, ptr align 2 %5, i64 %30, i1 false)
  %35 = tail call zeroext i8 @glUnmapBuffer(i32 noundef 34963) #3
  %36 = icmp eq i8 %35, 0
  br i1 %36, label %37, label %._crit_edge

._crit_edge:                                      ; preds = %34
  %.pre = load i32, ptr %9, align 4, !tbaa !12
  br label %38

37:                                               ; preds = %34
  tail call void (ptr, ...) @esLogMessage(ptr noundef nonnull @.str.3) #3
  br label %.critedge

38:                                               ; preds = %._crit_edge, %12, %6
  %39 = phi i32 [ %.pre, %._crit_edge ], [ 0, %12 ], [ %10, %6 ]
  tail call void @glBindBuffer(i32 noundef 34962, i32 noundef %39) #3
  %40 = getelementptr inbounds i8, ptr %8, i64 8
  %41 = load i32, ptr %40, align 4, !tbaa !12
  tail call void @glBindBuffer(i32 noundef 34963, i32 noundef %41) #3
  tail call void @glEnableVertexAttribArray(i32 noundef 0) #3
  tail call void @glEnableVertexAttribArray(i32 noundef 1) #3
  tail call void @glVertexAttribPointer(i32 noundef 0, i32 noundef 3, i32 noundef 5126, i8 noundef zeroext 0, i32 noundef %3, ptr noundef null) #3
  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)) #3
  tail call void @glDrawElements(i32 noundef 4, i32 noundef %4, i32 noundef 5123, ptr noundef null) #3
  tail call void @glDisableVertexAttribArray(i32 noundef 0) #3
  tail call void @glDisableVertexAttribArray(i32 noundef 1) #3
  tail call void @glBindBuffer(i32 noundef 34962, i32 noundef 0) #3
  tail call void @glBindBuffer(i32 noundef 34963, i32 noundef 0) #3
  br label %.critedge

.critedge:                                        ; preds = %22, %26, %33, %37, %38
  ret void
}

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

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

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

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

declare void @esLogMessage(ptr noundef, ...) local_unnamed_addr #2

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

declare void @glEnableVertexAttribArray(i32 noundef) local_unnamed_addr #2

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

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

declare void @glDisableVertexAttribArray(i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree 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, !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,SimplifyCFGPass,TailCallElimPass