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

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

23:                                               ; preds = %5
  %24 = load i32, ptr %11, align 4, !tbaa !10
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %11, align 4, !tbaa !10
  br label %26

26:                                               ; preds = %23, %5
  %27 = load i32, ptr %11, align 4, !tbaa !10
  %28 = zext i32 %27 to i64
  %29 = call ptr @llvm.stacksave.p0()
  store ptr %29, ptr %12, align 8
  %30 = alloca i8, i64 %28, align 16
  store i64 %28, ptr %13, align 8
  %31 = load i32, ptr %11, align 4, !tbaa !10
  %32 = zext i32 %31 to i64
  %33 = alloca i8, i64 %32, align 16
  store i64 %32, ptr %14, align 8
  %34 = load i8, ptr %7, align 1, !tbaa !9
  %35 = call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %34)
  %36 = getelementptr inbounds i8, ptr %30, i64 0
  store i8 %35, ptr %36, align 16, !tbaa !9
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  %38 = icmp ne ptr %37, null
  br i1 %38, label %39, label %60

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

40:                                               ; preds = %56, %39
  %41 = load i32, ptr %15, align 4, !tbaa !10
  %42 = load i32, ptr %10, align 4, !tbaa !10
  %43 = icmp ult i32 %41, %42
  br i1 %43, label %45, label %44

44:                                               ; preds = %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  br label %59

45:                                               ; preds = %40
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = load i32, ptr %15, align 4, !tbaa !10
  %48 = zext i32 %47 to i64
  %49 = getelementptr inbounds i8, ptr %46, i64 %48
  %50 = load i8, ptr %49, align 1, !tbaa !9
  %51 = call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %50)
  %52 = load i32, ptr %15, align 4, !tbaa !10
  %53 = add i32 %52, 1
  %54 = zext i32 %53 to i64
  %55 = getelementptr inbounds i8, ptr %30, i64 %54
  store i8 %51, ptr %55, align 1, !tbaa !9
  br label %56

56:                                               ; preds = %45
  %57 = load i32, ptr %15, align 4, !tbaa !10
  %58 = add i32 %57, 1
  store i32 %58, ptr %15, align 4, !tbaa !10
  br label %40, !llvm.loop !12

59:                                               ; preds = %44
  br label %60

60:                                               ; preds = %59, %26
  call void @llvm.lifetime.start.p0(i64 1, ptr %16) #4
  store i8 96, ptr %16, align 1, !tbaa !9
  %61 = getelementptr inbounds %class.Device, ptr %18, i32 0, i32 1
  %62 = load ptr, ptr %61, align 8, !tbaa !14
  %63 = call noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %62, ptr noundef %16, ptr noundef null, i32 noundef 8, i32 noundef 1)
  %64 = getelementptr inbounds %class.Device, ptr %18, i32 0, i32 1
  %65 = load ptr, ptr %64, align 8, !tbaa !14
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  %67 = icmp eq ptr %66, null
  br i1 %67, label %68, label %69

68:                                               ; preds = %60
  br label %70

69:                                               ; preds = %60
  br label %70

70:                                               ; preds = %69, %68
  %71 = phi ptr [ null, %68 ], [ %33, %69 ]
  %72 = load i32, ptr %11, align 4, !tbaa !10
  %73 = mul nsw i32 8, %72
  %74 = call noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %65, ptr noundef %30, ptr noundef %71, i32 noundef %73, i32 noundef 1)
  %75 = load ptr, ptr %9, align 8, !tbaa !5
  %76 = icmp ne ptr %75, null
  br i1 %76, label %77, label %111

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

78:                                               ; preds = %107, %77
  %79 = load i32, ptr %17, align 4, !tbaa !10
  %80 = load i32, ptr %10, align 4, !tbaa !10
  %81 = icmp ult i32 %79, %80
  br i1 %81, label %83, label %82

82:                                               ; preds = %78
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  br label %110

83:                                               ; preds = %78
  %84 = load i32, ptr %17, align 4, !tbaa !10
  %85 = add i32 %84, 1
  %86 = zext i32 %85 to i64
  %87 = getelementptr inbounds i8, ptr %33, i64 %86
  %88 = load i8, ptr %87, align 1, !tbaa !9
  %89 = zext i8 %88 to i32
  %90 = ashr i32 %89, 1
  %91 = trunc i32 %90 to i8
  %92 = call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %91)
  %93 = zext i8 %92 to i32
  %94 = load i32, ptr %17, align 4, !tbaa !10
  %95 = add i32 %94, 2
  %96 = zext i32 %95 to i64
  %97 = getelementptr inbounds i8, ptr %33, i64 %96
  %98 = load i8, ptr %97, align 1, !tbaa !9
  %99 = zext i8 %98 to i32
  %100 = and i32 %99, 1
  %101 = or i32 %93, %100
  %102 = trunc i32 %101 to i8
  %103 = load ptr, ptr %9, align 8, !tbaa !5
  %104 = load i32, ptr %17, align 4, !tbaa !10
  %105 = zext i32 %104 to i64
  %106 = getelementptr inbounds i8, ptr %103, i64 %105
  store i8 %102, ptr %106, align 1, !tbaa !9
  br label %107

107:                                              ; preds = %83
  %108 = load i32, ptr %17, align 4, !tbaa !10
  %109 = add i32 %108, 1
  store i32 %109, ptr %17, align 4, !tbaa !10
  br label %78, !llvm.loop !21

110:                                              ; preds = %82
  br label %111

111:                                              ; preds = %110, %70
  call void @llvm.lifetime.end.p0(i64 1, ptr %16) #4
  %112 = load ptr, ptr %12, align 8
  call void @llvm.stackrestore.p0(ptr %112)
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  ret i32 0
}

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

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

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

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 = { 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 = { nocallback nofree nosync nounwind willreturn }
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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7Anlogic7spi_putEhPKhPhj(ptr nocapture noundef nonnull readonly align 8 dereferenceable(144) %0, i8 noundef zeroext %1, ptr noundef readonly %2, ptr noundef writeonly %3, i32 noundef %4) unnamed_addr #2 align 2 {
  %6 = alloca i8, align 1
  %.not = icmp ne ptr %3, null
  %spec.select.v = select i1 %.not, i32 2, i32 1
  %spec.select = add i32 %spec.select.v, %4
  %7 = zext i32 %spec.select to i64
  %8 = alloca i8, i64 %7, align 16
  %9 = alloca i8, i64 %7, align 16
  %10 = tail call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %1)
  store i8 %10, ptr %8, align 16, !tbaa !5
  %.not28 = icmp ne ptr %2, null
  %11 = icmp ne i32 %4, 0
  %or.cond = and i1 %.not28, %11
  br i1 %or.cond, label %.lr.ph.preheader, label %.loopexit30

.lr.ph.preheader:                                 ; preds = %5
  %wide.trip.count = zext i32 %4 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 ]
  %12 = getelementptr inbounds i8, ptr %2, i64 %indvars.iv
  %13 = load i8, ptr %12, align 1, !tbaa !5
  %14 = tail call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %13)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %15 = getelementptr inbounds i8, ptr %8, i64 %indvars.iv.next
  store i8 %14, ptr %15, align 1, !tbaa !5
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit30, label %.lr.ph, !llvm.loop !8

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

.lr.ph33.preheader:                               ; preds = %.loopexit30
  %wide.trip.count39 = zext i32 %4 to i64
  br label %.lr.ph33

.lr.ph33:                                         ; preds = %.lr.ph33.preheader, %.lr.ph33
  %indvars.iv36 = phi i64 [ 0, %.lr.ph33.preheader ], [ %indvars.iv.next37, %.lr.ph33 ]
  %indvars.iv.next37 = add nuw nsw i64 %indvars.iv36, 1
  %22 = getelementptr inbounds i8, ptr %9, i64 %indvars.iv.next37
  %23 = load i8, ptr %22, align 1, !tbaa !5
  %24 = lshr i8 %23, 1
  %25 = call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %24)
  %26 = add nuw i64 %indvars.iv36, 2
  %27 = and i64 %26, 4294967295
  %28 = getelementptr inbounds i8, ptr %9, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !5
  %30 = and i8 %29, 1
  %31 = or i8 %30, %25
  %32 = getelementptr inbounds i8, ptr %3, i64 %indvars.iv36
  store i8 %31, ptr %32, align 1, !tbaa !5
  %exitcond40.not = icmp eq i64 %indvars.iv.next37, %wide.trip.count39
  br i1 %exitcond40.not, label %.loopexit, label %.lr.ph33, !llvm.loop !18

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

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

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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass