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

@buffer_max_per_line = external hidden global i32, align 4
@buffer_max_per_column = external hidden global i32, align 4
@currentProcess = external global ptr, align 8
@buffer_position = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @nextLine() #0 {
  %1 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %2 = getelementptr inbounds %struct.ProcessSlot, ptr %1, i32 0, i32 1
  %3 = getelementptr inbounds %struct.Process, ptr %2, i32 0, i32 4
  %4 = load i32, ptr %3, align 4, !tbaa !9
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %6, label %23

6:                                                ; preds = %0
  %7 = load i32, ptr @buffer_position, align 4, !tbaa !13
  %8 = load i32, ptr @buffer_max_per_line, align 4, !tbaa !13
  %9 = sdiv i32 %7, %8
  %10 = load i32, ptr @buffer_max_per_column, align 4, !tbaa !13
  %11 = sub nsw i32 %10, 3
  %12 = icmp eq i32 %9, %11
  br i1 %12, label %13, label %14

13:                                               ; preds = %6
  call void @move_screen()
  br label %22

14:                                               ; preds = %6
  %15 = load i32, ptr @buffer_max_per_line, align 4, !tbaa !13
  %16 = load i32, ptr @buffer_position, align 4, !tbaa !13
  %17 = load i32, ptr @buffer_max_per_line, align 4, !tbaa !13
  %18 = srem i32 %16, %17
  %19 = sub nsw i32 %15, %18
  %20 = load i32, ptr @buffer_position, align 4, !tbaa !13
  %21 = add nsw i32 %20, %19
  store i32 %21, ptr @buffer_position, align 4, !tbaa !13
  br label %22

22:                                               ; preds = %14, %13
  br label %23

23:                                               ; preds = %22, %0
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @move_screen() #0

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

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


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

@buffer_max_per_line = external hidden local_unnamed_addr global i32, align 4
@buffer_max_per_column = 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: nounwind uwtable
define dso_local void @nextLine() local_unnamed_addr #0 {
  %1 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 140
  %3 = load i32, ptr %2, align 4, !tbaa !9
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %5, label %17

5:                                                ; preds = %0
  %6 = load i32, ptr @buffer_position, align 4, !tbaa !13
  %7 = load i32, ptr @buffer_max_per_line, align 4, !tbaa !13
  %8 = sdiv i32 %6, %7
  %9 = srem i32 %6, %7
  %10 = load i32, ptr @buffer_max_per_column, align 4, !tbaa !13
  %11 = add nsw i32 %10, -3
  %12 = icmp eq i32 %8, %11
  br i1 %12, label %13, label %14

13:                                               ; preds = %5
  tail call void @move_screen()
  br label %17

14:                                               ; preds = %5
  %15 = add i32 %7, %6
  %16 = sub i32 %15, %9
  store i32 %16, ptr @buffer_position, align 4, !tbaa !13
  br label %17

17:                                               ; preds = %13, %14, %0
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @move_screen() local_unnamed_addr #0

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

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


; Effective O3 passes: DivRemPairsPass,EarlyCSEPass,GlobalOptPass,InstCombinePass,ReassociatePass,SimplifyCFGPass,TailCallElimPass