; LLVM IR from kala13x/libxutils :: src/sys (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"

%union.pthread_attr_t = type { i64, [48 x i8] }
%struct.XThread = type { ptr, ptr, i64, i32, i8, i8 }

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [54 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [68 x i8], align 1
@__FUNCTION__.XThread_Run = external hidden unnamed_addr constant [12 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [67 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [39 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @XThread_Run(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca %union.pthread_attr_t, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.XThread, ptr %6, i32 0, i32 0
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %11

10:                                               ; preds = %1
  store i32 0, ptr %2, align 4
  br label %66

11:                                               ; preds = %1
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.XThread, ptr %12, i32 0, i32 5
  store i8 -1, ptr %13, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 56, ptr %4) #4
  %14 = call i32 @pthread_attr_init(ptr noundef %4) #4
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %23, label %16

16:                                               ; preds = %11
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.XThread, ptr %17, i32 0, i32 3
  %19 = load i32, ptr %18, align 8, !tbaa !14
  %20 = zext i32 %19 to i64
  %21 = call i32 @pthread_attr_setstacksize(ptr noundef %4, i64 noundef %20) #4
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %29

23:                                               ; preds = %16, %11
  %24 = load ptr, ptr @stderr, align 8, !tbaa !5
  %25 = call ptr @__errno_location() #5
  %26 = load i32, ptr %25, align 4, !tbaa !15
  %27 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %24, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 47, ptr noundef @__FUNCTION__.XThread_Run, i32 noundef %26) #4
  %28 = call i32 @pthread_attr_destroy(ptr noundef %4) #4
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %65

29:                                               ; preds = %16
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.XThread, ptr %30, i32 0, i32 4
  %32 = load i8, ptr %31, align 4, !tbaa !16
  %33 = icmp ne i8 %32, 0
  br i1 %33, label %34, label %44

34:                                               ; preds = %29
  %35 = call i32 @pthread_attr_setdetachstate(ptr noundef %4, i32 noundef 1) #4
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %43

37:                                               ; preds = %34
  %38 = load ptr, ptr @stderr, align 8, !tbaa !5
  %39 = call ptr @__errno_location() #5
  %40 = load i32, ptr %39, align 4, !tbaa !15
  %41 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %38, ptr noundef @.str.2, ptr noundef @.str.1, i32 noundef 58, ptr noundef @__FUNCTION__.XThread_Run, i32 noundef %40) #4
  %42 = call i32 @pthread_attr_destroy(ptr noundef %4) #4
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %65

43:                                               ; preds = %34
  br label %44

44:                                               ; preds = %43, %29
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.XThread, ptr %45, i32 0, i32 2
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.XThread, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !9
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.XThread, ptr %50, i32 0, i32 1
  %52 = load ptr, ptr %51, align 8, !tbaa !17
  %53 = call i32 @pthread_create(ptr noundef %46, ptr noundef %4, ptr noundef %49, ptr noundef %52) #4
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %61

55:                                               ; preds = %44
  %56 = load ptr, ptr @stderr, align 8, !tbaa !5
  %57 = call ptr @__errno_location() #5
  %58 = load i32, ptr %57, align 4, !tbaa !15
  %59 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %56, ptr noundef @.str.3, ptr noundef @.str.1, i32 noundef 68, ptr noundef @__FUNCTION__.XThread_Run, i32 noundef %58) #4
  %60 = call i32 @pthread_attr_destroy(ptr noundef %4) #4
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %65

61:                                               ; preds = %44
  %62 = call i32 @pthread_attr_destroy(ptr noundef %4) #4
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.XThread, ptr %63, i32 0, i32 5
  store i8 1, ptr %64, align 1, !tbaa !13
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %65

65:                                               ; preds = %61, %55, %37, %23
  call void @llvm.lifetime.end.p0(i64 56, ptr %4) #4
  br label %66

66:                                               ; preds = %65, %10
  %67 = load i32, ptr %2, align 4
  ret i32 %67
}

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

; Function Attrs: nounwind
declare i32 @pthread_attr_init(ptr noundef) #2

; Function Attrs: nounwind
declare i32 @pthread_attr_setstacksize(ptr noundef, i64 noundef) #2

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #3

; Function Attrs: nounwind
declare i32 @pthread_attr_destroy(ptr noundef) #2

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

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

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

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 = { 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 willreturn memory(none) "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 }
attributes #5 = { nounwind willreturn memory(none) }

!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 0}
!10 = !{!"XThread", !6, i64 0, !6, i64 8, !11, i64 16, !12, i64 24, !7, i64 28, !7, i64 29}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !7, i64 29}
!14 = !{!10, !12, i64 24}
!15 = !{!12, !12, i64 0}
!16 = !{!10, !7, i64 28}
!17 = !{!10, !6, i64 8}


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

%union.pthread_attr_t = type { i64, [48 x i8] }

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [54 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [68 x i8], align 1
@__FUNCTION__.XThread_Run = external hidden unnamed_addr constant [12 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [67 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [39 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @XThread_Run(ptr noundef %0) local_unnamed_addr #0 {
  %2 = alloca %union.pthread_attr_t, align 8
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %45, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 29
  store i8 -1, ptr %6, align 1, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 56, ptr nonnull %2) #5
  %7 = call i32 @pthread_attr_init(ptr noundef nonnull %2) #5
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %13

8:                                                ; preds = %5
  %9 = getelementptr inbounds i8, ptr %0, i64 24
  %10 = load i32, ptr %9, align 8, !tbaa !13
  %11 = zext i32 %10 to i64
  %12 = call i32 @pthread_attr_setstacksize(ptr noundef nonnull %2, i64 noundef %11) #5
  %.not8 = icmp eq i32 %12, 0
  br i1 %.not8, label %19, label %13

13:                                               ; preds = %8, %5
  %14 = load ptr, ptr @stderr, align 8, !tbaa !14
  %15 = tail call ptr @__errno_location() #6
  %16 = load i32, ptr %15, align 4, !tbaa !15
  %17 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %14, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 47, ptr noundef nonnull @__FUNCTION__.XThread_Run, i32 noundef %16) #7
  %18 = call i32 @pthread_attr_destroy(ptr noundef nonnull %2) #5
  br label %44

19:                                               ; preds = %8
  %20 = getelementptr inbounds i8, ptr %0, i64 28
  %21 = load i8, ptr %20, align 4, !tbaa !16
  %.not9 = icmp eq i8 %21, 0
  br i1 %.not9, label %30, label %22

22:                                               ; preds = %19
  %23 = call i32 @pthread_attr_setdetachstate(ptr noundef nonnull %2, i32 noundef 1) #5
  %.not10 = icmp eq i32 %23, 0
  br i1 %.not10, label %30, label %24

24:                                               ; preds = %22
  %25 = load ptr, ptr @stderr, align 8, !tbaa !14
  %26 = tail call ptr @__errno_location() #6
  %27 = load i32, ptr %26, align 4, !tbaa !15
  %28 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %25, ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.1, i32 noundef 58, ptr noundef nonnull @__FUNCTION__.XThread_Run, i32 noundef %27) #7
  %29 = call i32 @pthread_attr_destroy(ptr noundef nonnull %2) #5
  br label %44

30:                                               ; preds = %22, %19
  %31 = getelementptr inbounds i8, ptr %0, i64 16
  %32 = load ptr, ptr %0, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %0, i64 8
  %34 = load ptr, ptr %33, align 8, !tbaa !17
  %35 = call i32 @pthread_create(ptr noundef nonnull %31, ptr noundef nonnull %2, ptr noundef %32, ptr noundef %34) #5
  %.not11 = icmp eq i32 %35, 0
  br i1 %.not11, label %42, label %36

36:                                               ; preds = %30
  %37 = load ptr, ptr @stderr, align 8, !tbaa !14
  %38 = tail call ptr @__errno_location() #6
  %39 = load i32, ptr %38, align 4, !tbaa !15
  %40 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %37, ptr noundef nonnull @.str.3, ptr noundef nonnull @.str.1, i32 noundef 68, ptr noundef nonnull @__FUNCTION__.XThread_Run, i32 noundef %39) #7
  %41 = call i32 @pthread_attr_destroy(ptr noundef nonnull %2) #5
  br label %44

42:                                               ; preds = %30
  %43 = call i32 @pthread_attr_destroy(ptr noundef nonnull %2) #5
  store i8 1, ptr %6, align 1, !tbaa !12
  br label %44

44:                                               ; preds = %42, %36, %24, %13
  %.0 = phi i32 [ -1, %13 ], [ -1, %24 ], [ -1, %36 ], [ 1, %42 ]
  call void @llvm.lifetime.end.p0(i64 56, ptr nonnull %2) #5
  br label %45

45:                                               ; preds = %1, %44
  %.1 = phi i32 [ %.0, %44 ], [ 0, %1 ]
  ret i32 %.1
}

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

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

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

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #4

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

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

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

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nofree 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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 = { nounwind }
attributes #6 = { nounwind willreturn memory(none) }
attributes #7 = { cold 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, !7, i64 0}
!6 = !{!"XThread", !7, i64 0, !7, i64 8, !10, i64 16, !11, i64 24, !8, i64 28, !8, i64 29}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !8, i64 29}
!13 = !{!6, !11, i64 24}
!14 = !{!7, !7, i64 0}
!15 = !{!11, !11, i64 0}
!16 = !{!6, !8, i64 28}
!17 = !{!6, !7, i64 8}


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