; LLVM IR from marcobambini/gravity :: src/shared (gravity_value)

; 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.gravity_value_t = type { ptr, %union.anon }
%union.anon = type { i64 }
%struct.gravity_instance_t = type { ptr, %struct.gravity_gc_s, ptr, ptr, ptr }
%struct.gravity_gc_s = type { i8, i8, ptr, ptr, ptr, ptr }
%struct.gravity_class_s = type { ptr, %struct.gravity_gc_s, ptr, ptr, i8, i8, i8, i8, ptr, ptr, ptr, ptr, i32, ptr }

@gravity_class_null = external global ptr, align 8
@gravity_class_instance = external global ptr, align 8

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

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

declare void @gravity_vm_transfer(ptr noundef, ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @gravity_instance_new(ptr noundef %0, ptr noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca %struct.gravity_value_t, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %8 = call ptr @gravity_calloc(ptr noundef null, i64 noundef 1, i64 noundef 72)
  store ptr %8, ptr %5, align 8, !tbaa !5
  %9 = load ptr, ptr @gravity_class_instance, align 8, !tbaa !5
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.gravity_instance_t, ptr %10, i32 0, i32 0
  store ptr %9, ptr %11, align 8, !tbaa !9
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.gravity_instance_t, ptr %13, i32 0, i32 2
  store ptr %12, ptr %14, align 8, !tbaa !13
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.gravity_class_s, ptr %15, i32 0, i32 12
  %17 = load i32, ptr %16, align 8, !tbaa !14
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %28

19:                                               ; preds = %2
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.gravity_class_s, ptr %20, i32 0, i32 12
  %22 = load i32, ptr %21, align 8, !tbaa !14
  %23 = zext i32 %22 to i64
  %24 = mul i64 %23, 16
  %25 = call ptr @gravity_calloc(ptr noundef null, i64 noundef 1, i64 noundef %24)
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.gravity_instance_t, ptr %26, i32 0, i32 4
  store ptr %25, ptr %27, align 8, !tbaa !17
  br label %28

28:                                               ; preds = %19, %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %6, align 4, !tbaa !18
  br label %29

29:                                               ; preds = %46, %28
  %30 = load i32, ptr %6, align 4, !tbaa !18
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.gravity_class_s, ptr %31, i32 0, i32 12
  %33 = load i32, ptr %32, align 8, !tbaa !14
  %34 = icmp ult i32 %30, %33
  br i1 %34, label %36, label %35

35:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %49

36:                                               ; preds = %29
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.gravity_instance_t, ptr %37, i32 0, i32 4
  %39 = load ptr, ptr %38, align 8, !tbaa !17
  %40 = load i32, ptr %6, align 4, !tbaa !18
  %41 = zext i32 %40 to i64
  %42 = getelementptr inbounds %struct.gravity_value_t, ptr %39, i64 %41
  %43 = getelementptr inbounds %struct.gravity_value_t, ptr %7, i32 0, i32 0
  %44 = load ptr, ptr @gravity_class_null, align 8, !tbaa !5
  store ptr %44, ptr %43, align 8, !tbaa !19
  %45 = getelementptr inbounds %struct.gravity_value_t, ptr %7, i32 0, i32 1
  store i64 0, ptr %45, align 8, !tbaa !21
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %42, ptr align 8 %7, i64 16, i1 false), !tbaa.struct !22
  br label %46

46:                                               ; preds = %36
  %47 = load i32, ptr %6, align 4, !tbaa !18
  %48 = add i32 %47, 1
  store i32 %48, ptr %6, align 4, !tbaa !18
  br label %29, !llvm.loop !23

49:                                               ; preds = %35
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = icmp ne ptr %50, null
  br i1 %51, label %52, label %55

52:                                               ; preds = %49
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  call void @gravity_vm_transfer(ptr noundef %53, ptr noundef %54)
  br label %55

55:                                               ; preds = %52, %49
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret ptr %56
}

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 }

!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, !6, i64 0}
!10 = !{!"", !6, i64 0, !11, i64 8, !6, i64 48, !6, i64 56, !6, i64 64}
!11 = !{!"gravity_gc_s", !12, i64 0, !12, i64 1, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!10, !6, i64 48}
!14 = !{!15, !16, i64 104}
!15 = !{!"gravity_class_s", !6, i64 0, !11, i64 8, !6, i64 48, !6, i64 56, !12, i64 64, !12, i64 65, !12, i64 66, !12, i64 67, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !16, i64 104, !6, i64 112}
!16 = !{!"int", !7, i64 0}
!17 = !{!10, !6, i64 64}
!18 = !{!16, !16, i64 0}
!19 = !{!20, !6, i64 0}
!20 = !{!"", !6, i64 0, !7, i64 8}
!21 = !{!7, !7, i64 0}
!22 = !{i64 0, i64 8, !5, i64 8, i64 8, !21}
!23 = distinct !{!23, !24}
!24 = !{!"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"

%struct.gravity_value_t = type { ptr, %union.anon }
%union.anon = type { i64 }

@gravity_class_null = external local_unnamed_addr global ptr, align 8
@gravity_class_instance = external local_unnamed_addr global ptr, align 8

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

declare void @gravity_vm_transfer(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @gravity_instance_new(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = tail call ptr @gravity_calloc(ptr noundef null, i64 noundef 1, i64 noundef 72) #2
  %4 = load ptr, ptr @gravity_class_instance, align 8, !tbaa !5
  store ptr %4, ptr %3, align 8, !tbaa !9
  %5 = getelementptr inbounds i8, ptr %3, i64 48
  store ptr %1, ptr %5, align 8, !tbaa !13
  %6 = getelementptr inbounds i8, ptr %1, i64 104
  %7 = load i32, ptr %6, align 8, !tbaa !14
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %._crit_edge, label %8

8:                                                ; preds = %2
  %9 = zext i32 %7 to i64
  %10 = shl nuw nsw i64 %9, 4
  %11 = tail call ptr @gravity_calloc(ptr noundef null, i64 noundef 1, i64 noundef %10) #2
  %12 = getelementptr inbounds i8, ptr %3, i64 64
  store ptr %11, ptr %12, align 8, !tbaa !17
  %.pre = load i32, ptr %6, align 8, !tbaa !14
  %13 = icmp eq i32 %.pre, 0
  br i1 %13, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %8
  %14 = getelementptr inbounds i8, ptr %3, i64 64
  br label %15

._crit_edge:                                      ; preds = %15, %2, %8
  %.not16 = icmp eq ptr %0, null
  br i1 %.not16, label %23, label %22

15:                                               ; preds = %.lr.ph, %15
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %15 ]
  %16 = load ptr, ptr %14, align 8, !tbaa !17
  %17 = getelementptr inbounds %struct.gravity_value_t, ptr %16, i64 %indvars.iv
  %18 = load ptr, ptr @gravity_class_null, align 8, !tbaa !5
  store ptr %18, ptr %17, align 8, !tbaa !5
  %.sroa.2.0..sroa_idx = getelementptr inbounds i8, ptr %17, i64 8
  store i64 0, ptr %.sroa.2.0..sroa_idx, align 8, !tbaa !18
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %19 = load i32, ptr %6, align 8, !tbaa !14
  %20 = zext i32 %19 to i64
  %21 = icmp ult i64 %indvars.iv.next, %20
  br i1 %21, label %15, label %._crit_edge, !llvm.loop !19

22:                                               ; preds = %._crit_edge
  tail call void @gravity_vm_transfer(ptr noundef nonnull %0, ptr noundef nonnull %3) #2
  br label %23

23:                                               ; preds = %22, %._crit_edge
  ret ptr %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 = { 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 #2 = { 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, !6, i64 0}
!10 = !{!"", !6, i64 0, !11, i64 8, !6, i64 48, !6, i64 56, !6, i64 64}
!11 = !{!"gravity_gc_s", !12, i64 0, !12, i64 1, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!10, !6, i64 48}
!14 = !{!15, !16, i64 104}
!15 = !{!"gravity_class_s", !6, i64 0, !11, i64 8, !6, i64 48, !6, i64 56, !12, i64 64, !12, i64 65, !12, i64 66, !12, i64 67, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !16, i64 104, !6, i64 112}
!16 = !{!"int", !7, i64 0}
!17 = !{!10, !6, i64 64}
!18 = !{!7, !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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