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

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

@GUI_button.btn_state = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @GUI_button() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 -1, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %6 = load volatile i32, ptr inttoptr (i64 4196352 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 = load i32, ptr @GUI_button.btn_state, align 4, !tbaa !5
  %9 = icmp ne i32 %7, %8
  br i1 %9, label %10, label %41

10:                                               ; preds = %0
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %35, %10
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = icmp slt i32 %12, 6
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  store i32 2, ptr %5, align 4
  br label %38

15:                                               ; preds = %11
  %16 = load i32, ptr @GUI_button.btn_state, align 4, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = shl i32 1, %17
  %19 = and i32 %16, %18
  %20 = load i32, ptr %3, align 4, !tbaa !5
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = shl i32 1, %21
  %23 = and i32 %20, %22
  %24 = icmp ne i32 %19, %23
  br i1 %24, label %25, label %34

25:                                               ; preds = %15
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = shl i32 1, %27
  %29 = and i32 %26, %28
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %33

31:                                               ; preds = %25
  %32 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %32, ptr %2, align 4, !tbaa !5
  store i32 2, ptr %5, align 4
  br label %38

33:                                               ; preds = %25
  br label %34

34:                                               ; preds = %33, %15
  br label %35

35:                                               ; preds = %34
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %4, align 4, !tbaa !5
  br label %11, !llvm.loop !9

38:                                               ; preds = %31, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %40, ptr @GUI_button.btn_state, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %39, %0
  %42 = call i32 (...) @UART_pollkey()
  store i32 %42, ptr %1, align 4, !tbaa !5
  %43 = load i32, ptr %1, align 4, !tbaa !5
  switch i32 %43, label %48 [
    i32 6977, label %44
    i32 6978, label %45
    i32 6979, label %46
    i32 13, label %46
    i32 32, label %46
    i32 6980, label %47
  ]

44:                                               ; preds = %41
  store i32 2, ptr %2, align 4, !tbaa !5
  br label %48

45:                                               ; preds = %41
  store i32 3, ptr %2, align 4, !tbaa !5
  br label %48

46:                                               ; preds = %41, %41, %41
  store i32 5, ptr %2, align 4, !tbaa !5
  br label %48

47:                                               ; preds = %41
  store i32 4, ptr %2, align 4, !tbaa !5
  br label %48

48:                                               ; preds = %47, %46, %45, %44, %41
  %49 = load i32, ptr %2, align 4, !tbaa !5
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret i32 %49
}

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

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

declare i32 @UART_pollkey(...) #2

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


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

@GUI_button.btn_state = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @GUI_button() local_unnamed_addr #0 {
  %1 = load volatile i32, ptr inttoptr (i64 4196352 to ptr), align 2048, !tbaa !5
  %2 = load i32, ptr @GUI_button.btn_state, align 4, !tbaa !5
  %.not = icmp eq i32 %1, %2
  br i1 %.not, label %22, label %.preheader

.preheader:                                       ; preds = %0
  %3 = xor i32 %2, -1
  %4 = and i32 %3, 1
  %5 = and i32 %4, %1
  %or.cond = icmp eq i32 %5, 0
  br i1 %or.cond, label %6, label %21

6:                                                ; preds = %.preheader
  %7 = and i32 %3, 2
  %8 = and i32 %7, %1
  %or.cond.1 = icmp eq i32 %8, 0
  br i1 %or.cond.1, label %9, label %21

9:                                                ; preds = %6
  %10 = and i32 %3, 4
  %11 = and i32 %10, %1
  %or.cond.2 = icmp eq i32 %11, 0
  br i1 %or.cond.2, label %12, label %21

12:                                               ; preds = %9
  %13 = and i32 %3, 8
  %14 = and i32 %13, %1
  %or.cond.3 = icmp eq i32 %14, 0
  br i1 %or.cond.3, label %15, label %21

15:                                               ; preds = %12
  %16 = and i32 %3, 16
  %17 = and i32 %16, %1
  %or.cond.4 = icmp eq i32 %17, 0
  br i1 %or.cond.4, label %18, label %21

18:                                               ; preds = %15
  %19 = and i32 %3, 32
  %20 = and i32 %19, %1
  %or.cond.5 = icmp eq i32 %20, 0
  %spec.select = select i1 %or.cond.5, i32 -1, i32 5
  br label %21

21:                                               ; preds = %18, %15, %12, %9, %6, %.preheader
  %.012 = phi i32 [ 0, %.preheader ], [ 1, %6 ], [ 2, %9 ], [ 3, %12 ], [ 4, %15 ], [ %spec.select, %18 ]
  store i32 %1, ptr @GUI_button.btn_state, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %21, %0
  %.1 = phi i32 [ %.012, %21 ], [ -1, %0 ]
  %23 = tail call i32 (...) @UART_pollkey() #2
  switch i32 %23, label %28 [
    i32 6977, label %24
    i32 6978, label %25
    i32 6979, label %26
    i32 13, label %26
    i32 32, label %26
    i32 6980, label %27
  ]

24:                                               ; preds = %22
  br label %28

25:                                               ; preds = %22
  br label %28

26:                                               ; preds = %22, %22, %22
  br label %28

27:                                               ; preds = %22
  br label %28

28:                                               ; preds = %27, %26, %25, %24, %22
  %.2 = phi i32 [ %.1, %22 ], [ 4, %27 ], [ 5, %26 ], [ 3, %25 ], [ 2, %24 ]
  ret i32 %.2
}

declare i32 @UART_pollkey(...) 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"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,LoopSimplifyPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass