; LLVM IR from lichen-community-systems/signaletic :: hosts/daisy/vendor/libDaisy/Drivers/STM32H7xx_HAL_Driver/Src (ReturnToReadMode)

; 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.NOR_HandleTypeDef = type { ptr, ptr, %struct.FMC_NORSRAM_InitTypeDef, i32, i32 }
%struct.FMC_NORSRAM_InitTypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @__DSB() #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @HAL_NOR_ReturnToReadMode(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.NOR_HandleTypeDef, ptr %7, i32 0, i32 4
  %9 = load volatile i32, ptr %8, align 8, !tbaa !9
  store i32 %9, ptr %5, align 4, !tbaa !13
  %10 = load i32, ptr %5, align 4, !tbaa !13
  %11 = icmp eq i32 %10, 2
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  store i32 2, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %75

13:                                               ; preds = %1
  %14 = load i32, ptr %5, align 4, !tbaa !13
  %15 = icmp eq i32 %14, 1
  br i1 %15, label %19, label %16

16:                                               ; preds = %13
  %17 = load i32, ptr %5, align 4, !tbaa !13
  %18 = icmp eq i32 %17, 4
  br i1 %18, label %19, label %72

19:                                               ; preds = %16, %13
  br label %20

20:                                               ; preds = %19
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.NOR_HandleTypeDef, ptr %21, i32 0, i32 3
  %23 = load i32, ptr %22, align 4, !tbaa !14
  %24 = icmp eq i32 %23, 1
  br i1 %24, label %25, label %26

25:                                               ; preds = %20
  store i32 2, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %75

26:                                               ; preds = %20
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.NOR_HandleTypeDef, ptr %27, i32 0, i32 3
  store i32 1, ptr %28, align 4, !tbaa !14
  br label %29

29:                                               ; preds = %26
  br label %30

30:                                               ; preds = %29
  br label %31

31:                                               ; preds = %30
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.NOR_HandleTypeDef, ptr %32, i32 0, i32 4
  store volatile i32 2, ptr %33, align 8, !tbaa !9
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.NOR_HandleTypeDef, ptr %34, i32 0, i32 2
  %36 = getelementptr inbounds %struct.FMC_NORSRAM_InitTypeDef, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 8, !tbaa !15
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %39, label %40

39:                                               ; preds = %31
  store i32 1610612736, ptr %4, align 4, !tbaa !13
  br label %57

40:                                               ; preds = %31
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.NOR_HandleTypeDef, ptr %41, i32 0, i32 2
  %43 = getelementptr inbounds %struct.FMC_NORSRAM_InitTypeDef, ptr %42, i32 0, i32 0
  %44 = load i32, ptr %43, align 8, !tbaa !15
  %45 = icmp eq i32 %44, 2
  br i1 %45, label %46, label %47

46:                                               ; preds = %40
  store i32 1677721600, ptr %4, align 4, !tbaa !13
  br label %56

47:                                               ; preds = %40
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.NOR_HandleTypeDef, ptr %48, i32 0, i32 2
  %50 = getelementptr inbounds %struct.FMC_NORSRAM_InitTypeDef, ptr %49, i32 0, i32 0
  %51 = load i32, ptr %50, align 8, !tbaa !15
  %52 = icmp eq i32 %51, 4
  br i1 %52, label %53, label %54

53:                                               ; preds = %47
  store i32 1744830464, ptr %4, align 4, !tbaa !13
  br label %55

54:                                               ; preds = %47
  store i32 1811939328, ptr %4, align 4, !tbaa !13
  br label %55

55:                                               ; preds = %54, %53
  br label %56

56:                                               ; preds = %55, %46
  br label %57

57:                                               ; preds = %56, %39
  br label %58

58:                                               ; preds = %57
  %59 = load i32, ptr %4, align 4, !tbaa !13
  %60 = zext i32 %59 to i64
  %61 = inttoptr i64 %60 to ptr
  store volatile i16 240, ptr %61, align 2, !tbaa !16
  call void @__DSB()
  br label %62

62:                                               ; preds = %58
  br label %63

63:                                               ; preds = %62
  %64 = load i32, ptr %5, align 4, !tbaa !13
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.NOR_HandleTypeDef, ptr %65, i32 0, i32 4
  store volatile i32 %64, ptr %66, align 8, !tbaa !9
  br label %67

67:                                               ; preds = %63
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.NOR_HandleTypeDef, ptr %68, i32 0, i32 3
  store i32 0, ptr %69, align 4, !tbaa !14
  br label %70

70:                                               ; preds = %67
  br label %71

71:                                               ; preds = %70
  br label %73

72:                                               ; preds = %16
  store i32 1, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %75

73:                                               ; preds = %71
  br label %74

74:                                               ; preds = %73
  store i32 0, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %75

75:                                               ; preds = %74, %72, %25, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %76 = load i32, ptr %2, align 4
  ret i32 %76
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { alwaysinline 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 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 = { 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, !12, i64 80}
!10 = !{!"", !6, i64 0, !6, i64 8, !11, i64 16, !12, i64 76, !12, i64 80}
!11 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44, !12, i64 48, !12, i64 52, !12, i64 56}
!12 = !{!"int", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!10, !12, i64 76}
!15 = !{!10, !12, i64 16}
!16 = !{!17, !17, i64 0}
!17 = !{!"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"

@switch.table.HAL_NOR_ReturnToReadMode = private unnamed_addr constant [5 x i64] [i64 1610612736, i64 1811939328, i64 1677721600, i64 1811939328, i64 1744830464], align 8

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @__DSB() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 3) i32 @HAL_NOR_ReturnToReadMode(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 80
  %3 = load volatile i32, ptr %2, align 8, !tbaa !5
  switch i32 %3, label %.fold.split [
    i32 2, label %15
    i32 4, label %4
    i32 1, label %4
  ]

4:                                                ; preds = %1, %1
  %5 = getelementptr inbounds i8, ptr %0, i64 76
  %6 = load i32, ptr %5, align 4, !tbaa !12
  %7 = icmp eq i32 %6, 1
  br i1 %7, label %15, label %8

8:                                                ; preds = %4
  store i32 1, ptr %5, align 4, !tbaa !12
  store volatile i32 2, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load i32, ptr %9, align 8, !tbaa !13
  %11 = icmp ult i32 %10, 5
  br i1 %11, label %switch.lookup, label %13

switch.lookup:                                    ; preds = %8
  %12 = zext nneg i32 %10 to i64
  %switch.gep = getelementptr inbounds [5 x i64], ptr @switch.table.HAL_NOR_ReturnToReadMode, i64 0, i64 %12
  %switch.load = load i64, ptr %switch.gep, align 8
  br label %13

13:                                               ; preds = %8, %switch.lookup
  %.015 = phi i64 [ %switch.load, %switch.lookup ], [ 1811939328, %8 ]
  %14 = inttoptr i64 %.015 to ptr
  store volatile i16 240, ptr %14, align 67108864, !tbaa !14
  tail call void @__DSB()
  store volatile i32 %3, ptr %2, align 8, !tbaa !5
  store i32 0, ptr %5, align 4, !tbaa !12
  br label %15

.fold.split:                                      ; preds = %1
  br label %15

15:                                               ; preds = %1, %.fold.split, %4, %13
  %.0 = phi i32 [ 0, %13 ], [ %3, %1 ], [ 2, %4 ], [ 1, %.fold.split ]
  ret i32 %.0
}

attributes #0 = { alwaysinline 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 = { 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, !11, i64 80}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16, !11, i64 76, !11, i64 80}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !11, i64 76}
!13 = !{!6, !11, i64 16}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !8, i64 0}


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