; 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.15 = external hidden unnamed_addr constant [16 x i8], align 1
@_ZL9init_done = external hidden global i32, align 4
@_ZZ15cuMemcpyAtoH_v2E9real_func = external hidden global ptr, align 8
@_ZGVZ15cuMemcpyAtoH_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 @cuMemcpyAtoH_v2(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3) #0 personality ptr @__gxx_personality_v0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i64 %3, ptr %9, align 8, !tbaa !9
  %14 = call i32 @pthread_once(ptr noundef @_ZL9init_done, ptr noundef @_Z10initializev)
  %15 = load atomic i8, ptr @_ZGVZ15cuMemcpyAtoH_v2E9real_func acquire, align 8
  %16 = icmp eq i8 %15, 0
  br i1 %16, label %17, label %23, !prof !11

17:                                               ; preds = %4
  %18 = call i32 @__cxa_guard_acquire(ptr @_ZGVZ15cuMemcpyAtoH_v2E9real_func) #3
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %20, label %23

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

22:                                               ; preds = %20
  store ptr %21, ptr @_ZZ15cuMemcpyAtoH_v2E9real_func, align 8, !tbaa !5
  call void @__cxa_guard_release(ptr @_ZGVZ15cuMemcpyAtoH_v2E9real_func) #3
  br label %23

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

26:                                               ; preds = %23
  %27 = load i32, ptr getelementptr inbounds ([27 x i32], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 4), i64 0, i64 23), align 4, !tbaa !17
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr getelementptr inbounds ([27 x i32], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 4), i64 0, i64 23), align 4, !tbaa !17
  br label %33

29:                                               ; preds = %20
  %30 = landingpad { ptr, i32 }
          cleanup
  %31 = extractvalue { ptr, i32 } %30, 0
  store ptr %31, ptr %10, align 8
  %32 = extractvalue { ptr, i32 } %30, 1
  store i32 %32, ptr %11, align 4
  call void @__cxa_guard_abort(ptr @_ZGVZ15cuMemcpyAtoH_v2E9real_func) #3
  br label %71

33:                                               ; preds = %26, %23
  %34 = load ptr, ptr getelementptr inbounds ([27 x ptr], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 2), i64 0, i64 23), align 8, !tbaa !5
  %35 = icmp ne ptr %34, null
  br i1 %35, label %36, label %43

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

43:                                               ; preds = %36, %33
  %44 = load i32, ptr %12, align 4, !tbaa !12
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %48

46:                                               ; preds = %43
  %47 = load i32, ptr %12, align 4, !tbaa !12
  store i32 %47, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %69

48:                                               ; preds = %43
  %49 = load ptr, ptr @_ZZ15cuMemcpyAtoH_v2E9real_func, align 8, !tbaa !5
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = load i64, ptr %8, align 8, !tbaa !9
  %53 = load i64, ptr %9, align 8, !tbaa !9
  %54 = call noundef i32 %49(ptr noundef %50, ptr noundef %51, i64 noundef %52, i64 noundef %53)
  store i32 %54, ptr %12, align 4, !tbaa !12
  %55 = load ptr, ptr getelementptr inbounds ([27 x ptr], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 3), i64 0, i64 23), align 8, !tbaa !5
  %56 = icmp ne ptr %55, null
  br i1 %56, label %57, label %67

57:                                               ; preds = %48
  %58 = load i32, ptr %12, align 4, !tbaa !12
  %59 = icmp eq i32 %58, 0
  br i1 %59, label %60, label %67

60:                                               ; preds = %57
  %61 = load ptr, ptr getelementptr inbounds ([27 x ptr], ptr getelementptr inbounds (%struct.hookInfo, ptr @_ZL8hook_inf, i32 0, i32 3), i64 0, i64 23), align 8, !tbaa !5
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = load i64, ptr %8, align 8, !tbaa !9
  %65 = load i64, ptr %9, align 8, !tbaa !9
  %66 = call noundef i32 %61(ptr noundef %62, ptr noundef %63, i64 noundef %64, i64 noundef %65)
  store i32 %66, ptr %12, align 4, !tbaa !12
  br label %67

67:                                               ; preds = %60, %57, %48
  %68 = load i32, ptr %12, align 4, !tbaa !12
  store i32 %68, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %69

69:                                               ; preds = %67, %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  %70 = load i32, ptr %5, align 4
  ret i32 %70

71:                                               ; preds = %29
  %72 = load ptr, ptr %10, align 8
  %73 = load i32, ptr %11, align 4
  %74 = insertvalue { ptr, i32 } poison, ptr %72, 0
  %75 = insertvalue { ptr, i32 } %74, i32 %73, 1
  resume { ptr, i32 } %75
}

; 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 = !{!"long", !7, i64 0}
!11 = !{!"branch_weights", i32 1, i32 1048575}
!12 = !{!13, !13, i64 0}
!13 = !{!"_ZTS14cudaError_enum", !7, i64 0}
!14 = !{!15, !16, i64 0}
!15 = !{!"_ZTS8hookInfo", !16, i64 0, !7, i64 8, !7, i64 224, !7, i64 440}
!16 = !{!"int", !7, i64 0}
!17 = !{!16, !16, 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.15 = external hidden unnamed_addr constant [16 x i8], align 1
@_ZL9init_done = external hidden global i32, align 4
@_ZZ15cuMemcpyAtoH_v2E9real_func = external hidden local_unnamed_addr global ptr, align 8
@_ZGVZ15cuMemcpyAtoH_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 @cuMemcpyAtoH_v2(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  %5 = tail call i32 @pthread_once(ptr noundef nonnull @_ZL9init_done, ptr noundef nonnull @_Z10initializev)
  %6 = load atomic i8, ptr @_ZGVZ15cuMemcpyAtoH_v2E9real_func acquire, align 8
  %7 = icmp eq i8 %6, 0
  br i1 %7, label %8, label %13, !prof !5

8:                                                ; preds = %4
  %9 = tail call i32 @__cxa_guard_acquire(ptr nonnull @_ZGVZ15cuMemcpyAtoH_v2E9real_func) #3
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %13, label %10

10:                                               ; preds = %8
  %11 = invoke noundef ptr @_ZL10real_dlsymPvPKc(ptr noundef nonnull inttoptr (i64 -1 to ptr), ptr noundef nonnull @.str.15)
          to label %12 unwind label %18

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

13:                                               ; preds = %12, %8, %4
  %14 = load i32, ptr @_ZL8hook_inf, align 8, !tbaa !10
  %.not22 = icmp eq i32 %14, 0
  br i1 %.not22, label %20, label %15

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

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

20:                                               ; preds = %15, %13
  %21 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZL8hook_inf, i64 192), align 8, !tbaa !6
  %.not23 = icmp eq ptr %21, null
  br i1 %.not23, label %.thread, label %22

22:                                               ; preds = %20
  %23 = tail call noundef i32 %21(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3)
  %.not24 = icmp eq i32 %23, 0
  br i1 %.not24, label %.thread, label %31

.thread:                                          ; preds = %20, %22
  %24 = load ptr, ptr @_ZZ15cuMemcpyAtoH_v2E9real_func, align 8, !tbaa !6
  %25 = tail call noundef i32 %24(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3)
  %26 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZL8hook_inf, i64 408), align 8, !tbaa !6
  %27 = icmp ne ptr %26, null
  %28 = icmp eq i32 %25, 0
  %or.cond = and i1 %28, %27
  br i1 %or.cond, label %29, label %31

29:                                               ; preds = %.thread
  %30 = tail call noundef i32 %26(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3)
  br label %31

31:                                               ; preds = %.thread, %29, %22
  %.019 = phi i32 [ %23, %22 ], [ %30, %29 ], [ %25, %.thread ]
  ret i32 %.019
}

; 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