; LLVM IR from NTHU-LSALAB/Gemini :: src (hook)

; 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.hookInfo = type <{ i32, [4 x i8], [27 x ptr], [27 x ptr], [27 x i32], [4 x i8] }>

@_ZL8hook_inf = external hidden global %struct.hookInfo, align 8
@.str.22 = external hidden unnamed_addr constant [15 x i8], align 1
@_ZL9init_done = external hidden global i32, align 4
@_ZZ14cuCtxCreate_v2E9real_func = external hidden global ptr, align 8
@_ZGVZ14cuCtxCreate_v2E9real_func = external hidden global i64, align 8

; Function Attrs: mustprogress uwtable
declare hidden noundef ptr @_ZL10real_dlsymPvPKc(ptr noundef, ptr noundef) #0

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
define dso_local i32 @cuCtxCreate_v2(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 personality ptr @__gxx_personality_v0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  %12 = call i32 @pthread_once(ptr noundef @_ZL9init_done, ptr noundef @_Z10initializev)
  %13 = load atomic i8, ptr @_ZGVZ14cuCtxCreate_v2E9real_func acquire, align 8
  %14 = icmp eq i8 %13, 0
  br i1 %14, label %15, label %21, !prof !11

15:                                               ; preds = %3
  %16 = call i32 @__cxa_guard_acquire(ptr @_ZGVZ14cuCtxCreate_v2E9real_func) #3
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %15
  %19 = invoke noundef ptr @_ZL10real_dlsymPvPKc(ptr noundef inttoptr (i64 -1 to ptr), ptr noundef @.str.22)
          to label %20 unwind label %27

20:                                               ; preds = %18
  store ptr %19, ptr @_ZZ14cuCtxCreate_v2E9real_func, align 8, !tbaa !5
  call void @__cxa_guard_release(ptr @_ZGVZ14cuCtxCreate_v2E9real_func) #3
  br label %21

21:                                               ; preds = %20, %15, %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !12
  %22 = load i32, ptr @_ZL8hook_inf, align 8, !tbaa !14
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %31

24:                                               ; preds = %21
  %25 = load i32, ptr getelementptr inbounds ([27 x i32], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 4), i64 0, i64 11), align 4, !tbaa !9
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr getelementptr inbounds ([27 x i32], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 4), i64 0, i64 11), align 4, !tbaa !9
  br label %31

27:                                               ; preds = %18
  %28 = landingpad { ptr, i32 }
          cleanup
  %29 = extractvalue { ptr, i32 } %28, 0
  store ptr %29, ptr %8, align 8
  %30 = extractvalue { ptr, i32 } %28, 1
  store i32 %30, ptr %9, align 4
  call void @__cxa_guard_abort(ptr @_ZGVZ14cuCtxCreate_v2E9real_func) #3
  br label %66

31:                                               ; preds = %24, %21
  %32 = load ptr, ptr getelementptr inbounds ([27 x ptr], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 2), i64 0, i64 11), align 8, !tbaa !5
  %33 = icmp ne ptr %32, null
  br i1 %33, label %34, label %40

34:                                               ; preds = %31
  %35 = load ptr, ptr getelementptr inbounds ([27 x ptr], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 2), i64 0, i64 11), align 8, !tbaa !5
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load i32, ptr %6, align 4, !tbaa !9
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = call noundef i32 %35(ptr noundef %36, i32 noundef %37, i32 noundef %38)
  store i32 %39, ptr %10, align 4, !tbaa !12
  br label %40

40:                                               ; preds = %34, %31
  %41 = load i32, ptr %10, align 4, !tbaa !12
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %43, label %45

43:                                               ; preds = %40
  %44 = load i32, ptr %10, align 4, !tbaa !12
  store i32 %44, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %64

45:                                               ; preds = %40
  %46 = load ptr, ptr @_ZZ14cuCtxCreate_v2E9real_func, align 8, !tbaa !5
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = load i32, ptr %6, align 4, !tbaa !9
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = call noundef i32 %46(ptr noundef %47, i32 noundef %48, i32 noundef %49)
  store i32 %50, ptr %10, align 4, !tbaa !12
  %51 = load ptr, ptr getelementptr inbounds ([27 x ptr], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 3), i64 0, i64 11), align 8, !tbaa !5
  %52 = icmp ne ptr %51, null
  br i1 %52, label %53, label %62

53:                                               ; preds = %45
  %54 = load i32, ptr %10, align 4, !tbaa !12
  %55 = icmp eq i32 %54, 0
  br i1 %55, label %56, label %62

56:                                               ; preds = %53
  %57 = load ptr, ptr getelementptr inbounds ([27 x ptr], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 3), i64 0, i64 11), align 8, !tbaa !5
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = load i32, ptr %6, align 4, !tbaa !9
  %60 = load i32, ptr %7, align 4, !tbaa !9
  %61 = call noundef i32 %57(ptr noundef %58, i32 noundef %59, i32 noundef %60)
  store i32 %61, ptr %10, align 4, !tbaa !12
  br label %62

62:                                               ; preds = %56, %53, %45
  %63 = load i32, ptr %10, align 4, !tbaa !12
  store i32 %63, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %64

64:                                               ; preds = %62, %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %65 = load i32, ptr %4, align 4
  ret i32 %65

66:                                               ; preds = %27
  %67 = load ptr, ptr %8, align 8
  %68 = load i32, ptr %9, align 4
  %69 = insertvalue { ptr, i32 } poison, ptr %67, 0
  %70 = insertvalue { ptr, i32 } %69, i32 %68, 1
  resume { ptr, i32 } %70
}

; 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: mustprogress uwtable
declare dso_local void @_Z10initializev() #0

declare i32 @pthread_once(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind
declare i32 @__cxa_guard_acquire(ptr) #3

; Function Attrs: nounwind
declare void @__cxa_guard_abort(ptr) #3

; Function Attrs: nounwind
declare void @__cxa_guard_release(ptr) #3

attributes #0 = { mustprogress 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 = { "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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!"branch_weights", i32 1, i32 1048575}
!12 = !{!13, !13, i64 0}
!13 = !{!"_ZTS14cudaError_enum", !7, i64 0}
!14 = !{!15, !10, i64 0}
!15 = !{!"_ZTS8hookInfo", !10, i64 0, !7, i64 8, !7, i64 224, !7, i64 440}


; 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.hookInfo = type <{ i32, [4 x i8], [27 x ptr], [27 x ptr], [27 x i32], [4 x i8] }>

@_ZL8hook_inf = external hidden local_unnamed_addr global %struct.hookInfo, align 8
@.str.22 = external hidden unnamed_addr constant [15 x i8], align 1
@_ZL9init_done = external hidden global i32, align 4
@_ZZ14cuCtxCreate_v2E9real_func = external hidden local_unnamed_addr global ptr, align 8
@_ZGVZ14cuCtxCreate_v2E9real_func = external hidden global i64, align 8

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

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @cuCtxCreate_v2(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  %4 = tail call i32 @pthread_once(ptr noundef nonnull @_ZL9init_done, ptr noundef nonnull @_Z10initializev)
  %5 = load atomic i8, ptr @_ZGVZ14cuCtxCreate_v2E9real_func acquire, align 8
  %6 = icmp eq i8 %5, 0
  br i1 %6, label %7, label %12, !prof !5

7:                                                ; preds = %3
  %8 = tail call i32 @__cxa_guard_acquire(ptr nonnull @_ZGVZ14cuCtxCreate_v2E9real_func) #3
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %12, label %9

9:                                                ; preds = %7
  %10 = invoke noundef ptr @_ZL10real_dlsymPvPKc(ptr noundef nonnull inttoptr (i64 -1 to ptr), ptr noundef nonnull @.str.22)
          to label %11 unwind label %17

11:                                               ; preds = %9
  store ptr %10, ptr @_ZZ14cuCtxCreate_v2E9real_func, align 8, !tbaa !6
  tail call void @__cxa_guard_release(ptr nonnull @_ZGVZ14cuCtxCreate_v2E9real_func) #3
  br label %12

12:                                               ; preds = %11, %7, %3
  %13 = load i32, ptr @_ZL8hook_inf, align 8, !tbaa !10
  %.not19 = icmp eq i32 %13, 0
  br i1 %.not19, label %19, label %14

14:                                               ; preds = %12
  %15 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL8hook_inf, i64 484), align 4, !tbaa !13
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr getelementptr inbounds (i8, ptr @_ZL8hook_inf, i64 484), align 4, !tbaa !13
  br label %19

17:                                               ; preds = %9
  %18 = landingpad { ptr, i32 }
          cleanup
  tail call void @__cxa_guard_abort(ptr nonnull @_ZGVZ14cuCtxCreate_v2E9real_func) #3
  resume { ptr, i32 } %18

19:                                               ; preds = %14, %12
  %20 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZL8hook_inf, i64 96), align 8, !tbaa !6
  %.not20 = icmp eq ptr %20, null
  br i1 %.not20, label %.thread, label %21

21:                                               ; preds = %19
  %22 = tail call noundef i32 %20(ptr noundef %0, i32 noundef %1, i32 noundef %2)
  %.not21 = icmp eq i32 %22, 0
  br i1 %.not21, label %.thread, label %30

.thread:                                          ; preds = %19, %21
  %23 = load ptr, ptr @_ZZ14cuCtxCreate_v2E9real_func, align 8, !tbaa !6
  %24 = tail call noundef i32 %23(ptr noundef %0, i32 noundef %1, i32 noundef %2)
  %25 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZL8hook_inf, i64 312), align 8, !tbaa !6
  %26 = icmp ne ptr %25, null
  %27 = icmp eq i32 %24, 0
  %or.cond = and i1 %27, %26
  br i1 %or.cond, label %28, label %30

28:                                               ; preds = %.thread
  %29 = tail call noundef i32 %25(ptr noundef %0, i32 noundef %1, i32 noundef %2)
  br label %30

30:                                               ; preds = %.thread, %28, %21
  %.016 = phi i32 [ %22, %21 ], [ %29, %28 ], [ %24, %.thread ]
  ret i32 %.016
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z10initializev() #0

declare i32 @pthread_once(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare i32 @__cxa_guard_acquire(ptr) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare void @__cxa_guard_abort(ptr) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare void @__cxa_guard_release(ptr) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { nofree nounwind }
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 = !{!"branch_weights", i32 1, i32 1048575}
!6 = !{!7, !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !12, i64 0}
!11 = !{!"_ZTS8hookInfo", !12, i64 0, !8, i64 8, !8, i64 224, !8, i64 440}
!12 = !{!"int", !8, i64 0}
!13 = !{!12, !12, i64 0}


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