; 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_matrix_t = type { i32, i32, ptr, i32 }

@stderr = external global ptr, align 8
@.str.38 = external hidden unnamed_addr constant [61 x i8], align 1
@.str.39 = external hidden unnamed_addr constant [59 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @rc_matrix_alloc(ptr noundef, i32 noundef, i32 noundef) #0

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #2

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

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

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

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

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

34:                                               ; preds = %20
  %35 = load ptr, ptr @stderr, align 8, !tbaa !5
  %36 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %35, ptr noundef @.str.39) #4
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %78

37:                                               ; preds = %20
  store i32 0, ptr %5, align 4, !tbaa !14
  br label %38

38:                                               ; preds = %74, %37
  %39 = load i32, ptr %5, align 4, !tbaa !14
  %40 = getelementptr inbounds %struct.rc_matrix_t, ptr %0, i32 0, i32 0
  %41 = load i32, ptr %40, align 8, !tbaa !13
  %42 = icmp slt i32 %39, %41
  br i1 %42, label %43, label %77

43:                                               ; preds = %38
  store i32 0, ptr %6, align 4, !tbaa !14
  br label %44

44:                                               ; preds = %70, %43
  %45 = load i32, ptr %6, align 4, !tbaa !14
  %46 = getelementptr inbounds %struct.rc_matrix_t, ptr %0, i32 0, i32 1
  %47 = load i32, ptr %46, align 4, !tbaa !12
  %48 = icmp slt i32 %45, %47
  br i1 %48, label %49, label %73

49:                                               ; preds = %44
  %50 = getelementptr inbounds %struct.rc_matrix_t, ptr %0, i32 0, i32 2
  %51 = load ptr, ptr %50, align 8, !tbaa !15
  %52 = load i32, ptr %5, align 4, !tbaa !14
  %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 %6, align 4, !tbaa !14
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds double, ptr %55, i64 %57
  %59 = load double, ptr %58, align 8, !tbaa !16
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.rc_matrix_t, ptr %60, i32 0, i32 2
  %62 = load ptr, ptr %61, align 8, !tbaa !15
  %63 = load i32, ptr %6, align 4, !tbaa !14
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds ptr, ptr %62, i64 %64
  %66 = load ptr, ptr %65, align 8, !tbaa !5
  %67 = load i32, ptr %5, align 4, !tbaa !14
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds double, ptr %66, i64 %68
  store double %59, ptr %69, align 8, !tbaa !16
  br label %70

70:                                               ; preds = %49
  %71 = load i32, ptr %6, align 4, !tbaa !14
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %6, align 4, !tbaa !14
  br label %44, !llvm.loop !18

73:                                               ; preds = %44
  br label %74

74:                                               ; preds = %73
  %75 = load i32, ptr %5, align 4, !tbaa !14
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %5, align 4, !tbaa !14
  br label %38, !llvm.loop !20

77:                                               ; preds = %38
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %78

78:                                               ; preds = %77, %34, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  %79 = load i32, ptr %3, align 4
  ret i32 %79
}

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


; 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_matrix_t = type { i32, i32, ptr, i32 }

