; LLVM IR from OpenFAST/openfast :: modules/map/src/bstring (bstrlib)

; 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.__va_list_tag = type { i32, i32, ptr, ptr }
%struct.tagbstring = type { i32, i32, ptr }

@.str = external hidden unnamed_addr constant [1 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @balloc(ptr noundef, i32 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 willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @bdestroy(ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_start.p0(ptr) #3

; Function Attrs: nounwind
declare i32 @vsnprintf(ptr noundef, i64 noundef, ptr noundef, ptr noundef) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_end.p0(ptr) #3

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

11:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %89

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = call i64 @strlen(ptr noundef %13) #6
  %15 = mul i64 2, %14
  %16 = trunc i64 %15 to i32
  store i32 %16, ptr %6, align 4, !tbaa !9
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = icmp slt i32 %17, 16
  br i1 %18, label %19, label %20

19:                                               ; preds = %12
  store i32 16, ptr %6, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %19, %12
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = add nsw i32 %21, 2
  %23 = call ptr @bfromcstralloc(i32 noundef %22, ptr noundef @.str)
  store ptr %23, ptr %5, align 8, !tbaa !5
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %34, label %26

26:                                               ; preds = %20
  store i32 1, ptr %6, align 4, !tbaa !9
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = add nsw i32 %27, 2
  %29 = call ptr @bfromcstralloc(i32 noundef %28, ptr noundef @.str)
  store ptr %29, ptr %5, align 8, !tbaa !5
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = icmp ne ptr %30, null
  br i1 %31, label %33, label %32

32:                                               ; preds = %26
  store ptr null, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %89

33:                                               ; preds = %26
  br label %34

34:                                               ; preds = %33, %20
  br label %35

35:                                               ; preds = %86, %34
  br label %36

36:                                               ; preds = %35
  %37 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %4, i64 0, i64 0
  call void @llvm.va_start.p0(ptr %37)
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.tagbstring, ptr %38, i32 0, i32 2
  %40 = load ptr, ptr %39, align 8, !tbaa !11
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  %43 = sext i32 %42 to i64
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %4, i64 0, i64 0
  %46 = call i32 @vsnprintf(ptr noundef %40, i64 noundef %43, ptr noundef %44, ptr noundef %45) #5
  store i32 %46, ptr %7, align 4, !tbaa !9
  %47 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %4, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %47)
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.tagbstring, ptr %48, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !11
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds i8, ptr %50, i64 %52
  store i8 0, ptr %53, align 1, !tbaa !13
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.tagbstring, ptr %54, i32 0, i32 2
  %56 = load ptr, ptr %55, align 8, !tbaa !11
  %57 = call i64 @strlen(ptr noundef %56) #6
  %58 = trunc i64 %57 to i32
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.tagbstring, ptr %59, i32 0, i32 1
  store i32 %58, ptr %60, align 4, !tbaa !14
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.tagbstring, ptr %61, i32 0, i32 1
  %63 = load i32, ptr %62, align 4, !tbaa !14
  %64 = load i32, ptr %6, align 4, !tbaa !9
  %65 = icmp slt i32 %63, %64
  br i1 %65, label %66, label %67

66:                                               ; preds = %36
  br label %87

67:                                               ; preds = %36
  %68 = load i32, ptr %7, align 4, !tbaa !9
  %69 = load i32, ptr %6, align 4, !tbaa !9
  %70 = icmp sgt i32 %68, %69
  br i1 %70, label %71, label %73

71:                                               ; preds = %67
  %72 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %72, ptr %6, align 4, !tbaa !9
  br label %77

73:                                               ; preds = %67
  %74 = load i32, ptr %6, align 4, !tbaa !9
  %75 = load i32, ptr %6, align 4, !tbaa !9
  %76 = add nsw i32 %75, %74
  store i32 %76, ptr %6, align 4, !tbaa !9
  br label %77

77:                                               ; preds = %73, %71
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  %79 = load i32, ptr %6, align 4, !tbaa !9
  %80 = add nsw i32 %79, 2
  %81 = call i32 @balloc(ptr noundef %78, i32 noundef %80)
  %82 = icmp ne i32 0, %81
  br i1 %82, label %83, label %86

83:                                               ; preds = %77
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = call i32 @bdestroy(ptr noundef %84)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %89

86:                                               ; preds = %77
  br label %35

87:                                               ; preds = %66
  %88 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %88, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %89

89:                                               ; preds = %87, %83, %32, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 24, ptr %4) #5
  %90 = load ptr, ptr %2, align 8
  ret ptr %90
}

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 willreturn memory(read) "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 nosync nounwind willreturn }
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 }
attributes #6 = { nounwind willreturn memory(read) }

!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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"tagbstring", !10, i64 0, !10, i64 4, !6, i64 8}
!13 = !{!7, !7, i64 0}
!14 = !{!12, !10, i64 4}


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

%struct.__va_list_tag = type { i32, i32, ptr, ptr }

@.str = external hidden unnamed_addr constant [1 x i8], align 1

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

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

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @bdestroy(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_start.p0(ptr) #3

; Function Attrs: nofree nounwind
declare noundef i32 @vsnprintf(ptr nocapture noundef, i64 noundef, ptr nocapture noundef readonly, ptr noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_end.p0(ptr) #3

; Function Attrs: nounwind uwtable
define dso_local ptr @bformat(ptr noundef readonly %0, ...) local_unnamed_addr #0 {
  %2 = alloca [1 x %struct.__va_list_tag], align 16
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %2) #6
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit, label %3

3:                                                ; preds = %1
  %4 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #7
  %.tr = trunc i64 %4 to i32
  %5 = shl i32 %.tr, 1
  %spec.store.select = tail call i32 @llvm.smax.i32(i32 %5, i32 16)
  %6 = add nuw nsw i32 %spec.store.select, 2
  %7 = tail call ptr @bfromcstralloc(i32 noundef %6, ptr noundef nonnull @.str)
  %.not31 = icmp eq ptr %7, null
  br i1 %.not31, label %8, label %10

8:                                                ; preds = %3
  %9 = tail call ptr @bfromcstralloc(i32 noundef 3, ptr noundef nonnull @.str)
  %.not32 = icmp eq ptr %9, null
  br i1 %.not32, label %.loopexit, label %10

10:                                               ; preds = %8, %3
  %.026 = phi ptr [ %7, %3 ], [ %9, %8 ]
  %.025 = phi i32 [ %spec.store.select, %3 ], [ 1, %8 ]
  %11 = getelementptr inbounds i8, ptr %.026, i64 8
  %12 = getelementptr inbounds i8, ptr %.026, i64 4
  br label %13

13:                                               ; preds = %25, %10
  %.1 = phi i32 [ %.025, %10 ], [ %.2, %25 ]
  call void @llvm.va_start.p0(ptr nonnull %2)
  %14 = load ptr, ptr %11, align 8, !tbaa !5
  %15 = add nsw i32 %.1, 1
  %16 = sext i32 %15 to i64
  %17 = call i32 @vsnprintf(ptr noundef %14, i64 noundef %16, ptr noundef nonnull %0, ptr noundef nonnull %2) #6
  call void @llvm.va_end.p0(ptr nonnull %2)
  %18 = load ptr, ptr %11, align 8, !tbaa !5
  %19 = sext i32 %.1 to i64
  %20 = getelementptr inbounds i8, ptr %18, i64 %19
  store i8 0, ptr %20, align 1, !tbaa !11
  %21 = load ptr, ptr %11, align 8, !tbaa !5
  %22 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %21) #7
  %23 = trunc i64 %22 to i32
  store i32 %23, ptr %12, align 4, !tbaa !12
  %24 = icmp sgt i32 %.1, %23
  br i1 %24, label %.loopexit, label %25

25:                                               ; preds = %13
  %26 = icmp sgt i32 %17, %.1
  %27 = shl nsw i32 %.1, 1
  %.2 = select i1 %26, i32 %17, i32 %27
  %28 = add nsw i32 %.2, 2
  %29 = call i32 @balloc(ptr noundef nonnull %.026, i32 noundef %28)
  %.not33 = icmp eq i32 %29, 0
  br i1 %.not33, label %13, label %30

30:                                               ; preds = %25
  %31 = call i32 @bdestroy(ptr noundef nonnull %.026)
  br label %.loopexit

.loopexit:                                        ; preds = %13, %8, %1, %30
  %.0 = phi ptr [ null, %30 ], [ null, %1 ], [ null, %8 ], [ %.026, %13 ]
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %2) #6
  ret ptr %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 nosync nounwind willreturn }
attributes #4 = { nofree 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(read) }

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass