; LLVM IR from embeddednirvana/A-Rudimentary-RTOS-for-Satellite-Control :: device (sd)

; 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._AT91S_PIO = type { i32, i32, i32, [1 x i32], i32, i32, i32, [1 x i32], i32, i32, i32, [1 x i32], i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, [1 x i32], i32, i32, i32, [1 x i32], i32, i32, i32, [9 x i32], i32, i32, i32 }

@error = external dso_local global i8, align 1
@arguments = external dso_local global [4 x i8], align 1
@response = external dso_local global [8 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare dso_local void @sd_delay(i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @sd_send_command(ptr noundef, i8 noundef zeroext, i8 noundef zeroext) #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
declare dso_local void @sd_packarg(ptr noundef, i32 noundef) #1

declare void @SPI_Write(ptr noundef, i32 noundef, i16 noundef zeroext) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @sd_write_block(ptr noundef %0, i32 noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %10 = load volatile i32, ptr getelementptr inbounds (%struct._AT91S_PIO, ptr inttoptr (i64 4294964224 to ptr), i32 0, i32 14), align 4, !tbaa !11
  %11 = and i32 %10, -8193
  store volatile i32 %11, ptr getelementptr inbounds (%struct._AT91S_PIO, ptr inttoptr (i64 4294964224 to ptr), i32 0, i32 14), align 4, !tbaa !11
  %12 = load i32, ptr %6, align 4, !tbaa !9
  call void @sd_packarg(ptr noundef @arguments, i32 noundef %12)
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = call i32 @sd_send_command(ptr noundef %13, i8 noundef zeroext 24, i8 noundef zeroext 1)
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %51

17:                                               ; preds = %3
  %18 = load i8, ptr @response, align 1, !tbaa !13
  %19 = zext i8 %18 to i32
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %28

21:                                               ; preds = %17
  %22 = load i8, ptr @error, align 1, !tbaa !13
  %23 = zext i8 %22 to i32
  %24 = or i32 %23, 32
  %25 = trunc i32 %24 to i8
  store i8 %25, ptr @error, align 1, !tbaa !13
  %26 = load volatile i32, ptr getelementptr inbounds (%struct._AT91S_PIO, ptr inttoptr (i64 4294964224 to ptr), i32 0, i32 14), align 4, !tbaa !11
  %27 = or i32 %26, 8192
  store volatile i32 %27, ptr getelementptr inbounds (%struct._AT91S_PIO, ptr inttoptr (i64 4294964224 to ptr), i32 0, i32 14), align 4, !tbaa !11
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %51

28:                                               ; preds = %17
  call void @sd_delay(i16 noundef zeroext 2)
  call void @SPI_Write(ptr noundef inttoptr (i64 4294836224 to ptr), i32 noundef 1, i16 noundef zeroext 254)
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %39, %28
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = icmp slt i32 %30, 512
  br i1 %31, label %32, label %42

32:                                               ; preds = %29
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i8, ptr %33, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !13
  %38 = zext i8 %37 to i16
  call void @SPI_Write(ptr noundef inttoptr (i64 4294836224 to ptr), i32 noundef 1, i16 noundef zeroext %38)
  br label %39

39:                                               ; preds = %32
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %8, align 4, !tbaa !9
  br label %29, !llvm.loop !14

42:                                               ; preds = %29
  call void @SPI_Write(ptr noundef inttoptr (i64 4294836224 to ptr), i32 noundef 1, i16 noundef zeroext 149)
  call void @sd_delay(i16 noundef zeroext 2)
  br label %43

43:                                               ; preds = %47, %42
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = call i32 @is_card_busy(ptr noundef %44)
  %46 = icmp ne i32 %45, 255
  br i1 %46, label %47, label %48

47:                                               ; preds = %43
  br label %43, !llvm.loop !16

48:                                               ; preds = %43
  call void @sd_delay(i16 noundef zeroext 4)
  %49 = load volatile i32, ptr getelementptr inbounds (%struct._AT91S_PIO, ptr inttoptr (i64 4294964224 to ptr), i32 0, i32 14), align 4, !tbaa !11
  %50 = or i32 %49, 8192
  store volatile i32 %50, ptr getelementptr inbounds (%struct._AT91S_PIO, ptr inttoptr (i64 4294964224 to ptr), i32 0, i32 14), align 4, !tbaa !11
  store i32 1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %51

51:                                               ; preds = %48, %21, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %52 = load i32, ptr %4, align 4
  ret i32 %52
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @is_card_busy(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 56}
!12 = !{!"_AT91S_PIO", !10, i64 0, !10, i64 4, !10, i64 8, !7, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !7, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !7, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !10, i64 64, !10, i64 68, !10, i64 72, !10, i64 76, !10, i64 80, !10, i64 84, !10, i64 88, !7, i64 92, !10, i64 96, !10, i64 100, !10, i64 104, !7, i64 108, !10, i64 112, !10, i64 116, !10, i64 120, !7, i64 124, !10, i64 160, !10, i64 164, !10, i64 168}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

@error = external dso_local local_unnamed_addr global i8, align 1
@arguments = external dso_local global [4 x i8], align 1
@response = external dso_local local_unnamed_addr global [8 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @sd_delay(i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @sd_send_command(ptr noundef, i8 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @sd_packarg(ptr noundef, i32 noundef) local_unnamed_addr #0

declare void @SPI_Write(ptr noundef, i32 noundef, i16 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @sd_write_block(ptr noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = load volatile i32, ptr inttoptr (i64 4294964280 to ptr), align 8, !tbaa !5
  %5 = and i32 %4, -8193
  store volatile i32 %5, ptr inttoptr (i64 4294964280 to ptr), align 8, !tbaa !5
  tail call void @sd_packarg(ptr noundef nonnull @arguments, i32 noundef %1)
  %6 = tail call i32 @sd_send_command(ptr noundef %0, i8 noundef zeroext 24, i8 noundef zeroext 1)
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %24, label %8

8:                                                ; preds = %3
  %9 = load i8, ptr @response, align 1, !tbaa !10
  %.not = icmp eq i8 %9, 0
  br i1 %.not, label %13, label %10

10:                                               ; preds = %8
  %11 = load i8, ptr @error, align 1, !tbaa !10
  %12 = or i8 %11, 32
  store i8 %12, ptr @error, align 1, !tbaa !10
  br label %.sink.split

13:                                               ; preds = %8
  tail call void @sd_delay(i16 noundef zeroext 2)
  tail call void @SPI_Write(ptr noundef nonnull inttoptr (i64 4294836224 to ptr), i32 noundef 1, i16 noundef zeroext 254) #2
  br label %14

14:                                               ; preds = %13, %14
  %indvars.iv = phi i64 [ 0, %13 ], [ %indvars.iv.next, %14 ]
  %15 = getelementptr inbounds i8, ptr %2, i64 %indvars.iv
  %16 = load i8, ptr %15, align 1, !tbaa !10
  %17 = zext i8 %16 to i16
  tail call void @SPI_Write(ptr noundef nonnull inttoptr (i64 4294836224 to ptr), i32 noundef 1, i16 noundef zeroext %17) #2
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 512
  br i1 %exitcond.not, label %18, label %14, !llvm.loop !11

18:                                               ; preds = %14
  tail call void @SPI_Write(ptr noundef nonnull inttoptr (i64 4294836224 to ptr), i32 noundef 1, i16 noundef zeroext 149) #2
  tail call void @sd_delay(i16 noundef zeroext 2)
  br label %19

19:                                               ; preds = %19, %18
  %20 = tail call i32 @is_card_busy(ptr noundef %0)
  %.not8 = icmp eq i32 %20, 255
  br i1 %.not8, label %21, label %19, !llvm.loop !13

21:                                               ; preds = %19
  tail call void @sd_delay(i16 noundef zeroext 4)
  br label %.sink.split

.sink.split:                                      ; preds = %10, %21
  %.07.ph = phi i32 [ 1, %21 ], [ 0, %10 ]
  %22 = load volatile i32, ptr inttoptr (i64 4294964280 to ptr), align 8, !tbaa !5
  %23 = or i32 %22, 8192
  store volatile i32 %23, ptr inttoptr (i64 4294964280 to ptr), align 8, !tbaa !5
  br label %24

24:                                               ; preds = %.sink.split, %3
  %.07 = phi i32 [ 0, %3 ], [ %.07.ph, %.sink.split ]
  ret i32 %.07
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @is_card_busy(ptr noundef) 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" }
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, !7, i64 56}
!6 = !{!"_AT91S_PIO", !7, i64 0, !7, i64 4, !7, i64 8, !8, i64 12, !7, i64 16, !7, i64 20, !7, i64 24, !8, i64 28, !7, i64 32, !7, i64 36, !7, i64 40, !8, i64 44, !7, i64 48, !7, i64 52, !7, i64 56, !7, i64 60, !7, i64 64, !7, i64 68, !7, i64 72, !7, i64 76, !7, i64 80, !7, i64 84, !7, i64 88, !8, i64 92, !7, i64 96, !7, i64 100, !7, i64 104, !8, i64 108, !7, i64 112, !7, i64 116, !7, i64 120, !8, i64 124, !7, i64 160, !7, i64 164, !7, i64 168}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!8, !8, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass