; LLVM IR from marcobambini/gravity :: src/utils (gravity_json)

; 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.json_t = type { ptr, i64, i64, ptr, i32, %struct.JSON_CONTEXT_R }
%struct.JSON_CONTEXT_R = type { i64, i64, ptr }

@.str.4 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [2 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare hidden void @json_check_comma(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @json_write_raw(ptr noundef, ptr noundef, i64 noundef, i1 noundef zeroext, i1 noundef zeroext) #1

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @json_clear_option(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @json_begin_array(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %5 = load ptr, ptr %3, align 8, !tbaa !5
  call void @json_check_comma(ptr noundef %5)
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.json_t, ptr %6, i32 0, i32 5
  %8 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %7, i32 0, i32 2
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.json_t, ptr %10, i32 0, i32 5
  %12 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %11, i32 0, i32 0
  %13 = load i64, ptr %12, align 8, !tbaa !14
  %14 = sub i64 %13, 1
  %15 = getelementptr inbounds i32, ptr %9, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !15
  %17 = icmp ne i32 %16, 1
  br i1 %17, label %18, label %19

18:                                               ; preds = %2
  store ptr null, ptr %4, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %18, %2
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %28

22:                                               ; preds = %19
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = call i64 @strlen(ptr noundef %25) #3
  call void @json_write_raw(ptr noundef %23, ptr noundef %24, i64 noundef %26, i1 noundef zeroext true, i1 noundef zeroext true)
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  call void @json_write_raw(ptr noundef %27, ptr noundef @.str.4, i64 noundef 1, i1 noundef zeroext false, i1 noundef zeroext false)
  br label %28

28:                                               ; preds = %22, %19
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.json_t, ptr %29, i32 0, i32 5
  %31 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %30, i32 0, i32 0
  %32 = load i64, ptr %31, align 8, !tbaa !14
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.json_t, ptr %33, i32 0, i32 5
  %35 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %34, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !16
  %37 = icmp eq i64 %32, %36
  br i1 %37, label %38, label %69

38:                                               ; preds = %28
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.json_t, ptr %39, i32 0, i32 5
  %41 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %40, i32 0, i32 1
  %42 = load i64, ptr %41, align 8, !tbaa !16
  %43 = icmp ne i64 %42, 0
  br i1 %43, label %44, label %50

44:                                               ; preds = %38
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.json_t, ptr %45, i32 0, i32 5
  %47 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %46, i32 0, i32 1
  %48 = load i64, ptr %47, align 8, !tbaa !16
  %49 = shl i64 %48, 1
  br label %51

50:                                               ; preds = %38
  br label %51

51:                                               ; preds = %50, %44
  %52 = phi i64 [ %49, %44 ], [ 8, %50 ]
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.json_t, ptr %53, i32 0, i32 5
  %55 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %54, i32 0, i32 1
  store i64 %52, ptr %55, align 8, !tbaa !16
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.json_t, ptr %56, i32 0, i32 5
  %58 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %57, i32 0, i32 2
  %59 = load ptr, ptr %58, align 8, !tbaa !9
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.json_t, ptr %60, i32 0, i32 5
  %62 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %61, i32 0, i32 1
  %63 = load i64, ptr %62, align 8, !tbaa !16
  %64 = mul i64 4, %63
  %65 = call ptr @realloc(ptr noundef %59, i64 noundef %64) #4
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.json_t, ptr %66, i32 0, i32 5
  %68 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %67, i32 0, i32 2
  store ptr %65, ptr %68, align 8, !tbaa !9
  br label %69

69:                                               ; preds = %51, %28
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.json_t, ptr %70, i32 0, i32 5
  %72 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %71, i32 0, i32 2
  %73 = load ptr, ptr %72, align 8, !tbaa !9
  %74 = load ptr, ptr %3, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.json_t, ptr %74, i32 0, i32 5
  %76 = getelementptr inbounds %struct.JSON_CONTEXT_R, ptr %75, i32 0, i32 0
  %77 = load i64, ptr %76, align 8, !tbaa !14
  %78 = add i64 %77, 1
  store i64 %78, ptr %76, align 8, !tbaa !14
  %79 = getelementptr inbounds i32, ptr %73, i64 %77
  store i32 2, ptr %79, align 4, !tbaa !15
  %80 = load ptr, ptr %3, align 8, !tbaa !5
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = icmp eq ptr %81, null
  call void @json_write_raw(ptr noundef %80, ptr noundef @.str.7, i64 noundef 1, i1 noundef zeroext false, i1 noundef zeroext %82)
  %83 = load ptr, ptr %3, align 8, !tbaa !5
  call void @json_clear_option(ptr noundef %83, i32 noundef 1)
  ret void
}

attributes #0 = { 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 #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 willreturn memory(read) "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 willreturn memory(read) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 56}
!10 = !{!"json_t", !6, i64 0, !11, i64 8, !11, i64 16, !6, i64 24, !12, i64 32, !13, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"", !11, i64 0, !11, i64 8, !6, i64 16}
!14 = !{!10, !11, i64 40}
!15 = !{!12, !12, i64 0}
!16 = !{!10, !11, i64 48}


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

@.str.4 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [2 x i8], align 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 #0

; Function Attrs: nounwind uwtable
declare hidden void @json_check_comma(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @json_write_raw(ptr noundef, ptr noundef, i64 noundef, i1 noundef zeroext, i1 noundef zeroext) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @json_clear_option(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @json_begin_array(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  tail call void @json_check_comma(ptr noundef %0)
  %3 = getelementptr inbounds i8, ptr %0, i64 40
  %4 = getelementptr inbounds i8, ptr %0, i64 56
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = load i64, ptr %3, align 8, !tbaa !13
  %7 = getelementptr i32, ptr %5, i64 %6
  %8 = getelementptr i8, ptr %7, i64 -4
  %9 = load i32, ptr %8, align 4, !tbaa !14
  %.not = icmp ne i32 %9, 1
  %.not2225 = icmp eq ptr %1, null
  %.not22 = or i1 %.not2225, %.not
  br i1 %.not22, label %12, label %10

10:                                               ; preds = %2
  %11 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #3
  tail call void @json_write_raw(ptr noundef nonnull %0, ptr noundef nonnull %1, i64 noundef %11, i1 noundef zeroext true, i1 noundef zeroext true)
  tail call void @json_write_raw(ptr noundef nonnull %0, ptr noundef nonnull @.str.4, i64 noundef 1, i1 noundef zeroext false, i1 noundef zeroext false)
  %.pre = load i64, ptr %3, align 8, !tbaa !13
  %.pre26.pre = load ptr, ptr %4, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %10, %2
  %.pre26 = phi ptr [ %.pre26.pre, %10 ], [ %5, %2 ]
  %13 = phi i64 [ %.pre, %10 ], [ %6, %2 ]
  %14 = getelementptr inbounds i8, ptr %0, i64 48
  %15 = load i64, ptr %14, align 8, !tbaa !15
  %16 = icmp eq i64 %13, %15
  br i1 %16, label %17, label %21

17:                                               ; preds = %12
  %.not23 = icmp eq i64 %13, 0
  %18 = shl i64 %13, 1
  %spec.select24 = select i1 %.not23, i64 8, i64 %18
  store i64 %spec.select24, ptr %14, align 8, !tbaa !15
  %19 = shl i64 %spec.select24, 2
  %20 = tail call ptr @realloc(ptr noundef %.pre26, i64 noundef %19) #4
  store ptr %20, ptr %4, align 8, !tbaa !5
  %.pre27 = load i64, ptr %3, align 8, !tbaa !13
  br label %21

21:                                               ; preds = %17, %12
  %22 = phi i64 [ %.pre27, %17 ], [ %13, %12 ]
  %23 = phi ptr [ %20, %17 ], [ %.pre26, %12 ]
  %24 = add i64 %22, 1
  store i64 %24, ptr %3, align 8, !tbaa !13
  %25 = getelementptr inbounds i32, ptr %23, i64 %22
  store i32 2, ptr %25, align 4, !tbaa !14
  tail call void @json_write_raw(ptr noundef nonnull %0, ptr noundef nonnull @.str.7, i64 noundef 1, i1 noundef zeroext false, i1 noundef zeroext %.not22)
  tail call void @json_clear_option(ptr noundef nonnull %0, i32 noundef 1)
  ret void
}

attributes #0 = { 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 #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 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 willreturn memory(read) }
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, !7, i64 56}
!6 = !{!"json_t", !7, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !11, i64 32, !12, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 8, !7, i64 16}
!13 = !{!6, !10, i64 40}
!14 = !{!11, !11, i64 0}
!15 = !{!6, !10, i64 48}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass