; 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
@currentProcess = external 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 @draw_filled_rectangle(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i8, align 1
  %14 = alloca i8, align 1
  %15 = alloca i8, align 1
  %16 = alloca i32, align 4
  %17 = alloca ptr, align 8
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  store i32 %4, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %13) #2
  %18 = load i32, ptr %10, align 4, !tbaa !5
  %19 = and i32 %18, 255
  %20 = trunc i32 %19 to i8
  store i8 %20, ptr %13, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %14) #2
  %21 = load i32, ptr %10, align 4, !tbaa !5
  %22 = ashr i32 %21, 8
  %23 = and i32 %22, 255
  %24 = trunc i32 %23 to i8
  store i8 %24, ptr %14, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %15) #2
  %25 = load i32, ptr %10, align 4, !tbaa !5
  %26 = ashr i32 %25, 16
  %27 = and i32 %26, 255
  %28 = trunc i32 %27 to i8
  store i8 %28, ptr %15, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #2
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = load i32, ptr @pixel_width, align 4, !tbaa !5
  %31 = mul nsw i32 %29, %30
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = load i32, ptr @pitch, align 4, !tbaa !5
  %34 = mul nsw i32 %32, %33
  %35 = add nsw i32 %31, %34
  store i32 %35, ptr %16, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #2
  %36 = load ptr, ptr @screen, align 8, !tbaa !10
  %37 = load i32, ptr %16, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i8, ptr %36, i64 %38
  store ptr %39, ptr %17, align 8, !tbaa !10
  %40 = load ptr, ptr @currentProcess, align 8, !tbaa !10
  %41 = getelementptr inbounds %struct.ProcessSlot, ptr %40, i32 0, i32 1
  %42 = getelementptr inbounds %struct.Process, ptr %41, i32 0, i32 4
  %43 = load i32, ptr %42, align 4, !tbaa !12
  %44 = icmp eq i32 %43, 0
  br i1 %44, label %45, label %95

45:                                               ; preds = %5
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %91, %45
  %47 = load i32, ptr %11, align 4, !tbaa !5
  %48 = load i32, ptr %9, align 4, !tbaa !5
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = sub nsw i32 %48, %49
  %51 = icmp sle i32 %47, %50
  br i1 %51, label %52, label %94

52:                                               ; preds = %46
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %53

53:                                               ; preds = %83, %52
  %54 = load i32, ptr %12, align 4, !tbaa !5
  %55 = load i32, ptr %8, align 4, !tbaa !5
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = sub nsw i32 %55, %56
  %58 = icmp sle i32 %54, %57
  br i1 %58, label %59, label %86

59:                                               ; preds = %53
  %60 = load i8, ptr %13, align 1, !tbaa !9
  %61 = load ptr, ptr %17, align 8, !tbaa !10
  %62 = load i32, ptr @pixel_width, align 4, !tbaa !5
  %63 = load i32, ptr %12, align 4, !tbaa !5
  %64 = mul nsw i32 %62, %63
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds i8, ptr %61, i64 %65
  store i8 %60, ptr %66, align 1, !tbaa !9
  %67 = load i8, ptr %14, align 1, !tbaa !9
  %68 = load ptr, ptr %17, align 8, !tbaa !10
  %69 = load i32, ptr @pixel_width, align 4, !tbaa !5
  %70 = load i32, ptr %12, align 4, !tbaa !5
  %71 = mul nsw i32 %69, %70
  %72 = add nsw i32 %71, 1
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds i8, ptr %68, i64 %73
  store i8 %67, ptr %74, align 1, !tbaa !9
  %75 = load i8, ptr %15, align 1, !tbaa !9
  %76 = load ptr, ptr %17, align 8, !tbaa !10
  %77 = load i32, ptr @pixel_width, align 4, !tbaa !5
  %78 = load i32, ptr %12, align 4, !tbaa !5
  %79 = mul nsw i32 %77, %78
  %80 = add nsw i32 %79, 2
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds i8, ptr %76, i64 %81
  store i8 %75, ptr %82, align 1, !tbaa !9
  br label %83

83:                                               ; preds = %59
  %84 = load i32, ptr %12, align 4, !tbaa !5
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %12, align 4, !tbaa !5
  br label %53, !llvm.loop !15

86:                                               ; preds = %53
  %87 = load i32, ptr @pitch, align 4, !tbaa !5
  %88 = load ptr, ptr %17, align 8, !tbaa !10
  %89 = sext i32 %87 to i64
  %90 = getelementptr inbounds i8, ptr %88, i64 %89
  store ptr %90, ptr %17, align 8, !tbaa !10
  br label %91

91:                                               ; preds = %86
  %92 = load i32, ptr %11, align 4, !tbaa !5
  %93 = add nsw i32 %92, 1
  store i32 %93, ptr %11, align 4, !tbaa !5
  br label %46, !llvm.loop !17

94:                                               ; preds = %46
  br label %95

95:                                               ; preds = %94, %5
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!13, !6, i64 140}
!13 = !{!"ProcessSlot", !11, i64 0, !14, i64 8}
!14 = !{!"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}
!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
@currentProcess = external local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @draw_filled_rectangle(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = trunc i32 %4 to i8
  %7 = lshr i32 %4, 8
  %8 = trunc i32 %7 to i8
  %9 = lshr i32 %4, 16
  %10 = trunc i32 %9 to i8
  %11 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 140
  %13 = load i32, ptr %12, align 4, !tbaa !9
  %14 = icmp ne i32 %13, 0
  %.not28 = icmp slt i32 %3, %1
  %or.cond = or i1 %14, %.not28
  %.not2526 = icmp slt i32 %2, %0
  %or.cond33 = or i1 %or.cond, %.not2526
  br i1 %or.cond33, label %.loopexit, label %.preheader.preheader

.preheader.preheader:                             ; preds = %5
  %15 = load ptr, ptr @screen, align 8, !tbaa !5
  %16 = load i32, ptr @pitch, align 4, !tbaa !13
  %17 = mul nsw i32 %16, %1
  %18 = load i32, ptr @pixel_width, align 4, !tbaa !13
  %19 = mul nsw i32 %18, %0
  %20 = add nsw i32 %17, %19
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %15, i64 %21
  %23 = add i32 %2, 1
  %24 = sub i32 %23, %0
  %25 = add i32 %3, 1
  %26 = sub i32 %25, %1
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %.030 = phi ptr [ %45, %._crit_edge ], [ %22, %.preheader.preheader ]
  %.02329 = phi i32 [ %46, %._crit_edge ], [ 0, %.preheader.preheader ]
  br label %27

27:                                               ; preds = %.preheader, %27
  %.02427 = phi i32 [ 0, %.preheader ], [ %42, %27 ]
  %28 = load i32, ptr @pixel_width, align 4, !tbaa !13
  %29 = mul nsw i32 %28, %.02427
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i8, ptr %.030, i64 %30
  store i8 %6, ptr %31, align 1, !tbaa !14
  %32 = load i32, ptr @pixel_width, align 4, !tbaa !13
  %33 = mul nsw i32 %32, %.02427
  %34 = add nsw i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i8, ptr %.030, i64 %35
  store i8 %8, ptr %36, align 1, !tbaa !14
  %37 = load i32, ptr @pixel_width, align 4, !tbaa !13
  %38 = mul nsw i32 %37, %.02427
  %39 = add nsw i32 %38, 2
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %.030, i64 %40
  store i8 %10, ptr %41, align 1, !tbaa !14
  %42 = add nuw i32 %.02427, 1
  %exitcond.not = icmp eq i32 %42, %24
  br i1 %exitcond.not, label %._crit_edge, label %27, !llvm.loop !15

._crit_edge:                                      ; preds = %27
  %43 = load i32, ptr @pitch, align 4, !tbaa !13
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %.030, i64 %44
  %46 = add nuw i32 %.02329, 1
  %exitcond32.not = icmp eq i32 %46, %26
  br i1 %exitcond32.not, label %.loopexit, label %.preheader, !llvm.loop !17

.loopexit:                                        ; preds = %._crit_edge, %5
  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, !12, i64 140}
!10 = !{!"ProcessSlot", !6, i64 0, !11, i64 8}
!11 = !{!"Process", !12, i64 0, !12, i64 4, !7, i64 8, !12, i64 128, !12, i64 132, !7, i64 136, !7, i64 192, !12, i64 352, !6, i64 360, !6, i64 368, !6, i64 376, !7, i64 384, !12, i64 464, !6, i64 472, !6, i64 480}
!12 = !{!"int", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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