; LLVM IR from marcobambini/gravity :: src/optionals (gravity_opt_env)

; 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_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 }

@argc = external hidden global i32, align 4
@argv = external hidden global ptr, align 8

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

13:                                               ; preds = %81, %3
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = icmp ult i32 %14, %15
  br i1 %16, label %18, label %17

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

18:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #5
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds ptr, ptr %20, i64 %22
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  %25 = call { ptr, i64 } @gravity_string_to_value(ptr noundef %19, ptr noundef %24, i32 noundef -1)
  %26 = getelementptr inbounds { ptr, i64 }, ptr %8, i32 0, i32 0
  %27 = extractvalue { ptr, i64 } %25, 0
  store ptr %27, ptr %26, align 8
  %28 = getelementptr inbounds { ptr, i64 }, ptr %8, i32 0, i32 1
  %29 = extractvalue { ptr, i64 } %25, 1
  store i64 %29, ptr %28, align 8
  %30 = load ptr, ptr @argv, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.gravity_list_t, ptr %30, i32 0, i32 2
  %32 = getelementptr inbounds %struct.gravity_value_r, ptr %31, i32 0, i32 0
  %33 = load i64, ptr %32, align 8, !tbaa !11
  %34 = load ptr, ptr @argv, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.gravity_list_t, ptr %34, i32 0, i32 2
  %36 = getelementptr inbounds %struct.gravity_value_r, ptr %35, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !17
  %38 = icmp eq i64 %33, %37
  br i1 %38, label %39, label %70

39:                                               ; preds = %18
  %40 = load ptr, ptr @argv, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.gravity_list_t, ptr %40, i32 0, i32 2
  %42 = getelementptr inbounds %struct.gravity_value_r, ptr %41, i32 0, i32 1
  %43 = load i64, ptr %42, align 8, !tbaa !17
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %45, label %51

45:                                               ; preds = %39
  %46 = load ptr, ptr @argv, 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 1
  %49 = load i64, ptr %48, align 8, !tbaa !17
  %50 = shl i64 %49, 1
  br label %52

51:                                               ; preds = %39
  br label %52

52:                                               ; preds = %51, %45
  %53 = phi i64 [ %50, %45 ], [ 8, %51 ]
  %54 = load ptr, ptr @argv, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.gravity_list_t, ptr %54, i32 0, i32 2
  %56 = getelementptr inbounds %struct.gravity_value_r, ptr %55, i32 0, i32 1
  store i64 %53, ptr %56, align 8, !tbaa !17
  %57 = load ptr, ptr @argv, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.gravity_list_t, ptr %57, i32 0, i32 2
  %59 = getelementptr inbounds %struct.gravity_value_r, ptr %58, i32 0, i32 2
  %60 = load ptr, ptr %59, align 8, !tbaa !18
  %61 = load ptr, ptr @argv, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.gravity_list_t, ptr %61, i32 0, i32 2
  %63 = getelementptr inbounds %struct.gravity_value_r, ptr %62, i32 0, i32 1
  %64 = load i64, ptr %63, align 8, !tbaa !17
  %65 = mul i64 16, %64
  %66 = call ptr @realloc(ptr noundef %60, i64 noundef %65) #6
  %67 = load ptr, ptr @argv, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.gravity_list_t, ptr %67, i32 0, i32 2
  %69 = getelementptr inbounds %struct.gravity_value_r, ptr %68, i32 0, i32 2
  store ptr %66, ptr %69, align 8, !tbaa !18
  br label %70

70:                                               ; preds = %52, %18
  %71 = load ptr, ptr @argv, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.gravity_list_t, ptr %71, i32 0, i32 2
  %73 = getelementptr inbounds %struct.gravity_value_r, ptr %72, i32 0, i32 2
  %74 = load ptr, ptr %73, align 8, !tbaa !18
  %75 = load ptr, ptr @argv, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.gravity_list_t, ptr %75, i32 0, i32 2
  %77 = getelementptr inbounds %struct.gravity_value_r, ptr %76, i32 0, i32 0
  %78 = load i64, ptr %77, align 8, !tbaa !11
  %79 = add i64 %78, 1
  store i64 %79, ptr %77, align 8, !tbaa !11
  %80 = getelementptr inbounds %struct.gravity_value_t, ptr %74, i64 %78
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %80, ptr align 8 %8, i64 16, i1 false), !tbaa.struct !19
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #5
  br label %81

81:                                               ; preds = %70
  %82 = load i32, ptr %7, align 4, !tbaa !9
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %7, align 4, !tbaa !9
  br label %13, !llvm.loop !21

84:                                               ; preds = %17
  ret void
}

declare ptr @gravity_list_new(ptr noundef, i32 noundef) #1

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

declare { ptr, i64 } @gravity_string_to_value(ptr noundef, ptr noundef, i32 noundef) #1

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

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

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

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 = { "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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nounwind }
attributes #6 = { 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !16, i64 48}
!12 = !{!"", !6, i64 0, !13, i64 8, !15, i64 48}
!13 = !{!"gravity_gc_s", !14, i64 0, !14, i64 1, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!"", !16, i64 0, !16, i64 8, !6, i64 16}
!16 = !{!"long", !7, i64 0}
!17 = !{!12, !16, i64 56}
!18 = !{!12, !6, i64 64}
!19 = !{i64 0, i64 8, !5, i64 8, i64 8, !20}
!20 = !{!7, !7, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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 }

@argc = external hidden local_unnamed_addr global i32, align 4
@argv = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @gravity_env_register_args(ptr noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  store i32 %1, ptr @argc, align 4, !tbaa !5
  %4 = tail call ptr @gravity_list_new(ptr noundef %0, i32 noundef %1) #3
  store ptr %4, ptr @argv, align 8, !tbaa !9
  %.not9 = icmp eq i32 %1, 0
  br i1 %.not9, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %wide.trip.count = zext i32 %1 to i64
  br label %.lr.ph

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

.lr.ph:                                           ; preds = %.lr.ph.preheader, %24
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %24 ]
  %5 = getelementptr inbounds ptr, ptr %2, i64 %indvars.iv
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = tail call { ptr, i64 } @gravity_string_to_value(ptr noundef %0, ptr noundef %6, i32 noundef -1) #3
  %8 = extractvalue { ptr, i64 } %7, 0
  %9 = extractvalue { ptr, i64 } %7, 1
  %10 = load ptr, ptr @argv, align 8, !tbaa !9
  %11 = getelementptr inbounds i8, ptr %10, i64 48
  %12 = load i64, ptr %11, align 8, !tbaa !11
  %13 = getelementptr inbounds i8, ptr %10, i64 56
  %14 = load i64, ptr %13, align 8, !tbaa !17
  %15 = icmp eq i64 %12, %14
  br i1 %15, label %16, label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph
  %.phi.trans.insert = getelementptr inbounds i8, ptr %10, i64 64
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !18
  br label %24

16:                                               ; preds = %.lr.ph
  %.not = icmp eq i64 %12, 0
  %17 = shl i64 %12, 1
  %spec.select = select i1 %.not, i64 8, i64 %17
  store i64 %spec.select, ptr %13, align 8, !tbaa !17
  %18 = getelementptr inbounds i8, ptr %10, i64 64
  %19 = load ptr, ptr %18, align 8, !tbaa !18
  %20 = shl i64 %spec.select, 4
  %21 = tail call ptr @realloc(ptr noundef %19, i64 noundef %20) #4
  %22 = load ptr, ptr @argv, align 8, !tbaa !9
  %23 = getelementptr inbounds i8, ptr %22, i64 64
  store ptr %21, ptr %23, align 8, !tbaa !18
  %.phi.trans.insert11 = getelementptr inbounds i8, ptr %22, i64 48
  %.pre12 = load i64, ptr %.phi.trans.insert11, align 8, !tbaa !11
  br label %24

24:                                               ; preds = %.lr.ph._crit_edge, %16
  %25 = phi i64 [ %.pre12, %16 ], [ %12, %.lr.ph._crit_edge ]
  %26 = phi ptr [ %21, %16 ], [ %.pre, %.lr.ph._crit_edge ]
  %27 = phi ptr [ %22, %16 ], [ %10, %.lr.ph._crit_edge ]
  %28 = getelementptr inbounds i8, ptr %27, i64 48
  %29 = add i64 %25, 1
  store i64 %29, ptr %28, align 8, !tbaa !11
  %30 = getelementptr inbounds %struct.gravity_value_t, ptr %26, i64 %25
  store ptr %8, ptr %30, align 8, !tbaa !9
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %30, i64 8
  store i64 %9, ptr %.sroa.4.0..sroa_idx, align 8, !tbaa !19
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !20
}

declare ptr @gravity_list_new(ptr noundef, i32 noundef) local_unnamed_addr #1

declare { ptr, i64 } @gravity_string_to_value(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; 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 #2

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


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