; 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.14 = external hidden unnamed_addr constant [17 x i8], align 1
@_ZL9init_done = external hidden global i32, align 4
@_ZZ16cuCtxSynchronizeE9real_func = external hidden global ptr, align 8
@_ZGVZ16cuCtxSynchronizeE9real_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 @cuCtxSynchronize() #0 personality ptr @__gxx_personality_v0 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = call i32 @pthread_once(ptr noundef @_ZL9init_done, ptr noundef @_Z10initializev)
  %7 = load atomic i8, ptr @_ZGVZ16cuCtxSynchronizeE9real_func acquire, align 8
  %8 = icmp eq i8 %7, 0
  br i1 %8, label %9, label %15, !prof !5

9:                                                ; preds = %0
  %10 = call i32 @__cxa_guard_acquire(ptr @_ZGVZ16cuCtxSynchronizeE9real_func) #3
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %15

12:                                               ; preds = %9
  %13 = invoke noundef ptr @_ZL10real_dlsymPvPKc(ptr noundef inttoptr (i64 -1 to ptr), ptr noundef @.str.14)
          to label %14 unwind label %21

14:                                               ; preds = %12
  store ptr %13, ptr @_ZZ16cuCtxSynchronizeE9real_func, align 8, !tbaa !6
  call void @__cxa_guard_release(ptr @_ZGVZ16cuCtxSynchronizeE9real_func) #3
  br label %15

15:                                               ; preds = %14, %9, %0
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !10
  %16 = load i32, ptr @_ZL8hook_inf, align 8, !tbaa !12
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %25

18:                                               ; preds = %15
  %19 = load i32, ptr getelementptr inbounds ([27 x i32], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 4), i64 0, i64 22), align 8, !tbaa !15
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr getelementptr inbounds ([27 x i32], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 4), i64 0, i64 22), align 8, !tbaa !15
  br label %25

21:                                               ; preds = %12
  %22 = landingpad { ptr, i32 }
          cleanup
  %23 = extractvalue { ptr, i32 } %22, 0
  store ptr %23, ptr %2, align 8
  %24 = extractvalue { ptr, i32 } %22, 1
  store i32 %24, ptr %3, align 4
  call void @__cxa_guard_abort(ptr @_ZGVZ16cuCtxSynchronizeE9real_func) #3
  br label %51

25:                                               ; preds = %18, %15
  %26 = load ptr, ptr getelementptr inbounds ([27 x ptr], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 2), i64 0, i64 22), align 8, !tbaa !6
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %31

28:                                               ; preds = %25
  %29 = load ptr, ptr getelementptr inbounds ([27 x ptr], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 2), i64 0, i64 22), align 8, !tbaa !6
  %30 = call noundef i32 %29()
  store i32 %30, ptr %4, align 4, !tbaa !10
  br label %31

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

34:                                               ; preds = %31
  %35 = load i32, ptr %4, align 4, !tbaa !10
  store i32 %35, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %49

36:                                               ; preds = %31
  %37 = load ptr, ptr @_ZZ16cuCtxSynchronizeE9real_func, align 8, !tbaa !6
  %38 = call noundef i32 %37()
  store i32 %38, ptr %4, align 4, !tbaa !10
  %39 = load ptr, ptr getelementptr inbounds ([27 x ptr], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 3), i64 0, i64 22), align 8, !tbaa !6
  %40 = icmp ne ptr %39, null
  br i1 %40, label %41, label %47

41:                                               ; preds = %36
  %42 = load i32, ptr %4, align 4, !tbaa !10
  %43 = icmp eq i32 %42, 0
  br i1 %43, label %44, label %47

44:                                               ; preds = %41
  %45 = load ptr, ptr getelementptr inbounds ([27 x ptr], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 3), i64 0, i64 22), align 8, !tbaa !6
  %46 = call noundef i32 %45()
  store i32 %46, ptr %4, align 4, !tbaa !10
  br label %47

47:                                               ; preds = %44, %41, %36
  %48 = load i32, ptr %4, align 4, !tbaa !10
  store i32 %48, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %49

49:                                               ; preds = %47, %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %50 = load i32, ptr %1, align 4
  ret i32 %50

51:                                               ; preds = %21
  %52 = load ptr, ptr %2, align 8
  %53 = load i32, ptr %3, align 4
  %54 = insertvalue { ptr, i32 } poison, ptr %52, 0
  %55 = insertvalue { ptr, i32 } %54, i32 %53, 1
  resume { ptr, i32 } %55
}

; 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 = !{!"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, !11, i64 0}
!11 = !{!"_ZTS14cudaError_enum", !8, i64 0}
!12 = !{!13, !14, i64 0}
!13 = !{!"_ZTS8hookInfo", !14, i64 0, !8, i64 8, !8, i64 224, !8, i64 440}
!14 = !{!"int", !8, i64 0}
!15 = !{!14, !14, 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"

%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.14 = external hidden unnamed_addr constant [17 x i8], align 1
@_ZL9init_done = external hidden global i32, align 4
@_ZZ16cuCtxSynchronizeE9real_func = external hidden local_unnamed_addr global ptr, align 8
@_ZGVZ16cuCtxSynchronizeE9real_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 @cuCtxSynchronize() local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  %1 = tail call i32 @pthread_once(ptr noundef nonnull @_ZL9init_done, ptr noundef nonnull @_Z10initializev)
  %2 = load atomic i8, ptr @_ZGVZ16cuCtxSynchronizeE9real_func acquire, align 8
  %3 = icmp eq i8 %2, 0
  br i1 %3, label %4, label %9, !prof !5

4:                                                ; preds = %0
  %5 = tail call i32 @__cxa_guard_acquire(ptr nonnull @_ZGVZ16cuCtxSynchronizeE9real_func) #3
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %9, label %6

6:                                                ; preds = %4
  %7 = invoke noundef ptr @_ZL10real_dlsymPvPKc(ptr noundef nonnull inttoptr (i64 -1 to ptr), ptr noundef nonnull @.str.14)
          to label %8 unwind label %14

8:                                                ; preds = %6
  store ptr %7, ptr @_ZZ16cuCtxSynchronizeE9real_func, align 8, !tbaa !6
  tail call void @__cxa_guard_release(ptr nonnull @_ZGVZ16cuCtxSynchronizeE9real_func) #3
  br label %9

9:                                                ; preds = %8, %4, %0
  %10 = load i32, ptr @_ZL8hook_inf, align 8, !tbaa !10
  %.not10 = icmp eq i32 %10, 0
  br i1 %.not10, label %16, label %11

11:                                               ; preds = %9
  %12 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL8hook_inf, i64 528), align 8, !tbaa !13
  %13 = add nsw i32 %12, 1
  store i32 %13, ptr getelementptr inbounds (i8, ptr @_ZL8hook_inf, i64 528), align 8, !tbaa !13
  br label %16

14:                                               ; preds = %6
  %15 = landingpad { ptr, i32 }
          cleanup
  tail call void @__cxa_guard_abort(ptr nonnull @_ZGVZ16cuCtxSynchronizeE9real_func) #3
  resume { ptr, i32 } %15

16:                                               ; preds = %11, %9
  %17 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZL8hook_inf, i64 184), align 8, !tbaa !6
  %.not11 = icmp eq ptr %17, null
  br i1 %.not11, label %.thread, label %18

18:                                               ; preds = %16
  %19 = tail call noundef i32 %17()
  %.not12 = icmp eq i32 %19, 0
  br i1 %.not12, label %.thread, label %27

.thread:                                          ; preds = %16, %18
  %20 = load ptr, ptr @_ZZ16cuCtxSynchronizeE9real_func, align 8, !tbaa !6
  %21 = tail call noundef i32 %20()
  %22 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZL8hook_inf, i64 400), align 8, !tbaa !6
  %23 = icmp ne ptr %22, null
  %24 = icmp eq i32 %21, 0
  %or.cond = and i1 %24, %23
  br i1 %or.cond, label %25, label %27

25:                                               ; preds = %.thread
  %26 = tail call noundef i32 %22()
  br label %27

27:                                               ; preds = %.thread, %25, %18
  %.07 = phi i32 [ %19, %18 ], [ %26, %25 ], [ %21, %.thread ]
  ret i32 %.07
}

; 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