; LLVM IR from YanzhenHuang/c-nn :: xlinalg (xmat_submat)

; 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.Matrix = type { i64, i64, ptr }

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [108 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [54 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(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare void @mat_write(ptr noundef, i64 noundef, i64 noundef, double noundef) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

declare ptr @mat_create(i64 noundef, i64 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @xmat_submat(ptr noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4) #3 {
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca i32, align 4
  %15 = alloca i64, align 8
  %16 = alloca double, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i64 %3, ptr %9, align 8, !tbaa !9
  store i64 %4, ptr %10, align 8, !tbaa !9
  %17 = load i64, ptr %7, align 8, !tbaa !9
  %18 = load i64, ptr %8, align 8, !tbaa !9
  %19 = icmp sgt i64 %17, %18
  br i1 %19, label %24, label %20

20:                                               ; preds = %5
  %21 = load i64, ptr %9, align 8, !tbaa !9
  %22 = load i64, ptr %10, align 8, !tbaa !9
  %23 = icmp sgt i64 %21, %22
  br i1 %23, label %24, label %27

24:                                               ; preds = %20, %5
  %25 = load ptr, ptr @stderr, align 8, !tbaa !5
  %26 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %25, ptr noundef @.str) #6
  call void @exit(i32 noundef 1) #7
  unreachable

27:                                               ; preds = %20
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = icmp slt i64 %28, 0
  br i1 %29, label %47, label %30

30:                                               ; preds = %27
  %31 = load i64, ptr %8, align 8, !tbaa !9
  %32 = sub nsw i64 %31, 1
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Matrix, ptr %33, i32 0, i32 0
  %35 = load i64, ptr %34, align 8, !tbaa !11
  %36 = icmp sge i64 %32, %35
  br i1 %36, label %47, label %37

37:                                               ; preds = %30
  %38 = load i64, ptr %9, align 8, !tbaa !9
  %39 = icmp slt i64 %38, 0
  br i1 %39, label %47, label %40

40:                                               ; preds = %37
  %41 = load i64, ptr %10, align 8, !tbaa !9
  %42 = sub nsw i64 %41, 1
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.Matrix, ptr %43, i32 0, i32 1
  %45 = load i64, ptr %44, align 8, !tbaa !13
  %46 = icmp sge i64 %42, %45
  br i1 %46, label %47, label %50

47:                                               ; preds = %40, %37, %30, %27
  %48 = load ptr, ptr @stderr, align 8, !tbaa !5
  %49 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %48, ptr noundef @.str.1) #6
  call void @exit(i32 noundef 1) #7
  unreachable

50:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %51 = load i64, ptr %8, align 8, !tbaa !9
  %52 = load i64, ptr %7, align 8, !tbaa !9
  %53 = sub nsw i64 %51, %52
  %54 = load i64, ptr %10, align 8, !tbaa !9
  %55 = load i64, ptr %9, align 8, !tbaa !9
  %56 = sub nsw i64 %54, %55
  %57 = mul nsw i64 %53, %56
  %58 = mul i64 %57, 8
  %59 = call noalias ptr @malloc(i64 noundef %58) #8
  store ptr %59, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %60 = load i64, ptr %8, align 8, !tbaa !9
  %61 = load i64, ptr %7, align 8, !tbaa !9
  %62 = sub nsw i64 %60, %61
  %63 = load i64, ptr %10, align 8, !tbaa !9
  %64 = load i64, ptr %9, align 8, !tbaa !9
  %65 = sub nsw i64 %63, %64
  %66 = load ptr, ptr %11, align 8, !tbaa !5
  %67 = call ptr @mat_create(i64 noundef %62, i64 noundef %65, ptr noundef %66)
  store ptr %67, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #6
  %68 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %68, ptr %13, align 8, !tbaa !9
  br label %69

69:                                               ; preds = %98, %50
  %70 = load i64, ptr %13, align 8, !tbaa !9
  %71 = load i64, ptr %8, align 8, !tbaa !9
  %72 = icmp slt i64 %70, %71
  br i1 %72, label %74, label %73

73:                                               ; preds = %69
  store i32 2, ptr %14, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #6
  br label %101

74:                                               ; preds = %69
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #6
  %75 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %75, ptr %15, align 8, !tbaa !9
  br label %76

76:                                               ; preds = %94, %74
  %77 = load i64, ptr %15, align 8, !tbaa !9
  %78 = load i64, ptr %10, align 8, !tbaa !9
  %79 = icmp slt i64 %77, %78
  br i1 %79, label %81, label %80

80:                                               ; preds = %76
  store i32 5, ptr %14, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #6
  br label %97

81:                                               ; preds = %76
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #6
  %82 = load ptr, ptr %6, align 8, !tbaa !5
  %83 = load i64, ptr %13, align 8, !tbaa !9
  %84 = load i64, ptr %15, align 8, !tbaa !9
  %85 = call double @mat_read(ptr noundef %82, i64 noundef %83, i64 noundef %84)
  store double %85, ptr %16, align 8, !tbaa !14
  %86 = load ptr, ptr %12, align 8, !tbaa !5
  %87 = load i64, ptr %13, align 8, !tbaa !9
  %88 = load i64, ptr %7, align 8, !tbaa !9
  %89 = sub nsw i64 %87, %88
  %90 = load i64, ptr %15, align 8, !tbaa !9
  %91 = load i64, ptr %9, align 8, !tbaa !9
  %92 = sub nsw i64 %90, %91
  %93 = load double, ptr %16, align 8, !tbaa !14
  call void @mat_write(ptr noundef %86, i64 noundef %89, i64 noundef %92, double noundef %93)
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #6
  br label %94

94:                                               ; preds = %81
  %95 = load i64, ptr %15, align 8, !tbaa !9
  %96 = add nsw i64 %95, 1
  store i64 %96, ptr %15, align 8, !tbaa !9
  br label %76, !llvm.loop !16

97:                                               ; preds = %80
  br label %98

98:                                               ; preds = %97
  %99 = load i64, ptr %13, align 8, !tbaa !9
  %100 = add nsw i64 %99, 1
  store i64 %100, ptr %13, align 8, !tbaa !9
  br label %69, !llvm.loop !18

101:                                              ; preds = %73
  %102 = load ptr, ptr %12, align 8, !tbaa !5
  store i32 1, ptr %14, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  ret ptr %102
}

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #5

declare double @mat_read(ptr noundef, i64 noundef, i64 noundef) #1

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 = { nounwind allocsize(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 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 "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 #5 = { noreturn 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 #6 = { nounwind }
attributes #7 = { noreturn nounwind }
attributes #8 = { nounwind allocsize(0) }

!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, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 8, !6, i64 16}
!13 = !{!12, !10, i64 8}
!14 = !{!15, !15, i64 0}
!15 = !{!"double", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [108 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [54 x i8], align 1

declare void @mat_write(ptr noundef, i64 noundef, i64 noundef, double noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

declare ptr @mat_create(i64 noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @xmat_submat(ptr noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4) local_unnamed_addr #2 {
  %6 = icmp sgt i64 %1, %2
  %7 = icmp sgt i64 %3, %4
  %or.cond42 = or i1 %6, %7
  br i1 %or.cond42, label %8, label %11

8:                                                ; preds = %5
  %9 = load ptr, ptr @stderr, align 8, !tbaa !5
  %10 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %9, ptr noundef nonnull @.str) #5
  tail call void @exit(i32 noundef 1) #6
  unreachable

11:                                               ; preds = %5
  %12 = icmp slt i64 %1, 0
  br i1 %12, label %20, label %13

13:                                               ; preds = %11
  %14 = load i64, ptr %0, align 8, !tbaa !9
  %15 = icmp slt i64 %14, %2
  %16 = icmp slt i64 %3, 0
  %or.cond = or i1 %16, %15
  br i1 %or.cond, label %20, label %17

17:                                               ; preds = %13
  %18 = getelementptr inbounds i8, ptr %0, i64 8
  %19 = load i64, ptr %18, align 8, !tbaa !12
  %.not.not = icmp slt i64 %19, %4
  br i1 %.not.not, label %20, label %23

20:                                               ; preds = %17, %13, %11
  %21 = load ptr, ptr @stderr, align 8, !tbaa !5
  %22 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %21, ptr noundef nonnull @.str.1) #5
  tail call void @exit(i32 noundef 1) #6
  unreachable

23:                                               ; preds = %17
  %24 = sub nsw i64 %2, %1
  %25 = sub nsw i64 %4, %3
  %26 = shl i64 %24, 3
  %27 = mul i64 %26, %25
  %28 = tail call noalias ptr @malloc(i64 noundef %27) #7
  %29 = tail call ptr @mat_create(i64 noundef %24, i64 noundef %25, ptr noundef %28) #8
  %30 = icmp slt i64 %1, %2
  %31 = icmp slt i64 %3, %4
  %or.cond48 = and i1 %30, %31
  br i1 %or.cond48, label %.preheader.us, label %._crit_edge45

.preheader.us:                                    ; preds = %23, %._crit_edge.us
  %.044.us = phi i64 [ %37, %._crit_edge.us ], [ %1, %23 ]
  %32 = sub nuw nsw i64 %.044.us, %1
  br label %33

33:                                               ; preds = %.preheader.us, %33
  %.03743.us = phi i64 [ %3, %.preheader.us ], [ %36, %33 ]
  %34 = tail call double @mat_read(ptr noundef nonnull %0, i64 noundef %.044.us, i64 noundef %.03743.us) #8
  %35 = sub nuw nsw i64 %.03743.us, %3
  tail call void @mat_write(ptr noundef %29, i64 noundef %32, i64 noundef %35, double noundef %34) #8
  %36 = add i64 %.03743.us, 1
  %exitcond.not = icmp eq i64 %36, %4
  br i1 %exitcond.not, label %._crit_edge.us, label %33, !llvm.loop !13

._crit_edge.us:                                   ; preds = %33
  %37 = add i64 %.044.us, 1
  %exitcond47.not = icmp eq i64 %37, %2
  br i1 %exitcond47.not, label %._crit_edge45, label %.preheader.us, !llvm.loop !15

._crit_edge45:                                    ; preds = %._crit_edge.us, %23
  ret ptr %29
}

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #4

declare double @mat_read(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { 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 #3 = { 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 #4 = { noreturn 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 #5 = { cold nounwind }
attributes #6 = { noreturn nounwind }
attributes #7 = { nounwind allocsize(0) }
attributes #8 = { 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 0}
!10 = !{!"", !11, i64 0, !11, i64 8, !6, i64 16}
!11 = !{!"long long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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