; LLVM IR from BrunoLevy/learn-fpga :: FemtoRV/FIRMWARE/LIBFEMTORV32 (keyboard)

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

; Function Attrs: nounwind uwtable
define dso_local i32 @UART_pollkey() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %6 = load volatile i32, ptr inttoptr (i64 4194312 to ptr), align 4, !tbaa !5
  store i32 %6, ptr %3, align 4, !tbaa !5
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = and i32 %7, 256
  %9 = icmp ne i32 %8, 0
  br i1 %9, label %11, label %10

10:                                               ; preds = %0
  store i32 0, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %44

11:                                               ; preds = %0
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = and i32 %12, 255
  store i32 %13, ptr %3, align 4, !tbaa !5
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = icmp ne i32 %14, 27
  br i1 %15, label %16, label %18

16:                                               ; preds = %11
  %17 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %17, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %44

18:                                               ; preds = %11
  %19 = call i64 (...) @milliseconds()
  store i64 %19, ptr %2, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %25, %18
  %21 = call i64 (...) @milliseconds()
  %22 = load i64, ptr %2, align 8, !tbaa !9
  %23 = sub i64 %21, %22
  %24 = icmp ult i64 %23, 10
  br i1 %24, label %25, label %26

25:                                               ; preds = %20
  br label %20, !llvm.loop !11

26:                                               ; preds = %20
  br label %27

27:                                               ; preds = %42, %26
  %28 = call i64 (...) @milliseconds()
  %29 = load i64, ptr %2, align 8, !tbaa !9
  %30 = sub i64 %28, %29
  %31 = icmp ult i64 %30, 100
  br i1 %31, label %32, label %43

32:                                               ; preds = %27
  %33 = load volatile i32, ptr inttoptr (i64 4194312 to ptr), align 4, !tbaa !5
  store i32 %33, ptr %4, align 4, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = and i32 %34, 256
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %42

37:                                               ; preds = %32
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = and i32 %38, 255
  store i32 %39, ptr %4, align 4, !tbaa !5
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = or i32 6912, %40
  store i32 %41, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %44

42:                                               ; preds = %32
  br label %27, !llvm.loop !13

43:                                               ; preds = %27
  store i32 27, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %44

44:                                               ; preds = %43, %37, %16, %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  %45 = load i32, ptr %1, align 4
  ret i32 %45
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare i64 @milliseconds(...) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 #3 = { 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 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 7168) i32 @UART_pollkey() local_unnamed_addr #0 {
  %1 = load volatile i32, ptr inttoptr (i64 4194312 to ptr), align 8, !tbaa !5
  %2 = and i32 %1, 256
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %.loopexit, label %3

3:                                                ; preds = %0
  %4 = and i32 %1, 255
  %.not11 = icmp eq i32 %4, 27
  br i1 %.not11, label %5, label %.loopexit

5:                                                ; preds = %3
  %6 = tail call i64 (...) @milliseconds() #2
  br label %7

7:                                                ; preds = %7, %5
  %8 = tail call i64 (...) @milliseconds() #2
  %9 = sub i64 %8, %6
  %10 = icmp ult i64 %9, 10
  br i1 %10, label %7, label %.preheader, !llvm.loop !9

.preheader:                                       ; preds = %7, %14
  %11 = tail call i64 (...) @milliseconds() #2
  %12 = sub i64 %11, %6
  %13 = icmp ult i64 %12, 100
  br i1 %13, label %14, label %.loopexit

14:                                               ; preds = %.preheader
  %15 = load volatile i32, ptr inttoptr (i64 4194312 to ptr), align 8, !tbaa !5
  %16 = and i32 %15, 256
  %.not12 = icmp eq i32 %16, 0
  br i1 %.not12, label %.preheader, label %17, !llvm.loop !11

17:                                               ; preds = %14
  %18 = and i32 %15, 255
  %19 = or disjoint i32 %18, 6912
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader, %3, %0, %17
  %.0 = phi i32 [ %19, %17 ], [ 0, %0 ], [ %4, %3 ], [ 27, %.preheader ]
  ret i32 %.0
}

declare i64 @milliseconds(...) local_unnamed_addr #1

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" }
attributes #1 = { "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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass