; LLVM IR from BrunoLevy/learn-fpga :: FemtoRV/TUTORIALS/FROM_BLINKER_TO_RISCV/FIRMWARE (ST_NICCC)

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

%union.anon = type { i32 }

@spi_addr = external dso_local global i32, align 4
@spi_word_addr = external dso_local global i32, align 4
@f = external dso_local global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [26 x i8], align 1
@spi_u = external dso_local global %union.anon, align 4

declare i32 @printf(ptr noundef, ...) #0

; 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

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @next_spi_byte() #2 {
  %1 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 1, ptr %1) #4
  %2 = load ptr, ptr @f, align 8, !tbaa !5
  %3 = icmp eq ptr %2, null
  br i1 %3, label %4, label %11

4:                                                ; preds = %0
  %5 = call noalias ptr @fopen(ptr noundef @.str.2, ptr noundef @.str.3)
  store ptr %5, ptr @f, align 8, !tbaa !5
  %6 = load ptr, ptr @f, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %10

8:                                                ; preds = %4
  %9 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  call void @exit(i32 noundef -1) #5
  unreachable

10:                                               ; preds = %4
  br label %11

11:                                               ; preds = %10, %0
  %12 = load i32, ptr @spi_word_addr, align 4, !tbaa !9
  %13 = load i32, ptr @spi_addr, align 4, !tbaa !9
  %14 = lshr i32 %13, 2
  %15 = icmp ne i32 %12, %14
  br i1 %15, label %16, label %27

16:                                               ; preds = %11
  %17 = load i32, ptr @spi_addr, align 4, !tbaa !9
  %18 = lshr i32 %17, 2
  store i32 %18, ptr @spi_word_addr, align 4, !tbaa !9
  %19 = load ptr, ptr @f, align 8, !tbaa !5
  %20 = load i32, ptr @spi_word_addr, align 4, !tbaa !9
  %21 = mul i32 %20, 4
  %22 = sub i32 %21, 1048576
  %23 = zext i32 %22 to i64
  %24 = call i32 @fseek(ptr noundef %19, i64 noundef %23, i32 noundef 0)
  %25 = load ptr, ptr @f, align 8, !tbaa !5
  %26 = call i64 @fread(ptr noundef @spi_u, i64 noundef 4, i64 noundef 1, ptr noundef %25)
  br label %27

27:                                               ; preds = %16, %11
  %28 = load i32, ptr @spi_addr, align 4, !tbaa !9
  %29 = and i32 %28, 3
  %30 = zext i32 %29 to i64
  %31 = getelementptr inbounds [4 x i8], ptr @spi_u, i64 0, i64 %30
  %32 = load i8, ptr %31, align 1, !tbaa !11
  store i8 %32, ptr %1, align 1, !tbaa !11
  %33 = load i32, ptr @spi_addr, align 4, !tbaa !9
  %34 = add i32 %33, 1
  store i32 %34, ptr @spi_addr, align 4, !tbaa !9
  %35 = load i8, ptr %1, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 1, ptr %1) #4
  ret i8 %35
}

declare noalias ptr @fopen(ptr noundef, ptr noundef) #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #3

declare i32 @fseek(ptr noundef, i64 noundef, i32 noundef) #0

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { noreturn nounwind "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 #4 = { nounwind }
attributes #5 = { noreturn 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !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"

%union.anon = type { i32 }

@spi_addr = external dso_local local_unnamed_addr global i32, align 4
@spi_word_addr = external dso_local local_unnamed_addr global i32, align 4
@f = external dso_local local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [26 x i8], align 1
@spi_u = external dso_local global %union.anon, align 4

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @next_spi_byte() local_unnamed_addr #1 {
  %1 = load ptr, ptr @f, align 8, !tbaa !5
  %2 = icmp eq ptr %1, null
  br i1 %2, label %3, label %8

3:                                                ; preds = %0
  %4 = tail call noalias ptr @fopen(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.3)
  store ptr %4, ptr @f, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  br i1 %5, label %6, label %8

6:                                                ; preds = %3
  %7 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  tail call void @exit(i32 noundef -1) #3
  unreachable

8:                                                ; preds = %3, %0
  %9 = phi ptr [ %4, %3 ], [ %1, %0 ]
  %10 = load i32, ptr @spi_word_addr, align 4, !tbaa !9
  %11 = load i32, ptr @spi_addr, align 4, !tbaa !9
  %12 = lshr i32 %11, 2
  %.not = icmp eq i32 %10, %12
  br i1 %.not, label %20, label %13

13:                                               ; preds = %8
  store i32 %12, ptr @spi_word_addr, align 4, !tbaa !9
  %14 = and i32 %11, -4
  %15 = add i32 %14, -1048576
  %16 = zext i32 %15 to i64
  %17 = tail call i32 @fseek(ptr noundef nonnull %9, i64 noundef %16, i32 noundef 0)
  %18 = load ptr, ptr @f, align 8, !tbaa !5
  %19 = tail call i64 @fread(ptr noundef nonnull @spi_u, i64 noundef 4, i64 noundef 1, ptr noundef %18)
  %.pre = load i32, ptr @spi_addr, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %13, %8
  %21 = phi i32 [ %.pre, %13 ], [ %11, %8 ]
  %22 = and i32 %21, 3
  %23 = zext nneg i32 %22 to i64
  %24 = getelementptr inbounds [4 x i8], ptr @spi_u, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !11
  %26 = add i32 %21, 1
  store i32 %26, ptr @spi_addr, align 4, !tbaa !9
  ret i8 %25
}

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fseek(ptr nocapture noundef, i64 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind
declare noundef i64 @fread(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #0

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass