; LLVM IR from rociodellasala/TP3_SO :: Kernel (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.kernelHeapHeader = type { i32, i32, [10 x i32], [50 x %struct.ProcessSlot], i32, [10 x i32], [100 x %struct.ThreadSlot], i32, [100 x %struct.s_mutex], i32, [10 x i32], [500 x %struct.s_pipe], i32, [10 x i32], [1000 x %struct.s_heapPage] }
%struct.ProcessSlot = type { ptr, %struct.Process }
%struct.Process = type { i32, i32, [30 x i32], i32, i32, [50 x i8], [20 x ptr], i32, ptr, ptr, ptr, [10 x ptr], i32, ptr, ptr }
%struct.ThreadSlot = type { ptr, %struct.Thread }
%struct.Thread = type { i32, ptr, ptr, ptr, i32, i32 }
%struct.s_mutex = type { i8, i8, [20 x i8], [10 x i32], [10 x i32] }
%struct.s_pipe = type { i32, i32, i32, i8, i8, i8, i8, i8, [40 x i8], i32, i32 }
%struct.s_heapPage = type { ptr, i32, i32, ptr }

@kernelHeader = external 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

declare i32 @getCurrentPid(...) #1

; Function Attrs: nounwind uwtable
declare dso_local void @removeFromQueue(ptr noundef) #2

declare void @unblockProcess(i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @isInQueue(ptr noundef) #2

declare void @blockProcess(i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @addToQueue(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @signal(i32 noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %6 = load ptr, ptr @kernelHeader, align 8, !tbaa !9
  %7 = getelementptr inbounds %struct.kernelHeapHeader, ptr %6, i32 0, i32 8
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [100 x %struct.s_mutex], ptr %7, i64 0, i64 %9
  store ptr %10, ptr %4, align 8, !tbaa !9
  %11 = load ptr, ptr %4, align 8, !tbaa !9
  %12 = getelementptr inbounds %struct.s_mutex, ptr %11, i32 0, i32 1
  %13 = load i8, ptr %12, align 1, !tbaa !11
  %14 = zext i8 %13 to i32
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %53

16:                                               ; preds = %1
  %17 = load ptr, ptr %4, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.s_mutex, ptr %17, i32 0, i32 4
  %19 = getelementptr inbounds [10 x i32], ptr %18, i64 0, i64 0
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = call i32 (...) @getCurrentPid()
  %22 = icmp eq i32 %20, %21
  br i1 %22, label %29, label %23

23:                                               ; preds = %16
  %24 = load ptr, ptr %4, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.s_mutex, ptr %24, i32 0, i32 4
  %26 = getelementptr inbounds [10 x i32], ptr %25, i64 0, i64 0
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = icmp eq i32 %27, -1
  br i1 %28, label %29, label %39

29:                                               ; preds = %23, %16
  %30 = load ptr, ptr %4, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.s_mutex, ptr %30, i32 0, i32 1
  store i8 1, ptr %31, align 1, !tbaa !11
  %32 = load ptr, ptr %4, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.s_mutex, ptr %32, i32 0, i32 4
  %34 = getelementptr inbounds [10 x i32], ptr %33, i64 0, i64 0
  call void @removeFromQueue(ptr noundef %34)
  %35 = load ptr, ptr %4, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.s_mutex, ptr %35, i32 0, i32 3
  %37 = getelementptr inbounds [10 x i32], ptr %36, i64 0, i64 0
  %38 = load i32, ptr %37, align 4, !tbaa !5
  call void @unblockProcess(i32 noundef %38)
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %75

39:                                               ; preds = %23
  %40 = load ptr, ptr %4, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.s_mutex, ptr %40, i32 0, i32 4
  %42 = getelementptr inbounds [10 x i32], ptr %41, i64 0, i64 0
  %43 = call zeroext i8 @isInQueue(ptr noundef %42)
  %44 = icmp ne i8 %43, 0
  br i1 %44, label %45, label %47

45:                                               ; preds = %39
  %46 = call i32 (...) @getCurrentPid()
  call void @blockProcess(i32 noundef %46)
  br label %52

47:                                               ; preds = %39
  %48 = load ptr, ptr %4, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.s_mutex, ptr %48, i32 0, i32 4
  %50 = getelementptr inbounds [10 x i32], ptr %49, i64 0, i64 0
  call void @addToQueue(ptr noundef %50)
  %51 = call i32 (...) @getCurrentPid()
  call void @blockProcess(i32 noundef %51)
  br label %52

52:                                               ; preds = %47, %45
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %75

53:                                               ; preds = %1
  %54 = load ptr, ptr %4, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.s_mutex, ptr %54, i32 0, i32 1
  %56 = load i8, ptr %55, align 1, !tbaa !11
  %57 = zext i8 %56 to i32
  %58 = icmp eq i32 %57, 1
  br i1 %58, label %59, label %73

59:                                               ; preds = %53
  %60 = load ptr, ptr %4, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.s_mutex, ptr %60, i32 0, i32 4
  %62 = getelementptr inbounds [10 x i32], ptr %61, i64 0, i64 0
  %63 = call zeroext i8 @isInQueue(ptr noundef %62)
  %64 = icmp ne i8 %63, 0
  br i1 %64, label %65, label %67

65:                                               ; preds = %59
  %66 = call i32 (...) @getCurrentPid()
  call void @blockProcess(i32 noundef %66)
  br label %72

67:                                               ; preds = %59
  %68 = load ptr, ptr %4, align 8, !tbaa !9
  %69 = getelementptr inbounds %struct.s_mutex, ptr %68, i32 0, i32 4
  %70 = getelementptr inbounds [10 x i32], ptr %69, i64 0, i64 0
  call void @addToQueue(ptr noundef %70)
  %71 = call i32 (...) @getCurrentPid()
  call void @blockProcess(i32 noundef %71)
  br label %72

72:                                               ; preds = %67, %65
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %75

73:                                               ; preds = %53
  br label %74

74:                                               ; preds = %73
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %75

75:                                               ; preds = %74, %72, %52, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %76 = load i32, ptr %2, align 4
  ret i32 %76
}

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 = { 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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !7, i64 1}
!12 = !{!"s_mutex", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 24, !7, i64 64}


; 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.s_mutex = type { i8, i8, [20 x i8], [10 x i32], [10 x i32] }

@kernelHeader = external local_unnamed_addr global ptr, align 8

declare i32 @getCurrentPid(...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @removeFromQueue(ptr noundef) local_unnamed_addr #1

declare void @unblockProcess(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @isInQueue(ptr noundef) local_unnamed_addr #1

declare void @blockProcess(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @addToQueue(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @signal(i32 noundef %0) local_unnamed_addr #1 {
  %2 = load ptr, ptr @kernelHeader, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 29700
  %4 = sext i32 %0 to i64
  %5 = getelementptr inbounds [100 x %struct.s_mutex], ptr %3, i64 0, i64 %4
  %6 = getelementptr inbounds i8, ptr %5, i64 1
  %7 = load i8, ptr %6, align 1, !tbaa !9
  switch i8 %7, label %32 [
    i8 0, label %8
    i8 1, label %25
  ]

8:                                                ; preds = %1
  %9 = getelementptr inbounds i8, ptr %5, i64 64
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = tail call i32 (...) @getCurrentPid() #2
  %12 = icmp eq i32 %10, %11
  br i1 %12, label %16, label %13

13:                                               ; preds = %8
  %14 = load i32, ptr %9, align 4, !tbaa !11
  %15 = icmp eq i32 %14, -1
  br i1 %15, label %16, label %19

16:                                               ; preds = %13, %8
  store i8 1, ptr %6, align 1, !tbaa !9
  tail call void @removeFromQueue(ptr noundef nonnull %9)
  %17 = getelementptr inbounds i8, ptr %5, i64 24
  %18 = load i32, ptr %17, align 4, !tbaa !11
  tail call void @unblockProcess(i32 noundef %18) #2
  br label %32

19:                                               ; preds = %13
  %20 = tail call zeroext i8 @isInQueue(ptr noundef nonnull %9)
  %.not13 = icmp eq i8 %20, 0
  br i1 %.not13, label %23, label %21

21:                                               ; preds = %19
  %22 = tail call i32 (...) @getCurrentPid() #2
  tail call void @blockProcess(i32 noundef %22) #2
  br label %32

23:                                               ; preds = %19
  tail call void @addToQueue(ptr noundef nonnull %9)
  %24 = tail call i32 (...) @getCurrentPid() #2
  tail call void @blockProcess(i32 noundef %24) #2
  br label %32

25:                                               ; preds = %1
  %26 = getelementptr inbounds i8, ptr %5, i64 64
  %27 = tail call zeroext i8 @isInQueue(ptr noundef nonnull %26)
  %.not = icmp eq i8 %27, 0
  br i1 %.not, label %30, label %28

28:                                               ; preds = %25
  %29 = tail call i32 (...) @getCurrentPid() #2
  tail call void @blockProcess(i32 noundef %29) #2
  br label %32

30:                                               ; preds = %25
  tail call void @addToQueue(ptr noundef nonnull %26)
  %31 = tail call i32 (...) @getCurrentPid() #2
  tail call void @blockProcess(i32 noundef %31) #2
  br label %32

32:                                               ; preds = %1, %28, %30, %21, %23, %16
  %.0 = phi i32 [ 0, %16 ], [ -1, %23 ], [ -1, %21 ], [ -1, %30 ], [ -1, %28 ], [ -1, %1 ]
  ret i32 %.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 = { 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, !7, i64 1}
!10 = !{!"s_mutex", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 24, !7, i64 64}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}


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