; LLVM IR from BrunoLevy/learn-fpga :: FemtoRV/FIRMWARE/LIBFEMTOGL (femtoGLtext)

; 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.GLFont = type { ptr, i8, i8 }

@GL_current_font = external dso_local global ptr, align 8
@cursor_Y = external hidden global i32, align 4
@scrolling = external hidden global i32, align 4
@display_start_line = external hidden global i32, align 4
@GL_height = external global i16, align 2
@GL_width = external global i16, align 2
@GL_bg = external global i16, align 2
@FGA_width = external global i16, align 2

declare void @oled1(i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @GL_tty_scroll() #1 {
  %1 = load i32, ptr @cursor_Y, align 4, !tbaa !5
  %2 = load i16, ptr @GL_height, align 2, !tbaa !9
  %3 = zext i16 %2 to i32
  %4 = icmp sge i32 %1, %3
  br i1 %4, label %5, label %6

5:                                                ; preds = %0
  store i32 1, ptr @scrolling, align 4, !tbaa !5
  store i32 0, ptr @cursor_Y, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %5, %0
  %7 = load i32, ptr @scrolling, align 4, !tbaa !5
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  br label %42

10:                                               ; preds = %6
  %11 = load i32, ptr @cursor_Y, align 4, !tbaa !5
  %12 = load i16, ptr @GL_width, align 2, !tbaa !9
  %13 = zext i16 %12 to i32
  %14 = sub nsw i32 %13, 1
  %15 = load i32, ptr @cursor_Y, align 4, !tbaa !5
  %16 = load ptr, ptr @GL_current_font, align 8, !tbaa !11
  %17 = getelementptr inbounds %struct.GLFont, ptr %16, i32 0, i32 2
  %18 = load i8, ptr %17, align 1, !tbaa !13
  %19 = zext i8 %18 to i32
  %20 = add nsw i32 %15, %19
  %21 = sub nsw i32 %20, 1
  %22 = load i16, ptr @GL_bg, align 2, !tbaa !9
  call void @GL_fill_rect(i32 noundef 0, i32 noundef %11, i32 noundef %14, i32 noundef %21, i16 noundef zeroext %22)
  %23 = load ptr, ptr @GL_current_font, align 8, !tbaa !11
  %24 = getelementptr inbounds %struct.GLFont, ptr %23, i32 0, i32 2
  %25 = load i8, ptr %24, align 1, !tbaa !13
  %26 = zext i8 %25 to i32
  %27 = load i32, ptr @display_start_line, align 4, !tbaa !5
  %28 = add nsw i32 %27, %26
  store i32 %28, ptr @display_start_line, align 4, !tbaa !5
  %29 = load i32, ptr @display_start_line, align 4, !tbaa !5
  %30 = load i16, ptr @GL_height, align 2, !tbaa !9
  %31 = zext i16 %30 to i32
  %32 = icmp sge i32 %29, %31
  br i1 %32, label %33, label %34

33:                                               ; preds = %10
  store i32 0, ptr @display_start_line, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %33, %10
  %35 = load i32, ptr @display_start_line, align 4, !tbaa !5
  call void @oled1(i32 noundef 161, i32 noundef %35)
  %36 = load i32, ptr @display_start_line, align 4, !tbaa !5
  %37 = load i16, ptr @FGA_width, align 2, !tbaa !9
  %38 = zext i16 %37 to i32
  %39 = mul nsw i32 %36, %38
  %40 = shl i32 %39, 8
  %41 = or i32 4, %40
  store volatile i32 %41, ptr inttoptr (i64 4198400 to ptr), align 4, !tbaa !5
  br label %42

42:                                               ; preds = %34, %9
  ret void
}

declare void @GL_fill_rect(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i16 noundef zeroext) #0

attributes #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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !7, i64 9}
!14 = !{!"", !12, i64 0, !7, i64 8, !7, i64 9}


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

