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

%struct.sdshdr8 = type { i8, i8, i8, [0 x i8] }
%struct.sdshdr16 = type <{ i16, i16, i8, [0 x i8] }>
%struct.sdshdr32 = type <{ i32, i32, i8, [0 x i8] }>
%struct.sdshdr64 = type <{ i64, i64, i8, [0 x i8] }>

@SDS_NOINIT = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @sdsnewlen(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 i8, align 1
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #6
  %16 = load i64, ptr %5, align 8, !tbaa !9
  %17 = call signext i8 @sdsReqType(i64 noundef %16)
  store i8 %17, ptr %8, align 1, !tbaa !11
  %18 = load i8, ptr %8, align 1, !tbaa !11
  %19 = sext i8 %18 to i32
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %25

21:                                               ; preds = %2
  %22 = load i64, ptr %5, align 8, !tbaa !9
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  store i8 1, ptr %8, align 1, !tbaa !11
  br label %25

25:                                               ; preds = %24, %21, %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #6
  %26 = load i8, ptr %8, align 1, !tbaa !11
  %27 = call i32 @sdsHdrSize(i8 noundef signext %26)
  store i32 %27, ptr %9, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  %28 = load i32, ptr %9, align 4, !tbaa !12
  %29 = sext i32 %28 to i64
  %30 = load i64, ptr %5, align 8, !tbaa !9
  %31 = add i64 %29, %30
  %32 = add i64 %31, 1
  %33 = call noalias ptr @malloc(i64 noundef %32) #7
  store ptr %33, ptr %6, align 8, !tbaa !5
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = icmp eq ptr %34, null
  br i1 %35, label %36, label %37

36:                                               ; preds = %25
  store ptr null, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %135

37:                                               ; preds = %25
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load ptr, ptr @SDS_NOINIT, align 8, !tbaa !5
  %40 = icmp eq ptr %38, %39
  br i1 %40, label %41, label %42

41:                                               ; preds = %37
  store ptr null, ptr %4, align 8, !tbaa !5
  br label %53

42:                                               ; preds = %37
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = icmp ne ptr %43, null
  br i1 %44, label %52, label %45

45:                                               ; preds = %42
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load i32, ptr %9, align 4, !tbaa !12
  %48 = sext i32 %47 to i64
  %49 = load i64, ptr %5, align 8, !tbaa !9
  %50 = add i64 %48, %49
  %51 = add i64 %50, 1
  call void @llvm.memset.p0.i64(ptr align 1 %46, i8 0, i64 %51, i1 false)
  br label %52

52:                                               ; preds = %45, %42
  br label %53

53:                                               ; preds = %52, %41
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = load i32, ptr %9, align 4, !tbaa !12
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i8, ptr %54, i64 %56
  store ptr %57, ptr %7, align 8, !tbaa !5
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i64 -1
  store ptr %59, ptr %10, align 8, !tbaa !5
  %60 = load i8, ptr %8, align 1, !tbaa !11
  %61 = sext i8 %60 to i32
  switch i32 %61, label %120 [
    i32 0, label %62
    i32 1, label %70
    i32 2, label %83
    i32 3, label %96
    i32 4, label %109
  ]

62:                                               ; preds = %53
  %63 = load i8, ptr %8, align 1, !tbaa !11
  %64 = sext i8 %63 to i64
  %65 = load i64, ptr %5, align 8, !tbaa !9
  %66 = shl i64 %65, 3
  %67 = or i64 %64, %66
  %68 = trunc i64 %67 to i8
  %69 = load ptr, ptr %10, align 8, !tbaa !5
  store i8 %68, ptr %69, align 1, !tbaa !11
  br label %120

70:                                               ; preds = %53
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = getelementptr inbounds i8, ptr %71, i64 -3
  store ptr %72, ptr %12, align 8, !tbaa !5
  %73 = load i64, ptr %5, align 8, !tbaa !9
  %74 = trunc i64 %73 to i8
  %75 = load ptr, ptr %12, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.sdshdr8, ptr %75, i32 0, i32 0
  store i8 %74, ptr %76, align 1, !tbaa !11
  %77 = load i64, ptr %5, align 8, !tbaa !9
  %78 = trunc i64 %77 to i8
  %79 = load ptr, ptr %12, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.sdshdr8, ptr %79, i32 0, i32 1
  store i8 %78, ptr %80, align 1, !tbaa !11
  %81 = load i8, ptr %8, align 1, !tbaa !11
  %82 = load ptr, ptr %10, align 8, !tbaa !5
  store i8 %81, ptr %82, align 1, !tbaa !11
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  br label %120

83:                                               ; preds = %53
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #6
  %84 = load ptr, ptr %7, align 8, !tbaa !5
  %85 = getelementptr inbounds i8, ptr %84, i64 -5
  store ptr %85, ptr %13, align 8, !tbaa !5
  %86 = load i64, ptr %5, align 8, !tbaa !9
  %87 = trunc i64 %86 to i16
  %88 = load ptr, ptr %13, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.sdshdr16, ptr %88, i32 0, i32 0
  store i16 %87, ptr %89, align 1, !tbaa !14
  %90 = load i64, ptr %5, align 8, !tbaa !9
  %91 = trunc i64 %90 to i16
  %92 = load ptr, ptr %13, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.sdshdr16, ptr %92, i32 0, i32 1
  store i16 %91, ptr %93, align 1, !tbaa !14
  %94 = load i8, ptr %8, align 1, !tbaa !11
  %95 = load ptr, ptr %10, align 8, !tbaa !5
  store i8 %94, ptr %95, align 1, !tbaa !11
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #6
  br label %120

96:                                               ; preds = %53
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #6
  %97 = load ptr, ptr %7, align 8, !tbaa !5
  %98 = getelementptr inbounds i8, ptr %97, i64 -9
  store ptr %98, ptr %14, align 8, !tbaa !5
  %99 = load i64, ptr %5, align 8, !tbaa !9
  %100 = trunc i64 %99 to i32
  %101 = load ptr, ptr %14, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.sdshdr32, ptr %101, i32 0, i32 0
  store i32 %100, ptr %102, align 1, !tbaa !12
  %103 = load i64, ptr %5, align 8, !tbaa !9
  %104 = trunc i64 %103 to i32
  %105 = load ptr, ptr %14, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.sdshdr32, ptr %105, i32 0, i32 1
  store i32 %104, ptr %106, align 1, !tbaa !12
  %107 = load i8, ptr %8, align 1, !tbaa !11
  %108 = load ptr, ptr %10, align 8, !tbaa !5
  store i8 %107, ptr %108, align 1, !tbaa !11
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #6
  br label %120

109:                                              ; preds = %53
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #6
  %110 = load ptr, ptr %7, align 8, !tbaa !5
  %111 = getelementptr inbounds i8, ptr %110, i64 -17
  store ptr %111, ptr %15, align 8, !tbaa !5
  %112 = load i64, ptr %5, align 8, !tbaa !9
  %113 = load ptr, ptr %15, align 8, !tbaa !5
  %114 = getelementptr inbounds %struct.sdshdr64, ptr %113, i32 0, i32 0
  store i64 %112, ptr %114, align 1, !tbaa !9
  %115 = load i64, ptr %5, align 8, !tbaa !9
  %116 = load ptr, ptr %15, align 8, !tbaa !5
  %117 = getelementptr inbounds %struct.sdshdr64, ptr %116, i32 0, i32 1
  store i64 %115, ptr %117, align 1, !tbaa !9
  %118 = load i8, ptr %8, align 1, !tbaa !11
  %119 = load ptr, ptr %10, align 8, !tbaa !5
  store i8 %118, ptr %119, align 1, !tbaa !11
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #6
  br label %120

120:                                              ; preds = %109, %96, %83, %70, %62, %53
  %121 = load i64, ptr %5, align 8, !tbaa !9
  %122 = icmp ne i64 %121, 0
  br i1 %122, label %123, label %130

123:                                              ; preds = %120
  %124 = load ptr, ptr %4, align 8, !tbaa !5
  %125 = icmp ne ptr %124, null
  br i1 %125, label %126, label %130

126:                                              ; preds = %123
  %127 = load ptr, ptr %7, align 8, !tbaa !5
  %128 = load ptr, ptr %4, align 8, !tbaa !5
  %129 = load i64, ptr %5, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %127, ptr align 1 %128, i64 %129, i1 false)
  br label %130

130:                                              ; preds = %126, %123, %120
  %131 = load ptr, ptr %7, align 8, !tbaa !5
  %132 = load i64, ptr %5, align 8, !tbaa !9
  %133 = getelementptr inbounds i8, ptr %131, i64 %132
  store i8 0, ptr %133, align 1, !tbaa !11
  %134 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %134, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %135

135:                                              ; preds = %130, %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  %136 = load ptr, ptr %3, align 8
  ret ptr %136
}

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

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

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

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

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

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

; 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) #5

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 = { 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 #3 = { 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { nounwind }
attributes #7 = { 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 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !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"

@SDS_NOINIT = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local noalias ptr @sdsnewlen(ptr noundef readonly %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = tail call signext i8 @sdsReqType(i64 noundef %1)
  %4 = icmp eq i8 %3, 0
  %5 = icmp eq i64 %1, 0
  %or.cond = and i1 %5, %4
  %spec.store.select = select i1 %or.cond, i8 1, i8 %3
  %6 = tail call i32 @sdsHdrSize(i8 noundef signext %spec.store.select)
  %7 = sext i32 %6 to i64
  %8 = add i64 %1, 1
  %9 = add i64 %8, %7
  %10 = tail call noalias ptr @malloc(i64 noundef %9) #5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %43, label %12

12:                                               ; preds = %2
  %13 = load ptr, ptr @SDS_NOINIT, align 8, !tbaa !5
  %14 = icmp eq ptr %13, %0
  br i1 %14, label %17, label %15

15:                                               ; preds = %12
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %16, label %17

16:                                               ; preds = %15
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %10, i8 0, i64 %9, i1 false)
  br label %17

17:                                               ; preds = %12, %15, %16
  %.058 = phi ptr [ %0, %15 ], [ null, %16 ], [ null, %12 ]
  %18 = getelementptr inbounds i8, ptr %10, i64 %7
  %19 = getelementptr inbounds i8, ptr %18, i64 -1
  switch i8 %spec.store.select, label %37 [
    i8 0, label %20
    i8 1, label %22
    i8 2, label %26
    i8 3, label %30
    i8 4, label %34
  ]

20:                                               ; preds = %17
  %.tr = trunc i64 %1 to i8
  %21 = shl i8 %.tr, 3
  br label %.sink.split

22:                                               ; preds = %17
  %23 = getelementptr inbounds i8, ptr %18, i64 -3
  %24 = trunc i64 %1 to i8
  store i8 %24, ptr %23, align 1, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %18, i64 -2
  store i8 %24, ptr %25, align 1, !tbaa !9
  br label %.sink.split

26:                                               ; preds = %17
  %27 = getelementptr inbounds i8, ptr %18, i64 -5
  %28 = trunc i64 %1 to i16
  store i16 %28, ptr %27, align 1, !tbaa !10
  %29 = getelementptr inbounds i8, ptr %18, i64 -3
  store i16 %28, ptr %29, align 1, !tbaa !10
  br label %.sink.split

30:                                               ; preds = %17
  %31 = getelementptr inbounds i8, ptr %18, i64 -9
  %32 = trunc i64 %1 to i32
  store i32 %32, ptr %31, align 1, !tbaa !12
  %33 = getelementptr inbounds i8, ptr %18, i64 -5
  store i32 %32, ptr %33, align 1, !tbaa !12
  br label %.sink.split

34:                                               ; preds = %17
  %35 = getelementptr inbounds i8, ptr %18, i64 -17
  store i64 %1, ptr %35, align 1, !tbaa !14
  %36 = getelementptr inbounds i8, ptr %18, i64 -9
  store i64 %1, ptr %36, align 1, !tbaa !14
  br label %.sink.split

.sink.split:                                      ; preds = %20, %22, %26, %30, %34
  %.sink = phi i8 [ 4, %34 ], [ 3, %30 ], [ 2, %26 ], [ 1, %22 ], [ %21, %20 ]
  store i8 %.sink, ptr %19, align 1, !tbaa !9
  br label %37

37:                                               ; preds = %.sink.split, %17
  %38 = icmp ne i64 %1, 0
  %39 = icmp ne ptr %.058, null
  %or.cond3 = and i1 %38, %39
  br i1 %or.cond3, label %40, label %41

40:                                               ; preds = %37
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %18, ptr nonnull align 1 %.058, i64 %1, i1 false)
  br label %41

41:                                               ; preds = %40, %37
  %42 = getelementptr inbounds i8, ptr %18, i64 %1
  store i8 0, ptr %42, align 1, !tbaa !9
  br label %43

43:                                               ; preds = %2, %41
  %.0 = phi ptr [ %18, %41 ], [ null, %2 ]
  ret ptr %.0
}

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

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

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

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

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

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 = { 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 = { 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 = !{!"short", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"long", !7, i64 0}


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