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

; 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.NN = type { i64, i64, i64, i64, ptr, ptr, ptr, ptr, ptr }
%struct.Layer = type { ptr }

; 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 ptr @xmat_traverse(ptr noundef, ptr noundef, ...) #1

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

; Function Attrs: nounwind uwtable
define dso_local ptr @nn_forward(ptr noundef %0, ptr noundef %1, i64 noundef %2) #3 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %13 = load i64, ptr %6, align 8, !tbaa !9
  %14 = add nsw i64 %13, 1
  %15 = mul i64 %14, 8
  %16 = call noalias ptr @malloc(i64 noundef %15) #5
  store ptr %16, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %17

17:                                               ; preds = %30, %3
  %18 = load i64, ptr %8, align 8, !tbaa !9
  %19 = load i64, ptr %6, align 8, !tbaa !9
  %20 = icmp slt i64 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %33

22:                                               ; preds = %17
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i64, ptr %8, align 8, !tbaa !9
  %25 = getelementptr inbounds double, ptr %23, i64 %24
  %26 = load double, ptr %25, align 8, !tbaa !11
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = load i64, ptr %8, align 8, !tbaa !9
  %29 = getelementptr inbounds double, ptr %27, i64 %28
  store double %26, ptr %29, align 8, !tbaa !11
  br label %30

30:                                               ; preds = %22
  %31 = load i64, ptr %8, align 8, !tbaa !9
  %32 = add nsw i64 %31, 1
  store i64 %32, ptr %8, align 8, !tbaa !9
  br label %17, !llvm.loop !13

33:                                               ; preds = %21
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = load i64, ptr %6, align 8, !tbaa !9
  %36 = getelementptr inbounds double, ptr %34, i64 %35
  store double 1.000000e+00, ptr %36, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %37 = load i64, ptr %6, align 8, !tbaa !9
  %38 = add nsw i64 %37, 1
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = call ptr @mat_create(i64 noundef 1, i64 noundef %38, ptr noundef %39)
  store ptr %40, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %73, %33
  %42 = load i64, ptr %10, align 8, !tbaa !9
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.NN, ptr %43, i32 0, i32 3
  %45 = load i64, ptr %44, align 8, !tbaa !15
  %46 = add nsw i64 %45, 2
  %47 = icmp slt i64 %42, %46
  br i1 %47, label %49, label %48

48:                                               ; preds = %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %76

49:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.NN, ptr %50, i32 0, i32 4
  %52 = load ptr, ptr %51, align 8, !tbaa !17
  %53 = load i64, ptr %10, align 8, !tbaa !9
  %54 = getelementptr inbounds ptr, ptr %52, i64 %53
  %55 = load ptr, ptr %54, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.Layer, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !18
  store ptr %57, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = load ptr, ptr %11, align 8, !tbaa !5
  %60 = call ptr @mat_multmat(ptr noundef %58, ptr noundef %59)
  store ptr %60, ptr %12, align 8, !tbaa !5
  %61 = load ptr, ptr %12, align 8, !tbaa !5
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.NN, ptr %62, i32 0, i32 7
  %64 = load ptr, ptr %63, align 8, !tbaa !20
  %65 = call ptr (ptr, ptr, ...) @xmat_traverse(ptr noundef %61, ptr noundef %64, i32 noundef 1)
  store ptr %65, ptr %12, align 8, !tbaa !5
  %66 = load ptr, ptr %12, align 8, !tbaa !5
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.NN, ptr %67, i32 0, i32 5
  %69 = load ptr, ptr %68, align 8, !tbaa !21
  %70 = load i64, ptr %10, align 8, !tbaa !9
  %71 = getelementptr inbounds ptr, ptr %69, i64 %70
  store ptr %66, ptr %71, align 8, !tbaa !5
  %72 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %72, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %73

73:                                               ; preds = %49
  %74 = load i64, ptr %10, align 8, !tbaa !9
  %75 = add nsw i64 %74, 1
  store i64 %75, ptr %10, align 8, !tbaa !9
  br label %41, !llvm.loop !22

76:                                               ; preds = %48
  %77 = load ptr, ptr %9, align 8, !tbaa !5
  %78 = call ptr @mat_transpose(ptr noundef %77)
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret ptr %78
}

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

declare ptr @mat_multmat(ptr noundef, ptr noundef) #1

declare ptr @mat_transpose(ptr 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 }
attributes #5 = { 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, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !10, i64 24}
!16 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64}
!17 = !{!16, !6, i64 32}
!18 = !{!19, !6, i64 0}
!19 = !{!"", !6, i64 0}
!20 = !{!16, !6, i64 56}
!21 = !{!16, !6, i64 40}
!22 = distinct !{!22, !14}


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

declare ptr @xmat_traverse(ptr noundef, ptr 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

; Function Attrs: nounwind uwtable
define dso_local ptr @nn_forward(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #2 {
  %4 = add nsw i64 %2, 1
  %5 = shl i64 %4, 3
  %6 = tail call noalias ptr @malloc(i64 noundef %5) #4
  %7 = icmp sgt i64 %2, 0
  br i1 %7, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %3
  %8 = shl nuw i64 %2, 3
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 8 %6, ptr align 8 %1, i64 %8, i1 false), !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %3
  %9 = getelementptr inbounds double, ptr %6, i64 %2
  store double 1.000000e+00, ptr %9, align 8, !tbaa !5
  %10 = tail call ptr @mat_create(i64 noundef 1, i64 noundef %4, ptr noundef %6) #5
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = load i64, ptr %11, align 8, !tbaa !9
  %13 = icmp sgt i64 %12, -2
  br i1 %13, label %.lr.ph32, label %._crit_edge33

.lr.ph32:                                         ; preds = %._crit_edge
  %14 = getelementptr inbounds i8, ptr %0, i64 32
  %15 = getelementptr inbounds i8, ptr %0, i64 56
  %16 = getelementptr inbounds i8, ptr %0, i64 40
  br label %18

._crit_edge33:                                    ; preds = %18, %._crit_edge
  %.025.lcssa = phi ptr [ %10, %._crit_edge ], [ %25, %18 ]
  %17 = tail call ptr @mat_transpose(ptr noundef %.025.lcssa) #5
  ret ptr %17

18:                                               ; preds = %.lr.ph32, %18
  %.02530 = phi ptr [ %10, %.lr.ph32 ], [ %25, %18 ]
  %.02629 = phi i64 [ 0, %.lr.ph32 ], [ %28, %18 ]
  %19 = load ptr, ptr %14, align 8, !tbaa !13
  %20 = getelementptr inbounds ptr, ptr %19, i64 %.02629
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  %22 = load ptr, ptr %21, align 8, !tbaa !15
  %23 = tail call ptr @mat_multmat(ptr noundef %.02530, ptr noundef %22) #5
  %24 = load ptr, ptr %15, align 8, !tbaa !17
  %25 = tail call ptr (ptr, ptr, ...) @xmat_traverse(ptr noundef %23, ptr noundef %24, i32 noundef 1) #5
  %26 = load ptr, ptr %16, align 8, !tbaa !18
  %27 = getelementptr inbounds ptr, ptr %26, i64 %.02629
  store ptr %25, ptr %27, align 8, !tbaa !14
  %28 = add nuw nsw i64 %.02629, 1
  %29 = load i64, ptr %11, align 8, !tbaa !9
  %.not = icmp sgt i64 %.02629, %29
  br i1 %.not, label %._crit_edge33, label %18, !llvm.loop !19
}

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

declare ptr @mat_multmat(ptr noundef, ptr noundef) local_unnamed_addr #0

declare ptr @mat_transpose(ptr noundef) local_unnamed_addr #0

; 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) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind allocsize(0) }
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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 24}
!10 = !{!"", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64}
!11 = !{!"long long", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!10, !12, i64 32}
!14 = !{!12, !12, i64 0}
!15 = !{!16, !12, i64 0}
!16 = !{!"", !12, i64 0}
!17 = !{!10, !12, i64 56}
!18 = !{!10, !12, i64 40}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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