; 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

; Function Attrs: nounwind uwtable
define dso_local i32 @getFreeMutex(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %7

7:                                                ; preds = %23, %1
  %8 = load i32, ptr %4, align 4, !tbaa !9
  %9 = icmp slt i32 %8, 100
  br i1 %9, label %10, label %26

10:                                               ; preds = %7
  %11 = load ptr, ptr @kernelHeader, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.kernelHeapHeader, ptr %11, i32 0, i32 8
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [100 x %struct.s_mutex], ptr %12, i64 0, i64 %14
  store ptr %15, ptr %5, align 8, !tbaa !5
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.s_mutex, ptr %16, i32 0, i32 0
  %18 = load i8, ptr %17, align 4, !tbaa !11
  %19 = zext i8 %18 to i32
  %20 = icmp eq i32 %19, 1
  br i1 %20, label %21, label %22

21:                                               ; preds = %10
  br label %26

22:                                               ; preds = %10
  br label %23

23:                                               ; preds = %22
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %4, align 4, !tbaa !9
  br label %7, !llvm.loop !13

26:                                               ; preds = %21, %7
  %27 = load i32, ptr %4, align 4, !tbaa !9
  %28 = icmp eq i32 %27, 100
  br i1 %28, label %29, label %30

29:                                               ; preds = %26
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %39

30:                                               ; preds = %26
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.s_mutex, ptr %31, i32 0, i32 0
  store i8 0, ptr %32, align 4, !tbaa !11
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.s_mutex, ptr %33, i32 0, i32 2
  %35 = getelementptr inbounds [20 x i8], ptr %34, i64 0, i64 0
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = call ptr @strcpy(ptr noundef %35, ptr noundef %36) #3
  %38 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %38, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %39

39:                                               ; preds = %30, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %40 = load i32, ptr %2, align 4
  ret i32 %40
}

; Function Attrs: nounwind
declare ptr @strcpy(ptr noundef, ptr 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 = { 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 }

!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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"s_mutex", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 24, !7, i64 64}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; 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

; Function Attrs: nofree nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local i32 @getFreeMutex(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @kernelHeader, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 29700
  br label %4

4:                                                ; preds = %24, %1
  %indvars.iv = phi i64 [ 0, %1 ], [ %indvars.iv.next.4, %24 ]
  %5 = getelementptr inbounds [100 x %struct.s_mutex], ptr %3, i64 0, i64 %indvars.iv
  %6 = load i8, ptr %5, align 4, !tbaa !9
  %7 = icmp eq i8 %6, 1
  br i1 %7, label %.thread, label %8

8:                                                ; preds = %4
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %9 = getelementptr inbounds [100 x %struct.s_mutex], ptr %3, i64 0, i64 %indvars.iv.next
  %10 = load i8, ptr %9, align 4, !tbaa !9
  %11 = icmp eq i8 %10, 1
  br i1 %11, label %.thread, label %12

12:                                               ; preds = %8
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %13 = getelementptr inbounds [100 x %struct.s_mutex], ptr %3, i64 0, i64 %indvars.iv.next.1
  %14 = load i8, ptr %13, align 4, !tbaa !9
  %15 = icmp eq i8 %14, 1
  br i1 %15, label %.thread, label %16

16:                                               ; preds = %12
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %17 = getelementptr inbounds [100 x %struct.s_mutex], ptr %3, i64 0, i64 %indvars.iv.next.2
  %18 = load i8, ptr %17, align 4, !tbaa !9
  %19 = icmp eq i8 %18, 1
  br i1 %19, label %.thread, label %20

20:                                               ; preds = %16
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %21 = getelementptr inbounds [100 x %struct.s_mutex], ptr %3, i64 0, i64 %indvars.iv.next.3
  %22 = load i8, ptr %21, align 4, !tbaa !9
  %23 = icmp eq i8 %22, 1
  br i1 %23, label %.thread, label %24

24:                                               ; preds = %20
  %indvars.iv.next.4 = add nuw nsw i64 %indvars.iv, 5
  %exitcond.not.4 = icmp eq i64 %indvars.iv.next.4, 100
  br i1 %exitcond.not.4, label %.loopexit, label %4, !llvm.loop !11

.thread:                                          ; preds = %20, %16, %12, %8, %4
  %indvars.iv.lcssa = phi i64 [ %indvars.iv, %4 ], [ %indvars.iv.next, %8 ], [ %indvars.iv.next.1, %12 ], [ %indvars.iv.next.2, %16 ], [ %indvars.iv.next.3, %20 ]
  %.lcssa = phi ptr [ %5, %4 ], [ %9, %8 ], [ %13, %12 ], [ %17, %16 ], [ %21, %20 ]
  %25 = trunc nuw nsw i64 %indvars.iv.lcssa to i32
  store i8 0, ptr %.lcssa, align 4, !tbaa !9
  %26 = getelementptr inbounds i8, ptr %.lcssa, i64 2
  %27 = tail call ptr @strcpy(ptr noundef nonnull dereferenceable(1) %26, ptr noundef nonnull dereferenceable(1) %0) #2
  br label %.loopexit

.loopexit:                                        ; preds = %24, %.thread
  %.010 = phi i32 [ %25, %.thread ], [ -1, %24 ]
  ret i32 %.010
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strcpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly) local_unnamed_addr #1

attributes #0 = { nofree nounwind memory(readwrite, inaccessiblemem: none) 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 nofree nounwind willreturn memory(argmem: readwrite) "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 0}
!10 = !{!"s_mutex", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 24, !7, i64 64}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass