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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @sys_write(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4) #0 {
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca [2 x i8], align 1
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i32, align 4
  store i64 %0, ptr %7, align 8, !tbaa !5
  store i64 %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !5
  store i64 %3, ptr %10, align 8, !tbaa !5
  store i64 %4, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %17 = call i32 (...) @getCurrentPid()
  store i32 %17, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %13) #4
  call void @llvm.memset.p0.i64(ptr align 1 %13, i8 0, i64 2, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %18 = load i32, ptr %12, align 4, !tbaa !9
  %19 = call ptr @getProcessFromPid(i32 noundef %18)
  store ptr %19, ptr %14, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %20 = load ptr, ptr %14, align 8, !tbaa !11
  %21 = getelementptr inbounds %struct.ProcessSlot, ptr %20, i32 0, i32 1
  %22 = getelementptr inbounds %struct.Process, ptr %21, i32 0, i32 14
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  store ptr %23, ptr %15, align 8, !tbaa !11
  %24 = load i64, ptr %7, align 8, !tbaa !5
  %25 = trunc i64 %24 to i8
  %26 = getelementptr inbounds [2 x i8], ptr %13, i64 0, i64 0
  store i8 %25, ptr %26, align 1, !tbaa !16
  %27 = load ptr, ptr %15, align 8, !tbaa !11
  %28 = icmp eq ptr %27, null
  br i1 %28, label %29, label %33

29:                                               ; preds = %5
  %30 = load i64, ptr %7, align 8, !tbaa !5
  %31 = trunc i64 %30 to i8
  call void @print_char(i8 noundef zeroext %31)
  %32 = load i64, ptr %8, align 8, !tbaa !5
  store i64 %32, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %41

33:                                               ; preds = %5
  %34 = load ptr, ptr %15, align 8, !tbaa !11
  %35 = getelementptr inbounds [2 x i8], ptr %13, i64 0, i64 0
  %36 = load i64, ptr %8, align 8, !tbaa !5
  %37 = trunc i64 %36 to i32
  %38 = load i32, ptr %12, align 4, !tbaa !9
  %39 = call i32 @write(ptr noundef %34, ptr noundef %35, i32 noundef %37, i32 noundef %38)
  %40 = sext i32 %39 to i64
  store i64 %40, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %41

41:                                               ; preds = %33, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  %42 = load i64, ptr %6, align 8
  ret i64 %42
}

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

declare i32 @getCurrentPid(...) #2

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

declare ptr @getProcessFromPid(i32 noundef) #2

declare void @print_char(i8 noundef zeroext) #2

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"long", !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, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !12, i64 488}
!14 = !{!"ProcessSlot", !12, i64 0, !15, i64 8}
!15 = !{!"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, !12, i64 360, !12, i64 368, !12, i64 376, !7, i64 384, !10, i64 464, !12, i64 472, !12, i64 480}
!16 = !{!7, !7, i64 0}


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

; Function Attrs: nounwind uwtable
define dso_local i64 @sys_write(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4) local_unnamed_addr #0 {
  %6 = alloca [2 x i8], align 2
  %7 = tail call i32 (...) @getCurrentPid() #3
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %6) #3
  store i16 0, ptr %6, align 2
  %8 = tail call ptr @getProcessFromPid(i32 noundef %7) #3
  %9 = getelementptr inbounds i8, ptr %8, i64 488
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  %11 = trunc i64 %0 to i8
  store i8 %11, ptr %6, align 2, !tbaa !12
  %12 = icmp eq ptr %10, null
  br i1 %12, label %13, label %14

13:                                               ; preds = %5
  tail call void @print_char(i8 noundef zeroext %11) #3
  br label %18

14:                                               ; preds = %5
  %15 = trunc i64 %1 to i32
  %16 = call i32 @write(ptr noundef nonnull %10, ptr noundef nonnull %6, i32 noundef %15, i32 noundef %7) #3
  %17 = sext i32 %16 to i64
  br label %18

18:                                               ; preds = %14, %13
  %.0 = phi i64 [ %1, %13 ], [ %17, %14 ]
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %6) #3
  ret i64 %.0
}

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

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

declare ptr @getProcessFromPid(i32 noundef) local_unnamed_addr #2

declare void @print_char(i8 noundef zeroext) local_unnamed_addr #2

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !7, i64 488}
!6 = !{!"ProcessSlot", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"Process", !11, i64 0, !11, i64 4, !8, i64 8, !11, i64 128, !11, i64 132, !8, i64 136, !8, i64 192, !11, i64 352, !7, i64 360, !7, i64 368, !7, i64 376, !8, i64 384, !11, i64 464, !7, i64 472, !7, i64 480}
!11 = !{!"int", !8, i64 0}
!12 = !{!8, !8, i64 0}


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