; LLVM IR from 6leetcode/6leetcode :: questions/library/c/sds (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"

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden signext i8 @sdsReqType(i64 noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @sdsHdrSize(i8 noundef signext) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @sdslen(ptr noundef) #1

; Function Attrs: nounwind
declare void @free(ptr noundef) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @sdssetlen(ptr noundef, i64 noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @sdsavail(ptr noundef) #1

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @sdssetalloc(ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @sdsRemoveFreeSpace(ptr noundef %0) #6 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #7
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #7
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #7
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 -1
  %15 = load i8, ptr %14, align 1, !tbaa !9
  %16 = sext i8 %15 to i32
  %17 = and i32 %16, 7
  %18 = trunc i32 %17 to i8
  store i8 %18, ptr %7, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #7
  %19 = load i8, ptr %7, align 1, !tbaa !9
  %20 = call i32 @sdsHdrSize(i8 noundef signext %19)
  store i32 %20, ptr %9, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #7
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = call i64 @sdslen(ptr noundef %21)
  store i64 %22, ptr %10, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #7
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = call i64 @sdsavail(ptr noundef %23)
  store i64 %24, ptr %11, align 8, !tbaa !12
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = load i32, ptr %9, align 4, !tbaa !10
  %27 = sext i32 %26 to i64
  %28 = sub i64 0, %27
  %29 = getelementptr inbounds i8, ptr %25, i64 %28
  store ptr %29, ptr %4, align 8, !tbaa !5
  %30 = load i64, ptr %11, align 8, !tbaa !12
  %31 = icmp eq i64 %30, 0
  br i1 %31, label %32, label %34

32:                                               ; preds = %1
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %33, ptr %2, align 8
  store i32 1, ptr %12, align 4
  br label %96

34:                                               ; preds = %1
  %35 = load i64, ptr %10, align 8, !tbaa !12
  %36 = call signext i8 @sdsReqType(i64 noundef %35)
  store i8 %36, ptr %6, align 1, !tbaa !9
  %37 = load i8, ptr %6, align 1, !tbaa !9
  %38 = call i32 @sdsHdrSize(i8 noundef signext %37)
  store i32 %38, ptr %8, align 4, !tbaa !10
  %39 = load i8, ptr %7, align 1, !tbaa !9
  %40 = sext i8 %39 to i32
  %41 = load i8, ptr %6, align 1, !tbaa !9
  %42 = sext i8 %41 to i32
  %43 = icmp eq i32 %40, %42
  br i1 %43, label %48, label %44

44:                                               ; preds = %34
  %45 = load i8, ptr %6, align 1, !tbaa !9
  %46 = sext i8 %45 to i32
  %47 = icmp sgt i32 %46, 1
  br i1 %47, label %48, label %64

48:                                               ; preds = %44, %34
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = load i32, ptr %9, align 4, !tbaa !10
  %51 = sext i32 %50 to i64
  %52 = load i64, ptr %10, align 8, !tbaa !12
  %53 = add i64 %51, %52
  %54 = add i64 %53, 1
  %55 = call ptr @realloc(ptr noundef %49, i64 noundef %54) #8
  store ptr %55, ptr %5, align 8, !tbaa !5
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = icmp eq ptr %56, null
  br i1 %57, label %58, label %59

58:                                               ; preds = %48
  store ptr null, ptr %2, align 8
  store i32 1, ptr %12, align 4
  br label %96

59:                                               ; preds = %48
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = load i32, ptr %9, align 4, !tbaa !10
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds i8, ptr %60, i64 %62
  store ptr %63, ptr %3, align 8, !tbaa !5
  br label %92

64:                                               ; preds = %44
  %65 = load i32, ptr %8, align 4, !tbaa !10
  %66 = sext i32 %65 to i64
  %67 = load i64, ptr %10, align 8, !tbaa !12
  %68 = add i64 %66, %67
  %69 = add i64 %68, 1
  %70 = call noalias ptr @malloc(i64 noundef %69) #9
  store ptr %70, ptr %5, align 8, !tbaa !5
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = icmp eq ptr %71, null
  br i1 %72, label %73, label %74

73:                                               ; preds = %64
  store ptr null, ptr %2, align 8
  store i32 1, ptr %12, align 4
  br label %96

74:                                               ; preds = %64
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = load i32, ptr %8, align 4, !tbaa !10
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i8, ptr %75, i64 %77
  %79 = load ptr, ptr %3, align 8, !tbaa !5
  %80 = load i64, ptr %10, align 8, !tbaa !12
  %81 = add i64 %80, 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %78, ptr align 1 %79, i64 %81, i1 false)
  %82 = load ptr, ptr %4, align 8, !tbaa !5
  call void @free(ptr noundef %82) #7
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  %84 = load i32, ptr %8, align 4, !tbaa !10
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds i8, ptr %83, i64 %85
  store ptr %86, ptr %3, align 8, !tbaa !5
  %87 = load i8, ptr %6, align 1, !tbaa !9
  %88 = load ptr, ptr %3, align 8, !tbaa !5
  %89 = getelementptr inbounds i8, ptr %88, i64 -1
  store i8 %87, ptr %89, align 1, !tbaa !9
  %90 = load ptr, ptr %3, align 8, !tbaa !5
  %91 = load i64, ptr %10, align 8, !tbaa !12
  call void @sdssetlen(ptr noundef %90, i64 noundef %91)
  br label %92

92:                                               ; preds = %74, %59
  %93 = load ptr, ptr %3, align 8, !tbaa !5
  %94 = load i64, ptr %10, align 8, !tbaa !12
  call void @sdssetalloc(ptr noundef %93, i64 noundef %94)
  %95 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %95, ptr %2, align 8
  store i32 1, ptr %12, align 4
  br label %96

96:                                               ; preds = %92, %73, %58, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #7
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #7
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #7
  %97 = load ptr, ptr %2, align 8
  ret ptr %97
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 allocsize(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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind "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 #5 = { 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 #6 = { 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 #7 = { nounwind }
attributes #8 = { nounwind allocsize(1) }
attributes #9 = { nounwind allocsize(0) }

!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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !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: inlinehint nounwind uwtable
declare hidden signext i8 @sdsReqType(i64 noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @sdsHdrSize(i8 noundef signext) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @sdslen(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @sdssetlen(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @sdsavail(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @sdssetalloc(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @sdsRemoveFreeSpace(ptr noundef %0) local_unnamed_addr #5 {
  %2 = getelementptr inbounds i8, ptr %0, i64 -1
  %3 = load i8, ptr %2, align 1, !tbaa !5
  %4 = and i8 %3, 7
  %5 = tail call i32 @sdsHdrSize(i8 noundef signext %4)
  %6 = tail call i64 @sdslen(ptr noundef %0)
  %7 = tail call i64 @sdsavail(ptr noundef %0)
  %8 = sext i32 %5 to i64
  %9 = sub nsw i64 0, %8
  %10 = getelementptr inbounds i8, ptr %0, i64 %9
  %11 = icmp eq i64 %7, 0
  br i1 %11, label %34, label %12

12:                                               ; preds = %1
  %13 = tail call signext i8 @sdsReqType(i64 noundef %6)
  %14 = tail call i32 @sdsHdrSize(i8 noundef signext %13)
  %15 = icmp eq i8 %4, %13
  %16 = icmp sgt i8 %13, 1
  %or.cond = or i1 %15, %16
  br i1 %or.cond, label %17, label %24

17:                                               ; preds = %12
  %18 = add i64 %6, 1
  %19 = add i64 %18, %8
  %20 = tail call ptr @realloc(ptr noundef nonnull %10, i64 noundef %19) #6
  %21 = icmp eq ptr %20, null
  br i1 %21, label %34, label %22

22:                                               ; preds = %17
  %23 = getelementptr inbounds i8, ptr %20, i64 %8
  br label %33

24:                                               ; preds = %12
  %25 = sext i32 %14 to i64
  %26 = add i64 %6, 1
  %27 = add i64 %26, %25
  %28 = tail call noalias ptr @malloc(i64 noundef %27) #7
  %29 = icmp eq ptr %28, null
  br i1 %29, label %34, label %30

30:                                               ; preds = %24
  %31 = getelementptr inbounds i8, ptr %28, i64 %25
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %31, ptr nonnull align 1 %0, i64 %26, i1 false)
  tail call void @free(ptr noundef nonnull %10) #8
  %32 = getelementptr inbounds i8, ptr %31, i64 -1
  store i8 %13, ptr %32, align 1, !tbaa !5
  tail call void @sdssetlen(ptr noundef nonnull %31, i64 noundef %6)
  br label %33

33:                                               ; preds = %30, %22
  %.038 = phi ptr [ %23, %22 ], [ %31, %30 ]
  tail call void @sdssetalloc(ptr noundef %.038, i64 noundef %6)
  br label %34

34:                                               ; preds = %24, %17, %1, %33
  %.0 = phi ptr [ %.038, %33 ], [ %0, %1 ], [ null, %17 ], [ null, %24 ]
  ret ptr %.0
}

attributes #0 = { inlinehint 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 allockind("alloc,uninitialized") allocsize(0) memory(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 #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress nounwind willreturn allockind("free") 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 #4 = { 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 #5 = { 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 #6 = { nounwind allocsize(1) }
attributes #7 = { nounwind allocsize(0) }
attributes #8 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


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