; LLVM IR from nickmqb/muon :: bootstrap/mu.ll_File (tryReadToStringBuilder__)

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

@mu_____string7 = external dso_local global %struct.string__, align 8
@top_____abandonFn__ = external dso_local thread_local global ptr, align 8

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #1

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @string__alloc_cstring__(ptr, i32) #2

; Function Attrs: nounwind uwtable
declare dso_local void @StringBuilder__reserveForWrite__(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind
declare i32 @feof(ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @File__tryReadToStringBuilder__(ptr %0, i32 %1, ptr noundef %2) #2 {
  %4 = alloca i8, align 1
  %5 = alloca %struct.string__, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = getelementptr inbounds { ptr, i32 }, ptr %5, i32 0, i32 0
  store ptr %0, ptr %12, align 8
  %13 = getelementptr inbounds { ptr, i32 }, ptr %5, i32 0, i32 1
  store i32 %1, ptr %13, align 8
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %14 = getelementptr inbounds { ptr, i32 }, ptr %5, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8
  %16 = getelementptr inbounds { ptr, i32 }, ptr %5, i32 0, i32 1
  %17 = load i32, ptr %16, align 8
  %18 = call ptr @string__alloc_cstring__(ptr %15, i32 %17)
  %19 = load ptr, ptr @mu_____string7, align 8, !tbaa !9
  %20 = call noalias ptr @fopen(ptr noundef %18, ptr noundef %19)
  store ptr %20, ptr %7, align 8, !tbaa !5
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = icmp eq ptr %21, null
  br i1 %22, label %23, label %24

23:                                               ; preds = %3
  store i8 0, ptr %4, align 1
  store i32 1, ptr %8, align 4
  br label %84

24:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 4096, ptr %9, align 4, !tbaa !12
  br label %25

25:                                               ; preds = %71, %24
  br label %26

26:                                               ; preds = %25
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = load i32, ptr %9, align 4, !tbaa !12
  call void @StringBuilder__reserveForWrite__(ptr noundef %27, i32 noundef %28)
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.tag_____StringBuilder__, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !13
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.tag_____StringBuilder__, ptr %32, i32 0, i32 1
  %34 = load i32, ptr %33, align 8, !tbaa !15
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i8, ptr %31, i64 %35
  %37 = load i32, ptr %9, align 4, !tbaa !12
  %38 = zext i32 %37 to i64
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = call i64 @fread(ptr noundef %36, i64 noundef 1, i64 noundef %38, ptr noundef %39)
  store i64 %40, ptr %10, align 8, !tbaa !16
  %41 = load i64, ptr %10, align 8, !tbaa !16
  %42 = icmp ugt i64 %41, 0
  br i1 %42, label %43, label %67

43:                                               ; preds = %26
  %44 = load i64, ptr %10, align 8, !tbaa !16
  %45 = load i32, ptr %9, align 4, !tbaa !12
  %46 = zext i32 %45 to i64
  %47 = icmp ule i64 %44, %46
  br i1 %47, label %51, label %48

48:                                               ; preds = %43
  %49 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %50 = load ptr, ptr %49, align 8, !tbaa !5
  call void %50(i32 noundef -2)
  br label %51

51:                                               ; preds = %48, %43
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %52 = load i64, ptr %10, align 8, !tbaa !16
  store i64 %52, ptr %11, align 8, !tbaa !16
  %53 = load i64, ptr %11, align 8, !tbaa !16
  %54 = icmp ule i64 %53, 2147483647
  br i1 %54, label %58, label %55

55:                                               ; preds = %51
  %56 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %57 = load ptr, ptr %56, align 8, !tbaa !5
  call void %57(i32 noundef -4)
  br label %58

58:                                               ; preds = %55, %51
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.tag_____StringBuilder__, ptr %59, i32 0, i32 1
  %61 = load i32, ptr %60, align 8, !tbaa !15
  %62 = load i64, ptr %11, align 8, !tbaa !16
  %63 = trunc i64 %62 to i32
  %64 = add nsw i32 %61, %63
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.tag_____StringBuilder__, ptr %65, i32 0, i32 1
  store i32 %64, ptr %66, align 8, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  br label %68

67:                                               ; preds = %26
  store i32 4, ptr %8, align 4
  br label %69

68:                                               ; preds = %58
  store i32 0, ptr %8, align 4
  br label %69

69:                                               ; preds = %68, %67
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  %70 = load i32, ptr %8, align 4
  switch i32 %70, label %83 [
    i32 0, label %71
    i32 4, label %72
  ]

71:                                               ; preds = %69
  br label %25

72:                                               ; preds = %69
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  %74 = call i32 @feof(ptr noundef %73) #5
  %75 = icmp eq i32 %74, 0
  br i1 %75, label %76, label %77

76:                                               ; preds = %72
  store i8 0, ptr %4, align 1
  store i32 1, ptr %8, align 4
  br label %83

77:                                               ; preds = %72
  %78 = load ptr, ptr %7, align 8, !tbaa !5
  %79 = call i32 @fclose(ptr noundef %78)
  %80 = icmp ne i32 %79, 0
  br i1 %80, label %81, label %82

81:                                               ; preds = %77
  store i8 0, ptr %4, align 1
  store i32 1, ptr %8, align 4
  br label %83

82:                                               ; preds = %77
  store i8 1, ptr %4, align 1
  store i32 1, ptr %8, align 4
  br label %83

83:                                               ; preds = %82, %81, %76, %69
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %84

84:                                               ; preds = %83, %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  %85 = load i8, ptr %4, align 1
  ret i8 %85
}

declare noalias ptr @fopen(ptr noundef, ptr noundef) #4

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #4

declare i32 @fclose(ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { 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 = { "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 }

!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, !6, i64 0}
!10 = !{!"", !6, i64 0, !11, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"tag_____StringBuilder__", !6, i64 0, !11, i64 8, !11, i64 12}
!15 = !{!14, !11, i64 8}
!16 = !{!17, !17, i64 0}
!17 = !{!"long", !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"

%struct.string__ = type { ptr, i32 }

@mu_____string7 = external dso_local local_unnamed_addr global %struct.string__, align 8
@top_____abandonFn__ = external dso_local thread_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @string__alloc_cstring__(ptr, i32) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @StringBuilder__reserveForWrite__(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @feof(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i8 @File__tryReadToStringBuilder__(ptr %0, i32 %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = tail call ptr @string__alloc_cstring__(ptr %0, i32 %1)
  %5 = load ptr, ptr @mu_____string7, align 8, !tbaa !5
  %6 = tail call noalias ptr @fopen(ptr noundef %4, ptr noundef %5)
  %7 = icmp eq ptr %6, null
  br i1 %7, label %35, label %.preheader

.preheader:                                       ; preds = %3
  tail call void @StringBuilder__reserveForWrite__(ptr noundef %2, i32 noundef 4096)
  %8 = load ptr, ptr %2, align 8, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %2, i64 8
  %10 = load i32, ptr %9, align 8, !tbaa !13
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds i8, ptr %8, i64 %11
  %13 = tail call i64 @fread(ptr noundef %12, i64 noundef 1, i64 noundef 4096, ptr noundef nonnull %6)
  %.not26 = icmp eq i64 %13, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %14 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  br label %15

15:                                               ; preds = %.lr.ph, %.thread
  %16 = phi i64 [ %13, %.lr.ph ], [ %30, %.thread ]
  %17 = icmp ult i64 %16, 4097
  br i1 %17, label %.thread, label %18

18:                                               ; preds = %15
  %19 = load ptr, ptr %14, align 8, !tbaa !14
  tail call void %19(i32 noundef -2) #3
  %20 = icmp ult i64 %16, 2147483648
  br i1 %20, label %.thread, label %21

21:                                               ; preds = %18
  %22 = load ptr, ptr %14, align 8, !tbaa !14
  tail call void %22(i32 noundef -4) #3
  br label %.thread

.thread:                                          ; preds = %15, %18, %21
  %23 = load i32, ptr %9, align 8, !tbaa !13
  %24 = trunc i64 %16 to i32
  %25 = add nsw i32 %23, %24
  store i32 %25, ptr %9, align 8, !tbaa !13
  tail call void @StringBuilder__reserveForWrite__(ptr noundef nonnull %2, i32 noundef 4096)
  %26 = load ptr, ptr %2, align 8, !tbaa !11
  %27 = load i32, ptr %9, align 8, !tbaa !13
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i8, ptr %26, i64 %28
  %30 = tail call i64 @fread(ptr noundef %29, i64 noundef 1, i64 noundef 4096, ptr noundef nonnull %6)
  %.not = icmp eq i64 %30, 0
  br i1 %.not, label %._crit_edge, label %15

._crit_edge:                                      ; preds = %.thread, %.preheader
  %31 = tail call i32 @feof(ptr noundef nonnull %6) #3
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %35, label %33

33:                                               ; preds = %._crit_edge
  %34 = tail call i32 @fclose(ptr noundef nonnull %6)
  %.not21 = icmp eq i32 %34, 0
  %. = zext i1 %.not21 to i8
  br label %35

35:                                               ; preds = %._crit_edge, %33, %3
  %.1 = phi i8 [ 0, %3 ], [ 0, %._crit_edge ], [ %., %33 ]
  ret i8 %.1
}

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i64 @fread(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { 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 = { 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 #3 = { 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"tag_____StringBuilder__", !7, i64 0, !10, i64 8, !10, i64 12}
!13 = !{!12, !10, i64 8}
!14 = !{!7, !7, i64 0}


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