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

; 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

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

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @gravity_list_append_list(ptr noundef %0, ptr noundef %1, ptr noundef %2) #3 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %9 = load ptr, ptr %6, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.gravity_list_t, ptr %9, i32 0, i32 2
  %11 = getelementptr inbounds %struct.gravity_value_r, ptr %10, i32 0, i32 0
  %12 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %12, ptr %7, align 8, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !15
  br label %13

13:                                               ; preds = %76, %3
  %14 = load i64, ptr %8, align 8, !tbaa !15
  %15 = load i64, ptr %7, align 8, !tbaa !15
  %16 = icmp ult i64 %14, %15
  br i1 %16, label %18, label %17

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

18:                                               ; preds = %13
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.gravity_list_t, ptr %19, i32 0, i32 2
  %21 = getelementptr inbounds %struct.gravity_value_r, ptr %20, i32 0, i32 0
  %22 = load i64, ptr %21, align 8, !tbaa !9
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.gravity_list_t, ptr %23, i32 0, i32 2
  %25 = getelementptr inbounds %struct.gravity_value_r, ptr %24, i32 0, i32 1
  %26 = load i64, ptr %25, align 8, !tbaa !16
  %27 = icmp eq i64 %22, %26
  br i1 %27, label %28, label %59

28:                                               ; preds = %18
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.gravity_list_t, ptr %29, i32 0, i32 2
  %31 = getelementptr inbounds %struct.gravity_value_r, ptr %30, i32 0, i32 1
  %32 = load i64, ptr %31, align 8, !tbaa !16
  %33 = icmp ne i64 %32, 0
  br i1 %33, label %34, label %40

34:                                               ; preds = %28
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.gravity_list_t, ptr %35, i32 0, i32 2
  %37 = getelementptr inbounds %struct.gravity_value_r, ptr %36, i32 0, i32 1
  %38 = load i64, ptr %37, align 8, !tbaa !16
  %39 = shl i64 %38, 1
  br label %41

40:                                               ; preds = %28
  br label %41

41:                                               ; preds = %40, %34
  %42 = phi i64 [ %39, %34 ], [ 8, %40 ]
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.gravity_list_t, ptr %43, i32 0, i32 2
  %45 = getelementptr inbounds %struct.gravity_value_r, ptr %44, i32 0, i32 1
  store i64 %42, ptr %45, align 8, !tbaa !16
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.gravity_list_t, ptr %46, i32 0, i32 2
  %48 = getelementptr inbounds %struct.gravity_value_r, ptr %47, i32 0, i32 2
  %49 = load ptr, ptr %48, align 8, !tbaa !17
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.gravity_list_t, ptr %50, i32 0, i32 2
  %52 = getelementptr inbounds %struct.gravity_value_r, ptr %51, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !16
  %54 = mul i64 16, %53
  %55 = call ptr @realloc(ptr noundef %49, i64 noundef %54) #5
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.gravity_list_t, ptr %56, i32 0, i32 2
  %58 = getelementptr inbounds %struct.gravity_value_r, ptr %57, i32 0, i32 2
  store ptr %55, ptr %58, align 8, !tbaa !17
  br label %59

59:                                               ; preds = %41, %18
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.gravity_list_t, ptr %60, i32 0, i32 2
  %62 = getelementptr inbounds %struct.gravity_value_r, ptr %61, i32 0, i32 2
  %63 = load ptr, ptr %62, align 8, !tbaa !17
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.gravity_list_t, ptr %64, i32 0, i32 2
  %66 = getelementptr inbounds %struct.gravity_value_r, ptr %65, i32 0, i32 0
  %67 = load i64, ptr %66, align 8, !tbaa !9
  %68 = add i64 %67, 1
  store i64 %68, ptr %66, align 8, !tbaa !9
  %69 = getelementptr inbounds %struct.gravity_value_t, ptr %63, i64 %67
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.gravity_list_t, ptr %70, i32 0, i32 2
  %72 = getelementptr inbounds %struct.gravity_value_r, ptr %71, i32 0, i32 2
  %73 = load ptr, ptr %72, align 8, !tbaa !17
  %74 = load i64, ptr %8, align 8, !tbaa !15
  %75 = getelementptr inbounds %struct.gravity_value_t, ptr %73, i64 %74
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %69, ptr align 8 %75, i64 16, i1 false), !tbaa.struct !18
  br label %76

76:                                               ; preds = %59
  %77 = load i64, ptr %8, align 8, !tbaa !15
  %78 = add i64 %77, 1
  store i64 %78, ptr %8, align 8, !tbaa !15
  br label %13, !llvm.loop !20

79:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind allocsize(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 #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(1) }

!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, !14, i64 48}
!10 = !{!"", !6, i64 0, !11, i64 8, !13, i64 48}
!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 = !{!"", !14, i64 0, !14, i64 8, !6, i64 16}
!14 = !{!"long", !7, i64 0}
!15 = !{!14, !14, i64 0}
!16 = !{!10, !14, i64 56}
!17 = !{!10, !6, i64 64}
!18 = !{i64 0, i64 8, !5, i64 8, i64 8, !19}
!19 = !{!7, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"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 }

; Function Attrs: mustprogress 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) #0

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @gravity_list_append_list(ptr nocapture noundef readnone %0, ptr nocapture noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #2 {
  %4 = getelementptr inbounds i8, ptr %2, i64 48
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %.not17 = icmp eq i64 %5, 0
  br i1 %.not17, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %6 = getelementptr inbounds i8, ptr %1, i64 48
  %7 = getelementptr inbounds i8, ptr %1, i64 56
  %8 = getelementptr inbounds i8, ptr %1, i64 64
  %9 = getelementptr inbounds i8, ptr %2, i64 64
  br label %10

._crit_edge:                                      ; preds = %18, %3
  ret void

10:                                               ; preds = %.lr.ph, %18
  %.016 = phi i64 [ 0, %.lr.ph ], [ %25, %18 ]
  %11 = load i64, ptr %6, align 8, !tbaa !5
  %12 = load i64, ptr %7, align 8, !tbaa !14
  %13 = icmp eq i64 %11, %12
  %.pre = load ptr, ptr %8, align 8, !tbaa !15
  br i1 %13, label %14, label %18

14:                                               ; preds = %10
  %.not = icmp eq i64 %11, 0
  %15 = shl i64 %11, 1
  %spec.select = select i1 %.not, i64 8, i64 %15
  store i64 %spec.select, ptr %7, align 8, !tbaa !14
  %16 = shl i64 %spec.select, 4
  %17 = tail call ptr @realloc(ptr noundef %.pre, i64 noundef %16) #3
  store ptr %17, ptr %8, align 8, !tbaa !15
  %.pre18 = load i64, ptr %6, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %14, %10
  %19 = phi i64 [ %.pre18, %14 ], [ %11, %10 ]
  %20 = phi ptr [ %17, %14 ], [ %.pre, %10 ]
  %21 = add i64 %19, 1
  store i64 %21, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.gravity_value_t, ptr %20, i64 %19
  %23 = load ptr, ptr %9, align 8, !tbaa !15
  %24 = getelementptr inbounds %struct.gravity_value_t, ptr %23, i64 %.016
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %22, ptr noundef nonnull align 8 dereferenceable(16) %24, i64 16, i1 false), !tbaa.struct !16
  %25 = add nuw i64 %.016, 1
  %exitcond.not = icmp eq i64 %25, %5
  br i1 %exitcond.not, label %._crit_edge, label %10, !llvm.loop !19
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, 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 = { nounwind allocsize(1) }

!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, !13, i64 48}
!6 = !{!"", !7, i64 0, !10, i64 8, !12, i64 48}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"gravity_gc_s", !11, i64 0, !11, i64 1, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 8, !7, i64 16}
!13 = !{!"long", !8, i64 0}
!14 = !{!6, !13, i64 56}
!15 = !{!6, !7, i64 64}
!16 = !{i64 0, i64 8, !17, i64 8, i64 8, !18}
!17 = !{!7, !7, i64 0}
!18 = !{!8, !8, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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