; 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_X = external hidden global i32, align 4
@cursor_Y = external hidden global i32, align 4
@GL_width = external global i16, align 2
@last_char_was_CR = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @GL_putchar(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %4 = load i32, ptr @last_char_was_CR, align 4, !tbaa !5
  %5 = icmp ne i32 %4, 0
  br i1 %5, label %6, label %7

6:                                                ; preds = %1
  call void @GL_tty_scroll()
  store i32 0, ptr @last_char_was_CR, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %6, %1
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = icmp eq i32 %8, 13
  br i1 %9, label %10, label %26

10:                                               ; preds = %7
  %11 = load i32, ptr @cursor_X, align 4, !tbaa !5
  %12 = load ptr, ptr @GL_current_font, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct.GLFont, ptr %12, i32 0, i32 1
  %14 = load i8, ptr %13, align 8, !tbaa !11
  %15 = zext i8 %14 to i32
  %16 = icmp sge i32 %11, %15
  br i1 %16, label %17, label %24

17:                                               ; preds = %10
  %18 = load ptr, ptr @GL_current_font, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.GLFont, ptr %18, i32 0, i32 1
  %20 = load i8, ptr %19, align 8, !tbaa !11
  %21 = zext i8 %20 to i32
  %22 = load i32, ptr @cursor_X, align 4, !tbaa !5
  %23 = sub nsw i32 %22, %21
  store i32 %23, ptr @cursor_X, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %17, %10
  %25 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %25, ptr %2, align 4
  br label %56

26:                                               ; preds = %7
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = icmp eq i32 %27, 10
  br i1 %28, label %29, label %37

29:                                               ; preds = %26
  store i32 1, ptr @last_char_was_CR, align 4, !tbaa !5
  store i32 0, ptr @cursor_X, align 4, !tbaa !5
  %30 = load ptr, ptr @GL_current_font, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.GLFont, ptr %30, i32 0, i32 2
  %32 = load i8, ptr %31, align 1, !tbaa !13
  %33 = zext i8 %32 to i32
  %34 = load i32, ptr @cursor_Y, align 4, !tbaa !5
  %35 = add nsw i32 %34, %33
  store i32 %35, ptr @cursor_Y, align 4, !tbaa !5
  %36 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %36, ptr %2, align 4
  br label %56

37:                                               ; preds = %26
  %38 = load i32, ptr @cursor_X, align 4, !tbaa !5
  %39 = load i32, ptr @cursor_Y, align 4, !tbaa !5
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = trunc i32 %40 to i8
  call void @GL_putchar_xy(i32 noundef %38, i32 noundef %39, i8 noundef signext %41)
  %42 = load ptr, ptr @GL_current_font, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.GLFont, ptr %42, i32 0, i32 1
  %44 = load i8, ptr %43, align 8, !tbaa !11
  %45 = zext i8 %44 to i32
  %46 = load i32, ptr @cursor_X, align 4, !tbaa !5
  %47 = add nsw i32 %46, %45
  store i32 %47, ptr @cursor_X, align 4, !tbaa !5
  %48 = load i32, ptr @cursor_X, align 4, !tbaa !5
  %49 = load i16, ptr @GL_width, align 2, !tbaa !14
  %50 = zext i16 %49 to i32
  %51 = icmp sge i32 %48, %50
  br i1 %51, label %52, label %54

52:                                               ; preds = %37
  %53 = call i32 @GL_putchar(i32 noundef 10)
  br label %54

54:                                               ; preds = %52, %37
  %55 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %55, ptr %2, align 4
  br label %56

56:                                               ; preds = %54, %29, %24
  %57 = load i32, ptr %2, align 4
  ret i32 %57
}

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

; Function Attrs: nounwind uwtable
declare dso_local void @GL_putchar_xy(i32 noundef, i32 noundef, i8 noundef signext) #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !7, i64 8}
!12 = !{!"", !10, i64 0, !7, i64 8, !7, i64 9}
!13 = !{!12, !7, i64 9}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !7, 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"

@GL_current_font = external dso_local local_unnamed_addr global ptr, align 8
@cursor_X = external hidden local_unnamed_addr global i32, align 4
@cursor_Y = external hidden local_unnamed_addr global i32, align 4
@GL_width = external local_unnamed_addr global i16, align 2
@last_char_was_CR = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @GL_putchar(i32 noundef returned %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @last_char_was_CR, align 4, !tbaa !5
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %4, label %3

3:                                                ; preds = %1
  tail call void @GL_tty_scroll()
  store i32 0, ptr @last_char_was_CR, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %3, %1
  switch i32 %0, label %20 [
    i32 13, label %5
    i32 10, label %13
  ]

5:                                                ; preds = %4
  %6 = load i32, ptr @cursor_X, align 4, !tbaa !5
  %7 = load ptr, ptr @GL_current_font, align 8, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %7, i64 8
  %9 = load i8, ptr %8, align 8, !tbaa !11
  %10 = zext i8 %9 to i32
  %.not10 = icmp slt i32 %6, %10
  br i1 %.not10, label %41, label %11

11:                                               ; preds = %5
  %12 = sub nsw i32 %6, %10
  store i32 %12, ptr @cursor_X, align 4, !tbaa !5
  br label %41

13:                                               ; preds = %4
  store i32 1, ptr @last_char_was_CR, align 4, !tbaa !5
  store i32 0, ptr @cursor_X, align 4, !tbaa !5
  %14 = load ptr, ptr @GL_current_font, align 8, !tbaa !9
  %15 = getelementptr inbounds i8, ptr %14, i64 9
  %16 = load i8, ptr %15, align 1, !tbaa !13
  %17 = zext i8 %16 to i32
  %18 = load i32, ptr @cursor_Y, align 4, !tbaa !5
  %19 = add nsw i32 %18, %17
  store i32 %19, ptr @cursor_Y, align 4, !tbaa !5
  br label %41

20:                                               ; preds = %4
  %21 = load i32, ptr @cursor_X, align 4, !tbaa !5
  %22 = load i32, ptr @cursor_Y, align 4, !tbaa !5
  %23 = trunc i32 %0 to i8
  tail call void @GL_putchar_xy(i32 noundef %21, i32 noundef %22, i8 noundef signext %23)
  %24 = load ptr, ptr @GL_current_font, align 8, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %24, i64 8
  %26 = load i8, ptr %25, align 8, !tbaa !11
  %27 = zext i8 %26 to i32
  %28 = load i32, ptr @cursor_X, align 4, !tbaa !5
  %29 = add nsw i32 %28, %27
  store i32 %29, ptr @cursor_X, align 4, !tbaa !5
  %30 = load i16, ptr @GL_width, align 2, !tbaa !14
  %31 = zext i16 %30 to i32
  %.not9 = icmp slt i32 %29, %31
  br i1 %.not9, label %41, label %32

32:                                               ; preds = %20
  %33 = load i32, ptr @last_char_was_CR, align 4, !tbaa !5
  %.not.i = icmp eq i32 %33, 0
  br i1 %.not.i, label %GL_putchar.exit, label %34

34:                                               ; preds = %32
  tail call void @GL_tty_scroll()
  %.pre = load ptr, ptr @GL_current_font, align 8, !tbaa !9
  br label %GL_putchar.exit

GL_putchar.exit:                                  ; preds = %32, %34
  %35 = phi ptr [ %24, %32 ], [ %.pre, %34 ]
  store i32 1, ptr @last_char_was_CR, align 4, !tbaa !5
  store i32 0, ptr @cursor_X, align 4, !tbaa !5
  %36 = getelementptr inbounds i8, ptr %35, i64 9
  %37 = load i8, ptr %36, align 1, !tbaa !13
  %38 = zext i8 %37 to i32
  %39 = load i32, ptr @cursor_Y, align 4, !tbaa !5
  %40 = add nsw i32 %39, %38
  store i32 %40, ptr @cursor_Y, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %20, %GL_putchar.exit, %5, %11, %13
  ret i32 %0
}

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

; Function Attrs: nounwind uwtable
declare dso_local void @GL_putchar_xy(i32 noundef, i32 noundef, i8 noundef signext) 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !7, i64 8}
!12 = !{!"", !10, i64 0, !7, i64 8, !7, i64 9}
!13 = !{!12, !7, i64 9}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !7, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InlinerPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass