; LLVM IR from brenocq/atta :: src/extern/glad/src (gles2)

; 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._glad_gles2_userptr = type { ptr, ptr }

@glad_eglGetProcAddress = external global ptr, align 8
@_glad_GLES2_loader_handle = external hidden global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @gladLoadGLES2UserPtr(ptr noundef, ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @gladLoaderLoadGLES2() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca %struct._glad_gles2_userptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca %struct._glad_gles2_userptr, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store ptr null, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #3
  %8 = load ptr, ptr @glad_eglGetProcAddress, align 8, !tbaa !9
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %11

10:                                               ; preds = %0
  store i32 0, ptr %1, align 4
  store i32 1, ptr %6, align 4
  br label %35

11:                                               ; preds = %0
  %12 = load ptr, ptr @_glad_GLES2_loader_handle, align 8, !tbaa !9
  %13 = icmp eq ptr %12, null
  %14 = zext i1 %13 to i32
  store i32 %14, ptr %4, align 4, !tbaa !5
  %15 = call ptr @glad_gles2_dlopen_handle()
  store ptr %15, ptr %3, align 8, !tbaa !9
  %16 = load ptr, ptr %3, align 8, !tbaa !9
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %33

18:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #3
  %19 = load ptr, ptr %3, align 8, !tbaa !9
  %20 = call { ptr, ptr } @glad_gles2_build_userptr(ptr noundef %19)
  %21 = getelementptr inbounds { ptr, ptr }, ptr %7, i32 0, i32 0
  %22 = extractvalue { ptr, ptr } %20, 0
  store ptr %22, ptr %21, align 8
  %23 = getelementptr inbounds { ptr, ptr }, ptr %7, i32 0, i32 1
  %24 = extractvalue { ptr, ptr } %20, 1
  store ptr %24, ptr %23, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %5, ptr align 8 %7, i64 16, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #3
  %25 = call i32 @gladLoadGLES2UserPtr(ptr noundef @glad_gles2_get_proc, ptr noundef %5)
  store i32 %25, ptr %2, align 4, !tbaa !5
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %32, label %28

28:                                               ; preds = %18
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %28
  call void @gladLoaderUnloadGLES2()
  br label %32

32:                                               ; preds = %31, %28, %18
  br label %33

33:                                               ; preds = %32, %11
  %34 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %34, ptr %1, align 4
  store i32 1, ptr %6, align 4
  br label %35

35:                                               ; preds = %33, %10
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  %36 = load i32, ptr %1, align 4
  ret i32 %36
}

; Function Attrs: nounwind uwtable
declare hidden ptr @glad_gles2_dlopen_handle() #0

; Function Attrs: nounwind uwtable
declare hidden { ptr, ptr } @glad_gles2_build_userptr(ptr noundef) #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) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @glad_gles2_get_proc(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @gladLoaderUnloadGLES2() #0

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{i64 0, i64 8, !9, i64 8, i64 8, !9}


; 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._glad_gles2_userptr = type { ptr, ptr }

@glad_eglGetProcAddress = external local_unnamed_addr global ptr, align 8
@_glad_GLES2_loader_handle = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @gladLoadGLES2UserPtr(ptr noundef, ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @gladLoaderLoadGLES2() local_unnamed_addr #0 {
  %1 = alloca %struct._glad_gles2_userptr, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %1) #2
  %2 = load ptr, ptr @glad_eglGetProcAddress, align 8, !tbaa !5
  %3 = icmp eq ptr %2, null
  br i1 %3, label %15, label %4

4:                                                ; preds = %0
  %5 = load ptr, ptr @_glad_GLES2_loader_handle, align 8, !tbaa !5
  %6 = tail call ptr @glad_gles2_dlopen_handle()
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %15, label %7

7:                                                ; preds = %4
  %8 = icmp eq ptr %5, null
  %9 = tail call { ptr, ptr } @glad_gles2_build_userptr(ptr noundef nonnull %6)
  %10 = extractvalue { ptr, ptr } %9, 0
  %11 = extractvalue { ptr, ptr } %9, 1
  store ptr %10, ptr %1, align 8, !tbaa !5
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %1, i64 8
  store ptr %11, ptr %.sroa.4.0..sroa_idx, align 8, !tbaa !5
  %12 = call i32 @gladLoadGLES2UserPtr(ptr noundef nonnull @glad_gles2_get_proc, ptr noundef nonnull %1)
  %13 = icmp eq i32 %12, 0
  %or.cond = select i1 %13, i1 %8, i1 false
  br i1 %or.cond, label %14, label %15

14:                                               ; preds = %7
  call void @gladLoaderUnloadGLES2()
  br label %15

15:                                               ; preds = %4, %14, %7, %0
  %.0 = phi i32 [ 0, %0 ], [ 0, %14 ], [ %12, %7 ], [ 0, %4 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %1) #2
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden ptr @glad_gles2_dlopen_handle() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden { ptr, ptr } @glad_gles2_build_userptr(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @glad_gles2_get_proc(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @gladLoaderUnloadGLES2() local_unnamed_addr #0

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 }

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


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