; LLVM IR from trabucayre/openFPGALoader :: src/lattice.ll (ZN7Lattice5wr_rdEhPhiS0_ib)

; 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] }

@.str.647 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.650 = external hidden unnamed_addr constant [6 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

declare i32 @printf(ptr noundef, ...) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN7Lattice5wr_rdEhPhiS0_ib(ptr noundef nonnull align 8 dereferenceable(152) %0, i8 noundef zeroext %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, i32 noundef %5, i1 noundef zeroext %6) #3 align 2 {
  %8 = alloca ptr, align 8
  %9 = alloca i8, align 1
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca i8, align 1
  %15 = alloca i32, align 4
  %16 = alloca ptr, align 8
  %17 = alloca i64, align 8
  %18 = alloca i64, align 8
  %19 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i8 %1, ptr %9, align 1, !tbaa !9
  store ptr %2, ptr %10, align 8, !tbaa !5
  store i32 %3, ptr %11, align 4, !tbaa !10
  store ptr %4, ptr %12, align 8, !tbaa !5
  store i32 %5, ptr %13, align 4, !tbaa !10
  %20 = zext i1 %6 to i8
  store i8 %20, ptr %14, align 1, !tbaa !12
  %21 = load ptr, ptr %8, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  %22 = load i32, ptr %13, align 4, !tbaa !10
  store i32 %22, ptr %15, align 4, !tbaa !10
  %23 = load i32, ptr %11, align 4, !tbaa !10
  %24 = load i32, ptr %13, align 4, !tbaa !10
  %25 = icmp sgt i32 %23, %24
  br i1 %25, label %26, label %28

26:                                               ; preds = %7
  %27 = load i32, ptr %11, align 4, !tbaa !10
  store i32 %27, ptr %15, align 4, !tbaa !10
  br label %28

28:                                               ; preds = %26, %7
  %29 = load i32, ptr %15, align 4, !tbaa !10
  %30 = zext i32 %29 to i64
  %31 = call ptr @llvm.stacksave.p0()
  store ptr %31, ptr %16, align 8
  %32 = alloca i8, i64 %30, align 16
  store i64 %30, ptr %17, align 8
  %33 = load i32, ptr %15, align 4, !tbaa !10
  %34 = zext i32 %33 to i64
  %35 = alloca i8, i64 %34, align 16
  store i64 %34, ptr %18, align 8
  %36 = load i32, ptr %15, align 4, !tbaa !10
  %37 = sext i32 %36 to i64
  call void @llvm.memset.p0.i64(ptr align 16 %32, i8 0, i64 %37, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %19) #5
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  %39 = icmp ne ptr %38, null
  br i1 %39, label %40, label %61

40:                                               ; preds = %28
  %41 = load i32, ptr %11, align 4, !tbaa !10
  %42 = icmp sgt i32 %41, 0
  br i1 %42, label %43, label %61

43:                                               ; preds = %40
  store i32 0, ptr %19, align 4, !tbaa !10
  br label %44

44:                                               ; preds = %57, %43
  %45 = load i32, ptr %19, align 4, !tbaa !10
  %46 = load i32, ptr %11, align 4, !tbaa !10
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %48, label %60

48:                                               ; preds = %44
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = load i32, ptr %19, align 4, !tbaa !10
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i8, ptr %49, i64 %51
  %53 = load i8, ptr %52, align 1, !tbaa !9
  %54 = load i32, ptr %19, align 4, !tbaa !10
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i8, ptr %32, i64 %55
  store i8 %53, ptr %56, align 1, !tbaa !9
  br label %57

57:                                               ; preds = %48
  %58 = load i32, ptr %19, align 4, !tbaa !10
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %19, align 4, !tbaa !10
  br label %44, !llvm.loop !14

60:                                               ; preds = %44
  br label %61

61:                                               ; preds = %60, %40, %28
  %62 = getelementptr inbounds %class.Device, ptr %21, i32 0, i32 1
  %63 = load ptr, ptr %62, align 8, !tbaa !16
  %64 = call noundef i32 @_ZN4Jtag7shiftIREPhS0_iNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %63, ptr noundef %9, ptr noundef null, i32 noundef 8, i32 noundef 13)
  %65 = load ptr, ptr %12, align 8, !tbaa !5
  %66 = icmp ne ptr %65, null
  br i1 %66, label %70, label %67

67:                                               ; preds = %61
  %68 = load ptr, ptr %10, align 8, !tbaa !5
  %69 = icmp ne ptr %68, null
  br i1 %69, label %70, label %82