@stderr = external local_unnamed_addr global ptr, align 8
@.str.38 = external hidden unnamed_addr constant [61 x i8], align 1
@.str.39 = external hidden unnamed_addr constant [59 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @rc_matrix_transpose(ptr nocapture noundef readonly byval(%struct.rc_matrix_t) align 8 %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, 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 = getelementptr inbounds i8, ptr %0, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !12
  %8 = load i32, ptr %0, align 8, !tbaa !13
  %9 = tail call i32 @rc_matrix_alloc(ptr noundef %1, i32 noundef %7, i32 noundef %8)
  %.not12 = icmp eq i32 %9, 0
  br i1 %.not12, label %.preheader13, label %.loopexit.sink.split, !prof !11

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

.preheader.lr.ph:                                 ; preds = %.preheader13
  %11 = icmp sgt i32 %7, 0
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load ptr, ptr %12, align 8
  br i1 %11, label %.preheader.lr.ph.split.us, label %.loopexit

.preheader.lr.ph.split.us:                        ; preds = %.preheader.lr.ph
  %14 = getelementptr inbounds i8, ptr %1, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !14
  %wide.trip.count21 = zext nneg i32 %8 to i64
  %wide.trip.count = zext nneg i32 %7 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %16 = icmp ult i32 %7, 4
  %unroll_iter = and i64 %wide.trip.count, 2147483644
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %._crit_edge.us, %.preheader.lr.ph.split.us
  %indvars.iv18 = phi i64 [ %indvars.iv.next19, %._crit_edge.us ], [ 0, %.preheader.lr.ph.split.us ]
  %17 = getelementptr inbounds ptr, ptr %13, i64 %indvars.iv18
  %18 = load ptr, ptr %17, align 8, !tbaa !15
  br i1 %16, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new

.preheader.us.new:                                ; preds = %.preheader.us, %.preheader.us.new
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.preheader.us.new ], [ 0, %.preheader.us ]
  %niter = phi i64 [ %niter.next.3, %.preheader.us.new ], [ 0, %.preheader.us ]
  %19 = getelementptr inbounds double, ptr %18, i64 %indvars.iv
  %20 = load double, ptr %19, align 8, !tbaa !16
  %21 = getelementptr inbounds ptr, ptr %15, i64 %indvars.iv
  %22 = load ptr, ptr %21, align 8, !tbaa !15
  %23 = getelementptr inbounds double, ptr %22, i64 %indvars.iv18
  store double %20, ptr %23, align 8, !tbaa !16
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %24 = getelementptr inbounds double, ptr %18, i64 %indvars.iv.next
  %25 = load double, ptr %24, align 8, !tbaa !16
  %26 = getelementptr inbounds ptr, ptr %15, i64 %indvars.iv.next
  %27 = load ptr, ptr %26, align 8, !tbaa !15
  %28 = getelementptr inbounds double, ptr %27, i64 %indvars.iv18
  store double %25, ptr %28, align 8, !tbaa !16
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %29 = getelementptr inbounds double, ptr %18, i64 %indvars.iv.next.1
  %30 = load double, ptr %29, align 8, !tbaa !16
  %31 = getelementptr inbounds ptr, ptr %15, i64 %indvars.iv.next.1
  %32 = load ptr, ptr %31, align 8, !tbaa !15
  %33 = getelementptr inbounds double, ptr %32, i64 %indvars.iv18
  store double %30, ptr %33, align 8, !tbaa !16
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %34 = getelementptr inbounds double, ptr %18, i64 %indvars.iv.next.2
  %35 = load double, ptr %34, align 8, !tbaa !16
  %36 = getelementptr inbounds ptr, ptr %15, i64 %indvars.iv.next.2
  %37 = load ptr, ptr %36, align 8, !tbaa !15
  %38 = getelementptr inbounds double, ptr %37, i64 %indvars.iv18
  store double %35, ptr %38, 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 %._crit_edge.us.unr-lcssa, label %.preheader.us.new, !llvm.loop !18

._crit_edge.us.unr-lcssa:                         ; preds = %.preheader.us.new, %.preheader.us
  %indvars.iv.unr = phi i64 [ 0, %.preheader.us ], [ %indvars.iv.next.3, %.preheader.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %._crit_edge.us.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.us.unr-lcssa ]
  %39 = getelementptr inbounds double, ptr %18, i64 %indvars.iv.epil
  %40 = load double, ptr %39, align 8, !tbaa !16
  %41 = getelementptr inbounds ptr, ptr %15, i64 %indvars.iv.epil
  %42 = load ptr, ptr %41, align 8, !tbaa !15
  %43 = getelementptr inbounds double, ptr %42, i64 %indvars.iv18
  store double %40, ptr %43, 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 %._crit_edge.us, label %.epil.preheader, !llvm.loop !20

._crit_edge.us:                                   ; preds = %.epil.preheader, %._crit_edge.us.unr-lcssa
  %indvars.iv.next19 = add nuw nsw i64 %indvars.iv18, 1
  %exitcond22.not = icmp eq i64 %indvars.iv.next19, %wide.trip.count21
  br i1 %exitcond22.not, label %.loopexit, label %.preheader.us, !llvm.loop !22

.loopexit.sink.split:                             ; preds = %5, %2
  %.str.39.sink = phi ptr [ @.str.38, %2 ], [ @.str.39, %5 ]
  %44 = load ptr, ptr @stderr, align 8, !tbaa !15
  %45 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %44, ptr noundef nonnull %.str.39.sink) #2
  br label %.loopexit

.loopexit:                                        ; preds = %._crit_edge.us, %.loopexit.sink.split, %.preheader.lr.ph, %.preheader13
  %.011 = phi i32 [ 0, %.preheader13 ], [ 0, %.preheader.lr.ph ], [ -1, %.loopexit.sink.split ], [ 0, %._crit_edge.us ]
  ret i32 %.011
}

attributes #0 = { 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 #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 = { 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_matrix_t", !7, i64 0, !7, i64 4, !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 4}
!13 = !{!6, !7, i64 0}
!14 = !{!6, !10, i64 8}
!15 = !{!10, !10, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"double", !8, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.unroll.disable"}
!22 = distinct !{!22, !19}


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