; LLVM IR from yinqiwen/ardb :: src/common/util (sds)

; 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.sdshdr = type { i32, i32, [0 x i8] }

; Function Attrs: nounwind uwtable
declare dso_local ptr @sdsnewlen(ptr noundef, i64 noundef) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @sdsMakeRoomFor(ptr noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @sdsgrowzero(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = icmp eq ptr null, %11
  br i1 %12, label %13, label %19

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %14 = load i64, ptr %5, align 8, !tbaa !9
  %15 = call ptr @sdsnewlen(ptr noundef null, i64 noundef %14)
  store ptr %15, ptr %6, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = load i64, ptr %5, align 8, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 1 %16, i8 0, i64 %17, i1 false)
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %18, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %73

19:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i64 -8
  store ptr %21, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.sdshdr, ptr %22, i32 0, i32 0
  %24 = load i32, ptr %23, align 4, !tbaa !11
  %25 = sext i32 %24 to i64
  store i64 %25, ptr %9, align 8, !tbaa !9
  %26 = load i64, ptr %5, align 8, !tbaa !9
  %27 = load i64, ptr %9, align 8, !tbaa !9
  %28 = icmp ule i64 %26, %27
  br i1 %28, label %29, label %31

29:                                               ; preds = %19
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %30, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %72

31:                                               ; preds = %19
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load i64, ptr %5, align 8, !tbaa !9
  %34 = load i64, ptr %9, align 8, !tbaa !9
  %35 = sub i64 %33, %34
  %36 = call ptr @sdsMakeRoomFor(ptr noundef %32, i64 noundef %35)
  store ptr %36, ptr %4, align 8, !tbaa !5
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = icmp eq ptr %37, null
  br i1 %38, label %39, label %40

39:                                               ; preds = %31
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %72

40:                                               ; preds = %31
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %41, i64 -8
  store ptr %42, ptr %7, align 8, !tbaa !5
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load i64, ptr %9, align 8, !tbaa !9
  %45 = getelementptr inbounds i8, ptr %43, i64 %44
  %46 = load i64, ptr %5, align 8, !tbaa !9
  %47 = load i64, ptr %9, align 8, !tbaa !9
  %48 = sub i64 %46, %47
  %49 = add i64 %48, 1
  call void @llvm.memset.p0.i64(ptr align 1 %45, i8 0, i64 %49, i1 false)
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.sdshdr, ptr %50, i32 0, i32 0
  %52 = load i32, ptr %51, align 4, !tbaa !11
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.sdshdr, ptr %53, i32 0, i32 1
  %55 = load i32, ptr %54, align 4, !tbaa !11
  %56 = add nsw i32 %52, %55
  %57 = sext i32 %56 to i64
  store i64 %57, ptr %8, align 8, !tbaa !9
  %58 = load i64, ptr %5, align 8, !tbaa !9
  %59 = trunc i64 %58 to i32
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.sdshdr, ptr %60, i32 0, i32 0
  store i32 %59, ptr %61, align 4, !tbaa !11
  %62 = load i64, ptr %8, align 8, !tbaa !9
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.sdshdr, ptr %63, i32 0, i32 0
  %65 = load i32, ptr %64, align 4, !tbaa !11
  %66 = sext i32 %65 to i64
  %67 = sub i64 %62, %66
  %68 = trunc i64 %67 to i32
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.sdshdr, ptr %69, i32 0, i32 1
  store i32 %68, ptr %70, align 4, !tbaa !11
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %71, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %72

72:                                               ; preds = %40, %39, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %73

73:                                               ; preds = %72, %13
  %74 = load ptr, ptr %3, align 8
  ret ptr %74
}

; 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 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !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 = !{!"int", !7, i64 0}


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

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @sdsgrowzero(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %4, label %6

4:                                                ; preds = %2
  %5 = tail call ptr @sdsnewlen(ptr noundef null, i64 noundef %1)
  tail call void @llvm.memset.p0.i64(ptr align 1 %5, i8 0, i64 %1, i1 false)
  br label %24

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 -8
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %.not = icmp ult i64 %9, %1
  br i1 %.not, label %10, label %24

10:                                               ; preds = %6
  %11 = sub i64 %1, %9
  %12 = tail call ptr @sdsMakeRoomFor(ptr noundef nonnull %0, i64 noundef %11)
  %13 = icmp eq ptr %12, null
  br i1 %13, label %24, label %14

14:                                               ; preds = %10
  %15 = getelementptr inbounds i8, ptr %12, i64 -8
  %16 = getelementptr inbounds i8, ptr %12, i64 %9
  %17 = add i64 %11, 1
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %16, i8 0, i64 %17, i1 false)
  %18 = load i32, ptr %15, align 4, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %12, i64 -4
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = trunc i64 %1 to i32
  store i32 %21, ptr %15, align 4, !tbaa !5
  %22 = sub i32 %18, %21
  %23 = add i32 %22, %20
  store i32 %23, ptr %19, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %14, %6, %10, %4
  %.1 = phi ptr [ %5, %4 ], [ %12, %14 ], [ %0, %6 ], [ null, %10 ]
  ret ptr %.1
}

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

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 nocallback nofree nounwind willreturn memory(argmem: write) }

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


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