; LLVM IR from rociodellasala/TP3_SO :: Kernel (heap)

; 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.s_pipe = type { i32, i32, i32, i8, i8, i8, i8, i8, [40 x i8], i32, i32 }
%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_heapPage = type { ptr, i32, i32, ptr }

@kernelHeader = external dso_local 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 void @releasePipeStruct(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.s_pipe, ptr %8, i32 0, i32 2
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %11 = call ptr @getProcessFromPid(i32 noundef %10)
  store ptr %11, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.s_pipe, ptr %12, i32 0, i32 1
  %14 = load i32, ptr %13, align 4, !tbaa !12
  %15 = call ptr @getProcessFromPid(i32 noundef %14)
  store ptr %15, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %22

18:                                               ; preds = %1
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %22

21:                                               ; preds = %18
  store i32 1, ptr %7, align 4
  br label %71

22:                                               ; preds = %18, %1
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = icmp eq ptr %23, null
  br i1 %24, label %25, label %26

25:                                               ; preds = %22
  store i32 1, ptr %7, align 4
  br label %71

26:                                               ; preds = %22
  store i32 0, ptr %5, align 4, !tbaa !13
  br label %27

27:                                               ; preds = %41, %26
  %28 = load i32, ptr %5, align 4, !tbaa !13
  %29 = icmp slt i32 %28, 500
  br i1 %29, label %30, label %44

30:                                               ; preds = %27
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = load ptr, ptr @kernelHeader, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.kernelHeapHeader, ptr %32, i32 0, i32 11
  %34 = load i32, ptr %5, align 4, !tbaa !13
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [500 x %struct.s_pipe], ptr %33, i64 0, i64 %35
  %37 = icmp eq ptr %31, %36
  br i1 %37, label %38, label %40

38:                                               ; preds = %30
  %39 = load i32, ptr %5, align 4, !tbaa !13
  store i32 %39, ptr %6, align 4, !tbaa !13
  br label %44

40:                                               ; preds = %30
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %5, align 4, !tbaa !13
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %5, align 4, !tbaa !13
  br label %27, !llvm.loop !14

44:                                               ; preds = %38, %27
  store i32 0, ptr %5, align 4, !tbaa !13
  br label %45

45:                                               ; preds = %64, %44
  %46 = load i32, ptr %5, align 4, !tbaa !13
  %47 = icmp slt i32 %46, 10
  br i1 %47, label %48, label %67

48:                                               ; preds = %45
  %49 = load ptr, ptr @kernelHeader, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.kernelHeapHeader, ptr %49, i32 0, i32 10
  %51 = load i32, ptr %5, align 4, !tbaa !13
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [10 x i32], ptr %50, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !13
  %55 = icmp eq i32 %54, -1
  br i1 %55, label %56, label %63

56:                                               ; preds = %48
  %57 = load i32, ptr %6, align 4, !tbaa !13
  %58 = load ptr, ptr @kernelHeader, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.kernelHeapHeader, ptr %58, i32 0, i32 10
  %60 = load i32, ptr %5, align 4, !tbaa !13
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [10 x i32], ptr %59, i64 0, i64 %61
  store i32 %57, ptr %62, align 4, !tbaa !13
  br label %67

63:                                               ; preds = %48
  br label %64

64:                                               ; preds = %63
  %65 = load i32, ptr %5, align 4, !tbaa !13
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %5, align 4, !tbaa !13
  br label %45, !llvm.loop !16

67:                                               ; preds = %56, %45
  %68 = load ptr, ptr %2, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.s_pipe, ptr %68, i32 0, i32 10
  %70 = load i32, ptr %69, align 4, !tbaa !17
  call void @freeMutex(i32 noundef %70)
  store i32 0, ptr %7, align 4
  br label %71

71:                                               ; preds = %67, %25, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %72 = load i32, ptr %7, align 4
  switch i32 %72, label %74 [
    i32 0, label %73
    i32 1, label %73
  ]

73:                                               ; preds = %71, %71
  ret void

74:                                               ; preds = %71
  unreachable
}

declare ptr @getProcessFromPid(i32 noundef) #2

declare void @freeMutex(i32 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 8}
!10 = !{!"s_pipe", !11, i64 0, !11, i64 4, !11, i64 8, !7, i64 12, !7, i64 13, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 17, !11, i64 60, !11, i64 64}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 4}
!13 = !{!11, !11, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!10, !11, 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_pipe = type { i32, i32, i32, i8, i8, i8, i8, i8, [40 x i8], i32, i32 }

@kernelHeader = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @releasePipeStruct(ptr noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = tail call ptr @getProcessFromPid(i32 noundef %3) #2
  %5 = getelementptr inbounds i8, ptr %0, i64 4
  %6 = load i32, ptr %5, align 4, !tbaa !10
  %7 = tail call ptr @getProcessFromPid(i32 noundef %6) #2
  %8 = icmp ne ptr %7, null
  %9 = icmp ne ptr %4, null
  %or.cond = select i1 %8, i1 %9, i1 false
  %10 = icmp eq ptr %0, null
  %or.cond17 = or i1 %10, %or.cond
  br i1 %or.cond17, label %73, label %.preheader

.preheader:                                       ; preds = %1
  %11 = load ptr, ptr @kernelHeader, align 8, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %11, i64 40144
  br label %13

13:                                               ; preds = %28, %.preheader
  %indvars.iv = phi i64 [ 0, %.preheader ], [ %indvars.iv.next.4, %28 ]
  %14 = getelementptr inbounds [500 x %struct.s_pipe], ptr %12, i64 0, i64 %indvars.iv
  %15 = icmp eq ptr %14, %0
  br i1 %15, label %.split.loop.exit, label %16

16:                                               ; preds = %13
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %17 = getelementptr inbounds [500 x %struct.s_pipe], ptr %12, i64 0, i64 %indvars.iv.next
  %18 = icmp eq ptr %17, %0
  br i1 %18, label %.split.loop.exit, label %19

19:                                               ; preds = %16
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %20 = getelementptr inbounds [500 x %struct.s_pipe], ptr %12, i64 0, i64 %indvars.iv.next.1
  %21 = icmp eq ptr %20, %0
  br i1 %21, label %.split.loop.exit, label %22

22:                                               ; preds = %19
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %23 = getelementptr inbounds [500 x %struct.s_pipe], ptr %12, i64 0, i64 %indvars.iv.next.2
  %24 = icmp eq ptr %23, %0
  br i1 %24, label %.split.loop.exit, label %25

25:                                               ; preds = %22
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %26 = getelementptr inbounds [500 x %struct.s_pipe], ptr %12, i64 0, i64 %indvars.iv.next.3
  %27 = icmp eq ptr %26, %0
  br i1 %27, label %.split.loop.exit, label %28

28:                                               ; preds = %25
  %indvars.iv.next.4 = add nuw nsw i64 %indvars.iv, 5
  %exitcond.not.4 = icmp eq i64 %indvars.iv.next.4, 500
  br i1 %exitcond.not.4, label %.split.loop.exit28, label %13, !llvm.loop !13

.split.loop.exit:                                 ; preds = %25, %22, %19, %16, %13
  %indvars.iv.lcssa = phi i64 [ %indvars.iv, %13 ], [ %indvars.iv.next, %16 ], [ %indvars.iv.next.1, %19 ], [ %indvars.iv.next.2, %22 ], [ %indvars.iv.next.3, %25 ]
  %29 = trunc nuw nsw i64 %indvars.iv.lcssa to i32
  br label %.split.loop.exit28

.split.loop.exit28:                               ; preds = %28, %.split.loop.exit
  %.016.lcssa = phi i32 [ %29, %.split.loop.exit ], [ 500, %28 ]
  %30 = getelementptr inbounds i8, ptr %11, i64 40104
  %31 = load i32, ptr %30, align 4, !tbaa !15
  %32 = icmp eq i32 %31, -1
  br i1 %32, label %69, label %33

33:                                               ; preds = %.split.loop.exit28
  %34 = getelementptr inbounds i8, ptr %11, i64 40108
  %35 = load i32, ptr %34, align 4, !tbaa !15
  %36 = icmp eq i32 %35, -1
  br i1 %36, label %69, label %37

37:                                               ; preds = %33
  %38 = getelementptr inbounds i8, ptr %11, i64 40112
  %39 = load i32, ptr %38, align 4, !tbaa !15
  %40 = icmp eq i32 %39, -1
  br i1 %40, label %69, label %41

41:                                               ; preds = %37
  %42 = getelementptr inbounds i8, ptr %11, i64 40116
  %43 = load i32, ptr %42, align 4, !tbaa !15
  %44 = icmp eq i32 %43, -1
  br i1 %44, label %69, label %45

45:                                               ; preds = %41
  %46 = getelementptr inbounds i8, ptr %11, i64 40120
  %47 = load i32, ptr %46, align 4, !tbaa !15
  %48 = icmp eq i32 %47, -1
  br i1 %48, label %69, label %49

49:                                               ; preds = %45
  %50 = getelementptr inbounds i8, ptr %11, i64 40124
  %51 = load i32, ptr %50, align 4, !tbaa !15
  %52 = icmp eq i32 %51, -1
  br i1 %52, label %69, label %53

53:                                               ; preds = %49
  %54 = getelementptr inbounds i8, ptr %11, i64 40128
  %55 = load i32, ptr %54, align 4, !tbaa !15
  %56 = icmp eq i32 %55, -1
  br i1 %56, label %69, label %57

57:                                               ; preds = %53
  %58 = getelementptr inbounds i8, ptr %11, i64 40132
  %59 = load i32, ptr %58, align 4, !tbaa !15
  %60 = icmp eq i32 %59, -1
  br i1 %60, label %69, label %61

61:                                               ; preds = %57
  %62 = getelementptr inbounds i8, ptr %11, i64 40136
  %63 = load i32, ptr %62, align 4, !tbaa !15
  %64 = icmp eq i32 %63, -1
  br i1 %64, label %69, label %65

65:                                               ; preds = %61
  %66 = getelementptr inbounds i8, ptr %11, i64 40140
  %67 = load i32, ptr %66, align 4, !tbaa !15
  %68 = icmp eq i32 %67, -1
  br i1 %68, label %69, label %.loopexit

69:                                               ; preds = %65, %61, %57, %53, %49, %45, %41, %37, %33, %.split.loop.exit28
  %.lcssa = phi i64 [ 0, %.split.loop.exit28 ], [ 1, %33 ], [ 2, %37 ], [ 3, %41 ], [ 4, %45 ], [ 5, %49 ], [ 6, %53 ], [ 7, %57 ], [ 8, %61 ], [ 9, %65 ]
  %70 = getelementptr inbounds [10 x i32], ptr %30, i64 0, i64 %.lcssa
  store i32 %.016.lcssa, ptr %70, align 4, !tbaa !15
  br label %.loopexit

.loopexit:                                        ; preds = %65, %69
  %71 = getelementptr inbounds i8, ptr %0, i64 64
  %72 = load i32, ptr %71, align 4, !tbaa !16
  tail call void @freeMutex(i32 noundef %72) #2
  br label %73

73:                                               ; preds = %1, %.loopexit
  ret void
}

declare ptr @getProcessFromPid(i32 noundef) local_unnamed_addr #1

declare void @freeMutex(i32 noundef) local_unnamed_addr #1

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


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