; LLVM IR from trabucayre/openFPGALoader :: src/anlogic.ll (ZN7Anlogic7spi_putEPKhPhj)

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

%class.Device = type <{ ptr, ptr, %"class.std::__cxx11::basic_string", %"class.std::__cxx11::basic_string", i32, i8, i8, i8, i8 }>
%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }

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

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

declare noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193), ptr noundef, ptr noundef, i32 noundef, i32 noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

declare noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7Anlogic7spi_putEPKhPhj(ptr noundef nonnull align 8 dereferenceable(144) %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) unnamed_addr #3 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  %14 = alloca i8, align 1
  %15 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !9
  %16 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %17 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %17, ptr %9, align 4, !tbaa !9
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %23

20:                                               ; preds = %4
  %21 = load i32, ptr %9, align 4, !tbaa !9
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %9, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %20, %4
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = zext i32 %24 to i64
  %26 = call ptr @llvm.stacksave.p0()
  store ptr %26, ptr %10, align 8
  %27 = alloca i8, i64 %25, align 16
  store i64 %25, ptr %11, align 8
  %28 = load i32, ptr %9, align 4, !tbaa !9
  %29 = zext i32 %28 to i64
  %30 = alloca i8, i64 %29, align 16
  store i64 %29, ptr %12, align 8
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = icmp ne ptr %31, null
  br i1 %32, label %33, label %53

33:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %49, %33
  %35 = load i32, ptr %13, align 4, !tbaa !9
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = icmp ult i32 %35, %36
  br i1 %37, label %39, label %38

38:                                               ; preds = %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  br label %52

39:                                               ; preds = %34
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = load i32, ptr %13, align 4, !tbaa !9
  %42 = zext i32 %41 to i64
  %43 = getelementptr inbounds i8, ptr %40, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !11
  %45 = call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %44)
  %46 = load i32, ptr %13, align 4, !tbaa !9
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds i8, ptr %27, i64 %47
  store i8 %45, ptr %48, align 1, !tbaa !11
  br label %49

49:                                               ; preds = %39
  %50 = load i32, ptr %13, align 4, !tbaa !9
  %51 = add i32 %50, 1
  store i32 %51, ptr %13, align 4, !tbaa !9
  br label %34, !llvm.loop !12

52:                                               ; preds = %38
  br label %53

53:                                               ; preds = %52, %23
  call void @llvm.lifetime.start.p0(i64 1, ptr %14) #4
  store i8 96, ptr %14, align 1, !tbaa !11
  %54 = getelementptr inbounds %class.Device, ptr %16, i32 0, i32 1
  %55 = load ptr, ptr %54, align 8, !tbaa !14
  %56 = call noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %55, ptr noundef %14, ptr noundef null, i32 noundef 8, i32 noundef 1)
  %57 = getelementptr inbounds %class.Device, ptr %16, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !14
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = icmp eq ptr %59, null
  br i1 %60, label %61, label %62

61:                                               ; preds = %53
  br label %63

62:                                               ; preds = %53
  br label %63

63:                                               ; preds = %62, %61
  %64 = phi ptr [ null, %61 ], [ %30, %62 ]
  %65 = load i32, ptr %9, align 4, !tbaa !9
  %66 = mul nsw i32 8, %65
  %67 = call noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %58, ptr noundef %27, ptr noundef %64, i32 noundef %66, i32 noundef 1)
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = icmp ne ptr %68, null
  br i1 %69, label %70, label %103

70:                                               ; preds = %63
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %71

71:                                               ; preds = %99, %70
  %72 = load i32, ptr %15, align 4, !tbaa !9
  %73 = load i32, ptr %8, align 4, !tbaa !9
  %74 = icmp ult i32 %72, %73
  br i1 %74, label %76, label %75

75:                                               ; preds = %71
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  br label %102

76:                                               ; preds = %71
  %77 = load i32, ptr %15, align 4, !tbaa !9
  %78 = zext i32 %77 to i64
  %79 = getelementptr inbounds i8, ptr %30, i64 %78
  %80 = load i8, ptr %79, align 1, !tbaa !11
  %81 = zext i8 %80 to i32
  %82 = ashr i32 %81, 1
  %83 = trunc i32 %82 to i8
  %84 = call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %83)
  %85 = zext i8 %84 to i32
  %86 = load i32, ptr %15, align 4, !tbaa !9
  %87 = add i32 %86, 1
  %88 = zext i32 %87 to i64
  %89 = getelementptr inbounds i8, ptr %30, i64 %88
  %90 = load i8, ptr %89, align 1, !tbaa !11
  %91 = zext i8 %90 to i32
  %92 = and i32 %91, 1
  %93 = or i32 %85, %92
  %94 = trunc i32 %93 to i8
  %95 = load ptr, ptr %7, align 8, !tbaa !5
  %96 = load i32, ptr %15, align 4, !tbaa !9
  %97 = zext i32 %96 to i64
  %98 = getelementptr inbounds i8, ptr %95, i64 %97
  store i8 %94, ptr %98, align 1, !tbaa !11
  br label %99

99:                                               ; preds = %76
  %100 = load i32, ptr %15, align 4, !tbaa !9
  %101 = add i32 %100, 1
  store i32 %101, ptr %15, align 4, !tbaa !9
  br label %71, !llvm.loop !21

102:                                              ; preds = %75
  br label %103

103:                                              ; preds = %102, %63
  call void @llvm.lifetime.end.p0(i64 1, ptr %14) #4
  %104 = load ptr, ptr %10, align 8
  call void @llvm.stackrestore.p0(ptr %104)
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret i32 0
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { mustprogress 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 #4 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 8}
!15 = !{!"_ZTS6Device", !6, i64 8, !16, i64 16, !16, i64 48, !19, i64 80, !20, i64 84, !20, i64 85, !20, i64 86}
!16 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", !17, i64 0, !18, i64 8, !7, i64 16}
!17 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderE", !6, i64 0}
!18 = !{!"long", !7, i64 0}
!19 = !{!"_ZTSN6Device9prog_modeE", !7, i64 0}
!20 = !{!"bool", !7, i64 0}
!21 = distinct !{!21, !13}


; 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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

declare noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193), ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

declare noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7Anlogic7spi_putEPKhPhj(ptr nocapture noundef nonnull readonly align 8 dereferenceable(144) %0, ptr noundef readonly %1, ptr noundef writeonly %2, i32 noundef %3) unnamed_addr #2 align 2 {
  %5 = alloca i8, align 1
  %.not = icmp ne ptr %2, null
  %6 = zext i1 %.not to i32
  %spec.select = add nsw i32 %6, %3
  %7 = zext i32 %spec.select to i64
  %8 = alloca i8, i64 %7, align 16
  %9 = alloca i8, i64 %7, align 16
  %.not27 = icmp ne ptr %1, null
  %10 = icmp ne i32 %3, 0
  %or.cond = and i1 %.not27, %10
  br i1 %or.cond, label %.lr.ph.preheader, label %.loopexit29

.lr.ph.preheader:                                 ; preds = %4
  %wide.trip.count = zext i32 %3 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %11 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv
  %12 = load i8, ptr %11, align 1, !tbaa !5
  %13 = tail call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %12)
  %14 = getelementptr inbounds i8, ptr %8, i64 %indvars.iv
  store i8 %13, ptr %14, align 1, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit29, label %.lr.ph, !llvm.loop !8

.loopexit29:                                      ; preds = %.lr.ph, %4
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %5) #3
  store i8 96, ptr %5, align 1, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %0, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !10
  %17 = call noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %16, ptr noundef nonnull %5, ptr noundef null, i32 noundef 8, i32 noundef 1)
  %18 = load ptr, ptr %15, align 8, !tbaa !10
  %. = select i1 %.not, ptr %9, ptr null
  %19 = shl nsw i32 %spec.select, 3
  %20 = call noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %18, ptr noundef nonnull %8, ptr noundef %., i32 noundef %19, i32 noundef 1)
  %or.cond33 = and i1 %.not, %10
  br i1 %or.cond33, label %.lr.ph32.preheader, label %.loopexit

.lr.ph32.preheader:                               ; preds = %.loopexit29
  %wide.trip.count38 = zext i32 %3 to i64
  %.pre = load i8, ptr %9, align 16, !tbaa !5
  br label %.lr.ph32

.lr.ph32:                                         ; preds = %.lr.ph32.preheader, %.lr.ph32
  %21 = phi i8 [ %.pre, %.lr.ph32.preheader ], [ %25, %.lr.ph32 ]
  %indvars.iv35 = phi i64 [ 0, %.lr.ph32.preheader ], [ %indvars.iv.next36, %.lr.ph32 ]
  %22 = lshr i8 %21, 1
  %23 = call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %22)
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, 1
  %24 = getelementptr inbounds i8, ptr %9, i64 %indvars.iv.next36
  %25 = load i8, ptr %24, align 1, !tbaa !5
  %26 = and i8 %25, 1
  %27 = or i8 %26, %23
  %28 = getelementptr inbounds i8, ptr %2, i64 %indvars.iv35
  store i8 %27, ptr %28, align 1, !tbaa !5
  %exitcond39.not = icmp eq i64 %indvars.iv.next36, %wide.trip.count38
  br i1 %exitcond39.not, label %.loopexit, label %.lr.ph32, !llvm.loop !18

.loopexit:                                        ; preds = %.lr.ph32, %.loopexit29
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %5) #3
  ret i32 0
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { mustprogress 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !12, i64 8}
!11 = !{!"_ZTS6Device", !12, i64 8, !13, i64 16, !13, i64 48, !16, i64 80, !17, i64 84, !17, i64 85, !17, i64 86}
!12 = !{!"any pointer", !6, i64 0}
!13 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", !14, i64 0, !15, i64 8, !6, i64 16}
!14 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderE", !12, i64 0}
!15 = !{!"long", !6, i64 0}
!16 = !{!"_ZTSN6Device9prog_modeE", !6, i64 0}
!17 = !{!"bool", !6, i64 0}
!18 = distinct !{!18, !9}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass