; LLVM IR from pmwkaa/monotone :: monotone/runtime/runtime (vfs)

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

; 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 willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nounwind uwtable
define dso_local i64 @vfs_writev(i32 noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %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 i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !11
  br label %11

11:                                               ; preds = %77, %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  br label %12

12:                                               ; preds = %25, %11
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = load ptr, ptr %6, align 8, !tbaa !9
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = call i64 @writev(i32 noundef %13, ptr noundef %14, i32 noundef %15)
  %17 = trunc i64 %16 to i32
  store i32 %17, ptr %9, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %12
  %19 = load i32, ptr %9, align 4, !tbaa !5
  %20 = icmp eq i32 %19, -1
  br i1 %20, label %21, label %25

21:                                               ; preds = %18
  %22 = call ptr @__errno_location() #5
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = icmp eq i32 %23, 4
  br label %25

25:                                               ; preds = %21, %18
  %26 = phi i1 [ false, %18 ], [ %24, %21 ]
  br i1 %26, label %12, label %27, !llvm.loop !13

27:                                               ; preds = %25
  %28 = load i32, ptr %9, align 4, !tbaa !5
  %29 = icmp slt i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %27
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %74

31:                                               ; preds = %27
  %32 = load i32, ptr %9, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = load i64, ptr %8, align 8, !tbaa !11
  %35 = add i64 %34, %33
  store i64 %35, ptr %8, align 8, !tbaa !11
  br label %36

36:                                               ; preds = %61, %31
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = icmp sgt i32 %37, 0
  br i1 %38, label %39, label %73

39:                                               ; preds = %36
  %40 = load ptr, ptr %6, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.iovec, ptr %40, i32 0, i32 1
  %42 = load i64, ptr %41, align 8, !tbaa !15
  %43 = load i32, ptr %9, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = icmp ugt i64 %42, %44
  br i1 %45, label %46, label %61

46:                                               ; preds = %39
  %47 = load ptr, ptr %6, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.iovec, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !17
  %50 = load i32, ptr %9, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i8, ptr %49, i64 %51
  %53 = load ptr, ptr %6, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.iovec, ptr %53, i32 0, i32 0
  store ptr %52, ptr %54, align 8, !tbaa !17
  %55 = load i32, ptr %9, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = load ptr, ptr %6, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.iovec, ptr %57, i32 0, i32 1
  %59 = load i64, ptr %58, align 8, !tbaa !15
  %60 = sub i64 %59, %56
  store i64 %60, ptr %58, align 8, !tbaa !15
  br label %73

61:                                               ; preds = %39
  %62 = load ptr, ptr %6, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.iovec, ptr %62, i32 0, i32 1
  %64 = load i64, ptr %63, align 8, !tbaa !15
  %65 = load i32, ptr %9, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = sub i64 %66, %64
  %68 = trunc i64 %67 to i32
  store i32 %68, ptr %9, align 4, !tbaa !5
  %69 = load ptr, ptr %6, align 8, !tbaa !9
  %70 = getelementptr inbounds %struct.iovec, ptr %69, i32 1
  store ptr %70, ptr %6, align 8, !tbaa !9
  %71 = load i32, ptr %7, align 4, !tbaa !5
  %72 = add nsw i32 %71, -1
  store i32 %72, ptr %7, align 4, !tbaa !5
  br label %36, !llvm.loop !18

73:                                               ; preds = %46, %36
  store i32 0, ptr %10, align 4
  br label %74

74:                                               ; preds = %73, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  %75 = load i32, ptr %10, align 4
  switch i32 %75, label %82 [
    i32 0, label %76
  ]

76:                                               ; preds = %74
  br label %77

77:                                               ; preds = %76
  %78 = load i32, ptr %7, align 4, !tbaa !5
  %79 = icmp sgt i32 %78, 0
  br i1 %79, label %11, label %80, !llvm.loop !19

80:                                               ; preds = %77
  %81 = load i64, ptr %8, align 8, !tbaa !11
  store i64 %81, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %82

82:                                               ; preds = %80, %74
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %83 = load i64, ptr %4, align 8
  ret i64 %83
}

declare i64 @writev(i32 noundef, ptr noundef, i32 noundef) #3

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

!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/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"}
!15 = !{!16, !12, i64 8}
!16 = !{!"iovec", !10, i64 0, !12, i64 8}
!17 = !{!16, !10, i64 0}
!18 = distinct !{!18, !14}
!19 = distinct !{!19, !14}


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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @vfs_writev(i32 noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  br label %4

4:                                                ; preds = %22, %3
  %.026 = phi ptr [ %1, %3 ], [ %.12756, %22 ]
  %.024 = phi i32 [ %2, %3 ], [ %.12557, %22 ]
  %.023 = phi i64 [ 0, %3 ], [ %16, %22 ]
  br label %5

5:                                                ; preds = %9, %4
  %6 = tail call i64 @writev(i32 noundef %0, ptr noundef %.026, i32 noundef %.024) #3
  %7 = trunc i64 %6 to i32
  %8 = icmp eq i32 %7, -1
  br i1 %8, label %9, label %.critedge

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

.critedge:                                        ; preds = %5
  %13 = icmp sgt i32 %7, -1
  br i1 %13, label %14, label %.critedge.thread

14:                                               ; preds = %.critedge
  %15 = and i64 %6, 2147483647
  %16 = add i64 %15, %.023
  %17 = icmp sgt i32 %.024, 0
  br i1 %17, label %.lr.ph, label %.critedge.thread

.lr.ph:                                           ; preds = %14, %27
  %.02258 = phi i64 [ %28, %27 ], [ %6, %14 ]
  %.12557 = phi i32 [ %30, %27 ], [ %.024, %14 ]
  %.12756 = phi ptr [ %29, %27 ], [ %.026, %14 ]
  %18 = getelementptr inbounds i8, ptr %.12756, i64 8
  %19 = load i64, ptr %18, align 8, !tbaa !11
  %sext = shl i64 %.02258, 32
  %20 = ashr exact i64 %sext, 32
  %21 = icmp ugt i64 %19, %20
  br i1 %21, label %22, label %27

22:                                               ; preds = %.lr.ph
  %23 = getelementptr inbounds i8, ptr %.12756, i64 8
  %24 = load ptr, ptr %.12756, align 8, !tbaa !15
  %25 = getelementptr inbounds i8, ptr %24, i64 %20
  store ptr %25, ptr %.12756, align 8, !tbaa !15
  %26 = sub i64 %19, %20
  store i64 %26, ptr %23, align 8, !tbaa !11
  br label %4, !llvm.loop !16

27:                                               ; preds = %.lr.ph
  %28 = sub i64 %20, %19
  %29 = getelementptr inbounds i8, ptr %.12756, i64 16
  %30 = add nsw i32 %.12557, -1
  %31 = icmp sgt i32 %.12557, 1
  br i1 %31, label %.lr.ph, label %.critedge.thread, !llvm.loop !17

.critedge.thread:                                 ; preds = %.critedge, %14, %9, %27
  %.231 = phi i64 [ %16, %27 ], [ -1, %9 ], [ -1, %.critedge ], [ %16, %14 ]
  ret i64 %.231
}

declare i64 @writev(i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

attributes #0 = { 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 #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 = { "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 = { nounwind willreturn memory(none) }

!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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !14, i64 8}
!12 = !{!"iovec", !13, i64 0, !14, i64 8}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!12, !13, i64 0}
!16 = distinct !{!16, !10}
!17 = distinct !{!17, !10}


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