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

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

@mu_____string8 = 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

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

declare i32 @fclose(ptr noundef) #3

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

30:                                               ; preds = %4
  store i8 0, ptr %5, align 1
  store i32 1, ptr %9, align 4
  br label %90

31:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 4096, ptr %10, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %32 = getelementptr inbounds %struct.string__, ptr %7, i32 0, i32 1
  %33 = load i32, ptr %32, align 8, !tbaa !13
  store i32 %33, ptr %11, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !12
  br label %34

34:                                               ; preds = %82, %31
  br label %35

35:                                               ; preds = %34
  %36 = load i32, ptr %12, align 4, !tbaa !12
  %37 = load i32, ptr %11, align 4, !tbaa !12
  %38 = icmp ult i32 %36, %37
  br i1 %38, label %40, label %39

39:                                               ; preds = %35
  br label %83

40:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %41 = load i32, ptr %10, align 4, !tbaa !12
  store i32 %41, ptr %13, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %42 = load i32, ptr %11, align 4, !tbaa !12
  %43 = load i32, ptr %12, align 4, !tbaa !12
  %44 = sub i32 %42, %43
  store i32 %44, ptr %14, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  %45 = load i32, ptr %13, align 4, !tbaa !12
  %46 = load i32, ptr %14, align 4, !tbaa !12
  %47 = icmp ult i32 %45, %46
  br i1 %47, label %48, label %50

48:                                               ; preds = %40
  %49 = load i32, ptr %13, align 4, !tbaa !12
  br label %52

50:                                               ; preds = %40
  %51 = load i32, ptr %14, align 4, !tbaa !12
  br label %52

52:                                               ; preds = %50, %48
  %53 = phi i32 [ %49, %48 ], [ %51, %50 ]
  store i32 %53, ptr %15, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %54 = getelementptr inbounds %struct.string__, ptr %7, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !5
  %56 = load i32, ptr %12, align 4, !tbaa !12
  %57 = zext i32 %56 to i64
  %58 = getelementptr inbounds i8, ptr %55, i64 %57
  %59 = load i32, ptr %15, align 4, !tbaa !12
  %60 = zext i32 %59 to i64
  %61 = load ptr, ptr %8, align 8, !tbaa !11
  %62 = call i64 @fwrite(ptr noundef %58, i64 noundef 1, i64 noundef %60, ptr noundef %61)
  store i64 %62, ptr %16, align 8, !tbaa !14
  %63 = load i64, ptr %16, align 8, !tbaa !14
  %64 = icmp ugt i64 %63, 0
  br i1 %64, label %65, label %78

65:                                               ; preds = %52
  %66 = load i64, ptr %16, align 8, !tbaa !14
  %67 = load i32, ptr %15, align 4, !tbaa !12
  %68 = zext i32 %67 to i64
  %69 = icmp ule i64 %66, %68
  br i1 %69, label %73, label %70

70:                                               ; preds = %65
  %71 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %72 = load ptr, ptr %71, align 8, !tbaa !11
  call void %72(i32 noundef -2)
  br label %73

73:                                               ; preds = %70, %65
  %74 = load i32, ptr %12, align 4, !tbaa !12
  %75 = load i64, ptr %16, align 8, !tbaa !14
  %76 = trunc i64 %75 to i32
  %77 = add i32 %74, %76
  store i32 %77, ptr %12, align 4, !tbaa !12
  br label %79

78:                                               ; preds = %52
  store i8 0, ptr %5, align 1
  store i32 1, ptr %9, align 4
  br label %80

79:                                               ; preds = %73
  store i32 0, ptr %9, align 4
  br label %80

80:                                               ; preds = %79, %78
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  %81 = load i32, ptr %9, align 4
  switch i32 %81, label %89 [
    i32 0, label %82
  ]

82:                                               ; preds = %80
  br label %34

83:                                               ; preds = %39
  %84 = load ptr, ptr %8, align 8, !tbaa !11
  %85 = call i32 @fclose(ptr noundef %84)
  %86 = icmp ne i32 %85, 0
  br i1 %86, label %87, label %88

87:                                               ; preds = %83
  store i8 0, ptr %5, align 1
  store i32 1, ptr %9, align 4
  br label %89

88:                                               ; preds = %83
  store i8 1, ptr %5, align 1
  store i32 1, ptr %9, align 4
  br label %89

89:                                               ; preds = %88, %87, %80
  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 4, ptr %10) #4
  br label %90

90:                                               ; preds = %89, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %91 = load i8, ptr %5, align 1
  ret i8 %91
}

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

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 = { "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 }

!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 = !{!7, !7, i64 0}
!12 = !{!10, !10, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!15, !15, i64 0}
!15 = !{!"long", !8, 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_____string8 = 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: 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 i32 @fclose(ptr nocapture noundef) local_unnamed_addr #2

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

.preheader:                                       ; preds = %4
  %.not32 = icmp eq i32 %3, 0
  br i1 %.not32, label %._crit_edge, label %.lr.ph

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

10:                                               ; preds = %.lr.ph, %20
  %.02331 = phi i32 [ 0, %.lr.ph ], [ %22, %20 ]
  %11 = sub i32 %3, %.02331
  %12 = tail call i32 @llvm.umin.i32(i32 %11, i32 4096)
  %13 = zext i32 %.02331 to i64
  %14 = getelementptr inbounds i8, ptr %2, i64 %13
  %15 = zext nneg i32 %12 to i64
  %16 = tail call i64 @fwrite(ptr noundef %14, i64 noundef 1, i64 noundef %15, ptr noundef nonnull %7)
  %.not28.not = icmp eq i64 %16, 0
  br i1 %.not28.not, label %.thread, label %17

17:                                               ; preds = %10
  %.not29 = icmp ugt i64 %16, %15
  br i1 %.not29, label %18, label %20

18:                                               ; preds = %17
  %19 = load ptr, ptr %9, align 8, !tbaa !11
  tail call void %19(i32 noundef -2) #4
  br label %20

20:                                               ; preds = %17, %18
  %21 = trunc i64 %16 to i32
  %22 = add i32 %.02331, %21
  %23 = icmp ult i32 %22, %3
  br i1 %23, label %10, label %._crit_edge

._crit_edge:                                      ; preds = %20, %.preheader
  %24 = tail call i32 @fclose(ptr noundef nonnull %7)
  %.not = icmp eq i32 %24, 0
  %. = zext i1 %.not to i8
  br label %.thread

.thread:                                          ; preds = %10, %._crit_edge, %4
  %.3 = phi i8 [ 0, %4 ], [ %., %._crit_edge ], [ 0, %10 ]
  ret i8 %.3
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #3

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 = !{!7, !7, i64 0}


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