; LLVM IR from StanfordVL/GibsonEnv :: gibson/core/channels/glad (egl)

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

@gladLoaderLoadEGL.NAMES = external hidden global [2 x ptr], align 16
@_egl_handle = external hidden global ptr, align 8
@.str.6 = external hidden unnamed_addr constant [18 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @gladLoadEGLUserPtr(ptr noundef, 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 @gladLoaderLoadEGL(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca %struct._glad_egl_userptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #2
  %6 = load ptr, ptr @_egl_handle, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %13

8:                                                ; preds = %1
  %9 = call ptr @glad_get_dlopen_handle(ptr noundef @gladLoaderLoadEGL.NAMES, i32 noundef 2)
  store ptr %9, ptr @_egl_handle, align 8, !tbaa !5
  %10 = load ptr, ptr @_egl_handle, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  %12 = zext i1 %11 to i32
  store i32 %12, ptr %4, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %8, %1
  %14 = load ptr, ptr @_egl_handle, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %37

16:                                               ; preds = %13
  %17 = load ptr, ptr @_egl_handle, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct._glad_egl_userptr, ptr %5, i32 0, i32 0
  store ptr %17, ptr %18, align 8, !tbaa !11
  %19 = load ptr, ptr @_egl_handle, align 8, !tbaa !5
  %20 = call ptr @glad_dlsym_handle(ptr noundef %19, ptr noundef @.str.6)
  %21 = getelementptr inbounds %struct._glad_egl_userptr, ptr %5, i32 0, i32 1
  store ptr %20, ptr %21, align 8, !tbaa !13
  %22 = getelementptr inbounds %struct._glad_egl_userptr, ptr %5, i32 0, i32 1
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %28

25:                                               ; preds = %16
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = call i32 @gladLoadEGLUserPtr(ptr noundef %26, ptr noundef @glad_egl_get_proc, ptr noundef %5)
  store i32 %27, ptr %3, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %25, %16
  %29 = load i32, ptr %3, align 4, !tbaa !9
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %36, label %31

31:                                               ; preds = %28
  %32 = load i32, ptr %4, align 4, !tbaa !9
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %36

34:                                               ; preds = %31
  %35 = load ptr, ptr @_egl_handle, align 8, !tbaa !5
  call void @glad_close_dlopen_handle(ptr noundef %35)
  store ptr null, ptr @_egl_handle, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %34, %31, %28
  br label %37

37:                                               ; preds = %36, %13
  %38 = load i32, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %38
}

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

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

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

; Function Attrs: nounwind uwtable
declare hidden void @glad_close_dlopen_handle(ptr noundef) #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 = { 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 0}
!12 = !{!"_glad_egl_userptr", !6, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}


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

@gladLoaderLoadEGL.NAMES = external hidden global [2 x ptr], align 16
@_egl_handle = external hidden local_unnamed_addr global ptr, align 8
@.str.6 = external hidden unnamed_addr constant [18 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @gladLoadEGLUserPtr(ptr noundef, 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 @gladLoaderLoadEGL(ptr noundef %0) local_unnamed_addr #0 {
  %2 = alloca %struct._glad_egl_userptr, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #2
  %3 = load ptr, ptr @_egl_handle, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %.thread

5:                                                ; preds = %1
  %6 = tail call ptr @glad_get_dlopen_handle(ptr noundef nonnull @gladLoaderLoadEGL.NAMES, i32 noundef 2)
  store ptr %6, ptr @_egl_handle, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %17, label %.thread

.thread:                                          ; preds = %1, %5
  %.09 = phi i1 [ %7, %5 ], [ false, %1 ]
  %8 = phi ptr [ %6, %5 ], [ %3, %1 ]
  store ptr %8, ptr %2, align 8, !tbaa !9
  %9 = tail call ptr @glad_dlsym_handle(ptr noundef nonnull %8, ptr noundef nonnull @.str.6)
  %10 = getelementptr inbounds i8, ptr %2, i64 8
  store ptr %9, ptr %10, align 8, !tbaa !11
  %.not6 = icmp eq ptr %9, null
  br i1 %.not6, label %13, label %11

11:                                               ; preds = %.thread
  %12 = call i32 @gladLoadEGLUserPtr(ptr noundef %0, ptr noundef nonnull @glad_egl_get_proc, ptr noundef nonnull %2)
  br label %13

13:                                               ; preds = %11, %.thread
  %.04 = phi i32 [ %12, %11 ], [ 0, %.thread ]
  %14 = icmp eq i32 %.04, 0
  %or.cond = and i1 %.09, %14
  br i1 %or.cond, label %15, label %17

15:                                               ; preds = %13
  %16 = load ptr, ptr @_egl_handle, align 8, !tbaa !5
  call void @glad_close_dlopen_handle(ptr noundef %16)
  store ptr null, ptr @_egl_handle, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %13, %15, %5
  %.1 = phi i32 [ 0, %15 ], [ %.04, %13 ], [ 0, %5 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #2
  ret i32 %.1
}

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

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

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

; Function Attrs: nounwind uwtable
declare hidden void @glad_close_dlopen_handle(ptr noundef) 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"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_glad_egl_userptr", !6, i64 0, !6, i64 8}
!11 = !{!10, !6, i64 8}


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