@GL_current_font = external dso_local local_unnamed_addr global ptr, align 8
@cursor_Y = external hidden local_unnamed_addr global i32, align 4
@scrolling = external hidden local_unnamed_addr global i32, align 4
@display_start_line = external hidden local_unnamed_addr global i32, align 4
@GL_height = external local_unnamed_addr global i16, align 2
@GL_width = external local_unnamed_addr global i16, align 2
@GL_bg = external local_unnamed_addr global i16, align 2
@FGA_width = external local_unnamed_addr global i16, align 2

declare void @oled1(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @GL_tty_scroll() local_unnamed_addr #1 {
  %1 = load i32, ptr @cursor_Y, align 4, !tbaa !5
  %2 = load i16, ptr @GL_height, align 2, !tbaa !9
  %3 = zext i16 %2 to i32
  %.not = icmp slt i32 %1, %3
  br i1 %.not, label %4, label %.thread

.thread:                                          ; preds = %0
  store i32 1, ptr @scrolling, align 4, !tbaa !5
  store i32 0, ptr @cursor_Y, align 4, !tbaa !5
  br label %5

4:                                                ; preds = %0
  %.pr = load i32, ptr @scrolling, align 4, !tbaa !5
  %.not1 = icmp eq i32 %.pr, 0
  br i1 %.not1, label %31, label %5

5:                                                ; preds = %.thread, %4
  %6 = phi i32 [ 0, %.thread ], [ %1, %4 ]
  %7 = load i16, ptr @GL_width, align 2, !tbaa !9
  %8 = zext i16 %7 to i32
  %9 = add nsw i32 %8, -1
  %10 = load ptr, ptr @GL_current_font, align 8, !tbaa !11
  %11 = getelementptr inbounds i8, ptr %10, i64 9
  %12 = load i8, ptr %11, align 1, !tbaa !13
  %13 = zext i8 %12 to i32
  %14 = add i32 %6, -1
  %15 = add i32 %14, %13
  %16 = load i16, ptr @GL_bg, align 2, !tbaa !9
  tail call void @GL_fill_rect(i32 noundef 0, i32 noundef %6, i32 noundef %9, i32 noundef %15, i16 noundef zeroext %16) #2
  %17 = load ptr, ptr @GL_current_font, align 8, !tbaa !11
  %18 = getelementptr inbounds i8, ptr %17, i64 9
  %19 = load i8, ptr %18, align 1, !tbaa !13
  %20 = zext i8 %19 to i32
  %21 = load i32, ptr @display_start_line, align 4, !tbaa !5
  %22 = add nsw i32 %21, %20
  %23 = load i16, ptr @GL_height, align 2, !tbaa !9
  %24 = zext i16 %23 to i32
  %.not2 = icmp slt i32 %22, %24
  %spec.store.select = select i1 %.not2, i32 %22, i32 0
  store i32 %spec.store.select, ptr @display_start_line, align 4
  tail call void @oled1(i32 noundef 161, i32 noundef %spec.store.select) #2
  %25 = load i32, ptr @display_start_line, align 4, !tbaa !5
  %26 = load i16, ptr @FGA_width, align 2, !tbaa !9
  %27 = zext i16 %26 to i32
  %28 = shl i32 %25, 8
  %29 = mul i32 %28, %27
  %30 = or disjoint i32 %29, 4
  store volatile i32 %30, ptr inttoptr (i64 4198400 to ptr), align 4096, !tbaa !5
  br label %31

31:                                               ; preds = %4, %5
  ret void
}

declare void @GL_fill_rect(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i16 noundef zeroext) local_unnamed_addr #0

attributes #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 = { 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 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !7, i64 9}
!14 = !{!"", !12, i64 0, !7, i64 8, !7, i64 9}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferAlignmentPass,InstCombinePass,JumpThreadingPass,ReassociatePass,SimplifyCFGPass,TailCallElimPass