70:                                               ; preds = %67, %61
  %71 = getelementptr inbounds %class.Device, ptr %21, i32 0, i32 1
  %72 = load ptr, ptr %71, align 8, !tbaa !16
  %73 = load ptr, ptr %12, align 8, !tbaa !5
  %74 = icmp ne ptr %73, null
  br i1 %74, label %75, label %76

75:                                               ; preds = %70
  br label %77

76:                                               ; preds = %70
  br label %77

77:                                               ; preds = %76, %75
  %78 = phi ptr [ %35, %75 ], [ null, %76 ]
  %79 = load i32, ptr %15, align 4, !tbaa !10
  %80 = mul nsw i32 8, %79
  %81 = call noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %72, ptr noundef %32, ptr noundef %78, i32 noundef %80, i32 noundef 6)
  br label %82

82:                                               ; preds = %77, %67
  %83 = load ptr, ptr %12, align 8, !tbaa !5
  %84 = icmp ne ptr %83, null
  br i1 %84, label %85, label %124

85:                                               ; preds = %82
  %86 = load i8, ptr %14, align 1, !tbaa !12, !range !22, !noundef !23
  %87 = trunc i8 %86 to i1
  br i1 %87, label %88, label %106

88:                                               ; preds = %85
  %89 = load i32, ptr %15, align 4, !tbaa !10
  %90 = sub nsw i32 %89, 1
  store i32 %90, ptr %19, align 4, !tbaa !10
  br label %91

91:                                               ; preds = %101, %88
  %92 = load i32, ptr %19, align 4, !tbaa !10
  %93 = icmp sge i32 %92, 0
  br i1 %93, label %94, label %104

94:                                               ; preds = %91
  %95 = load i32, ptr %19, align 4, !tbaa !10
  %96 = sext i32 %95 to i64
  %97 = getelementptr inbounds i8, ptr %35, i64 %96
  %98 = load i8, ptr %97, align 1, !tbaa !9
  %99 = zext i8 %98 to i32
  %100 = call i32 (ptr, ...) @printf(ptr noundef @.str.650, i32 noundef %99)
  br label %101

101:                                              ; preds = %94
  %102 = load i32, ptr %19, align 4, !tbaa !10
  %103 = add nsw i32 %102, -1
  store i32 %103, ptr %19, align 4, !tbaa !10
  br label %91, !llvm.loop !24

104:                                              ; preds = %91
  %105 = call i32 (ptr, ...) @printf(ptr noundef @.str.647)
  br label %106

106:                                              ; preds = %104, %85
  store i32 0, ptr %19, align 4, !tbaa !10
  br label %107

107:                                              ; preds = %120, %106
  %108 = load i32, ptr %19, align 4, !tbaa !10
  %109 = load i32, ptr %13, align 4, !tbaa !10
  %110 = icmp slt i32 %108, %109
  br i1 %110, label %111, label %123

111:                                              ; preds = %107
  %112 = load i32, ptr %19, align 4, !tbaa !10
  %113 = sext i32 %112 to i64
  %114 = getelementptr inbounds i8, ptr %35, i64 %113
  %115 = load i8, ptr %114, align 1, !tbaa !9
  %116 = load ptr, ptr %12, align 8, !tbaa !5
  %117 = load i32, ptr %19, align 4, !tbaa !10
  %118 = sext i32 %117 to i64
  %119 = getelementptr inbounds i8, ptr %116, i64 %118
  store i8 %115, ptr %119, align 1, !tbaa !9
  br label %120

120:                                              ; preds = %111
  %121 = load i32, ptr %19, align 4, !tbaa !10
  %122 = add nsw i32 %121, 1
  store i32 %122, ptr %19, align 4, !tbaa !10
  br label %107, !llvm.loop !25

123:                                              ; preds = %107
  br label %124

124:                                              ; preds = %123, %82
  call void @llvm.lifetime.end.p0(i64 4, ptr %19) #5
  %125 = load ptr, ptr %16, align 8
  call void @llvm.stackrestore.p0(ptr %125)
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  ret i1 true
}

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

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

declare noundef i32 @_ZN4Jtag7shiftIREPhS0_iNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193), ptr noundef, ptr noundef, i32 noundef, i32 noundef) #2

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { 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 = { nocallback nofree nosync nounwind willreturn }
attributes #5 = { 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 = !{!13, !13, i64 0}
!13 = !{!"bool", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !6, i64 8}
!17 = !{!"_ZTS6Device", !6, i64 8, !18, i64 16, !18, i64 48, !21, i64 80, !13, i64 84, !13, i64 85, !13, i64 86}
!18 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", !19, i64 0, !20, i64 8, !7, i64 16}
!19 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderE", !6, i64 0}
!20 = !{!"long", !7, i64 0}
!21 = !{!"_ZTSN6Device9prog_modeE", !7, i64 0}
!22 = !{i8 0, i8 2}
!23 = !{}
!24 = distinct !{!24, !15}
!25 = distinct !{!25, !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"

@.str.647 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.650 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN7Lattice5wr_rdEhPhiS0_ib(ptr nocapture noundef nonnull readonly align 8 dereferenceable(152) %0, i8 noundef zeroext %1, ptr noundef readonly %2, i32 noundef %3, ptr noundef writeonly %4, i32 noundef %5, i1 noundef zeroext %6) local_unnamed_addr #2 align 2 {
  %8 = alloca i8, align 1
  store i8 %1, ptr %8, align 1, !tbaa !5
  %spec.select = tail call i32 @llvm.smax.i32(i32 %3, i32 %5)
  %9 = zext i32 %spec.select to i64
  %10 = alloca i8, i64 %9, align 16
  %11 = alloca i8, i64 %9, align 16
  %12 = sext i32 %spec.select to i64
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %10, i8 0, i64 %12, i1 false)
  %13 = icmp ne ptr %2, null
  %14 = icmp sgt i32 %3, 0
  %or.cond = and i1 %13, %14
  br i1 %or.cond, label %.preheader41.preheader, label %.loopexit

.preheader41.preheader:                           ; preds = %7
  %15 = zext nneg i32 %3 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %10, ptr nonnull align 1 %2, i64 %15, i1 false), !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader41.preheader, %7
  %16 = getelementptr inbounds i8, ptr %0, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !8
  %18 = call noundef i32 @_ZN4Jtag7shiftIREPhS0_iNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %17, ptr noundef nonnull %8, ptr noundef null, i32 noundef 8, i32 noundef 13)
  %19 = icmp ne ptr %4, null
  %or.cond3 = or i1 %13, %19
  br i1 %or.cond3, label %20, label %.thread

20:                                               ; preds = %.loopexit
  %21 = load ptr, ptr %16, align 8, !tbaa !8
  %. = select i1 %19, ptr %11, ptr null
  %22 = shl nsw i32 %spec.select, 3
  %23 = call noundef i32 @_ZN4Jtag7shiftDREPKhPhiNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193) %21, ptr noundef nonnull %10, ptr noundef %., i32 noundef %22, i32 noundef 6)
  br i1 %19, label %24, label %.thread

24:                                               ; preds = %20
  br i1 %6, label %.preheader, label %32

.preheader:                                       ; preds = %24
  %25 = icmp sgt i32 %spec.select, 0
  br i1 %25, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %9, %.preheader ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %26 = getelementptr inbounds i8, ptr %11, i64 %indvars.iv.next
  %27 = load i8, ptr %26, align 1, !tbaa !5
  %28 = zext i8 %27 to i32
  %29 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.650, i32 noundef %28)
  %30 = icmp ugt i64 %indvars.iv, 1
  br i1 %30, label %.lr.ph, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %31 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.647)
  br label %32

32:                                               ; preds = %._crit_edge, %24
  %33 = icmp sgt i32 %5, 0
  br i1 %33, label %.lr.ph46.preheader, label %.thread

.lr.ph46.preheader:                               ; preds = %32
  %34 = zext nneg i32 %5 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %4, ptr nonnull align 16 %11, i64 %34, i1 false), !tbaa !5
  br label %.thread

.thread:                                          ; preds = %.lr.ph46.preheader, %32, %.loopexit, %20
  ret i1 true
}

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

declare noundef i32 @_ZN4Jtag7shiftIREPhS0_iNS_10tapState_tE(ptr noundef nonnull align 8 dereferenceable(193), ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #4

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #5

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { nofree nounwind "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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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 = !{!9, !10, i64 8}
!9 = !{!"_ZTS6Device", !10, i64 8, !11, i64 16, !11, i64 48, !14, i64 80, !15, i64 84, !15, i64 85, !15, i64 86}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", !12, i64 0, !13, i64 8, !6, i64 16}
!12 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderE", !10, i64 0}
!13 = !{!"long", !6, i64 0}
!14 = !{!"_ZTSN6Device9prog_modeE", !6, i64 0}
!15 = !{!"bool", !6, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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