; LLVM IR from Aeternum-Veritas/Merry :: subsystest (subsys)

; 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.QueueItem = type { i64, [4 x i64] }

@c = external hidden global ptr, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @simple_subsys(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.QueueItem, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %9 = call ptr (...) @lock_init()
  store ptr %9, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %10 = call ptr (...) @merry_thread_init()
  store ptr %10, ptr %5, align 8, !tbaa !5
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = call zeroext i8 @merry_create_detached_thread(ptr noundef %11, ptr noundef @queue_manager, ptr noundef null)
  br label %13

13:                                               ; preds = %51, %1
  br label %14

14:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 40, ptr %6) #3
  %15 = call zeroext i8 @queue_get_request(ptr noundef %6)
  %16 = zext i8 %15 to i32
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %14
  %19 = load ptr, ptr @c, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  call void @merry_cond_wait(ptr noundef %19, ptr noundef %20)
  br label %48

21:                                               ; preds = %14
  %22 = getelementptr inbounds %struct.QueueItem, ptr %6, i32 0, i32 0
  %23 = load i64, ptr %22, align 8, !tbaa !9
  switch i64 %23, label %47 [
    i64 0, label %24
    i64 2, label %27
  ]

24:                                               ; preds = %21
  call void (...) @queue_manager_signal()
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  call void @lock_destroy(ptr noundef %25)
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  call void @merry_thread_destroy(ptr noundef %26)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %49

27:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %28 = getelementptr inbounds %struct.QueueItem, ptr %6, i32 0, i32 1
  %29 = getelementptr inbounds [4 x i64], ptr %28, i64 0, i64 0
  %30 = load i64, ptr %29, align 8, !tbaa !12
  %31 = getelementptr inbounds %struct.QueueItem, ptr %6, i32 0, i32 1
  %32 = getelementptr inbounds [4 x i64], ptr %31, i64 0, i64 1
  %33 = load i64, ptr %32, align 8, !tbaa !12
  %34 = add i64 %30, %33
  %35 = getelementptr inbounds %struct.QueueItem, ptr %6, i32 0, i32 1
  %36 = getelementptr inbounds [4 x i64], ptr %35, i64 0, i64 2
  %37 = load i64, ptr %36, align 8, !tbaa !12
  %38 = add i64 %34, %37
  %39 = getelementptr inbounds %struct.QueueItem, ptr %6, i32 0, i32 1
  %40 = getelementptr inbounds [4 x i64], ptr %39, i64 0, i64 3
  %41 = load i64, ptr %40, align 8, !tbaa !12
  %42 = add i64 %38, %41
  store i64 %42, ptr %8, align 8, !tbaa !12
  %43 = getelementptr inbounds %struct.QueueItem, ptr %6, i32 0, i32 0
  %44 = load i64, ptr %43, align 8, !tbaa !9
  %45 = load i64, ptr %8, align 8, !tbaa !12
  %46 = call zeroext i8 @queue_send_reply(i64 noundef %44, i64 noundef %45)
  store i32 4, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %47

47:                                               ; preds = %27, %21
  br label %48

48:                                               ; preds = %47, %18
  store i32 0, ptr %7, align 4
  br label %49

49:                                               ; preds = %48, %24
  call void @llvm.lifetime.end.p0(i64 40, ptr %6) #3
  %50 = load i32, ptr %7, align 4
  switch i32 %50, label %52 [
    i32 0, label %51
  ]

51:                                               ; preds = %49
  br label %13

52:                                               ; preds = %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %53 = load ptr, ptr %2, align 8
  ret ptr %53
}

declare ptr @lock_init(...) #2

declare ptr @merry_thread_init(...) #2

declare zeroext i8 @merry_create_detached_thread(ptr noundef, ptr noundef, ptr noundef) #2

declare ptr @queue_manager(ptr noundef) #2

declare zeroext i8 @queue_get_request(ptr noundef) #2

declare void @merry_cond_wait(ptr noundef, ptr noundef) #2

declare void @queue_manager_signal(...) #2

declare void @lock_destroy(ptr noundef) #2

declare void @merry_thread_destroy(ptr noundef) #2

declare zeroext i8 @queue_send_reply(i64 noundef, i64 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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, !11, i64 0}
!10 = !{!"QueueItem", !11, i64 0, !7, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!11, !11, 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.QueueItem = type { i64, [4 x i64] }

@c = external hidden local_unnamed_addr global ptr, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @simple_subsys(ptr nocapture noundef readnone %0) local_unnamed_addr #1 {
  %2 = alloca %struct.QueueItem, align 8
  %3 = tail call ptr (...) @lock_init() #4
  %4 = tail call ptr (...) @merry_thread_init() #4
  %5 = tail call zeroext i8 @merry_create_detached_thread(ptr noundef %4, ptr noundef nonnull @queue_manager, ptr noundef null) #4
  %6 = getelementptr inbounds i8, ptr %2, i64 8
  br label %7

7:                                                ; preds = %18, %1
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %2) #4
  %8 = call zeroext i8 @queue_get_request(ptr noundef nonnull %2) #4
  %9 = icmp eq i8 %8, 0
  br i1 %9, label %10, label %12

10:                                               ; preds = %7
  %11 = load ptr, ptr @c, align 8, !tbaa !5
  call void @merry_cond_wait(ptr noundef %11, ptr noundef %3) #4
  br label %18

12:                                               ; preds = %7
  %13 = load i64, ptr %2, align 8, !tbaa !9
  switch i64 %13, label %18 [
    i64 0, label %.critedge
    i64 2, label %14
  ]

.critedge:                                        ; preds = %12
  call void (...) @queue_manager_signal() #4
  call void @lock_destroy(ptr noundef %3) #4
  call void @merry_thread_destroy(ptr noundef %4) #4
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %2) #4
  ret ptr null

14:                                               ; preds = %12
  %15 = load <4 x i64>, ptr %6, align 8, !tbaa !12
  %16 = call i64 @llvm.vector.reduce.add.v4i64(<4 x i64> %15)
  %17 = call zeroext i8 @queue_send_reply(i64 noundef 2, i64 noundef %16) #4
  br label %18

18:                                               ; preds = %10, %14, %12
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %2) #4
  br label %7
}

declare ptr @lock_init(...) local_unnamed_addr #2

declare ptr @merry_thread_init(...) local_unnamed_addr #2

declare zeroext i8 @merry_create_detached_thread(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare ptr @queue_manager(ptr noundef) #2

declare zeroext i8 @queue_get_request(ptr noundef) local_unnamed_addr #2

declare void @merry_cond_wait(ptr noundef, ptr noundef) local_unnamed_addr #2

declare void @queue_manager_signal(...) local_unnamed_addr #2

declare void @lock_destroy(ptr noundef) local_unnamed_addr #2

declare void @merry_thread_destroy(ptr noundef) local_unnamed_addr #2

declare zeroext i8 @queue_send_reply(i64 noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v4i64(<4 x i64>) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !11, i64 0}
!10 = !{!"QueueItem", !11, i64 0, !7, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!11, !11, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass