; 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
declare dso_local void @draw_char(i8 noundef zeroext, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @print_char(i8 noundef zeroext %0) #0 {
  %2 = alloca i8, align 1
  store i8 %0, ptr %2, align 1, !tbaa !5
  %3 = load ptr, ptr @currentProcess, align 8, !tbaa !8
  %4 = getelementptr inbounds %struct.ProcessSlot, ptr %3, i32 0, i32 1
  %5 = getelementptr inbounds %struct.Process, ptr %4, i32 0, i32 4
  %6 = load i32, ptr %5, align 4, !tbaa !10
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %8, label %35

8:                                                ; preds = %1
  %9 = load i8, ptr %2, align 1, !tbaa !5
  %10 = zext i8 %9 to i32
  %11 = icmp eq i32 %10, 10
  br i1 %11, label %12, label %13

12:                                               ; preds = %8
  call void @nextLine()
  br label %35

13:                                               ; preds = %8
  %14 = load i8, ptr %2, align 1, !tbaa !5
  %15 = load i32, ptr @buffer_position, align 4, !tbaa !14
  %16 = load i32, ptr @buffer_max_per_line, align 4, !tbaa !14
  %17 = srem i32 %15, %16
  %18 = mul nsw i32 %17, 10
  %19 = load i32, ptr @buffer_position, align 4, !tbaa !14
  %20 = load i32, ptr @buffer_max_per_line, align 4, !tbaa !14
  %21 = sdiv i32 %19, %20
  %22 = mul nsw i32 %21, 15
  call void @draw_char(i8 noundef zeroext %14, i32 noundef %18, i32 noundef %22)
  %23 = load i32, ptr @buffer_position, align 4, !tbaa !14
  %24 = load i32, ptr @buffer_max_per_line, align 4, !tbaa !14
  %25 = sdiv i32 %23, %24
  %26 = load i32, ptr @buffer_max_per_column, align 4, !tbaa !14
  %27 = icmp eq i32 %25, %26
  br i1 %27, label %28, label %32

28:                                               ; preds = %13
  call void @move_screen()
  %29 = load i32, ptr @buffer_max_per_line, align 4, !tbaa !14
  %30 = load i32, ptr @buffer_position, align 4, !tbaa !14
  %31 = sub nsw i32 %30, %29
  store i32 %31, ptr @buffer_position, align 4, !tbaa !14
  br label %32

32:                                               ; preds = %28, %13
  %33 = load i32, ptr @buffer_position, align 4, !tbaa !14
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr @buffer_position, align 4, !tbaa !14
  br label %35

35:                                               ; preds = %32, %12, %1
  ret void
}

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

; 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !13, i64 140}
!11 = !{!"ProcessSlot", !9, i64 0, !12, i64 8}
!12 = !{!"Process", !13, i64 0, !13, i64 4, !6, i64 8, !13, i64 128, !13, i64 132, !6, i64 136, !6, i64 192, !13, i64 352, !9, i64 360, !9, i64 368, !9, i64 376, !6, i64 384, !13, i64 464, !9, i64 472, !9, i64 480}
!13 = !{!"int", !6, i64 0}
!14 = !{!13, !13, 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
declare dso_local void @draw_char(i8 noundef zeroext, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @print_char(i8 noundef zeroext %0) local_unnamed_addr #0 {
  %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 !9
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %6, label %28

6:                                                ; preds = %1
  %7 = icmp eq i8 %0, 10
  br i1 %7, label %8, label %9

8:                                                ; preds = %6
  tail call void @nextLine()
  br label %28

9:                                                ; preds = %6
  %10 = load i32, ptr @buffer_position, align 4, !tbaa !13
  %11 = load i32, ptr @buffer_max_per_line, align 4, !tbaa !13
  %12 = srem i32 %10, %11
  %13 = mul nsw i32 %12, 10
  %14 = sdiv i32 %10, %11
  %15 = mul nsw i32 %14, 15
  tail call void @draw_char(i8 noundef zeroext %0, i32 noundef %13, i32 noundef %15)
  %16 = load i32, ptr @buffer_position, align 4, !tbaa !13
  %17 = load i32, ptr @buffer_max_per_line, align 4, !tbaa !13
  %18 = sdiv i32 %16, %17
  %19 = load i32, ptr @buffer_max_per_column, align 4, !tbaa !13
  %20 = icmp eq i32 %18, %19
  br i1 %20, label %21, label %25

21:                                               ; preds = %9
  tail call void @move_screen()
  %22 = load i32, ptr @buffer_max_per_line, align 4, !tbaa !13
  %23 = load i32, ptr @buffer_position, align 4, !tbaa !13
  %24 = sub nsw i32 %23, %22
  br label %25

25:                                               ; preds = %21, %9
  %26 = phi i32 [ %24, %21 ], [ %16, %9 ]
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr @buffer_position, align 4, !tbaa !13
  br label %28

28:                                               ; preds = %25, %8, %1
  ret void
}

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

; 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: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,SROAPass,TailCallElimPass