; LLVM IR from mirohs/gc :: util (trim_right)

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

@stderr = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [50 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local { ptr, i64 } @trim_right(ptr %0, i64 %1) #3 {
  %3 = alloca %struct.String, align 8
  %4 = alloca %struct.String, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = getelementptr inbounds { ptr, i64 }, ptr %4, i32 0, i32 0
  store ptr %0, ptr %7, align 8
  %8 = getelementptr inbounds { ptr, i64 }, ptr %4, i32 0, i32 1
  store i64 %1, ptr %8, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %9 = getelementptr inbounds %struct.String, ptr %4, i32 0, i32 1
  %10 = load i32, ptr %9, align 8, !tbaa !5
  %11 = sub nsw i32 %10, 1
  store i32 %11, ptr %5, align 4, !tbaa !11
  br label %12

12:                                               ; preds = %38, %2
  %13 = load i32, ptr %5, align 4, !tbaa !11
  %14 = icmp sge i32 %13, 0
  br i1 %14, label %15, label %35

15:                                               ; preds = %12
  %16 = getelementptr inbounds %struct.String, ptr %4, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !12
  %18 = load i32, ptr %5, align 4, !tbaa !11
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i8, ptr %17, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !13
  %22 = sext i8 %21 to i32
  %23 = icmp eq i32 %22, 32
  br i1 %23, label %33, label %24

24:                                               ; preds = %15
  %25 = getelementptr inbounds %struct.String, ptr %4, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !12
  %27 = load i32, ptr %5, align 4, !tbaa !11
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i8, ptr %26, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !13
  %31 = sext i8 %30 to i32
  %32 = icmp eq i32 %31, 9
  br label %33

33:                                               ; preds = %24, %15
  %34 = phi i1 [ true, %15 ], [ %32, %24 ]
  br label %35

35:                                               ; preds = %33, %12
  %36 = phi i1 [ false, %12 ], [ %34, %33 ]
  br i1 %36, label %37, label %41

37:                                               ; preds = %35
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %5, align 4, !tbaa !11
  %40 = add nsw i32 %39, -1
  store i32 %40, ptr %5, align 4, !tbaa !11
  br label %12, !llvm.loop !14

41:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %42 = load i32, ptr %5, align 4, !tbaa !11
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %6, align 4, !tbaa !11
  %44 = load i32, ptr %6, align 4, !tbaa !11
  %45 = icmp sge i32 %44, 0
  br i1 %45, label %49, label %46

46:                                               ; preds = %41
  %47 = load ptr, ptr @stderr, align 8, !tbaa !16
  %48 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %47, ptr noundef @.str.28, ptr noundef @.str.1, i32 noundef 298, ptr noundef @.str.29, ptr noundef @.str.4) #4
  call void @exit(i32 noundef 1) #5
  unreachable

49:                                               ; preds = %41
  %50 = load i32, ptr %6, align 4, !tbaa !11
  %51 = getelementptr inbounds %struct.String, ptr %4, i32 0, i32 1
  %52 = load i32, ptr %51, align 8, !tbaa !5
  %53 = icmp sle i32 %50, %52
  br i1 %53, label %57, label %54

54:                                               ; preds = %49
  %55 = load ptr, ptr @stderr, align 8, !tbaa !16
  %56 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %55, ptr noundef @.str.28, ptr noundef @.str.1, i32 noundef 299, ptr noundef @.str.30, ptr noundef @.str.31) #4
  call void @exit(i32 noundef 1) #5
  unreachable

57:                                               ; preds = %49
  %58 = getelementptr inbounds %struct.String, ptr %3, i32 0, i32 0
  %59 = getelementptr inbounds %struct.String, ptr %4, i32 0, i32 0
  %60 = load ptr, ptr %59, align 8, !tbaa !12
  store ptr %60, ptr %58, align 8, !tbaa !12
  %61 = getelementptr inbounds %struct.String, ptr %3, i32 0, i32 1
  %62 = load i32, ptr %6, align 4, !tbaa !11
  store i32 %62, ptr %61, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.String, ptr %3, i32 0, i32 2
  %64 = getelementptr inbounds %struct.String, ptr %4, i32 0, i32 2
  %65 = load i32, ptr %64, align 4, !tbaa !17
  store i32 %65, ptr %63, align 4, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  %66 = load { ptr, i64 }, ptr %3, align 8
  ret { ptr, i64 } %66
}

