; LLVM IR from trabucayre/openFPGALoader :: src/lattice.ll (ZN7Lattice7spi_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 zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext) #1

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7Lattice7spi_putEhPKhPhj(ptr noundef nonnull align 8 dereferenceable(152) %0, i8 noundef zeroext %1, ptr noundef %2, ptr noundef %3, i32 noundef %4) unnamed_addr #3 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 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
  %17 = load ptr, ptr %6, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %18 = load i32, ptr %10, align 4, !tbaa !10
  %19 = add i32 %18, 1
  store i32 %19, ptr %11, align 4, !tbaa !10
  %20 = load i32, ptr %11, align 4, !tbaa !10
  %21 = zext i32 %20 to i64
  %22 = call ptr @llvm.stacksave.p0()
  store ptr %22, ptr %12, align 8
  %23 = alloca i8, i64 %21, align 16
  store i64 %21, ptr %13, align 8
  %24 = load i32, ptr %11, align 4, !tbaa !10
  %25 = zext i32 %24 to i64
  %26 = alloca i8, i64 %25, align 16
  store i64 %25, ptr %14, align 8
  %27 = load i8, ptr %7, align 1, !tbaa !9
  %28 = call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %27)
  %29 = getelementptr inbounds i8, ptr %23, i64 0
  store i8 %28, ptr %29, align 16, !tbaa !9
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %53

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

33:                                               ; preds = %49, %32
  %34 = load i32, ptr %15, align 4, !tbaa !10
  %35 = load i32, ptr %10, align 4, !tbaa !10
  %36 = icmp ult i32 %34, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  br label %52

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

49:                                               ; preds = %38
  %50 = load i32, ptr %15, align 4, !tbaa !10
  %51 = add i32 %50, 1
  store i32 %51, ptr %15, align 4, !tbaa !10
  br label %33, !llvm.loop !12

52:                                               ; preds = %37
  br label %53

53:                                               ; preds = %52, %5
  %54 = getelementptr inbounds %class.Device, ptr %17, i32 0, i32 1
  %55 = load ptr, ptr %54, align 8, !tbaa !14
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = icmp eq ptr %56, null
  br i1 %57, label %58, label %59

58:                                               ; preds = %53
  br label %60

59:                                               ; preds = %53
  br label %60

60:                                               ; preds = %59, %58
  %61 = phi ptr [ null, %58 ], [ %26, %59 ]
  %62 = load i32, ptr %11, align 4, !tbaa !10
  %63 = mul nsw i32 8, %62
  %64 = call noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %55, ptr noundef %23, ptr noundef %61, i32 noundef %63, i32 noundef 1)
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = icmp ne ptr %65, null
  br i1 %66, label %67, label %88

67:                                               ; preds = %60
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  store i32 0, ptr %16, align 4, !tbaa !10
  br label %68

68:                                               ; preds = %84, %67
  %69 = load i32, ptr %16, align 4, !tbaa !10
  %70 = load i32, ptr %10, align 4, !tbaa !10
  %71 = icmp ult i32 %69, %70
  br i1 %71, label %73, label %72

72:                                               ; preds = %68
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  br label %87

73:                                               ; preds = %68
  %74 = load i32, ptr %16, align 4, !tbaa !10
  %75 = add i32 %74, 1
  %76 = zext i32 %75 to i64
  %77 = getelementptr inbounds i8, ptr %26, i64 %76
  %78 = load i8, ptr %77, align 1, !tbaa !9
  %79 = call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %78)
  %80 = load ptr, ptr %9, align 8, !tbaa !5
  %81 = load i32, ptr %16, align 4, !tbaa !10
  %82 = zext i32 %81 to i64
  %83 = getelementptr inbounds i8, ptr %80, i64 %82
  store i8 %79, ptr %83, align 1, !tbaa !9
  br label %84

84:                                               ; preds = %73
  %85 = load i32, ptr %16, align 4, !tbaa !10
  %86 = add i32 %85, 1
  store i32 %86, ptr %16, align 4, !tbaa !10
  br label %68, !llvm.loop !21

87:                                               ; preds = %72
  br label %88

88:                                               ; preds = %87, %60
  %89 = load ptr, ptr %12, align 8
  call void @llvm.stackrestore.p0(ptr %89)
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #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 = !{!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"

declare noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext) local_unnamed_addr #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 #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7Lattice7spi_putEhPKhPhj(ptr nocapture noundef nonnull readonly align 8 dereferenceable(152) %0, i8 noundef zeroext %1, ptr noundef readonly %2, ptr noundef writeonly %3, i32 noundef %4) unnamed_addr #1 align 2 {
  %6 = add i32 %4, 1
  %7 = zext i32 %6 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
  %.not = icmp ne ptr %2, null
  %11 = icmp ne i32 %4, 0
  %or.cond = and i1 %.not, %11
  br i1 %or.cond, label %.lr.ph.preheader, label %.loopexit26

.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 %.loopexit26, label %.lr.ph, !llvm.loop !8

.loopexit26:                                      ; preds = %.lr.ph, %5
  %16 = getelementptr inbounds i8, ptr %0, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !10
  %18 = icmp ne ptr %3, null
  %. = select i1 %18, ptr %9, ptr null
  %19 = shl nsw i32 %6, 3
  %20 = call noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %17, ptr noundef nonnull %8, ptr noundef %., i32 noundef %19, i32 noundef 1)
  %or.cond31 = and i1 %18, %11
  br i1 %or.cond31, label %.lr.ph29.preheader, label %.loopexit

.lr.ph29.preheader:                               ; preds = %.loopexit26
  %wide.trip.count36 = zext i32 %4 to i64
  br label %.lr.ph29

.lr.ph29:                                         ; preds = %.lr.ph29.preheader, %.lr.ph29
  %indvars.iv33 = phi i64 [ 0, %.lr.ph29.preheader ], [ %indvars.iv.next34, %.lr.ph29 ]
  %indvars.iv.next34 = add nuw nsw i64 %indvars.iv33, 1
  %21 = getelementptr inbounds i8, ptr %9, i64 %indvars.iv.next34
  %22 = load i8, ptr %21, align 1, !tbaa !5
  %23 = call noundef zeroext i8 @_ZN21ConfigBitstreamParser11reverseByteEh(i8 noundef zeroext %22)
  %24 = getelementptr inbounds i8, ptr %3, i64 %indvars.iv33
  store i8 %23, ptr %24, align 1, !tbaa !5
  %exitcond37.not = icmp eq i64 %indvars.iv.next34, %wide.trip.count36
  br i1 %exitcond37.not, label %.loopexit, label %.lr.ph29, !llvm.loop !18

.loopexit:                                        ; preds = %.lr.ph29, %.loopexit26
  ret i32 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 = { 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" }

!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,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass