; LLVM IR from goldsborough/ipc-bench :: source/other (pthread-test)

; 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_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.Task = type { i32, i32 }

@mutex = external dso_local global %union.pthread_mutex_t, align 8
@sum = external dso_local global i32, align 4
@array = external dso_local global [10 x i32], align 16

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

; 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_mutex_lock(ptr noundef) #2

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

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

declare i32 @pthread_join(i64 noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local void @recursive_addition(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca %struct.Task, align 4
  %5 = alloca %struct.Task, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.Task, ptr %6, i32 0, i32 1
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.Task, ptr %9, i32 0, i32 0
  %11 = load i32, ptr %10, align 4, !tbaa !12
  %12 = sub nsw i32 %8, %11
  %13 = icmp sle i32 %12, 2
  br i1 %13, label %14, label %40

14:                                               ; preds = %1
  br label %15

15:                                               ; preds = %34, %14
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.Task, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 4, !tbaa !12
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.Task, ptr %19, i32 0, i32 1
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = icmp slt i32 %18, %21
  br i1 %22, label %23, label %39

23:                                               ; preds = %15
  %24 = call i32 @pthread_mutex_lock(ptr noundef @mutex) #4
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.Task, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 4, !tbaa !12
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [10 x i32], ptr @array, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !13
  %31 = load i32, ptr @sum, align 4, !tbaa !13
  %32 = add nsw i32 %31, %30
  store i32 %32, ptr @sum, align 4, !tbaa !13
  %33 = call i32 @pthread_mutex_unlock(ptr noundef @mutex) #4
  br label %34

34:                                               ; preds = %23
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.Task, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 4, !tbaa !12
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %36, align 4, !tbaa !12
  br label %15, !llvm.loop !14

39:                                               ; preds = %15
  br label %67

40:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %41 = getelementptr inbounds %struct.Task, ptr %4, i32 0, i32 0
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.Task, ptr %42, i32 0, i32 0
  %44 = load i32, ptr %43, align 4, !tbaa !12
  store i32 %44, ptr %41, align 4, !tbaa !12
  %45 = getelementptr inbounds %struct.Task, ptr %4, i32 0, i32 1
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.Task, ptr %46, i32 0, i32 0
  %48 = load i32, ptr %47, align 4, !tbaa !12
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.Task, ptr %49, i32 0, i32 1
  %51 = load i32, ptr %50, align 4, !tbaa !9
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.Task, ptr %52, i32 0, i32 0
  %54 = load i32, ptr %53, align 4, !tbaa !12
  %55 = sub nsw i32 %51, %54
  %56 = sdiv i32 %55, 2
  %57 = add nsw i32 %48, %56
  store i32 %57, ptr %45, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %58 = getelementptr inbounds %struct.Task, ptr %5, i32 0, i32 0
  %59 = getelementptr inbounds %struct.Task, ptr %4, i32 0, i32 1
  %60 = load i32, ptr %59, align 4, !tbaa !9
  store i32 %60, ptr %58, align 4, !tbaa !12
  %61 = getelementptr inbounds %struct.Task, ptr %5, i32 0, i32 1
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.Task, ptr %62, i32 0, i32 1
  %64 = load i32, ptr %63, align 4, !tbaa !9
  store i32 %64, ptr %61, align 4, !tbaa !9
  call void @create_thread(ptr noundef %3, ptr noundef @recursive_addition, ptr noundef %4)
  call void @recursive_addition(ptr noundef %5)
  %65 = load i64, ptr %3, align 8, !tbaa !16
  %66 = call i32 @pthread_join(i64 noundef %65, ptr noundef null)
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  br label %67

67:                                               ; preds = %40, %39
  ret void
}

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 = { "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, !11, i64 4}
!10 = !{!"Task", !11, i64 0, !11, i64 4}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!11, !11, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !17, i64 0}
!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"

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.Task = type { i32, i32 }

@mutex = external dso_local global %union.pthread_mutex_t, align 8
@sum = external dso_local local_unnamed_addr global i32, align 4
@array = external dso_local local_unnamed_addr global [10 x i32], align 16

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

; 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_mutex_lock(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(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

declare i32 @pthread_join(i64 noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local void @recursive_addition(ptr nocapture noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca %struct.Task, align 4
  %4 = alloca %struct.Task, align 4
  %5 = getelementptr inbounds i8, ptr %0, i64 4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = load i32, ptr %0, align 4, !tbaa !10
  %8 = sub nsw i32 %6, %7
  %9 = icmp slt i32 %8, 3
  br i1 %9, label %.preheader, label %23

.preheader:                                       ; preds = %1
  %10 = icmp slt i32 %7, %6
  br i1 %10, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %11 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @mutex) #4
  %12 = load i32, ptr %0, align 4, !tbaa !10
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [10 x i32], ptr @array, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = load i32, ptr @sum, align 4, !tbaa !11
  %17 = add nsw i32 %16, %15
  store i32 %17, ptr @sum, align 4, !tbaa !11
  %18 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull @mutex) #4
  %19 = load i32, ptr %0, align 4, !tbaa !10
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %0, align 4, !tbaa !10
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %.lr.ph, label %.loopexit, !llvm.loop !12

23:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #4
  store i32 %7, ptr %3, align 4, !tbaa !10
  %24 = getelementptr inbounds i8, ptr %3, i64 4
  %25 = lshr i32 %8, 1
  %26 = add nsw i32 %25, %7
  store i32 %26, ptr %24, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #4
  store i32 %26, ptr %4, align 4, !tbaa !10
  %27 = getelementptr inbounds i8, ptr %4, i64 4
  store i32 %6, ptr %27, align 4, !tbaa !5
  call void @create_thread(ptr noundef nonnull %2, ptr noundef nonnull @recursive_addition, ptr noundef nonnull %3)
  call void @recursive_addition(ptr noundef nonnull %4)
  %28 = load i64, ptr %2, align 8, !tbaa !14
  %29 = call i32 @pthread_join(i64 noundef %28, ptr noundef null) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #4
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %.preheader, %23
  ret void
}

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 = { "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, !7, i64 4}
!6 = !{!"Task", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !15, i64 0}
!15 = !{!"long", !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass