; LLVM IR from KhawajaAbaid/machine-learning-in-c :: gan (generator_train_step)

; 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.metrics = type { double, double }
%struct.mlp = type { ptr, i64 }
%struct.layer = type { ptr, ptr, i64, i64 }
%struct.grad_and_metrics = type { ptr, ptr }

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

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

declare ptr @copy_mlp(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 ptr @generator_backprop(ptr noundef, ptr noundef, ptr noundef) #3

declare void @initialize_mlp_zeros_(ptr noundef) #2

declare void @accumulate_grad_(ptr noundef, ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @free_grad_and_metrics(ptr noundef) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @sample_noise(i32 noundef, i64 noundef) #4

; Function Attrs: nounwind
declare i64 @time(ptr noundef) #5

declare void @divide_grad_by_batch_size_(ptr noundef, i64 noundef) #2

declare void @update_weights_(ptr noundef, ptr noundef, double noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @free_gradient(ptr noundef) #4

; Function Attrs: nounwind uwtable
define dso_local ptr @generator_train_step(ptr noundef %0, ptr noundef %1, double noundef %2, i64 noundef %3) #3 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca double, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store double %2, ptr %7, align 8, !tbaa !9
  store i64 %3, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  %16 = call noalias ptr @malloc(i64 noundef 16) #7
  store ptr %16, ptr %9, align 8, !tbaa !5
  %17 = load ptr, ptr %9, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.metrics, ptr %17, i32 0, i32 0
  store double 0.000000e+00, ptr %18, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mlp, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !15
  %22 = getelementptr inbounds ptr, ptr %21, i64 0
  %23 = load ptr, ptr %22, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.layer, ptr %23, i32 0, i32 2
  %25 = load i64, ptr %24, align 8, !tbaa !17
  store i64 %25, ptr %10, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mlp, ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !15
  %29 = getelementptr inbounds ptr, ptr %28, i64 0
  %30 = load ptr, ptr %29, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.layer, ptr %30, i32 0, i32 2
  %32 = load i64, ptr %31, align 8, !tbaa !17
  store i64 %32, ptr %11, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = call ptr @copy_mlp(ptr noundef %33)
  store ptr %34, ptr %12, align 8, !tbaa !5
  %35 = load ptr, ptr %12, align 8, !tbaa !5
  call void @initialize_mlp_zeros_(ptr noundef %35)
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #6
  store i64 0, ptr %15, align 8, !tbaa !11
  br label %36

36:                                               ; preds = %69, %4
  %37 = load i64, ptr %15, align 8, !tbaa !11
  %38 = load i64, ptr %8, align 8, !tbaa !11
  %39 = icmp ult i64 %37, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #6
  br label %72

41:                                               ; preds = %36
  %42 = call i64 @time(ptr noundef null) #6
  %43 = load i64, ptr %15, align 8, !tbaa !11
  %44 = load i64, ptr %15, align 8, !tbaa !11
  %45 = add i64 %44, 1
  %46 = mul i64 %43, %45
  %47 = add i64 %42, %46
  %48 = trunc i64 %47 to i32
  %49 = load i64, ptr %10, align 8, !tbaa !11
  %50 = call ptr @sample_noise(i32 noundef %48, i64 noundef %49)
  store ptr %50, ptr %13, align 8, !tbaa !5
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = load ptr, ptr %13, align 8, !tbaa !5
  %54 = call ptr @generator_backprop(ptr noundef %51, ptr noundef %52, ptr noundef %53)
  store ptr %54, ptr %14, align 8, !tbaa !5
  %55 = load ptr, ptr %12, align 8, !tbaa !5
  %56 = load ptr, ptr %14, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.grad_and_metrics, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !19
  call void @accumulate_grad_(ptr noundef %55, ptr noundef %58)
  %59 = load ptr, ptr %14, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.grad_and_metrics, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !21
  %62 = getelementptr inbounds %struct.metrics, ptr %61, i32 0, i32 0
  %63 = load double, ptr %62, align 8, !tbaa !13
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.metrics, ptr %64, i32 0, i32 0
  %66 = load double, ptr %65, align 8, !tbaa !13
  %67 = fadd double %66, %63
  store double %67, ptr %65, align 8, !tbaa !13
  %68 = load ptr, ptr %14, align 8, !tbaa !5
  call void @free_grad_and_metrics(ptr noundef %68)
  br label %69

69:                                               ; preds = %41
  %70 = load i64, ptr %15, align 8, !tbaa !11
  %71 = add i64 %70, 1
  store i64 %71, ptr %15, align 8, !tbaa !11
  br label %36, !llvm.loop !22

72:                                               ; preds = %40
  %73 = load ptr, ptr %12, align 8, !tbaa !5
  %74 = load i64, ptr %8, align 8, !tbaa !11
  call void @divide_grad_by_batch_size_(ptr noundef %73, i64 noundef %74)
  %75 = load i64, ptr %8, align 8, !tbaa !11
  %76 = uitofp i64 %75 to double
  %77 = load ptr, ptr %9, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.metrics, ptr %77, i32 0, i32 0
  %79 = load double, ptr %78, align 8, !tbaa !13
  %80 = fdiv double %79, %76
  store double %80, ptr %78, align 8, !tbaa !13
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = load ptr, ptr %12, align 8, !tbaa !5
  %83 = load double, ptr %7, align 8, !tbaa !9
  call void @update_weights_(ptr noundef %81, ptr noundef %82, double noundef %83)
  %84 = load ptr, ptr %12, align 8, !tbaa !5
  call void @free_gradient(ptr noundef %84)
  %85 = load ptr, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  ret ptr %85
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { "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 = { inlinehint 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 #5 = { 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 = { 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 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"metrics", !10, i64 0, !10, i64 8}
!15 = !{!16, !6, i64 0}
!16 = !{!"", !6, i64 0, !12, i64 8}
!17 = !{!18, !12, i64 16}
!18 = !{!"", !6, i64 0, !6, i64 8, !12, i64 16, !12, i64 24}
!19 = !{!20, !6, i64 0}
!20 = !{!"", !6, i64 0, !6, i64 8}
!21 = !{!20, !6, i64 8}
!22 = distinct !{!22, !23}
!23 = !{!"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"

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

declare ptr @copy_mlp(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @generator_backprop(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare void @initialize_mlp_zeros_(ptr noundef) local_unnamed_addr #1

declare void @accumulate_grad_(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @free_grad_and_metrics(ptr noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @sample_noise(i32 noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare i64 @time(ptr noundef) local_unnamed_addr #4

declare void @divide_grad_by_batch_size_(ptr noundef, i64 noundef) local_unnamed_addr #1

declare void @update_weights_(ptr noundef, ptr noundef, double noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @free_gradient(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @generator_train_step(ptr noundef %0, ptr noundef %1, double noundef %2, i64 noundef %3) local_unnamed_addr #2 {
  %5 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #5
  %6 = load ptr, ptr %0, align 8, !tbaa !5
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  %9 = load i64, ptr %8, align 8, !tbaa !12
  %10 = tail call ptr @copy_mlp(ptr noundef nonnull %0) #6
  tail call void @initialize_mlp_zeros_(ptr noundef %10) #6
  %.not = icmp eq i64 %3, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %4
  %11 = phi double [ 0.000000e+00, %4 ], [ %26, %.lr.ph ]
  tail call void @divide_grad_by_batch_size_(ptr noundef %10, i64 noundef %3) #6
  %12 = uitofp i64 %3 to double
  %13 = fdiv double %11, %12
  store double %13, ptr %5, align 8, !tbaa !14
  tail call void @update_weights_(ptr noundef nonnull %0, ptr noundef %10, double noundef %2) #6
  tail call void @free_gradient(ptr noundef %10)
  ret ptr %5

.lr.ph:                                           ; preds = %4, %.lr.ph
  %14 = phi double [ %26, %.lr.ph ], [ 0.000000e+00, %4 ]
  %.028 = phi i64 [ %16, %.lr.ph ], [ 0, %4 ]
  %15 = tail call i64 @time(ptr noundef null) #6
  %16 = add nuw i64 %.028, 1
  %17 = mul i64 %16, %.028
  %18 = add i64 %15, %17
  %19 = trunc i64 %18 to i32
  %20 = tail call ptr @sample_noise(i32 noundef %19, i64 noundef %9)
  %21 = tail call ptr @generator_backprop(ptr noundef nonnull %0, ptr noundef %1, ptr noundef %20)
  %22 = load ptr, ptr %21, align 8, !tbaa !17
  tail call void @accumulate_grad_(ptr noundef %10, ptr noundef %22) #6
  %23 = getelementptr inbounds i8, ptr %21, i64 8
  %24 = load ptr, ptr %23, align 8, !tbaa !19
  %25 = load double, ptr %24, align 8, !tbaa !14
  %26 = fadd double %25, %14
  tail call void @free_grad_and_metrics(ptr noundef nonnull %21)
  %exitcond.not = icmp eq i64 %16, %3
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !20
}

attributes #0 = { 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 #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 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 = { inlinehint 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 = { nounwind allocsize(0) }
attributes #6 = { 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 0}
!6 = !{!"", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 16}
!13 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24}
!14 = !{!15, !16, i64 0}
!15 = !{!"metrics", !16, i64 0, !16, i64 8}
!16 = !{!"double", !8, i64 0}
!17 = !{!18, !7, i64 0}
!18 = !{!"", !7, i64 0, !7, i64 8}
!19 = !{!18, !7, i64 8}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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