; LLVM IR from YanzhenHuang/c-nn :: linalg (mat_transpose)

; 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.7 = external hidden unnamed_addr constant [52 x i8], align 1

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

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

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

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local double @mat_read(ptr noundef, i64 noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @mat_write(ptr noundef, i64 noundef, i64 noundef, double noundef) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @mat_transpose(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.Matrix, ptr %8, i32 0, i32 0
  %10 = load i64, ptr %9, align 8, !tbaa !9
  %11 = icmp sle i64 %10, 0
  br i1 %11, label %17, label %12

12:                                               ; preds = %1
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.Matrix, ptr %13, i32 0, i32 1
  %15 = load i64, ptr %14, align 8, !tbaa !12
  %16 = icmp sle i64 %15, 0
  br i1 %16, label %17, label %20

17:                                               ; preds = %12, %1
  %18 = load ptr, ptr @stderr, align 8, !tbaa !5
  %19 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %18, ptr noundef @.str.7) #5
  call void @exit(i32 noundef 1) #6
  unreachable

20:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Matrix, ptr %21, i32 0, i32 0
  %23 = load i64, ptr %22, align 8, !tbaa !9
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Matrix, ptr %24, i32 0, i32 1
  %26 = load i64, ptr %25, align 8, !tbaa !12
  %27 = mul nsw i64 %23, %26
  %28 = mul i64 %27, 8
  %29 = call noalias ptr @malloc(i64 noundef %28) #7
  store ptr %29, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Matrix, ptr %30, i32 0, i32 1
  %32 = load i64, ptr %31, align 8, !tbaa !12
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Matrix, ptr %33, i32 0, i32 0
  %35 = load i64, ptr %34, align 8, !tbaa !9
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = call ptr @mat_create(i64 noundef %32, i64 noundef %35, ptr noundef %36)
  store ptr %37, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  store i64 0, ptr %5, align 8, !tbaa !13
  br label %38

38:                                               ; preds = %65, %20
  %39 = load i64, ptr %5, align 8, !tbaa !13
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.Matrix, ptr %40, i32 0, i32 0
  %42 = load i64, ptr %41, align 8, !tbaa !9
  %43 = icmp slt i64 %39, %42
  br i1 %43, label %45, label %44

44:                                               ; preds = %38
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  br label %68

45:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  store i64 0, ptr %7, align 8, !tbaa !13
  br label %46

46:                                               ; preds = %61, %45
  %47 = load i64, ptr %7, align 8, !tbaa !13
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Matrix, ptr %48, i32 0, i32 1
  %50 = load i64, ptr %49, align 8, !tbaa !12
  %51 = icmp slt i64 %47, %50
  br i1 %51, label %53, label %52

52:                                               ; preds = %46
  store i32 5, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %64

53:                                               ; preds = %46
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = load i64, ptr %7, align 8, !tbaa !13
  %56 = load i64, ptr %5, align 8, !tbaa !13
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = load i64, ptr %5, align 8, !tbaa !13
  %59 = load i64, ptr %7, align 8, !tbaa !13
  %60 = call double @mat_read(ptr noundef %57, i64 noundef %58, i64 noundef %59)
  call void @mat_write(ptr noundef %54, i64 noundef %55, i64 noundef %56, double noundef %60)
  br label %61

61:                                               ; preds = %53
  %62 = load i64, ptr %7, align 8, !tbaa !13
  %63 = add nsw i64 %62, 1
  store i64 %63, ptr %7, align 8, !tbaa !13
  br label %46, !llvm.loop !14

64:                                               ; preds = %52
  br label %65

65:                                               ; preds = %64
  %66 = load i64, ptr %5, align 8, !tbaa !13
  %67 = add nsw i64 %66, 1
  store i64 %67, ptr %5, align 8, !tbaa !13
  br label %38, !llvm.loop !16

68:                                               ; preds = %44
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  ret ptr %69
}

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 = { 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 = { 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 #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { noreturn nounwind }
attributes #7 = { 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, !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 = !{!11, !11, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !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"

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @mat_create(i64 noundef, i64 noundef, ptr 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: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
declare dso_local double @mat_read(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @mat_write(ptr noundef, i64 noundef, i64 noundef, double noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @mat_transpose(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr %0, align 8, !tbaa !5
  %3 = icmp slt i64 %2, 1
  br i1 %3, label %8, label %4

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !11
  %7 = icmp slt i64 %6, 1
  br i1 %7, label %8, label %11

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

11:                                               ; preds = %4
  %12 = shl i64 %2, 3
  %13 = mul i64 %12, %6
  %14 = tail call noalias ptr @malloc(i64 noundef %13) #6
  %15 = tail call ptr @mat_create(i64 noundef %6, i64 noundef %2, ptr noundef %14)
  %16 = load i64, ptr %0, align 8, !tbaa !5
  %17 = icmp sgt i64 %16, 0
  br i1 %17, label %.preheader.lr.ph, label %._crit_edge24

.preheader.lr.ph:                                 ; preds = %11
  %18 = load i64, ptr %5, align 8, !tbaa !11
  %19 = icmp sgt i64 %18, 0
  br i1 %19, label %.preheader, label %._crit_edge24

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %20 = phi i64 [ %23, %._crit_edge ], [ %16, %.preheader.lr.ph ]
  %21 = phi i64 [ %24, %._crit_edge ], [ %18, %.preheader.lr.ph ]
  %.01923 = phi i64 [ %25, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  %22 = icmp sgt i64 %21, 0
  br i1 %22, label %.lr.ph, label %._crit_edge

._crit_edge24:                                    ; preds = %._crit_edge, %.preheader.lr.ph, %11
  ret ptr %15

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load i64, ptr %0, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %23 = phi i64 [ %.pre, %._crit_edge.loopexit ], [ %20, %.preheader ]
  %24 = phi i64 [ %29, %._crit_edge.loopexit ], [ %21, %.preheader ]
  %25 = add nuw nsw i64 %.01923, 1
  %26 = icmp slt i64 %25, %23
  br i1 %26, label %.preheader, label %._crit_edge24, !llvm.loop !13

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.022 = phi i64 [ %28, %.lr.ph ], [ 0, %.preheader ]
  %27 = tail call double @mat_read(ptr noundef nonnull %0, i64 noundef %.01923, i64 noundef %.022)
  tail call void @mat_write(ptr noundef %15, i64 noundef %.022, i64 noundef %.01923, double noundef %27)
  %28 = add nuw nsw i64 %.022, 1
  %29 = load i64, ptr %5, align 8, !tbaa !11
  %30 = icmp slt i64 %28, %29
  br i1 %30, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !16
}

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 = { 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 #3 = { 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 #4 = { cold nounwind }
attributes #5 = { noreturn nounwind }
attributes #6 = { 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 8}
!12 = !{!10, !10, i64 0}
!13 = distinct !{!13, !14, !15}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.unswitch.partial.disable"}
!16 = distinct !{!16, !14}


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