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

; 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.ThreadInfo = type { i32, i64, ptr, ptr, ptr, ptr }

@morn_thread_ok = external dso_local global i32, align 4
@.str.1 = external hidden unnamed_addr constant [77 x i8], align 1
@__FUNCTION__.m_Thread = external hidden unnamed_addr constant [9 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [20 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @ThreadList() #0

; 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

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

; Function Attrs: nounwind uwtable
declare dso_local void @ThreadFuc(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @m_Thread(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  store i32 0, ptr @morn_thread_ok, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %11 = call ptr @ThreadList()
  store ptr %11, ptr %7, align 8, !tbaa !5
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = ptrtoint ptr %12 to i64
  %14 = icmp slt i64 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %3
  store ptr null, ptr %6, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %15, %3
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %21

19:                                               ; preds = %16
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 0, ptr %20, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %19, %16
  br label %22

22:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = icmp eq ptr %23, null
  br i1 %24, label %29, label %25

25:                                               ; preds = %22
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = ptrtoint ptr %26 to i64
  %28 = icmp eq i64 %27, -1
  br label %29

29:                                               ; preds = %25, %22
  %30 = phi i1 [ true, %22 ], [ %28, %25 ]
  %31 = zext i1 %30 to i32
  store i32 %31, ptr %8, align 4, !tbaa !9
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %36

34:                                               ; preds = %29
  %35 = load i32, ptr %8, align 4, !tbaa !9
  call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef %35, i32 noundef -1, ptr noundef @.str.1, i32 noundef 126, ptr noundef @__FUNCTION__.m_Thread, ptr noundef @.str.2)
  br label %36

36:                                               ; preds = %34, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %37

37:                                               ; preds = %36
  br label %38

38:                                               ; preds = %37
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = ptrtoint ptr %39 to i64
  %41 = icmp eq i64 %40, -1
  br i1 %41, label %42, label %43

42:                                               ; preds = %38
  store ptr null, ptr %6, align 8, !tbaa !5
  br label %43

43:                                               ; preds = %42, %38
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = ptrtoint ptr %44 to i64
  %46 = icmp eq i64 %45, -1
  br i1 %46, label %47, label %48

47:                                               ; preds = %43
  store ptr null, ptr %5, align 8, !tbaa !5
  br label %48

48:                                               ; preds = %47, %43
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = call ptr @mListWrite(ptr noundef %49, i32 noundef -1, ptr noundef null, i32 noundef 48)
  store ptr %50, ptr %9, align 8, !tbaa !5
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.ThreadInfo, ptr %52, i32 0, i32 2
  store ptr %51, ptr %53, align 8, !tbaa !11
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = load ptr, ptr %9, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.ThreadInfo, ptr %55, i32 0, i32 3
  store ptr %54, ptr %56, align 8, !tbaa !14
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.ThreadInfo, ptr %58, i32 0, i32 4
  store ptr %57, ptr %59, align 8, !tbaa !15
  %60 = load ptr, ptr %9, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.ThreadInfo, ptr %60, i32 0, i32 5
  store ptr null, ptr %61, align 8, !tbaa !16
  br label %62

62:                                               ; preds = %48
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.ThreadInfo, ptr %63, i32 0, i32 1
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = call i32 @pthread_create(ptr noundef %64, ptr noundef null, ptr noundef @ThreadFuc, ptr noundef %65) #4
  %67 = icmp ne i32 %66, 0
  %68 = zext i1 %67 to i32
  store i32 %68, ptr %10, align 4, !tbaa !9
  %69 = load i32, ptr %10, align 4, !tbaa !9
  %70 = icmp ne i32 %69, 0
  br i1 %70, label %71, label %73

71:                                               ; preds = %62
  %72 = load i32, ptr %10, align 4, !tbaa !9
  call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef %72, i32 noundef -1, ptr noundef @.str.1, i32 noundef 131, ptr noundef @__FUNCTION__.m_Thread, ptr noundef @.str.3)
  br label %73

73:                                               ; preds = %71, %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %74

74:                                               ; preds = %73
  br label %75

75:                                               ; preds = %74
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret void
}

declare void @m_Exception(i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ...) #2

; Function Attrs: nounwind
declare i32 @pthread_create(ptr noundef, ptr noundef, 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 = { "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 "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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"ThreadInfo", !10, i64 0, !13, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !6, i64 24}
!15 = !{!12, !6, i64 32}
!16 = !{!12, !6, i64 40}


; 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_thread_ok = external dso_local local_unnamed_addr global i32, align 4
@.str.1 = external hidden unnamed_addr constant [77 x i8], align 1
@__FUNCTION__.m_Thread = external hidden unnamed_addr constant [9 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [20 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @ThreadList() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local void @ThreadFuc(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @m_Thread(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  store i32 0, ptr @morn_thread_ok, align 4, !tbaa !5
  %4 = tail call ptr @ThreadList()
  %5 = icmp slt ptr %2, null
  %spec.store.select = select i1 %5, ptr null, ptr %2
  %.not = icmp eq ptr %spec.store.select, null
  br i1 %.not, label %7, label %6

6:                                                ; preds = %3
  store i32 0, ptr %spec.store.select, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %3, %6
  %magicptr = ptrtoint ptr %0 to i64
  switch i64 %magicptr, label %13 [
    i64 -1, label %8
    i64 0, label %8
  ]

8:                                                ; preds = %7, %7
  %9 = icmp eq ptr %0, null
  %10 = icmp eq ptr %0, inttoptr (i64 -1 to ptr)
  %11 = or i1 %9, %10
  %12 = zext i1 %11 to i32
  tail call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef %12, i32 noundef -1, ptr noundef nonnull @.str.1, i32 noundef 126, ptr noundef nonnull @__FUNCTION__.m_Thread, ptr noundef nonnull @.str.2) #3
  br label %13

13:                                               ; preds = %7, %8
  %14 = icmp eq ptr %1, inttoptr (i64 -1 to ptr)
  %spec.store.select1 = select i1 %14, ptr null, ptr %1
  %15 = tail call ptr @mListWrite(ptr noundef %4, i32 noundef -1, ptr noundef null, i32 noundef 48) #3
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  store ptr %0, ptr %16, align 8, !tbaa !9
  %17 = getelementptr inbounds i8, ptr %15, i64 24
  store ptr %spec.store.select1, ptr %17, align 8, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %15, i64 32
  store ptr %spec.store.select, ptr %18, align 8, !tbaa !14
  %19 = getelementptr inbounds i8, ptr %15, i64 40
  store ptr null, ptr %19, align 8, !tbaa !15
  %20 = getelementptr inbounds i8, ptr %15, i64 8
  %21 = tail call i32 @pthread_create(ptr noundef nonnull %20, ptr noundef null, ptr noundef nonnull @ThreadFuc, ptr noundef %15) #3
  %.not22 = icmp eq i32 %21, 0
  br i1 %.not22, label %23, label %22

22:                                               ; preds = %13
  tail call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef 1, i32 noundef -1, ptr noundef nonnull @.str.1, i32 noundef 131, ptr noundef nonnull @__FUNCTION__.m_Thread, ptr noundef nonnull @.str.3) #3
  br label %23

23:                                               ; preds = %22, %13
  ret void
}

declare void @m_Exception(i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ...) local_unnamed_addr #1

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

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 = { "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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 16}
!10 = !{!"ThreadInfo", !6, i64 0, !11, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!10, !12, i64 24}
!14 = !{!10, !12, i64 32}
!15 = !{!10, !12, i64 40}


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