; LLVM IR from DanEngelbrecht/longtail :: src/ext (stb_ds)

; 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.stbds_array_header = type { i64, i64, ptr, i64 }

; Function Attrs: nounwind uwtable
define dso_local ptr @stbds_arrgrowf(ptr noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i64 %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %20

15:                                               ; preds = %4
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.stbds_array_header, ptr %16, i64 -1
  %18 = getelementptr inbounds %struct.stbds_array_header, ptr %17, i32 0, i32 0
  %19 = load i64, ptr %18, align 8, !tbaa !11
  br label %21

20:                                               ; preds = %4
  br label %21

21:                                               ; preds = %20, %15
  %22 = phi i64 [ %19, %15 ], [ 0, %20 ]
  %23 = load i64, ptr %8, align 8, !tbaa !9
  %24 = add i64 %22, %23
  store i64 %24, ptr %11, align 8, !tbaa !9
  %25 = load i64, ptr %11, align 8, !tbaa !9
  %26 = load i64, ptr %9, align 8, !tbaa !9
  %27 = icmp ugt i64 %25, %26
  br i1 %27, label %28, label %30

28:                                               ; preds = %21
  %29 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %29, ptr %9, align 8, !tbaa !9
  br label %30

30:                                               ; preds = %28, %21
  %31 = load i64, ptr %9, align 8, !tbaa !9
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = icmp ne ptr %32, null
  br i1 %33, label %34, label %39

34:                                               ; preds = %30
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.stbds_array_header, ptr %35, i64 -1
  %37 = getelementptr inbounds %struct.stbds_array_header, ptr %36, i32 0, i32 1
  %38 = load i64, ptr %37, align 8, !tbaa !13
  br label %40

39:                                               ; preds = %30
  br label %40

40:                                               ; preds = %39, %34
  %41 = phi i64 [ %38, %34 ], [ 0, %39 ]
  %42 = icmp ule i64 %31, %41
  br i1 %42, label %43, label %45

43:                                               ; preds = %40
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %44, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %108

45:                                               ; preds = %40
  %46 = load i64, ptr %9, align 8, !tbaa !9
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = icmp ne ptr %47, null
  br i1 %48, label %49, label %54

49:                                               ; preds = %45
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.stbds_array_header, ptr %50, i64 -1
  %52 = getelementptr inbounds %struct.stbds_array_header, ptr %51, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !13
  br label %55

54:                                               ; preds = %45
  br label %55

55:                                               ; preds = %54, %49
  %56 = phi i64 [ %53, %49 ], [ 0, %54 ]
  %57 = mul i64 2, %56
  %58 = icmp ult i64 %46, %57
  br i1 %58, label %59, label %71

59:                                               ; preds = %55
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = icmp ne ptr %60, null
  br i1 %61, label %62, label %67

62:                                               ; preds = %59
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.stbds_array_header, ptr %63, i64 -1
  %65 = getelementptr inbounds %struct.stbds_array_header, ptr %64, i32 0, i32 1
  %66 = load i64, ptr %65, align 8, !tbaa !13
  br label %68

67:                                               ; preds = %59
  br label %68

68:                                               ; preds = %67, %62
  %69 = phi i64 [ %66, %62 ], [ 0, %67 ]
  %70 = mul i64 2, %69
  store i64 %70, ptr %9, align 8, !tbaa !9
  br label %76

71:                                               ; preds = %55
  %72 = load i64, ptr %9, align 8, !tbaa !9
  %73 = icmp ult i64 %72, 4
  br i1 %73, label %74, label %75

74:                                               ; preds = %71
  store i64 4, ptr %9, align 8, !tbaa !9
  br label %75

75:                                               ; preds = %74, %71
  br label %76

76:                                               ; preds = %75, %68
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = icmp ne ptr %77, null
  br i1 %78, label %79, label %82

79:                                               ; preds = %76
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.stbds_array_header, ptr %80, i64 -1
  br label %83

82:                                               ; preds = %76
  br label %83

83:                                               ; preds = %82, %79
  %84 = phi ptr [ %81, %79 ], [ null, %82 ]
  %85 = load i64, ptr %7, align 8, !tbaa !9
  %86 = load i64, ptr %9, align 8, !tbaa !9
  %87 = mul i64 %85, %86
  %88 = add i64 %87, 32
  %89 = call ptr @realloc(ptr noundef %84, i64 noundef %88) #4
  store ptr %89, ptr %10, align 8, !tbaa !5
  %90 = load ptr, ptr %10, align 8, !tbaa !5
  %91 = getelementptr inbounds i8, ptr %90, i64 32
  store ptr %91, ptr %10, align 8, !tbaa !5
  %92 = load ptr, ptr %6, align 8, !tbaa !5
  %93 = icmp eq ptr %92, null
  br i1 %93, label %94, label %101

94:                                               ; preds = %83
  %95 = load ptr, ptr %10, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.stbds_array_header, ptr %95, i64 -1
  %97 = getelementptr inbounds %struct.stbds_array_header, ptr %96, i32 0, i32 0
  store i64 0, ptr %97, align 8, !tbaa !11
  %98 = load ptr, ptr %10, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.stbds_array_header, ptr %98, i64 -1
  %100 = getelementptr inbounds %struct.stbds_array_header, ptr %99, i32 0, i32 2
  store ptr null, ptr %100, align 8, !tbaa !14
  br label %102

101:                                              ; preds = %83
  br label %102

102:                                              ; preds = %101, %94
  %103 = load i64, ptr %9, align 8, !tbaa !9
  %104 = load ptr, ptr %10, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct.stbds_array_header, ptr %104, i64 -1
  %106 = getelementptr inbounds %struct.stbds_array_header, ptr %105, i32 0, i32 1
  store i64 %103, ptr %106, align 8, !tbaa !13
  %107 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %107, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %108

108:                                              ; preds = %102, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %109 = load ptr, ptr %5, align 8
  ret ptr %109
}

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(1) }

!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 0}
!12 = !{!"", !10, i64 0, !10, i64 8, !6, i64 16, !10, i64 24}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !6, i64 16}


; 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: mustprogress nounwind willreturn uwtable
define dso_local ptr @stbds_arrgrowf(ptr noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %5, label %.thread35

5:                                                ; preds = %4
  %spec.select33 = tail call i64 @llvm.umax.i64(i64 %2, i64 %3)
  %.not32.not = icmp eq i64 %spec.select33, 0
  br i1 %.not32.not, label %24, label %.thread43

.thread35:                                        ; preds = %4
  %6 = getelementptr inbounds i8, ptr %0, i64 -32
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %8 = add i64 %7, %2
  %spec.select = tail call i64 @llvm.umax.i64(i64 %8, i64 %3)
  %9 = getelementptr inbounds i8, ptr %0, i64 -24
  %10 = load i64, ptr %9, align 8, !tbaa !11
  %.not3237 = icmp ugt i64 %spec.select, %10
  br i1 %.not3237, label %.thread39, label %24

.thread39:                                        ; preds = %.thread35
  %11 = shl i64 %10, 1
  %12 = icmp ult i64 %spec.select, %11
  br i1 %12, label %13, label %.thread43

.thread43:                                        ; preds = %5, %.thread39
  %spec.select34384245 = phi i64 [ %spec.select, %.thread39 ], [ %spec.select33, %5 ]
  %spec.store.select = tail call i64 @llvm.umax.i64(i64 %spec.select34384245, i64 4)
  br label %13

13:                                               ; preds = %.thread39, %.thread43
  %.1 = phi i64 [ %spec.store.select, %.thread43 ], [ %11, %.thread39 ]
  %14 = getelementptr inbounds i8, ptr %0, i64 -32
  %15 = select i1 %.not, ptr null, ptr %14
  %16 = mul i64 %.1, %1
  %17 = add i64 %16, 32
  %18 = tail call ptr @realloc(ptr noundef %15, i64 noundef %17) #3
  %19 = getelementptr inbounds i8, ptr %18, i64 32
  br i1 %.not, label %20, label %22

20:                                               ; preds = %13
  store i64 0, ptr %18, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %18, i64 16
  store ptr null, ptr %21, align 8, !tbaa !12
  br label %22

22:                                               ; preds = %13, %20
  %23 = getelementptr inbounds i8, ptr %18, i64 8
  store i64 %.1, ptr %23, align 8, !tbaa !11
  br label %24

24:                                               ; preds = %.thread35, %5, %22
  %.0 = phi ptr [ %19, %22 ], [ null, %5 ], [ %0, %.thread35 ]
  ret ptr %.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 #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #2

attributes #0 = { mustprogress nounwind willreturn 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 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 #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nounwind allocsize(1) }

!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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16, !7, i64 24}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 8}
!12 = !{!6, !10, i64 16}


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