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

; 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.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 }

@.str.15 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.16 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [3 x i8], align 1

; 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
declare dso_local ptr @getProcessFromPid(i32 noundef) #1

declare void @nextLine(...) #2

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

8:                                                ; preds = %13, %2
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = icmp slt i32 %9, %10
  br i1 %11, label %12, label %16

12:                                               ; preds = %8
  call void @print_stringColor(ptr noundef @.str.15, ptr noundef @.str.16)
  br label %13

13:                                               ; preds = %12
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %5, align 4, !tbaa !9
  br label %8, !llvm.loop !11

16:                                               ; preds = %8
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.ProcessSlot, ptr %17, i32 0, i32 1
  %19 = getelementptr inbounds %struct.Process, ptr %18, i32 0, i32 5
  %20 = getelementptr inbounds [50 x i8], ptr %19, i64 0, i64 0
  call void @print_stringColor(ptr noundef %20, ptr noundef @.str.16)
  call void @print_stringColor(ptr noundef @.str.17, ptr noundef @.str.16)
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %46, %16
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = icmp slt i32 %22, 30
  br i1 %23, label %24, label %49

24:                                               ; preds = %21
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.ProcessSlot, ptr %25, i32 0, i32 1
  %27 = getelementptr inbounds %struct.Process, ptr %26, i32 0, i32 2
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [30 x i32], ptr %27, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = icmp ne i32 %31, -1
  br i1 %32, label %33, label %45

33:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.ProcessSlot, ptr %34, i32 0, i32 1
  %36 = getelementptr inbounds %struct.Process, ptr %35, i32 0, i32 2
  %37 = load i32, ptr %6, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [30 x i32], ptr %36, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = call ptr @getProcessFromPid(i32 noundef %40)
  store ptr %41, ptr %7, align 8, !tbaa !5
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = load i32, ptr %4, align 4, !tbaa !9
  %44 = add nsw i32 %43, 8
  call void @printProcessTree(ptr noundef %42, i32 noundef %44)
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %45

45:                                               ; preds = %33, %24
  br label %46

46:                                               ; preds = %45
  %47 = load i32, ptr %6, align 4, !tbaa !9
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %6, align 4, !tbaa !9
  br label %21, !llvm.loop !13

49:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

declare void @print_stringColor(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 = { "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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@.str.15 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.16 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @getProcessFromPid(i32 noundef) local_unnamed_addr #0

declare void @nextLine(...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @printProcessTree(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  tail call void (...) @nextLine() #2
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.014 = phi i32 [ %4, %.lr.ph ], [ 0, %2 ]
  tail call void @print_stringColor(ptr noundef nonnull @.str.15, ptr noundef nonnull @.str.16) #2
  %4 = add nuw nsw i32 %.014, 1
  %exitcond.not = icmp eq i32 %4, %1
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph, %2
  %5 = getelementptr inbounds i8, ptr %0, i64 144
  tail call void @print_stringColor(ptr noundef nonnull %5, ptr noundef nonnull @.str.16) #2
  tail call void @print_stringColor(ptr noundef nonnull @.str.17, ptr noundef nonnull @.str.16) #2
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = add nsw i32 %1, 8
  br label %8

8:                                                ; preds = %._crit_edge, %13
  %indvars.iv = phi i64 [ 0, %._crit_edge ], [ %indvars.iv.next, %13 ]
  %9 = getelementptr inbounds [30 x i32], ptr %6, i64 0, i64 %indvars.iv
  %10 = load i32, ptr %9, align 4, !tbaa !7
  %.not = icmp eq i32 %10, -1
  br i1 %.not, label %13, label %11

11:                                               ; preds = %8
  %12 = tail call ptr @getProcessFromPid(i32 noundef %10)
  tail call void @printProcessTree(ptr noundef %12, i32 noundef %7)
  br label %13

13:                                               ; preds = %8, %11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond17.not = icmp eq i64 %indvars.iv.next, 30
  br i1 %exitcond17.not, label %14, label %8, !llvm.loop !11

14:                                               ; preds = %13
  ret void
}

declare void @print_stringColor(ptr noundef, ptr 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = distinct !{!11, !6}


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