; LLVM IR from kala13x/libxutils :: src/data (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.xjson_writer_ = type { %struct.xjson_format_, i8, i64, i64, i64, i64, i64, ptr, ptr, i8 }
%struct.xjson_format_ = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }

declare ptr @xalloc(ptr noundef, i64 noundef) #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @XJSON_InitWriter(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  %10 = load ptr, ptr %6, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.xjson_writer_, ptr %10, i32 0, i32 0
  call void @XJSON_FormatInit(ptr noundef %11)
  %12 = load i64, ptr %9, align 8, !tbaa !9
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.xjson_writer_, ptr %13, i32 0, i32 5
  store i64 %12, ptr %14, align 8, !tbaa !11
  %15 = load ptr, ptr %8, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.xjson_writer_, ptr %16, i32 0, i32 7
  store ptr %15, ptr %17, align 8, !tbaa !14
  %18 = load i64, ptr %9, align 8, !tbaa !9
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.xjson_writer_, ptr %19, i32 0, i32 6
  store i64 %18, ptr %20, align 8, !tbaa !15
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.xjson_writer_, ptr %22, i32 0, i32 8
  store ptr %21, ptr %23, align 8, !tbaa !16
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.xjson_writer_, ptr %24, i32 0, i32 9
  store i8 0, ptr %25, align 8, !tbaa !17
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.xjson_writer_, ptr %26, i32 0, i32 7
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  %29 = icmp eq ptr %28, null
  br i1 %29, label %30, label %51

30:                                               ; preds = %4
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.xjson_writer_, ptr %31, i32 0, i32 6
  %33 = load i64, ptr %32, align 8, !tbaa !15
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %51

35:                                               ; preds = %30
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.xjson_writer_, ptr %37, i32 0, i32 6
  %39 = load i64, ptr %38, align 8, !tbaa !15
  %40 = call ptr @xalloc(ptr noundef %36, i64 noundef %39)
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.xjson_writer_, ptr %41, i32 0, i32 7
  store ptr %40, ptr %42, align 8, !tbaa !14
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.xjson_writer_, ptr %43, i32 0, i32 7
  %45 = load ptr, ptr %44, align 8, !tbaa !14
  %46 = icmp eq ptr %45, null
  br i1 %46, label %47, label %48

47:                                               ; preds = %35
  store i32 0, ptr %5, align 4
  br label %70

48:                                               ; preds = %35
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.xjson_writer_, ptr %49, i32 0, i32 9
  store i8 1, ptr %50, align 8, !tbaa !17
  br label %51

51:                                               ; preds = %48, %30, %4
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.xjson_writer_, ptr %52, i32 0, i32 7
  %54 = load ptr, ptr %53, align 8, !tbaa !14
  %55 = icmp ne ptr %54, null
  br i1 %55, label %56, label %61

56:                                               ; preds = %51
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.xjson_writer_, ptr %57, i32 0, i32 7
  %59 = load ptr, ptr %58, align 8, !tbaa !14
  %60 = getelementptr inbounds i8, ptr %59, i64 0
  store i8 0, ptr %60, align 1, !tbaa !18
  br label %61

61:                                               ; preds = %56, %51
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.xjson_writer_, ptr %62, i32 0, i32 2
  store i64 0, ptr %63, align 8, !tbaa !19
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.xjson_writer_, ptr %64, i32 0, i32 1
  store i8 0, ptr %65, align 8, !tbaa !20
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.xjson_writer_, ptr %66, i32 0, i32 3
  store i64 0, ptr %67, align 8, !tbaa !21
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.xjson_writer_, ptr %68, i32 0, i32 4
  store i64 0, ptr %69, align 8, !tbaa !22
  store i32 1, ptr %5, align 4
  br label %70

70:                                               ; preds = %61, %47
  %71 = load i32, ptr %5, align 4
  ret i32 %71
}

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

!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, !10, i64 128}
!12 = !{!"xjson_writer_", !13, i64 0, !7, i64 96, !10, i64 104, !10, i64 112, !10, i64 120, !10, i64 128, !10, i64 136, !6, i64 144, !6, i64 152, !7, i64 160}
!13 = !{!"xjson_format_", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88}
!14 = !{!12, !6, i64 144}
!15 = !{!12, !10, i64 136}
!16 = !{!12, !6, i64 152}
!17 = !{!12, !7, i64 160}
!18 = !{!7, !7, i64 0}
!19 = !{!12, !10, i64 104}
!20 = !{!12, !7, i64 96}
!21 = !{!12, !10, i64 112}
!22 = !{!12, !10, i64 120}


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

declare ptr @xalloc(ptr noundef, i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @XJSON_InitWriter(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #1 {
  tail call void @XJSON_FormatInit(ptr noundef %0)
  %5 = getelementptr inbounds i8, ptr %0, i64 128
  store i64 %3, ptr %5, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 144
  store ptr %2, ptr %6, align 8, !tbaa !12
  %7 = getelementptr inbounds i8, ptr %0, i64 136
  store i64 %3, ptr %7, align 8, !tbaa !13
  %8 = getelementptr inbounds i8, ptr %0, i64 152
  store ptr %1, ptr %8, align 8, !tbaa !14
  %9 = getelementptr inbounds i8, ptr %0, i64 160
  store i8 0, ptr %9, align 8, !tbaa !15
  %10 = icmp ne ptr %2, null
  %.not = icmp eq i64 %3, 0
  %or.cond = or i1 %10, %.not
  br i1 %or.cond, label %14, label %11

11:                                               ; preds = %4
  %12 = tail call ptr @xalloc(ptr noundef %1, i64 noundef %3) #3
  store ptr %12, ptr %6, align 8, !tbaa !12
  %13 = icmp eq ptr %12, null
  br i1 %13, label %20, label %.thread

.thread:                                          ; preds = %11
  store i8 1, ptr %9, align 8, !tbaa !15
  br label %15

14:                                               ; preds = %4
  %.not25 = icmp eq ptr %2, null
  br i1 %.not25, label %17, label %15

15:                                               ; preds = %.thread, %14
  %16 = phi ptr [ %12, %.thread ], [ %2, %14 ]
  store i8 0, ptr %16, align 1, !tbaa !16
  br label %17

17:                                               ; preds = %15, %14
  %18 = getelementptr inbounds i8, ptr %0, i64 104
  %19 = getelementptr inbounds i8, ptr %0, i64 96
  store i8 0, ptr %19, align 8, !tbaa !17
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %18, i8 0, i64 24, i1 false)
  br label %20

20:                                               ; preds = %11, %17
  %.0 = phi i32 [ 1, %17 ], [ 0, %11 ]
  ret i32 %.0
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !11, i64 128}
!6 = !{!"xjson_writer_", !7, i64 0, !9, i64 96, !11, i64 104, !11, i64 112, !11, i64 120, !11, i64 128, !11, i64 136, !8, i64 144, !8, i64 152, !9, i64 160}
!7 = !{!"xjson_format_", !8, i64 0, !8, i64 8, !8, i64 16, !8, i64 24, !8, i64 32, !8, i64 40, !8, i64 48, !8, i64 56, !8, i64 64, !8, i64 72, !8, i64 80, !8, i64 88}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!6, !8, i64 144}
!13 = !{!6, !11, i64 136}
!14 = !{!6, !8, i64 152}
!15 = !{!6, !9, i64 160}
!16 = !{!9, !9, i64 0}
!17 = !{!6, !9, i64 96}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InstCombinePass,JumpThreadingPass,MemCpyOptPass,SROAPass,SimplifyCFGPass,TailCallElimPass