; LLVM IR from narke/Aragveli :: src/kernel/process (mutex)

; 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.mutex_t = type { ptr, i64, %struct.anon }
%struct.anon = type { ptr, ptr }
%struct.thread = type { i32, i64, [32 x i8], i32, ptr, i8, %struct.anon.0 }
%struct.anon.0 = type { ptr, ptr }

; 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

declare ptr @thread_get_current() #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mutex_unlock(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.mutex_t, ptr %5, i32 0, i32 1
  %7 = load volatile i64, ptr %6, align 8, !tbaa !9
  call void @atomic_dec(i64 noundef %7)
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.mutex_t, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = call ptr @thread_get_current()
  %12 = icmp ne ptr %10, %11
  br i1 %12, label %13, label %14

13:                                               ; preds = %1
  store i32 -6, ptr %3, align 4, !tbaa !14
  br label %72

14:                                               ; preds = %1
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.mutex_t, ptr %15, i32 0, i32 2
  %17 = getelementptr inbounds %struct.anon, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %17, align 8, !tbaa !16
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %23

20:                                               ; preds = %14
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mutex_t, ptr %21, i32 0, i32 0
  store ptr null, ptr %22, align 8, !tbaa !13
  store i32 0, ptr %3, align 4, !tbaa !14
  br label %71

23:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.mutex_t, ptr %24, i32 0, i32 2
  %26 = getelementptr inbounds %struct.anon, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !16
  store ptr %27, ptr %4, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %23
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.thread, ptr %29, i32 0, i32 6
  %31 = getelementptr inbounds %struct.anon.0, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !17
  %33 = icmp ne ptr %32, null
  br i1 %33, label %34, label %45

34:                                               ; preds = %28
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.thread, ptr %35, i32 0, i32 6
  %37 = getelementptr inbounds %struct.anon.0, ptr %36, i32 0, i32 1
  %38 = load ptr, ptr %37, align 8, !tbaa !19
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.thread, ptr %39, i32 0, i32 6
  %41 = getelementptr inbounds %struct.anon.0, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !17
  %43 = getelementptr inbounds %struct.thread, ptr %42, i32 0, i32 6
  %44 = getelementptr inbounds %struct.anon.0, ptr %43, i32 0, i32 1
  store ptr %38, ptr %44, align 8, !tbaa !19
  br label %53

45:                                               ; preds = %28
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.thread, ptr %46, i32 0, i32 6
  %48 = getelementptr inbounds %struct.anon.0, ptr %47, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !19
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.mutex_t, ptr %50, i32 0, i32 2
  %52 = getelementptr inbounds %struct.anon, ptr %51, i32 0, i32 1
  store ptr %49, ptr %52, align 8, !tbaa !20
  br label %53

53:                                               ; preds = %45, %34
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.thread, ptr %54, i32 0, i32 6
  %56 = getelementptr inbounds %struct.anon.0, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !17
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.thread, ptr %58, i32 0, i32 6
  %60 = getelementptr inbounds %struct.anon.0, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !19
  store ptr %57, ptr %61, align 8, !tbaa !5
  br label %62

62:                                               ; preds = %53
  br label %63

63:                                               ; preds = %62
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.thread, ptr %64, i32 0, i32 3
  %66 = load i32, ptr %65, align 8, !tbaa !21
  %67 = icmp ne i32 %66, 2
  br i1 %67, label %68, label %70

68:                                               ; preds = %63
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  call void @scheduler_insert_thread(ptr noundef %69)
  br label %70

70:                                               ; preds = %68, %63
  store i32 0, ptr %3, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %71

71:                                               ; preds = %70, %20
  br label %72

72:                                               ; preds = %71, %13
  %73 = load i32, ptr %3, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret i32 %73
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @atomic_dec(i64 noundef) #3

declare void @scheduler_insert_thread(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #3 = { inlinehint 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 #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 8}
!10 = !{!"", !6, i64 0, !11, i64 8, !12, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = !{!"", !6, i64 0, !6, i64 8}
!13 = !{!10, !6, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!10, !6, i64 16}
!17 = !{!18, !6, i64 72}
!18 = !{!"thread", !15, i64 0, !11, i64 8, !7, i64 16, !15, i64 48, !6, i64 56, !7, i64 64, !12, i64 72}
!19 = !{!18, !6, i64 80}
!20 = !{!10, !6, i64 24}
!21 = !{!18, !15, i64 48}


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

declare ptr @thread_get_current() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -6, 1) i32 @mutex_unlock(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load volatile i64, ptr %2, align 8, !tbaa !5
  tail call void @atomic_dec(i64 noundef %3)
  %4 = load ptr, ptr %0, align 8, !tbaa !12
  %5 = tail call ptr @thread_get_current() #3
  %.not = icmp eq ptr %4, %5
  br i1 %.not, label %6, label %21

6:                                                ; preds = %1
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = load ptr, ptr %7, align 8, !tbaa !13
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %11

10:                                               ; preds = %6
  store ptr null, ptr %0, align 8, !tbaa !12
  br label %21

11:                                               ; preds = %6
  %12 = getelementptr inbounds i8, ptr %8, i64 72
  %13 = load ptr, ptr %12, align 8, !tbaa !14
  %.not16 = icmp eq ptr %13, null
  %14 = getelementptr inbounds i8, ptr %8, i64 80
  %15 = load ptr, ptr %14, align 8, !tbaa !17
  %16 = getelementptr inbounds i8, ptr %0, i64 24
  %17 = getelementptr inbounds i8, ptr %13, i64 80
  %.sink = select i1 %.not16, ptr %16, ptr %17
  store ptr %15, ptr %.sink, align 8, !tbaa !18
  store ptr %13, ptr %15, align 8, !tbaa !18
  %18 = getelementptr inbounds i8, ptr %8, i64 48
  %19 = load i32, ptr %18, align 8, !tbaa !19
  %.not17 = icmp eq i32 %19, 2
  br i1 %.not17, label %21, label %20

20:                                               ; preds = %11
  tail call void @scheduler_insert_thread(ptr noundef nonnull %8) #3
  br label %21

21:                                               ; preds = %11, %20, %1, %10
  %.0 = phi i32 [ 0, %10 ], [ -6, %1 ], [ 0, %20 ], [ 0, %11 ]
  ret i32 %.0
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @atomic_dec(i64 noundef) local_unnamed_addr #2

declare void @scheduler_insert_thread(ptr noundef) local_unnamed_addr #0

attributes #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 = { 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 = { inlinehint 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 #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, !10, i64 8}
!6 = !{!"", !7, i64 0, !10, i64 8, !11, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"", !7, i64 0, !7, i64 8}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !7, i64 16}
!14 = !{!15, !7, i64 72}
!15 = !{!"thread", !16, i64 0, !10, i64 8, !8, i64 16, !16, i64 48, !7, i64 56, !8, i64 64, !11, i64 72}
!16 = !{!"int", !8, i64 0}
!17 = !{!15, !7, i64 80}
!18 = !{!7, !7, i64 0}
!19 = !{!15, !16, i64 48}


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