attributes #0 = { 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 #1 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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, !10, i64 8}
!6 = !{!"String", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!8, !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!7, !7, i64 0}
!17 = !{!6, !10, i64 12}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [50 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local { ptr, i64 } @trim_right(ptr %0, i64 %1) local_unnamed_addr #2 {
  %.sroa.4.8.extract.trunc = trunc i64 %1 to i32
  %3 = icmp sgt i32 %.sroa.4.8.extract.trunc, 0
  br i1 %3, label %.lr.ph.preheader, label %.critedge

.lr.ph.preheader:                                 ; preds = %2
  %4 = and i64 %1, 2147483647
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.critedge2
  %indvars.iv = phi i64 [ %4, %.lr.ph.preheader ], [ %indvars.iv.next, %.critedge2 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %5 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next
  %6 = load i8, ptr %5, align 1, !tbaa !5
  switch i8 %6, label %.critedge.thread.loopexit [
    i8 32, label %.critedge2
    i8 9, label %.critedge2
  ]

.critedge2:                                       ; preds = %.lr.ph, %.lr.ph
  %7 = icmp ugt i64 %indvars.iv, 1
  br i1 %7, label %.lr.ph, label %.critedge.loopexit, !llvm.loop !8

.critedge.loopexit:                               ; preds = %.critedge2
  %8 = trunc nuw nsw i64 %indvars.iv.next to i32
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit, %2
  %.0.in.lcssa = phi i32 [ %.sroa.4.8.extract.trunc, %2 ], [ %8, %.critedge.loopexit ]
  %9 = icmp sgt i32 %.0.in.lcssa, -1
  br i1 %9, label %.critedge.thread, label %10

10:                                               ; preds = %.critedge
  %11 = load ptr, ptr @stderr, align 8, !tbaa !10
  %12 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %11, ptr noundef nonnull @.str.28, ptr noundef nonnull @.str.1, i32 noundef 298, ptr noundef nonnull @.str.29, ptr noundef nonnull @.str.4) #3
  tail call void @exit(i32 noundef 1) #4
  unreachable

.critedge.thread.loopexit:                        ; preds = %.lr.ph
  %13 = trunc nuw nsw i64 %indvars.iv to i32
  br label %.critedge.thread

.critedge.thread:                                 ; preds = %.critedge.thread.loopexit, %.critedge
  %.0.in18 = phi i32 [ %.0.in.lcssa, %.critedge ], [ %13, %.critedge.thread.loopexit ]
  %.not = icmp sgt i32 %.0.in18, %.sroa.4.8.extract.trunc
  br i1 %.not, label %14, label %17

14:                                               ; preds = %.critedge.thread
  %15 = load ptr, ptr @stderr, align 8, !tbaa !10
  %16 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %15, ptr noundef nonnull @.str.28, ptr noundef nonnull @.str.1, i32 noundef 299, ptr noundef nonnull @.str.30, ptr noundef nonnull @.str.31) #3
  tail call void @exit(i32 noundef 1) #4
  unreachable

17:                                               ; preds = %.critedge.thread
  %.sroa.7.8.extract.shift = and i64 %1, -4294967296
  %.fca.0.insert = insertvalue { ptr, i64 } poison, ptr %0, 0
  %.sroa.2.8.insert.ext = zext nneg i32 %.0.in18 to i64
  %.sroa.2.8.insert.insert = or disjoint i64 %.sroa.7.8.extract.shift, %.sroa.2.8.insert.ext
  %.fca.1.insert = insertvalue { ptr, i64 } %.fca.0.insert, i64 %.sroa.2.8.insert.insert, 1
  ret { ptr, i64 } %.fca.1.insert
}

attributes #0 = { 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 #1 = { 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 #2 = { 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 = { cold 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !6, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass