; LLVM IR from jingweizhanghuai/Morn :: src/util (morn_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.HandleJSON = type { [64 x %struct.ListLayer], [64 x %struct.ArrayLayer], [2 x %struct.ListNode], [2 x %struct.ArrayNode], ptr, i32, ptr }
%struct.ListLayer = type { ptr, i32, i32 }
%struct.ArrayLayer = type { ptr, i32, i32 }
%struct.ListNode = type <{ %union.JsonData, i8, i32, [3 x i8], ptr }>
%union.JsonData = type { double }
%struct.ArrayNode = type <{ %union.JsonData, i8, i32, [3 x i8] }>

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

declare void @m_Free(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: nounwind uwtable
define dso_local void @endJSON(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %4

4:                                                ; preds = %43, %1
  %5 = load i32, ptr %3, align 4, !tbaa !9
  %6 = icmp slt i32 %5, 64
  br i1 %6, label %8, label %7

7:                                                ; preds = %4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %46

8:                                                ; preds = %4
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.HandleJSON, ptr %9, i32 0, i32 0
  %11 = load i32, ptr %3, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [64 x %struct.ListLayer], ptr %10, i64 0, i64 %12
  %14 = getelementptr inbounds %struct.ListLayer, ptr %13, i32 0, i32 2
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = icmp sgt i32 %15, 1
  br i1 %16, label %17, label %25

17:                                               ; preds = %8
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.HandleJSON, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %3, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [64 x %struct.ListLayer], ptr %19, i64 0, i64 %21
  %23 = getelementptr inbounds %struct.ListLayer, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  call void @m_Free(ptr noundef %24)
  br label %25

25:                                               ; preds = %17, %8
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.HandleJSON, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %3, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [64 x %struct.ArrayLayer], ptr %27, i64 0, i64 %29
  %31 = getelementptr inbounds %struct.ArrayLayer, ptr %30, i32 0, i32 2
  %32 = load i32, ptr %31, align 4, !tbaa !14
  %33 = icmp sgt i32 %32, 1
  br i1 %33, label %34, label %42

34:                                               ; preds = %25
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.HandleJSON, ptr %35, i32 0, i32 1
  %37 = load i32, ptr %3, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [64 x %struct.ArrayLayer], ptr %36, i64 0, i64 %38
  %40 = getelementptr inbounds %struct.ArrayLayer, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !16
  call void @m_Free(ptr noundef %41)
  br label %42

42:                                               ; preds = %34, %25
  br label %43

43:                                               ; preds = %42
  %44 = load i32, ptr %3, align 4, !tbaa !9
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %3, align 4, !tbaa !9
  br label %4, !llvm.loop !17

46:                                               ; preds = %7
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.HandleJSON, ptr %47, i32 0, i32 4
  %49 = load ptr, ptr %48, align 8, !tbaa !19
  %50 = icmp ne ptr %49, null
  br i1 %50, label %51, label %55

51:                                               ; preds = %46
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.HandleJSON, ptr %52, i32 0, i32 4
  %54 = load ptr, ptr %53, align 8, !tbaa !19
  call void @m_Free(ptr noundef %54)
  br label %55

55:                                               ; preds = %51, %46
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.HandleJSON, ptr %56, i32 0, i32 6
  %58 = load ptr, ptr %57, align 8, !tbaa !21
  %59 = icmp ne ptr %58, null
  br i1 %59, label %60, label %64

60:                                               ; preds = %55
  %61 = load ptr, ptr %2, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.HandleJSON, ptr %61, i32 0, i32 6
  %63 = load ptr, ptr %62, align 8, !tbaa !21
  call void @mMemoryRelease(ptr noundef %63)
  br label %64

64:                                               ; preds = %60, %55
  ret void
}

declare void @mMemoryRelease(ptr noundef) #1

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 = { 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 }

!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, !10, i64 12}
!12 = !{!"ListLayer", !6, i64 0, !10, i64 8, !10, i64 12}
!13 = !{!12, !6, i64 0}
!14 = !{!15, !10, i64 12}
!15 = !{!"ArrayLayer", !6, i64 0, !10, i64 8, !10, i64 12}
!16 = !{!15, !6, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!20, !6, i64 2128}
!20 = !{!"HandleJSON", !7, i64 0, !7, i64 1024, !7, i64 2048, !7, i64 2096, !6, i64 2128, !10, i64 2136, !6, i64 2144}
!21 = !{!20, !6, i64 2144}


; 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.ListLayer = type { ptr, i32, i32 }
%struct.ArrayLayer = type { ptr, i32, i32 }

declare void @m_Free(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @endJSON(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 1024
  br label %6

3:                                                ; preds = %20
  %4 = getelementptr inbounds i8, ptr %0, i64 2128
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %22, label %21

6:                                                ; preds = %1, %20
  %indvars.iv = phi i64 [ 0, %1 ], [ %indvars.iv.next, %20 ]
  %7 = getelementptr inbounds [64 x %struct.ListLayer], ptr %0, i64 0, i64 %indvars.iv
  %8 = getelementptr inbounds i8, ptr %7, i64 12
  %9 = load i32, ptr %8, align 4, !tbaa !11
  %10 = icmp sgt i32 %9, 1
  br i1 %10, label %11, label %13

11:                                               ; preds = %6
  %12 = load ptr, ptr %7, align 8, !tbaa !13
  tail call void @m_Free(ptr noundef %12) #2
  br label %13

13:                                               ; preds = %11, %6
  %14 = getelementptr inbounds [64 x %struct.ArrayLayer], ptr %2, i64 0, i64 %indvars.iv
  %15 = getelementptr inbounds i8, ptr %14, i64 12
  %16 = load i32, ptr %15, align 4, !tbaa !14
  %17 = icmp sgt i32 %16, 1
  br i1 %17, label %18, label %20

18:                                               ; preds = %13
  %19 = load ptr, ptr %14, align 8, !tbaa !16
  tail call void @m_Free(ptr noundef %19) #2
  br label %20

20:                                               ; preds = %13, %18
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 64
  br i1 %exitcond.not, label %3, label %6, !llvm.loop !17

21:                                               ; preds = %3
  tail call void @m_Free(ptr noundef nonnull %5) #2
  br label %22

22:                                               ; preds = %21, %3
  %23 = getelementptr inbounds i8, ptr %0, i64 2144
  %24 = load ptr, ptr %23, align 8, !tbaa !19
  %.not15 = icmp eq ptr %24, null
  br i1 %.not15, label %26, label %25

25:                                               ; preds = %22
  tail call void @mMemoryRelease(ptr noundef nonnull %24) #2
  br label %26

26:                                               ; preds = %25, %22
  ret void
}

declare void @mMemoryRelease(ptr noundef) local_unnamed_addr #0

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, !9, i64 2128}
!6 = !{!"HandleJSON", !7, i64 0, !7, i64 1024, !7, i64 2048, !7, i64 2096, !9, i64 2128, !10, i64 2136, !9, i64 2144}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"any pointer", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 12}
!12 = !{!"ListLayer", !9, i64 0, !10, i64 8, !10, i64 12}
!13 = !{!12, !9, i64 0}
!14 = !{!15, !10, i64 12}
!15 = !{!"ArrayLayer", !9, i64 0, !10, i64 8, !10, i64 12}
!16 = !{!15, !9, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!6, !9, i64 2144}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass