; LLVM IR from jingweizhanghuai/Morn :: src/ptc (morn_coroutine)

; 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.HandleCoroutine = type { ptr, ptr, ptr }
%struct.CoroutineInfo = type { ptr, ptr, %struct.ucontext_t, i32, ptr, ptr, ptr }
%struct.ucontext_t = type { i64, ptr, %struct.stack_t, %struct.mcontext_t, %struct.__sigset_t, %struct._libc_fpstate, [4 x i64] }
%struct.stack_t = type { ptr, i32, i64 }
%struct.mcontext_t = type { [23 x i64], ptr, [8 x i64] }
%struct.__sigset_t = type { [16 x i64] }
%struct._libc_fpstate = type { i16, i16, i16, i16, i64, i64, i32, i32, [8 x %struct._libc_fpxreg], [16 x %struct._libc_xmmreg], [24 x i32] }
%struct._libc_fpxreg = type { [4 x i16], i16, [3 x i16] }
%struct._libc_xmmreg = type { [4 x i32] }

@morn_coroutine_handle = external dso_local thread_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @endCoroutineFunc() #0 {
  %1 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  %2 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @morn_coroutine_handle)
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %3, ptr %1, align 8, !tbaa !5
  %4 = load ptr, ptr %1, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.HandleCoroutine, ptr %4, i32 0, i32 1
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = load ptr, ptr %1, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.HandleCoroutine, ptr %7, i32 0, i32 2
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = getelementptr inbounds %struct.CoroutineInfo, ptr %9, i32 0, i32 2
  %11 = call i32 @swapcontext(ptr noundef %6, ptr noundef %10) #4
  br label %12

12:                                               ; preds = %39, %0
  br label %13

13:                                               ; preds = %12
  %14 = load ptr, ptr %1, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.HandleCoroutine, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %39

18:                                               ; preds = %13
  %19 = load ptr, ptr %1, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.HandleCoroutine, ptr %19, i32 0, i32 2
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %22 = getelementptr inbounds %struct.CoroutineInfo, ptr %21, i32 0, i32 4
  %23 = load ptr, ptr %22, align 8, !tbaa !12
  store i32 1, ptr %23, align 4, !tbaa !22
  %24 = load ptr, ptr %1, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.HandleCoroutine, ptr %24, i32 0, i32 2
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  %27 = getelementptr inbounds %struct.CoroutineInfo, ptr %26, i32 0, i32 5
  %28 = load ptr, ptr %27, align 8, !tbaa !23
  %29 = load ptr, ptr %1, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.HandleCoroutine, ptr %29, i32 0, i32 2
  store ptr %28, ptr %30, align 8, !tbaa !11
  %31 = load ptr, ptr %1, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.HandleCoroutine, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !9
  %34 = load ptr, ptr %1, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.HandleCoroutine, ptr %34, i32 0, i32 2
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  %37 = getelementptr inbounds %struct.CoroutineInfo, ptr %36, i32 0, i32 2
  %38 = call i32 @swapcontext(ptr noundef %33, ptr noundef %37) #4
  br label %39

39:                                               ; preds = %18, %13
  br label %12
}

; 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 speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #2

; Function Attrs: nounwind
declare i32 @swapcontext(ptr noundef, ptr noundef) #3

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 #4 = { 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 8}
!10 = !{!"HandleCoroutine", !6, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!10, !6, i64 16}
!12 = !{!13, !6, i64 992}
!13 = !{!"CoroutineInfo", !6, i64 0, !6, i64 8, !14, i64 16, !17, i64 984, !6, i64 992, !6, i64 1000, !6, i64 1008}
!14 = !{!"ucontext_t", !15, i64 0, !6, i64 8, !16, i64 16, !18, i64 40, !19, i64 296, !20, i64 424, !7, i64 936}
!15 = !{!"long", !7, i64 0}
!16 = !{!"", !6, i64 0, !17, i64 8, !15, i64 16}
!17 = !{!"int", !7, i64 0}
!18 = !{!"", !7, i64 0, !6, i64 184, !7, i64 192}
!19 = !{!"", !7, i64 0}
!20 = !{!"_libc_fpstate", !21, i64 0, !21, i64 2, !21, i64 4, !21, i64 6, !15, i64 8, !15, i64 16, !17, i64 24, !17, i64 28, !7, i64 32, !7, i64 160, !7, i64 416}
!21 = !{!"short", !7, i64 0}
!22 = !{!17, !17, i64 0}
!23 = !{!13, !6, i64 1000}


; 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"

@morn_coroutine_handle = external dso_local thread_local local_unnamed_addr global ptr, align 8

; Function Attrs: noreturn nounwind uwtable
define dso_local void @endCoroutineFunc() local_unnamed_addr #0 {
  %1 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @morn_coroutine_handle)
  %2 = load ptr, ptr %1, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 8
  %4 = load ptr, ptr %3, align 8, !tbaa !9
  %5 = getelementptr inbounds i8, ptr %2, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  %8 = tail call i32 @swapcontext(ptr noundef %4, ptr noundef nonnull %7) #3
  %9 = load ptr, ptr %5, align 8, !tbaa !11
  %10 = icmp eq ptr %9, null
  br i1 %10, label %.split.us, label %.split

.split.us:                                        ; preds = %0, %.split.us
  br label %.split.us

.split:                                           ; preds = %0, %.split.backedge
  %.pre8 = phi ptr [ %.pre8.be, %.split.backedge ], [ %9, %0 ]
  %.not = icmp eq ptr %.pre8, null
  br i1 %.not, label %.split.backedge, label %11

11:                                               ; preds = %.split
  %12 = getelementptr inbounds i8, ptr %.pre8, i64 992
  %13 = load ptr, ptr %12, align 8, !tbaa !12
  store i32 1, ptr %13, align 4, !tbaa !22
  %14 = getelementptr inbounds i8, ptr %.pre8, i64 1000
  %15 = load ptr, ptr %14, align 8, !tbaa !23
  store ptr %15, ptr %5, align 8, !tbaa !11
  %16 = load ptr, ptr %3, align 8, !tbaa !9
  %17 = getelementptr inbounds i8, ptr %15, i64 16
  %18 = tail call i32 @swapcontext(ptr noundef %16, ptr noundef nonnull %17) #3
  %.pre.pre = load ptr, ptr %5, align 8, !tbaa !11
  br label %.split.backedge

.split.backedge:                                  ; preds = %11, %.split
  %.pre8.be = phi ptr [ %.pre.pre, %11 ], [ null, %.split ]
  br label %.split, !llvm.loop !24
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #1

; Function Attrs: nounwind
declare i32 @swapcontext(ptr noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { noreturn 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 speculatable willreturn memory(none) }
attributes #2 = { 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 #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/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"HandleCoroutine", !6, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!10, !6, i64 16}
!12 = !{!13, !6, i64 992}
!13 = !{!"CoroutineInfo", !6, i64 0, !6, i64 8, !14, i64 16, !17, i64 984, !6, i64 992, !6, i64 1000, !6, i64 1008}
!14 = !{!"ucontext_t", !15, i64 0, !6, i64 8, !16, i64 16, !18, i64 40, !19, i64 296, !20, i64 424, !7, i64 936}
!15 = !{!"long", !7, i64 0}
!16 = !{!"", !6, i64 0, !17, i64 8, !15, i64 16}
!17 = !{!"int", !7, i64 0}
!18 = !{!"", !7, i64 0, !6, i64 184, !7, i64 192}
!19 = !{!"", !7, i64 0}
!20 = !{!"_libc_fpstate", !21, i64 0, !21, i64 2, !21, i64 4, !21, i64 6, !15, i64 8, !15, i64 16, !17, i64 24, !17, i64 28, !7, i64 32, !7, i64 160, !7, i64 416}
!21 = !{!"short", !7, i64 0}
!22 = !{!17, !17, i64 0}
!23 = !{!13, !6, i64 1000}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.unswitch.partial.disable"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass,TailCallElimPass