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

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

@screen = external hidden global ptr, align 8
@pitch = external hidden global i32, align 4
@pixel_width = external hidden global i32, align 4
@xres = external hidden global i32, align 4
@yres = external hidden global i32, align 4
@currentProcess = external global ptr, align 8
@buffer_position = external hidden global i32, align 4

; 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 @clear_screen() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %4 = load ptr, ptr @screen, align 8, !tbaa !5
  store ptr %4, ptr %3, align 8, !tbaa !5
  store i32 0, ptr @buffer_position, align 4, !tbaa !9
  %5 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.ProcessSlot, ptr %5, i32 0, i32 1
  %7 = getelementptr inbounds %struct.Process, ptr %6, i32 0, i32 4
  %8 = load i32, ptr %7, align 4, !tbaa !11
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %53

10:                                               ; preds = %0
  store i32 0, ptr %1, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %49, %10
  %12 = load i32, ptr %1, align 4, !tbaa !9
  %13 = load i32, ptr @yres, align 4, !tbaa !9
  %14 = icmp sle i32 %12, %13
  br i1 %14, label %15, label %52

15:                                               ; preds = %11
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %41, %15
  %17 = load i32, ptr %2, align 4, !tbaa !9
  %18 = load i32, ptr @xres, align 4, !tbaa !9
  %19 = icmp sle i32 %17, %18
  br i1 %19, label %20, label %44

20:                                               ; preds = %16
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = load i32, ptr @pixel_width, align 4, !tbaa !9
  %23 = load i32, ptr %2, align 4, !tbaa !9
  %24 = mul nsw i32 %22, %23
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i8, ptr %21, i64 %25
  store i8 0, ptr %26, align 1, !tbaa !14
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = load i32, ptr @pixel_width, align 4, !tbaa !9
  %29 = load i32, ptr %2, align 4, !tbaa !9
  %30 = mul nsw i32 %28, %29
  %31 = add nsw i32 %30, 1
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i8, ptr %27, i64 %32
  store i8 0, ptr %33, align 1, !tbaa !14
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load i32, ptr @pixel_width, align 4, !tbaa !9
  %36 = load i32, ptr %2, align 4, !tbaa !9
  %37 = mul nsw i32 %35, %36
  %38 = add nsw i32 %37, 2
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i8, ptr %34, i64 %39
  store i8 0, ptr %40, align 1, !tbaa !14
  br label %41

41:                                               ; preds = %20
  %42 = load i32, ptr %2, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %2, align 4, !tbaa !9
  br label %16, !llvm.loop !15

44:                                               ; preds = %16
  %45 = load i32, ptr @pitch, align 4, !tbaa !9
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = sext i32 %45 to i64
  %48 = getelementptr inbounds i8, ptr %46, i64 %47
  store ptr %48, ptr %3, align 8, !tbaa !5
  br label %49

49:                                               ; preds = %44
  %50 = load i32, ptr %1, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %1, align 4, !tbaa !9
  br label %11, !llvm.loop !17

52:                                               ; preds = %11
  br label %53

53:                                               ; preds = %52, %0
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret void
}

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 }

!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 140}
!12 = !{!"ProcessSlot", !6, i64 0, !13, i64 8}
!13 = !{!"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}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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

@screen = external hidden local_unnamed_addr global ptr, align 8
@pitch = external hidden local_unnamed_addr global i32, align 4
@pixel_width = external hidden local_unnamed_addr global i32, align 4
@xres = external hidden local_unnamed_addr global i32, align 4
@yres = external hidden local_unnamed_addr global i32, align 4
@currentProcess = external local_unnamed_addr global ptr, align 8
@buffer_position = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @clear_screen() local_unnamed_addr #0 {
  %1 = load ptr, ptr @screen, align 8, !tbaa !5
  store i32 0, ptr @buffer_position, align 4, !tbaa !9
  %2 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 140
  %4 = load i32, ptr %3, align 4, !tbaa !11
  %5 = icmp ne i32 %4, 0
  %6 = load i32, ptr @yres, align 4
  %.not16 = icmp slt i32 %6, 0
  %or.cond = select i1 %5, i1 true, i1 %.not16
  %7 = load i32, ptr @xres, align 4
  %8 = icmp slt i32 %7, 0
  %or.cond21 = select i1 %or.cond, i1 true, i1 %8
  br i1 %or.cond21, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %0, %._crit_edge
  %9 = phi i32 [ %27, %._crit_edge ], [ %6, %0 ]
  %10 = phi i32 [ %28, %._crit_edge ], [ %7, %0 ]
  %.018 = phi ptr [ %31, %._crit_edge ], [ %1, %0 ]
  %.01117 = phi i32 [ %32, %._crit_edge ], [ 0, %0 ]
  %.not1214 = icmp slt i32 %10, 0
  br i1 %.not1214, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.01015 = phi i32 [ %25, %.lr.ph ], [ 0, %.preheader ]
  %11 = load i32, ptr @pixel_width, align 4, !tbaa !9
  %12 = mul nsw i32 %11, %.01015
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds i8, ptr %.018, i64 %13
  store i8 0, ptr %14, align 1, !tbaa !14
  %15 = load i32, ptr @pixel_width, align 4, !tbaa !9
  %16 = mul nsw i32 %15, %.01015
  %17 = add nsw i32 %16, 1
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i8, ptr %.018, i64 %18
  store i8 0, ptr %19, align 1, !tbaa !14
  %20 = load i32, ptr @pixel_width, align 4, !tbaa !9
  %21 = mul nsw i32 %20, %.01015
  %22 = add nsw i32 %21, 2
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i8, ptr %.018, i64 %23
  store i8 0, ptr %24, align 1, !tbaa !14
  %25 = add nuw nsw i32 %.01015, 1
  %26 = load i32, ptr @xres, align 4, !tbaa !9
  %.not12.not = icmp slt i32 %.01015, %26
  br i1 %.not12.not, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load i32, ptr @yres, align 4, !tbaa !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %27 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %9, %.preheader ]
  %28 = phi i32 [ %26, %._crit_edge.loopexit ], [ %10, %.preheader ]
  %29 = load i32, ptr @pitch, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i8, ptr %.018, i64 %30
  %32 = add nuw nsw i32 %.01117, 1
  %.not.not = icmp slt i32 %.01117, %27
  br i1 %.not.not, label %.preheader, label %.loopexit, !llvm.loop !17

.loopexit:                                        ; preds = %._crit_edge, %0
  ret void
}

attributes #0 = { nofree norecurse nosync 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" }

!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 140}
!12 = !{!"ProcessSlot", !6, i64 0, !13, i64 8}
!13 = !{!"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}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16, !18}
!18 = !{!"llvm.loop.unswitch.partial.disable"}


; Effective O3 passes: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass