; LLVM IR from rprichard/winpty :: src/unix-adapter/Util.ll (Z8writeAlliPKvm)

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

@.str = external hidden unnamed_addr constant [55 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [75 x i8], align 1
@__PRETTY_FUNCTION__._Z8writeAlliPKvm = external hidden unnamed_addr constant [41 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z8writeAlliPKvm(i32 noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca i1, align 1
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  store i64 0, ptr %8, align 8, !tbaa !11
  br label %11

11:                                               ; preds = %61, %59, %3
  %12 = load i64, ptr %8, align 8, !tbaa !11
  %13 = load i64, ptr %7, align 8, !tbaa !11
  %14 = icmp ult i64 %12, %13
  br i1 %14, label %15, label %62

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = load ptr, ptr %6, align 8, !tbaa !9
  %18 = load i64, ptr %8, align 8, !tbaa !11
  %19 = getelementptr inbounds i8, ptr %17, i64 %18
  %20 = load i64, ptr %7, align 8, !tbaa !11
  %21 = load i64, ptr %8, align 8, !tbaa !11
  %22 = sub i64 %20, %21
  %23 = call i64 @write(i32 noundef %16, ptr noundef %19, i64 noundef %22)
  %24 = trunc i64 %23 to i32
  store i32 %24, ptr %9, align 4, !tbaa !5
  %25 = load i32, ptr %9, align 4, !tbaa !5
  %26 = icmp eq i32 %25, -1
  br i1 %26, label %27, label %32

27:                                               ; preds = %15
  %28 = call ptr @__errno_location() #6
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = icmp eq i32 %29, 4
  br i1 %30, label %31, label %32

31:                                               ; preds = %27
  store i32 2, ptr %10, align 4
  br label %59, !llvm.loop !13

32:                                               ; preds = %27, %15
  %33 = load i32, ptr %9, align 4, !tbaa !5
  %34 = icmp sle i32 %33, 0
  br i1 %34, label %35, label %44

35:                                               ; preds = %32
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = call ptr @__errno_location() #6
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = load i64, ptr %7, align 8, !tbaa !11
  %40 = trunc i64 %39 to i32
  %41 = load i64, ptr %8, align 8, !tbaa !11
  %42 = trunc i64 %41 to i32
  %43 = load i32, ptr %9, align 4, !tbaa !5
  call void (ptr, ...) @_Z5tracePKcz(ptr noundef @.str, i32 noundef %36, i32 noundef %38, i32 noundef %40, i32 noundef %42, i32 noundef %43)
  store i1 false, ptr %4, align 1
  store i32 1, ptr %10, align 4
  br label %59

44:                                               ; preds = %32
  %45 = load i32, ptr %9, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = load i64, ptr %7, align 8, !tbaa !11
  %48 = load i64, ptr %8, align 8, !tbaa !11
  %49 = sub i64 %47, %48
  %50 = icmp ule i64 %46, %49
  br i1 %50, label %51, label %52

51:                                               ; preds = %44
  br label %54

52:                                               ; preds = %44
  call void @__assert_fail(ptr noundef @.str.1, ptr noundef @.str.2, i32 noundef 51, ptr noundef @__PRETTY_FUNCTION__._Z8writeAlliPKvm) #7
  unreachable

53:                                               ; No predecessors!
  br label %54

54:                                               ; preds = %53, %51
  %55 = load i32, ptr %9, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = load i64, ptr %8, align 8, !tbaa !11
  %58 = add i64 %57, %56
  store i64 %58, ptr %8, align 8, !tbaa !11
  store i32 0, ptr %10, align 4
  br label %59

59:                                               ; preds = %54, %35, %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  %60 = load i32, ptr %10, align 4
  switch i32 %60, label %70 [
    i32 0, label %61
    i32 2, label %11
  ]

61:                                               ; preds = %59
  br label %11, !llvm.loop !13

62:                                               ; preds = %11
  %63 = load i64, ptr %8, align 8, !tbaa !11
  %64 = load i64, ptr %7, align 8, !tbaa !11
  %65 = icmp eq i64 %63, %64
  br i1 %65, label %66, label %67

66:                                               ; preds = %62
  br label %69

67:                                               ; preds = %62
  call void @__assert_fail(ptr noundef @.str.3, ptr noundef @.str.2, i32 noundef 54, ptr noundef @__PRETTY_FUNCTION__._Z8writeAlliPKvm) #7
  unreachable

68:                                               ; No predecessors!
  br label %69

69:                                               ; preds = %68, %66
  store i1 true, ptr %4, align 1
  store i32 1, ptr %10, align 4
  br label %70

70:                                               ; preds = %69, %59
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %71 = load i1, ptr %4, align 1
  ret i1 %71
}

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

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

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #3

declare void @_Z5tracePKcz(ptr noundef, ...) #2

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #4

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

attributes #0 = { mustprogress 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 = { nounwind willreturn memory(none) "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 = { 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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(none) }
attributes #7 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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

@.str = external hidden unnamed_addr constant [55 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [75 x i8], align 1
@__PRETTY_FUNCTION__._Z8writeAlliPKvm = external hidden unnamed_addr constant [41 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z8writeAlliPKvm(i32 noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq i64 %2, 0
  br i1 %4, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %24
  %.02238 = phi i64 [ %.123, %24 ], [ 0, %3 ]
  %5 = getelementptr inbounds i8, ptr %1, i64 %.02238
  %6 = sub i64 %2, %.02238
  %7 = tail call i64 @write(i32 noundef %0, ptr noundef %5, i64 noundef %6)
  %8 = trunc i64 %7 to i32
  %9 = icmp eq i32 %8, -1
  br i1 %9, label %10, label %14

10:                                               ; preds = %.lr.ph
  %11 = tail call ptr @__errno_location() #5
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = icmp eq i32 %12, 4
  br i1 %13, label %24, label %.thread29, !llvm.loop !9

14:                                               ; preds = %.lr.ph
  %15 = icmp slt i32 %8, 1
  br i1 %15, label %..thread29_crit_edge, label %19

..thread29_crit_edge:                             ; preds = %14
  %.pre = tail call ptr @__errno_location() #5
  br label %.thread29

.thread29:                                        ; preds = %10, %..thread29_crit_edge
  %.pre-phi = phi ptr [ %.pre, %..thread29_crit_edge ], [ %11, %10 ]
  %16 = load i32, ptr %.pre-phi, align 4, !tbaa !5
  %17 = trunc i64 %2 to i32
  %18 = trunc i64 %.02238 to i32
  tail call void (ptr, ...) @_Z5tracePKcz(ptr noundef nonnull @.str, i32 noundef %0, i32 noundef %16, i32 noundef %17, i32 noundef %18, i32 noundef %8)
  br label %27

19:                                               ; preds = %14
  %20 = and i64 %7, 2147483647
  %.not = icmp ugt i64 %20, %6
  br i1 %.not, label %21, label %22

21:                                               ; preds = %19
  tail call void @__assert_fail(ptr noundef nonnull @.str.1, ptr noundef nonnull @.str.2, i32 noundef 51, ptr noundef nonnull @__PRETTY_FUNCTION__._Z8writeAlliPKvm) #6
  unreachable

22:                                               ; preds = %19
  %23 = add i64 %20, %.02238
  br label %24

24:                                               ; preds = %10, %22
  %.123 = phi i64 [ %23, %22 ], [ %.02238, %10 ]
  %.not49 = icmp ult i64 %.123, %2
  br i1 %.not49, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %24, %3
  %.022.lcssa = phi i64 [ 0, %3 ], [ %.123, %24 ]
  %25 = icmp eq i64 %.022.lcssa, %2
  br i1 %25, label %27, label %26

26:                                               ; preds = %._crit_edge
  tail call void @__assert_fail(ptr noundef nonnull @.str.3, ptr noundef nonnull @.str.2, i32 noundef 54, ptr noundef nonnull @__PRETTY_FUNCTION__._Z8writeAlliPKvm) #6
  unreachable

27:                                               ; preds = %.thread29, %._crit_edge
  %28 = phi i1 [ false, %.thread29 ], [ true, %._crit_edge ]
  ret i1 %28
}

; Function Attrs: nofree
declare noundef i64 @write(i32 noundef, ptr nocapture noundef readonly, i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #2

declare void @_Z5tracePKcz(ptr noundef, ...) local_unnamed_addr #3

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #4

attributes #0 = { mustprogress 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 = { nofree "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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 = { 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 #5 = { nounwind willreturn memory(none) }
attributes #6 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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