; LLVM IR from cjmcv/hpc :: coroutine/libco/libco/src (co_hook_sys_call)

; 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.rpchook_t = type { i32, %struct.sockaddr_in, i32, %struct.timeval, %struct.timeval }
%struct.sockaddr_in = type { i16, i16, %struct.in_addr, [8 x i8] }
%struct.in_addr = type { i32 }
%struct.timeval = type { i64, i64 }

@_ZL17g_sys_socket_func = external hidden global ptr, align 8
@.str = external hidden unnamed_addr constant [7 x i8], align 1
@_ZL16g_sys_fcntl_func = external hidden global ptr, align 8

; Function Attrs: nounwind
declare ptr @dlsym(ptr noundef, ptr noundef) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local i32 @socket(i32 noundef %0, i32 noundef %1, i32 noundef %2) #1 personality ptr @__gxx_personality_v0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  %11 = load ptr, ptr @_ZL17g_sys_socket_func, align 8, !tbaa !9
  %12 = icmp ne ptr %11, null
  br i1 %12, label %15, label %13

13:                                               ; preds = %3
  %14 = call ptr @dlsym(ptr noundef inttoptr (i64 -1 to ptr), ptr noundef @.str) #7
  store ptr %14, ptr @_ZL17g_sys_socket_func, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %13, %3
  %16 = invoke noundef zeroext i1 @_Z21co_is_enable_sys_hookv()
          to label %17 unwind label %54

17:                                               ; preds = %15
  br i1 %16, label %25, label %18

18:                                               ; preds = %17
  %19 = load ptr, ptr @_ZL17g_sys_socket_func, align 8, !tbaa !9
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = invoke noundef i32 %19(i32 noundef %20, i32 noundef %21, i32 noundef %22)
          to label %24 unwind label %54

24:                                               ; preds = %18
  store i32 %23, ptr %4, align 4
  br label %52

25:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #7
  %26 = load ptr, ptr @_ZL17g_sys_socket_func, align 8, !tbaa !9
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = invoke noundef i32 %26(i32 noundef %27, i32 noundef %28, i32 noundef %29)
          to label %31 unwind label %54

31:                                               ; preds = %25
  store i32 %30, ptr %8, align 4, !tbaa !5
  %32 = load i32, ptr %8, align 4, !tbaa !5
  %33 = icmp slt i32 %32, 0
  br i1 %33, label %34, label %36

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

36:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #7
  %37 = load i32, ptr %8, align 4, !tbaa !5
  %38 = invoke noundef ptr @_ZL11alloc_by_fdi(i32 noundef %37)
          to label %39 unwind label %54

39:                                               ; preds = %36
  store ptr %38, ptr %10, align 8, !tbaa !9
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = load ptr, ptr %10, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.rpchook_t, ptr %41, i32 0, i32 2
  store i32 %40, ptr %42, align 4, !tbaa !11
  %43 = load i32, ptr %8, align 4, !tbaa !5
  %44 = load ptr, ptr @_ZL16g_sys_fcntl_func, align 8, !tbaa !9
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = invoke noundef i32 (i32, i32, ...) %44(i32 noundef %45, i32 noundef 3, i32 noundef 0)
          to label %47 unwind label %54

47:                                               ; preds = %39
  %48 = invoke i32 (i32, i32, ...) @fcntl(i32 noundef %43, i32 noundef 4, i32 noundef %46)
          to label %49 unwind label %54

49:                                               ; preds = %47
  %50 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %50, ptr %4, align 4
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #7
  br label %51

51:                                               ; preds = %49, %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #7
  br label %52

52:                                               ; preds = %51, %24
  %53 = load i32, ptr %4, align 4
  ret i32 %53

54:                                               ; preds = %47, %39, %36, %25, %18, %15
  %55 = landingpad { ptr, i32 }
          catch ptr null
  %56 = extractvalue { ptr, i32 } %55, 0
  call void @__clang_call_terminate(ptr %56) #8
  unreachable
}

declare noundef zeroext i1 @_Z21co_is_enable_sys_hookv() #2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: noinline noreturn nounwind uwtable
declare hidden void @__clang_call_terminate(ptr noundef) #3

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef ptr @_ZL11alloc_by_fdi(i32 noundef) #5

; Function Attrs: mustprogress uwtable
declare dso_local i32 @fcntl(i32 noundef, i32 noundef, ...) #6

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

attributes #0 = { nounwind "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 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 #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 = { noinline noreturn nounwind uwtable "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 #4 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { inlinehint mustprogress 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 #6 = { 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 #7 = { nounwind }
attributes #8 = { noreturn 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 20}
!12 = !{!"_ZTS9rpchook_t", !6, i64 0, !13, i64 4, !6, i64 20, !16, i64 24, !16, i64 40}
!13 = !{!"_ZTS11sockaddr_in", !14, i64 0, !14, i64 2, !15, i64 4, !7, i64 8}
!14 = !{!"short", !7, i64 0}
!15 = !{!"_ZTS7in_addr", !6, i64 0}
!16 = !{!"_ZTS7timeval", !17, i64 0, !17, i64 8}
!17 = !{!"long", !7, 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"

@_ZL17g_sys_socket_func = external hidden local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [7 x i8], align 1
@_ZL16g_sys_fcntl_func = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind
declare ptr @dlsym(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @socket(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 personality ptr @__gxx_personality_v0 {
  %4 = load ptr, ptr @_ZL17g_sys_socket_func, align 8, !tbaa !5
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %5, label %7

5:                                                ; preds = %3
  %6 = tail call ptr @dlsym(ptr noundef nonnull inttoptr (i64 -1 to ptr), ptr noundef nonnull @.str) #6
  store ptr %6, ptr @_ZL17g_sys_socket_func, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %5, %3
  %8 = invoke noundef zeroext i1 @_Z21co_is_enable_sys_hookv()
          to label %9 unwind label %25

9:                                                ; preds = %7
  %10 = load ptr, ptr @_ZL17g_sys_socket_func, align 8, !tbaa !5
  br i1 %8, label %13, label %11

11:                                               ; preds = %9
  %12 = invoke noundef i32 %10(i32 noundef %0, i32 noundef %1, i32 noundef %2)
          to label %24 unwind label %25

13:                                               ; preds = %9
  %14 = invoke noundef i32 %10(i32 noundef %0, i32 noundef %1, i32 noundef %2)
          to label %15 unwind label %25

15:                                               ; preds = %13
  %16 = icmp slt i32 %14, 0
  br i1 %16, label %24, label %17

17:                                               ; preds = %15
  %18 = tail call noundef ptr @_ZL11alloc_by_fdi(i32 noundef %14)
  %19 = getelementptr inbounds i8, ptr %18, i64 20
  store i32 %0, ptr %19, align 4, !tbaa !9
  %20 = load ptr, ptr @_ZL16g_sys_fcntl_func, align 8, !tbaa !5
  %21 = invoke noundef i32 (i32, i32, ...) %20(i32 noundef %14, i32 noundef 3, i32 noundef 0)
          to label %22 unwind label %25

22:                                               ; preds = %17
  %23 = invoke i32 (i32, i32, ...) @fcntl(i32 noundef %14, i32 noundef 4, i32 noundef %21)
          to label %24 unwind label %25

24:                                               ; preds = %15, %22, %11
  %.1 = phi i32 [ %12, %11 ], [ %14, %22 ], [ %14, %15 ]
  ret i32 %.1

25:                                               ; preds = %22, %17, %13, %11, %7
  %26 = landingpad { ptr, i32 }
          catch ptr null
  %27 = extractvalue { ptr, i32 } %26, 0
  tail call void @__clang_call_terminate(ptr %27) #7
  unreachable
}

declare noundef zeroext i1 @_Z21co_is_enable_sys_hookv() local_unnamed_addr #2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: noinline noreturn nounwind uwtable
declare hidden void @__clang_call_terminate(ptr noundef) local_unnamed_addr #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef ptr @_ZL11alloc_by_fdi(i32 noundef) local_unnamed_addr #4

; Function Attrs: mustprogress uwtable
declare dso_local i32 @fcntl(i32 noundef, i32 noundef, ...) local_unnamed_addr #5

attributes #0 = { nounwind "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 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 #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 = { noinline noreturn nounwind uwtable "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 #4 = { inlinehint mustprogress 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 #5 = { 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 #6 = { nounwind }
attributes #7 = { noreturn 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, !11, i64 20}
!10 = !{!"_ZTS9rpchook_t", !11, i64 0, !12, i64 4, !11, i64 20, !15, i64 24, !15, i64 40}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_ZTS11sockaddr_in", !13, i64 0, !13, i64 2, !14, i64 4, !7, i64 8}
!13 = !{!"short", !7, i64 0}
!14 = !{!"_ZTS7in_addr", !11, i64 0}
!15 = !{!"_ZTS7timeval", !16, i64 0, !16, i64 8}
!16 = !{!"long", !7, i64 0}


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