; LLVM IR from beagleboard/librobotcontrol :: library/src/math (matrix)

; 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.rc_vector_t = type { i32, ptr, i32 }
%struct.rc_matrix_t = type { i32, i32, ptr, i32 }

@stderr = external global ptr, align 8
@.str.10 = external hidden unnamed_addr constant [53 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [56 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 nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

; 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 i32 @rc_matrix_zeros(ptr noundef, i32 noundef, i32 noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @rc_matrix_diagonal(ptr noundef %0, ptr noundef byval(%struct.rc_vector_t) align 8 %1) #3 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %7 = getelementptr inbounds %struct.rc_vector_t, ptr %1, i32 0, i32 2
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = icmp ne i32 %8, 1
  %10 = xor i1 %9, true
  %11 = xor i1 %10, true
  %12 = zext i1 %11 to i32
  %13 = sext i32 %12 to i64
  %14 = call i64 @llvm.expect.i64(i64 %13, i64 0)
  %15 = icmp ne i64 %14, 0
  br i1 %15, label %16, label %19

16:                                               ; preds = %2
  %17 = load ptr, ptr @stderr, align 8, !tbaa !5
  %18 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %17, ptr noundef @.str.10) #4
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %6, align 4
  br label %63

19:                                               ; preds = %2
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.rc_vector_t, ptr %1, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !12
  %23 = getelementptr inbounds %struct.rc_vector_t, ptr %1, i32 0, i32 0
  %24 = load i32, ptr %23, align 8, !tbaa !12
  %25 = call i32 @rc_matrix_zeros(ptr noundef %20, i32 noundef %22, i32 noundef %24)
  %26 = icmp ne i32 %25, 0
  %27 = xor i1 %26, true
  %28 = xor i1 %27, true
  %29 = zext i1 %28 to i32
  %30 = sext i32 %29 to i64
  %31 = call i64 @llvm.expect.i64(i64 %30, i64 0)
  %32 = icmp ne i64 %31, 0
  br i1 %32, label %33, label %36

33:                                               ; preds = %19
  %34 = load ptr, ptr @stderr, align 8, !tbaa !5
  %35 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %34, ptr noundef @.str.11) #4
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %6, align 4
  br label %63

36:                                               ; preds = %19
  store i32 0, ptr %5, align 4, !tbaa !13
  br label %37

37:                                               ; preds = %59, %36
  %38 = load i32, ptr %5, align 4, !tbaa !13
  %39 = getelementptr inbounds %struct.rc_vector_t, ptr %1, i32 0, i32 0
  %40 = load i32, ptr %39, align 8, !tbaa !12
  %41 = icmp slt i32 %38, %40
  br i1 %41, label %42, label %62

42:                                               ; preds = %37
  %43 = getelementptr inbounds %struct.rc_vector_t, ptr %1, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !14
  %45 = load i32, ptr %5, align 4, !tbaa !13
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds double, ptr %44, i64 %46
  %48 = load double, ptr %47, align 8, !tbaa !15
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.rc_matrix_t, ptr %49, i32 0, i32 2
  %51 = load ptr, ptr %50, align 8, !tbaa !17
  %52 = load i32, ptr %5, align 4, !tbaa !13
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds ptr, ptr %51, i64 %53
  %55 = load ptr, ptr %54, align 8, !tbaa !5
  %56 = load i32, ptr %5, align 4, !tbaa !13
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds double, ptr %55, i64 %57
  store double %48, ptr %58, align 8, !tbaa !15
  br label %59

59:                                               ; preds = %42
  %60 = load i32, ptr %5, align 4, !tbaa !13
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %5, align 4, !tbaa !13
  br label %37, !llvm.loop !19

62:                                               ; preds = %37
  store i32 0, ptr %3, align 4
  store i32 1, ptr %6, align 4
  br label %63

63:                                               ; preds = %62, %33, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  %64 = load i32, ptr %3, align 4
  ret i32 %64
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #2 = { 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 #3 = { 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 #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/C++ TBAA"}
!9 = !{!10, !11, i64 16}
!10 = !{!"rc_vector_t", !11, i64 0, !6, i64 8, !11, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 8}
!15 = !{!16, !16, i64 0}
!16 = !{!"double", !7, i64 0}
!17 = !{!18, !6, i64 8}
!18 = !{!"rc_matrix_t", !11, i64 0, !11, i64 4, !6, i64 8, !11, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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

%struct.rc_vector_t = type { i32, ptr, i32 }

@stderr = external local_unnamed_addr global ptr, align 8
@.str.10 = external hidden unnamed_addr constant [53 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [56 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @rc_matrix_diagonal(ptr noundef %0, ptr nocapture noundef readonly byval(%struct.rc_vector_t) align 8 %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %1, i64 16
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %.not = icmp eq i32 %4, 1
  br i1 %.not, label %5, label %.loopexit.sink.split, !prof !11

5:                                                ; preds = %2
  %6 = load i32, ptr %1, align 8, !tbaa !12
  %7 = tail call i32 @rc_matrix_zeros(ptr noundef %0, i32 noundef %6, i32 noundef %6)
  %.not8 = icmp eq i32 %7, 0
  br i1 %.not8, label %.preheader, label %.loopexit.sink.split, !prof !11

.preheader:                                       ; preds = %5
  %8 = icmp sgt i32 %6, 0
  br i1 %8, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader
  %9 = getelementptr inbounds i8, ptr %1, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !14
  %wide.trip.count = zext nneg i32 %6 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %13 = icmp ult i32 %6, 4
  br i1 %13, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %wide.trip.count, 2147483644
  br label %14

14:                                               ; preds = %14, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.3, %14 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.3, %14 ]
  %15 = getelementptr inbounds double, ptr %10, i64 %indvars.iv
  %16 = load double, ptr %15, align 8, !tbaa !16
  %17 = getelementptr inbounds ptr, ptr %12, i64 %indvars.iv
  %18 = load ptr, ptr %17, align 8, !tbaa !18
  %19 = getelementptr inbounds double, ptr %18, i64 %indvars.iv
  store double %16, ptr %19, align 8, !tbaa !16
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %20 = getelementptr inbounds double, ptr %10, i64 %indvars.iv.next
  %21 = load double, ptr %20, align 8, !tbaa !16
  %22 = getelementptr inbounds ptr, ptr %12, i64 %indvars.iv.next
  %23 = load ptr, ptr %22, align 8, !tbaa !18
  %24 = getelementptr inbounds double, ptr %23, i64 %indvars.iv.next
  store double %21, ptr %24, align 8, !tbaa !16
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %25 = getelementptr inbounds double, ptr %10, i64 %indvars.iv.next.1
  %26 = load double, ptr %25, align 8, !tbaa !16
  %27 = getelementptr inbounds ptr, ptr %12, i64 %indvars.iv.next.1
  %28 = load ptr, ptr %27, align 8, !tbaa !18
  %29 = getelementptr inbounds double, ptr %28, i64 %indvars.iv.next.1
  store double %26, ptr %29, align 8, !tbaa !16
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %30 = getelementptr inbounds double, ptr %10, i64 %indvars.iv.next.2
  %31 = load double, ptr %30, align 8, !tbaa !16
  %32 = getelementptr inbounds ptr, ptr %12, i64 %indvars.iv.next.2
  %33 = load ptr, ptr %32, align 8, !tbaa !18
  %34 = getelementptr inbounds double, ptr %33, i64 %indvars.iv.next.2
  store double %31, ptr %34, align 8, !tbaa !16
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %.loopexit.loopexit.unr-lcssa, label %14, !llvm.loop !19

.loopexit.sink.split:                             ; preds = %5, %2
  %.str.11.sink = phi ptr [ @.str.10, %2 ], [ @.str.11, %5 ]
  %35 = load ptr, ptr @stderr, align 8, !tbaa !18
  %36 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %35, ptr noundef nonnull %.str.11.sink) #2
  br label %.loopexit

.loopexit.loopexit.unr-lcssa:                     ; preds = %14, %.lr.ph
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.3, %14 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.epil.preheader

.epil.preheader:                                  ; preds = %.loopexit.loopexit.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %.loopexit.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  %37 = getelementptr inbounds double, ptr %10, i64 %indvars.iv.epil
  %38 = load double, ptr %37, align 8, !tbaa !16
  %39 = getelementptr inbounds ptr, ptr %12, i64 %indvars.iv.epil
  %40 = load ptr, ptr %39, align 8, !tbaa !18
  %41 = getelementptr inbounds double, ptr %40, i64 %indvars.iv.epil
  store double %38, ptr %41, align 8, !tbaa !16
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.loopexit, label %.epil.preheader, !llvm.loop !21

.loopexit:                                        ; preds = %.loopexit.loopexit.unr-lcssa, %.epil.preheader, %.loopexit.sink.split, %.preheader
  %.07 = phi i32 [ 0, %.preheader ], [ -1, %.loopexit.sink.split ], [ 0, %.epil.preheader ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  ret i32 %.07
}

attributes #0 = { 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 #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 = { cold 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 16}
!6 = !{!"rc_vector_t", !7, i64 0, !10, i64 8, !7, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!15, !10, i64 8}
!15 = !{!"rc_matrix_t", !7, i64 0, !7, i64 4, !10, i64 8, !7, i64 16}
!16 = !{!17, !17, i64 0}
!17 = !{!"double", !8, i64 0}
!18 = !{!10, !10, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LowerExpectIntrinsicPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass