; LLVM IR from NOAA-PMEL/Ferret :: fer/ccr (c_substr)

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

@STRING_MISSING_VALUE = external global ptr, align 8
@.str = external hidden unnamed_addr constant [69 x i8], align 1

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

18:                                               ; preds = %4
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = load ptr, ptr %19, align 8, !tbaa !5
  %21 = load ptr, ptr @STRING_MISSING_VALUE, align 8, !tbaa !5
  %22 = icmp ne ptr %20, %21
  br i1 %22, label %23, label %26

23:                                               ; preds = %18
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = load ptr, ptr %24, align 8, !tbaa !5
  call void @FerMem_Free(ptr noundef %25, ptr noundef @.str, i32 noundef 60)
  br label %26

26:                                               ; preds = %23, %18, %4
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  %29 = icmp eq ptr %28, null
  br i1 %29, label %30, label %32

30:                                               ; preds = %26
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr null, ptr %31, align 8, !tbaa !5
  store i32 1, ptr %14, align 4
  br label %109

32:                                               ; preds = %26
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = load ptr, ptr %33, align 8, !tbaa !5
  %35 = load ptr, ptr @STRING_MISSING_VALUE, align 8, !tbaa !5
  %36 = icmp eq ptr %34, %35
  br i1 %36, label %37, label %40

37:                                               ; preds = %32
  %38 = load ptr, ptr @STRING_MISSING_VALUE, align 8, !tbaa !5
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %38, ptr %39, align 8, !tbaa !5
  store i32 1, ptr %14, align 4
  br label %109

40:                                               ; preds = %32
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = load i32, ptr %41, align 4, !tbaa !9
  %43 = sitofp i32 %42 to double
  %44 = fadd double %43, 5.000000e-01
  %45 = fptosi double %44 to i32
  store i32 %45, ptr %12, align 4, !tbaa !9
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load i32, ptr %46, align 4, !tbaa !9
  %48 = sitofp i32 %47 to double
  %49 = fadd double %48, 5.000000e-01
  %50 = fptosi double %49 to i32
  %51 = sub nsw i32 %50, 1
  store i32 %51, ptr %13, align 4, !tbaa !9
  %52 = load i32, ptr %12, align 4, !tbaa !9
  %53 = add nsw i32 %52, 1
  %54 = sext i32 %53 to i64
  %55 = mul i64 1, %54
  %56 = call ptr @FerMem_Malloc(i64 noundef %55, ptr noundef @.str, i32 noundef 75)
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %56, ptr %57, align 8, !tbaa !5
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = load ptr, ptr %58, align 8, !tbaa !5
  %60 = icmp eq ptr %59, null
  br i1 %60, label %61, label %62

61:                                               ; preds = %40
  call void @abort() #5
  unreachable

62:                                               ; preds = %40
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = load ptr, ptr %63, align 8, !tbaa !5
  store ptr %64, ptr %10, align 8, !tbaa !5
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %65

65:                                               ; preds = %79, %62
  %66 = load i32, ptr %11, align 4, !tbaa !9
  %67 = load i32, ptr %13, align 4, !tbaa !9
  %68 = icmp slt i32 %66, %67
  br i1 %68, label %69, label %74

69:                                               ; preds = %65
  %70 = load ptr, ptr %10, align 8, !tbaa !5
  %71 = load i8, ptr %70, align 1, !tbaa !11
  %72 = sext i8 %71 to i32
  %73 = icmp ne i32 %72, 0
  br label %74

74:                                               ; preds = %69, %65
  %75 = phi i1 [ false, %65 ], [ %73, %69 ]
  br i1 %75, label %76, label %82

76:                                               ; preds = %74
  %77 = load ptr, ptr %10, align 8, !tbaa !5
  %78 = getelementptr inbounds i8, ptr %77, i32 1
  store ptr %78, ptr %10, align 8, !tbaa !5
  br label %79

79:                                               ; preds = %76
  %80 = load i32, ptr %11, align 4, !tbaa !9
  %81 = add nsw i32 %80, 1
  store i32 %81, ptr %11, align 4, !tbaa !9
  br label %65, !llvm.loop !12

82:                                               ; preds = %74
  %83 = load ptr, ptr %8, align 8, !tbaa !5
  %84 = load ptr, ptr %83, align 8, !tbaa !5
  store ptr %84, ptr %9, align 8, !tbaa !5
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %85

85:                                               ; preds = %104, %82
  %86 = load i32, ptr %11, align 4, !tbaa !9
  %87 = load i32, ptr %12, align 4, !tbaa !9
  %88 = icmp slt i32 %86, %87
  br i1 %88, label %89, label %94

89:                                               ; preds = %85
  %90 = load ptr, ptr %10, align 8, !tbaa !5
  %91 = load i8, ptr %90, align 1, !tbaa !11
  %92 = sext i8 %91 to i32
  %93 = icmp ne i32 %92, 0
  br label %94

94:                                               ; preds = %89, %85
  %95 = phi i1 [ false, %85 ], [ %93, %89 ]
  br i1 %95, label %96, label %107

96:                                               ; preds = %94
  %97 = load ptr, ptr %10, align 8, !tbaa !5
  %98 = load i8, ptr %97, align 1, !tbaa !11
  %99 = load ptr, ptr %9, align 8, !tbaa !5
  store i8 %98, ptr %99, align 1, !tbaa !11
  %100 = load ptr, ptr %9, align 8, !tbaa !5
  %101 = getelementptr inbounds i8, ptr %100, i32 1
  store ptr %101, ptr %9, align 8, !tbaa !5
  %102 = load ptr, ptr %10, align 8, !tbaa !5
  %103 = getelementptr inbounds i8, ptr %102, i32 1
  store ptr %103, ptr %10, align 8, !tbaa !5
  br label %104

104:                                              ; preds = %96
  %105 = load i32, ptr %11, align 4, !tbaa !9
  %106 = add nsw i32 %105, 1
  store i32 %106, ptr %11, align 4, !tbaa !9
  br label %85, !llvm.loop !14

107:                                              ; preds = %94
  %108 = load ptr, ptr %9, align 8, !tbaa !5
  store i8 0, ptr %108, align 1, !tbaa !11
  store i32 0, ptr %14, align 4
  br label %109

109:                                              ; preds = %107, %37, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  %110 = load i32, ptr %14, align 4
  switch i32 %110, label %112 [
    i32 0, label %111
    i32 1, label %111
  ]

111:                                              ; preds = %109, %109
  ret void

112:                                              ; preds = %109
  unreachable
}

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

declare void @FerMem_Free(ptr noundef, ptr noundef, i32 noundef) #2

declare ptr @FerMem_Malloc(i64 noundef, ptr noundef, i32 noundef) #2

; Function Attrs: noreturn nounwind
declare void @abort() #3

