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

@currentProcessId = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [6 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 i32 @checkIfForegroundOrBackground(ptr noundef) #1

declare zeroext i8 @strcmp(ptr noundef, ptr noundef) #2

declare i32 @strlen(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @createProcess(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca %struct.Process, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 488, ptr %7) #4
  %8 = load i32, ptr @currentProcessId, align 4, !tbaa !9
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr @currentProcessId, align 4, !tbaa !9
  %10 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 0
  store i32 %8, ptr %10, align 8, !tbaa !11
  %11 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 10
  store ptr null, ptr %11, align 8, !tbaa !13
  %12 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 3
  store i32 3, ptr %12, align 8, !tbaa !14
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = call i32 @checkIfForegroundOrBackground(ptr noundef %13)
  %15 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 4
  store i32 %14, ptr %15, align 4, !tbaa !15
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = call zeroext i8 @strcmp(ptr noundef %16, ptr noundef @.str)
  %18 = icmp ne i8 %17, 0
  br i1 %18, label %22, label %19

19:                                               ; preds = %2
  %20 = call i32 (...) @getCurrentPid()
  %21 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 1
  store i32 %20, ptr %21, align 4, !tbaa !16
  br label %24

22:                                               ; preds = %2
  %23 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 1
  store i32 -1, ptr %23, align 4, !tbaa !16
  br label %24

24:                                               ; preds = %22, %19
  %25 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 0
  %26 = load i32, ptr %25, align 8, !tbaa !11
  %27 = call i32 (...) @getCurrentPid()
  %28 = call ptr @getProcessFromPid(i32 noundef %27)
  call void @addChild(i32 noundef %26, ptr noundef %28)
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %37, %24
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = icmp slt i32 %30, 30
  br i1 %31, label %32, label %40

32:                                               ; preds = %29
  %33 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 2
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [30 x i32], ptr %33, i64 0, i64 %35
  store i32 -1, ptr %36, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %32
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %6, align 4, !tbaa !9
  br label %29, !llvm.loop !17

40:                                               ; preds = %29
  %41 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 4
  %42 = load i32, ptr %41, align 4, !tbaa !15
  %43 = icmp eq i32 %42, 1
  br i1 %43, label %49, label %44

44:                                               ; preds = %40
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = call zeroext i8 @strcmp(ptr noundef %45, ptr noundef @.str)
  %47 = zext i8 %46 to i32
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %54

49:                                               ; preds = %44, %40
  %50 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 5
  %51 = getelementptr inbounds [50 x i8], ptr %50, i64 0, i64 0
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = call ptr @strcpy(ptr noundef %51, ptr noundef %52) #4
  br label %62

54:                                               ; preds = %44
  %55 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 5
  %56 = getelementptr inbounds [50 x i8], ptr %55, i64 0, i64 0
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = call i32 @strlen(ptr noundef %58)
  %60 = sub nsw i32 %59, 1
  %61 = call ptr @strncpy(ptr noundef %56, ptr noundef %57, i32 noundef %60)
  br label %62

62:                                               ; preds = %54, %49
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %63

63:                                               ; preds = %71, %62
  %64 = load i32, ptr %5, align 4, !tbaa !9
  %65 = icmp slt i32 %64, 10
  br i1 %65, label %66, label %74

66:                                               ; preds = %63
  %67 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 11
  %68 = load i32, ptr %5, align 4, !tbaa !9
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [10 x ptr], ptr %67, i64 0, i64 %69
  store ptr null, ptr %70, align 8, !tbaa !5
  br label %71

71:                                               ; preds = %66
  %72 = load i32, ptr %5, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %5, align 4, !tbaa !9
  br label %63, !llvm.loop !19

74:                                               ; preds = %63
  %75 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 12
  store i32 0, ptr %75, align 8, !tbaa !20
  %76 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 13
  store ptr null, ptr %76, align 8, !tbaa !21
  %77 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 14
  store ptr null, ptr %77, align 8, !tbaa !22
  %78 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 7
  store i32 1, ptr %78, align 8, !tbaa !23
  %79 = load ptr, ptr %3, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 7
  %81 = load i32, ptr %80, align 8, !tbaa !23
  %82 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 5
  %83 = getelementptr inbounds [50 x i8], ptr %82, i64 0, i64 0
  %84 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 0
  %85 = load i32, ptr %84, align 8, !tbaa !11
  %86 = call ptr @createThread(ptr noundef %79, i32 noundef %81, ptr noundef %83, i32 noundef %85)
  %87 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 9
  store ptr %86, ptr %87, align 8, !tbaa !24
  %88 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 8
  store ptr %86, ptr %88, align 8, !tbaa !25
  call void @addProcessToPCB(ptr noundef byval(%struct.Process) align 8 %7)
  %89 = getelementptr inbounds %struct.Process, ptr %7, i32 0, i32 0
  %90 = load i32, ptr %89, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 488, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret i32 %90
}

declare i32 @getCurrentPid(...) #2

; Function Attrs: nounwind uwtable
declare dso_local void @addChild(i32 noundef, ptr noundef) #1

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

; Function Attrs: nounwind
declare ptr @strcpy(ptr noundef, ptr noundef) #3

declare ptr @strncpy(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @createThread(ptr noundef, i32 noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @addProcessToPCB(ptr noundef byval(%struct.Process) align 8) #1

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 "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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"Process", !10, i64 0, !10, i64 4, !7, i64 8, !10, i64 128, !10, i64 132, !7, i64 136, !7, i64 192, !10, i64 352, !6, i64 360, !6, i64 368, !6, i64 376, !7, i64 384, !10, i64 464, !6, i64 472, !6, i64 480}
!13 = !{!12, !6, i64 376}
!14 = !{!12, !10, i64 128}
!15 = !{!12, !10, i64 132}
!16 = !{!12, !10, i64 4}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}
!20 = !{!12, !10, i64 464}
!21 = !{!12, !6, i64 472}
!22 = !{!12, !6, i64 480}
!23 = !{!12, !10, i64 352}
!24 = !{!12, !6, i64 368}
!25 = !{!12, !6, i64 360}


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

@currentProcessId = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress 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 i32 @checkIfForegroundOrBackground(ptr noundef) local_unnamed_addr #1

declare zeroext i8 @strcmp(ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @strlen(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @createProcess(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = alloca %struct.Process, align 8
  call void @llvm.lifetime.start.p0(i64 488, ptr nonnull %3) #5
  %4 = load i32, ptr @currentProcessId, align 4, !tbaa !5
  %5 = add nsw i32 %4, 1
  store i32 %5, ptr @currentProcessId, align 4, !tbaa !5
  store i32 %4, ptr %3, align 8, !tbaa !9
  %6 = getelementptr inbounds i8, ptr %3, i64 376
  store ptr null, ptr %6, align 8, !tbaa !12
  %7 = getelementptr inbounds i8, ptr %3, i64 128
  store i32 3, ptr %7, align 8, !tbaa !13
  %8 = tail call i32 @checkIfForegroundOrBackground(ptr noundef %1)
  %9 = getelementptr inbounds i8, ptr %3, i64 132
  store i32 %8, ptr %9, align 4, !tbaa !14
  %10 = tail call zeroext i8 @strcmp(ptr noundef %1, ptr noundef nonnull @.str) #5
  %.not = icmp eq i8 %10, 0
  br i1 %.not, label %11, label %13

11:                                               ; preds = %2
  %12 = tail call i32 (...) @getCurrentPid() #5
  br label %13

13:                                               ; preds = %2, %11
  %.sink = phi i32 [ %12, %11 ], [ -1, %2 ]
  %14 = getelementptr inbounds i8, ptr %3, i64 4
  store i32 %.sink, ptr %14, align 4
  %15 = tail call i32 (...) @getCurrentPid() #5
  %16 = tail call ptr @getProcessFromPid(i32 noundef %15)
  tail call void @addChild(i32 noundef %4, ptr noundef %16)
  %17 = getelementptr inbounds i8, ptr %3, i64 8
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(120) %17, i8 -1, i64 120, i1 false), !tbaa !5
  %18 = icmp eq i32 %8, 1
  br i1 %18, label %21, label %19

19:                                               ; preds = %13
  %20 = tail call zeroext i8 @strcmp(ptr noundef %1, ptr noundef nonnull @.str) #5
  %.not13 = icmp eq i8 %20, 0
  br i1 %.not13, label %24, label %21

21:                                               ; preds = %19, %13
  %22 = getelementptr inbounds i8, ptr %3, i64 136
  %23 = call ptr @strcpy(ptr noundef nonnull dereferenceable(1) %22, ptr noundef nonnull dereferenceable(1) %1) #5
  br label %29

24:                                               ; preds = %19
  %25 = getelementptr inbounds i8, ptr %3, i64 136
  %26 = tail call i32 @strlen(ptr noundef %1) #5
  %27 = add nsw i32 %26, -1
  %28 = call ptr @strncpy(ptr noundef nonnull %25, ptr noundef %1, i32 noundef %27) #5
  %.pre = load i32, ptr %3, align 8, !tbaa !9
  br label %29

29:                                               ; preds = %24, %21
  %30 = phi i32 [ %.pre, %24 ], [ %4, %21 ]
  %31 = getelementptr inbounds i8, ptr %3, i64 384
  %32 = getelementptr inbounds i8, ptr %3, i64 472
  %33 = getelementptr inbounds i8, ptr %3, i64 352
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(84) %31, i8 0, i64 84, i1 false)
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %32, i8 0, i64 16, i1 false)
  store i32 1, ptr %33, align 8, !tbaa !15
  %34 = getelementptr inbounds i8, ptr %3, i64 136
  %35 = call ptr @createThread(ptr noundef %0, i32 noundef 1, ptr noundef nonnull %34, i32 noundef %30)
  %36 = getelementptr inbounds i8, ptr %3, i64 368
  store ptr %35, ptr %36, align 8, !tbaa !16
  %37 = getelementptr inbounds i8, ptr %3, i64 360
  store ptr %35, ptr %37, align 8, !tbaa !17
  call void @addProcessToPCB(ptr noundef nonnull byval(%struct.Process) align 8 %3)
  %38 = load i32, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 488, ptr nonnull %3) #5
  ret i32 %38
}

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

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

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

; 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 #3

declare ptr @strncpy(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @createThread(ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @addProcessToPCB(ptr noundef byval(%struct.Process) align 8) local_unnamed_addr #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

attributes #0 = { mustprogress 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 = { 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { 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, !6, i64 0}
!10 = !{!"Process", !6, i64 0, !6, i64 4, !7, i64 8, !6, i64 128, !6, i64 132, !7, i64 136, !7, i64 192, !6, i64 352, !11, i64 360, !11, i64 368, !11, i64 376, !7, i64 384, !6, i64 464, !11, i64 472, !11, i64 480}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 376}
!13 = !{!10, !6, i64 128}
!14 = !{!10, !6, i64 132}
!15 = !{!10, !6, i64 352}
!16 = !{!10, !11, i64 368}
!17 = !{!10, !11, i64 360}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,MemCpyOptPass,MergedLoadStoreMotionPass,SROAPass,SimplifyCFGPass,TailCallElimPass