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

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

; 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: 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
define dso_local void @json_add_string(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i8, align 1
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i64 %3, ptr %8, align 8, !tbaa !9
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  call void @json_check_comma(ptr noundef %12)
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %18, label %15

15:                                               ; preds = %4
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  call void @json_add_null(ptr noundef %16, ptr noundef %17)
  br label %66

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

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

27:                                               ; preds = %21, %18
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #4
  store i8 0, ptr %9, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %28

28:                                               ; preds = %42, %27
  %29 = load i64, ptr %10, align 8, !tbaa !9
  %30 = load i64, ptr %8, align 8, !tbaa !9
  %31 = icmp ult i64 %29, %30
  br i1 %31, label %33, label %32

32:                                               ; preds = %28
  store i32 2, ptr %11, align 4
  br label %45

33:                                               ; preds = %28
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = load i64, ptr %10, align 8, !tbaa !9
  %36 = getelementptr inbounds i8, ptr %34, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !13
  %38 = sext i8 %37 to i32
  %39 = icmp eq i32 %38, 34
  br i1 %39, label %40, label %41

40:                                               ; preds = %33
  store i8 1, ptr %9, align 1, !tbaa !11
  store i32 2, ptr %11, align 4
  br label %45

41:                                               ; preds = %33
  br label %42

42:                                               ; preds = %41
  %43 = load i64, ptr %10, align 8, !tbaa !9
  %44 = add i64 %43, 1
  store i64 %44, ptr %10, align 8, !tbaa !9
  br label %28, !llvm.loop !14

45:                                               ; preds = %40, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %46

46:                                               ; preds = %45
  %47 = load i64, ptr %8, align 8, !tbaa !9
  %48 = icmp eq i64 %47, 0
  br i1 %48, label %49, label %50

49:                                               ; preds = %46
  store i8 1, ptr %9, align 1, !tbaa !11
  br label %50

50:                                               ; preds = %49, %46
  %51 = load i8, ptr %9, align 1, !tbaa !11, !range !16, !noundef !17
  %52 = trunc i8 %51 to i1
  br i1 %52, label %53, label %59

53:                                               ; preds = %50
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = load i64, ptr %8, align 8, !tbaa !9
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = icmp eq ptr %57, null
  call void @json_write_escaped(ptr noundef %54, ptr noundef %55, i64 noundef %56, i1 noundef zeroext true, i1 noundef zeroext %58)
  br label %65

59:                                               ; preds = %50
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = load i64, ptr %8, align 8, !tbaa !9
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = icmp eq ptr %63, null
  call void @json_write_raw(ptr noundef %60, ptr noundef %61, i64 noundef %62, i1 noundef zeroext true, i1 noundef zeroext %64)
  br label %65

65:                                               ; preds = %59, %53
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #4
  br label %66

66:                                               ; preds = %65, %15
  ret void
}

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

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

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

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{i8 0, i8 2}
!17 = !{}


; 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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @json_add_string(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  tail call void @json_check_comma(ptr noundef %0)
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %5, label %6

5:                                                ; preds = %4
  tail call void @json_add_null(ptr noundef %0, ptr noundef %1)
  br label %15

6:                                                ; preds = %4
  %.not26 = icmp eq ptr %1, null
  br i1 %.not26, label %9, label %7

7:                                                ; preds = %6
  %8 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #2
  tail call void @json_write_raw(ptr noundef %0, ptr noundef nonnull %1, i64 noundef %8, i1 noundef zeroext true, i1 noundef zeroext true)
  tail call void @json_write_raw(ptr noundef %0, ptr noundef nonnull @.str.4, i64 noundef 1, i1 noundef zeroext false, i1 noundef zeroext false)
  br label %9

9:                                                ; preds = %7, %6
  %.not30 = icmp eq i64 %3, 0
  br i1 %.not30, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %9, %13
  %.029 = phi i64 [ %14, %13 ], [ 0, %9 ]
  %10 = getelementptr inbounds i8, ptr %2, i64 %.029
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %12 = icmp eq i8 %11, 34
  br i1 %12, label %.thread, label %13

13:                                               ; preds = %.lr.ph
  %14 = add nuw i64 %.029, 1
  %exitcond.not = icmp eq i64 %14, %3
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

.thread:                                          ; preds = %.lr.ph, %9
  tail call void @json_write_escaped(ptr noundef %0, ptr noundef nonnull %2, i64 noundef %3, i1 noundef zeroext true, i1 noundef zeroext %.not26)
  br label %15

._crit_edge:                                      ; preds = %13
  tail call void @json_write_raw(ptr noundef %0, ptr noundef nonnull %2, i64 noundef %3, i1 noundef zeroext true, i1 noundef zeroext %.not26)
  br label %15

15:                                               ; preds = %.thread, %._crit_edge, %5
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @json_add_null(ptr noundef, ptr noundef) local_unnamed_addr #0

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

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 = { 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 #2 = { nounwind willreturn memory(read) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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