; 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 = { "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 = { noreturn 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 #4 = { nounwind }
attributes #5 = { noreturn 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 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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

@STRING_MISSING_VALUE = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [69 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @c_substr_(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %2, ptr nocapture noundef %3) local_unnamed_addr #0 {
  %5 = load ptr, ptr %3, align 8, !tbaa !5
  %.not = icmp eq ptr %5, null
  %6 = load ptr, ptr @STRING_MISSING_VALUE, align 8
  %.not35 = icmp eq ptr %5, %6
  %or.cond = select i1 %.not, i1 true, i1 %.not35
  br i1 %or.cond, label %8, label %7

7:                                                ; preds = %4
  tail call void @FerMem_Free(ptr noundef nonnull %5, ptr noundef nonnull @.str, i32 noundef 60) #3
  br label %8

8:                                                ; preds = %7, %4
  %9 = load ptr, ptr %0, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %8
  store ptr null, ptr %3, align 8, !tbaa !5
  br label %46

12:                                               ; preds = %8
  %13 = load ptr, ptr @STRING_MISSING_VALUE, align 8, !tbaa !5
  %14 = icmp eq ptr %9, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %12
  store ptr %13, ptr %3, align 8, !tbaa !5
  br label %46

16:                                               ; preds = %12
  %17 = load i32, ptr %2, align 4, !tbaa !9
  %18 = sitofp i32 %17 to double
  %19 = fadd double %18, 5.000000e-01
  %20 = fptosi double %19 to i32
  %21 = load i32, ptr %1, align 4, !tbaa !9
  %22 = sitofp i32 %21 to double
  %23 = fadd double %22, 5.000000e-01
  %24 = fptosi double %23 to i32
  %25 = add nsw i32 %20, 1
  %26 = sext i32 %25 to i64
  %27 = tail call ptr @FerMem_Malloc(i64 noundef %26, ptr noundef nonnull @.str, i32 noundef 75) #3
  store ptr %27, ptr %3, align 8, !tbaa !5
  %28 = icmp eq ptr %27, null
  br i1 %28, label %29, label %30

29:                                               ; preds = %16
  tail call void @abort() #4
  unreachable

30:                                               ; preds = %16
  %31 = load ptr, ptr %0, align 8, !tbaa !5
  %32 = icmp sgt i32 %24, 1
  br i1 %32, label %.lr.ph.preheader, label %.critedge

.lr.ph.preheader:                                 ; preds = %30
  %33 = zext nneg i32 %24 to i64
  %34 = getelementptr i8, ptr %31, i64 %33
  %scevgep = getelementptr i8, ptr %34, i64 -1
  %35 = add nsw i32 %24, -2
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %37
  %.02939 = phi ptr [ %38, %37 ], [ %31, %.lr.ph.preheader ]
  %.03038 = phi i32 [ %39, %37 ], [ 0, %.lr.ph.preheader ]
  %36 = load i8, ptr %.02939, align 1, !tbaa !11
  %.not36 = icmp eq i8 %36, 0
  br i1 %.not36, label %.critedge, label %37

37:                                               ; preds = %.lr.ph
  %38 = getelementptr inbounds i8, ptr %.02939, i64 1
  %39 = add nuw nsw i32 %.03038, 1
  %exitcond.not = icmp eq i32 %.03038, %35
  br i1 %exitcond.not, label %.critedge, label %.lr.ph, !llvm.loop !12

.critedge:                                        ; preds = %.lr.ph, %37, %30
  %.029.lcssa = phi ptr [ %31, %30 ], [ %scevgep, %37 ], [ %.02939, %.lr.ph ]
  %40 = icmp sgt i32 %20, 0
  br i1 %40, label %.lr.ph45, label %.critedge2

.lr.ph45:                                         ; preds = %.critedge, %42
  %.044 = phi ptr [ %43, %42 ], [ %27, %.critedge ]
  %.143 = phi ptr [ %44, %42 ], [ %.029.lcssa, %.critedge ]
  %.13142 = phi i32 [ %45, %42 ], [ 0, %.critedge ]
  %41 = load i8, ptr %.143, align 1, !tbaa !11
  %.not37 = icmp eq i8 %41, 0
  br i1 %.not37, label %.critedge2, label %42

42:                                               ; preds = %.lr.ph45
  store i8 %41, ptr %.044, align 1, !tbaa !11
  %43 = getelementptr inbounds i8, ptr %.044, i64 1
  %44 = getelementptr inbounds i8, ptr %.143, i64 1
  %45 = add nuw nsw i32 %.13142, 1
  %exitcond49.not = icmp eq i32 %45, %20
  br i1 %exitcond49.not, label %.critedge2, label %.lr.ph45, !llvm.loop !14

.critedge2:                                       ; preds = %.lr.ph45, %42, %.critedge
  %.0.lcssa = phi ptr [ %27, %.critedge ], [ %43, %42 ], [ %.044, %.lr.ph45 ]
  store i8 0, ptr %.0.lcssa, align 1, !tbaa !11
  br label %46

46:                                               ; preds = %.critedge2, %15, %11
  ret void
}

declare void @FerMem_Free(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

declare ptr @FerMem_Malloc(i64 noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: noreturn nounwind
declare void @abort() local_unnamed_addr #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 = { "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 = { noreturn 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 #3 = { nounwind }
attributes #4 = { noreturn 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 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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