; 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_pwritev(i32 noundef %0, i64 noundef %1, ptr noundef %2, i32 noundef %3) #2 {
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !11
  store i32 %3, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %13

13:                                               ; preds = %83, %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  br label %14

14:                                               ; preds = %30, %13
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = load ptr, ptr %8, align 8, !tbaa !11
  %17 = load i32, ptr %9, align 4, !tbaa !5
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = load i64, ptr %10, align 8, !tbaa !9
  %20 = add i64 %18, %19
  %21 = call i64 @pwritev(i32 noundef %15, ptr noundef %16, i32 noundef %17, i64 noundef %20)
  %22 = trunc i64 %21 to i32
  store i32 %22, ptr %11, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %14
  %24 = load i32, ptr %11, align 4, !tbaa !5
  %25 = icmp eq i32 %24, -1
  br i1 %25, label %26, label %30

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

30:                                               ; preds = %26, %23
  %31 = phi i1 [ false, %23 ], [ %29, %26 ]
  br i1 %31, label %14, label %32, !llvm.loop !13

32:                                               ; preds = %30
  %33 = load i32, ptr %11, align 4, !tbaa !5
  %34 = icmp slt i32 %33, 0
  br i1 %34, label %35, label %36

35:                                               ; preds = %32
  store i64 -1, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %80

36:                                               ; preds = %32
  %37 = load i32, ptr %11, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = load i64, ptr %10, align 8, !tbaa !9
  %40 = add i64 %39, %38
  store i64 %40, ptr %10, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %78, %36
  %42 = load i32, ptr %9, align 4, !tbaa !5
  %43 = icmp sgt i32 %42, 0
  br i1 %43, label %44, label %79

44:                                               ; preds = %41
  %45 = load ptr, ptr %8, align 8, !tbaa !11
  %46 = getelementptr inbounds %struct.iovec, ptr %45, i32 0, i32 1
  %47 = load i64, ptr %46, align 8, !tbaa !15
  %48 = load i32, ptr %11, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = icmp ugt i64 %47, %49
  br i1 %50, label %51, label %66

51:                                               ; preds = %44
  %52 = load ptr, ptr %8, align 8, !tbaa !11
  %53 = getelementptr inbounds %struct.iovec, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !17
  %55 = load i32, ptr %11, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i8, ptr %54, i64 %56
  %58 = load ptr, ptr %8, align 8, !tbaa !11
  %59 = getelementptr inbounds %struct.iovec, ptr %58, i32 0, i32 0
  store ptr %57, ptr %59, align 8, !tbaa !17
  %60 = load i32, ptr %11, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = load ptr, ptr %8, align 8, !tbaa !11
  %63 = getelementptr inbounds %struct.iovec, ptr %62, i32 0, i32 1
  %64 = load i64, ptr %63, align 8, !tbaa !15
  %65 = sub i64 %64, %61
  store i64 %65, ptr %63, align 8, !tbaa !15
  br label %79

66:                                               ; preds = %44
  %67 = load ptr, ptr %8, align 8, !tbaa !11
  %68 = getelementptr inbounds %struct.iovec, ptr %67, i32 0, i32 1
  %69 = load i64, ptr %68, align 8, !tbaa !15
  %70 = load i32, ptr %11, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = sub i64 %71, %69
  %73 = trunc i64 %72 to i32
  store i32 %73, ptr %11, align 4, !tbaa !5
  %74 = load ptr, ptr %8, align 8, !tbaa !11
  %75 = getelementptr inbounds %struct.iovec, ptr %74, i32 1
  store ptr %75, ptr %8, align 8, !tbaa !11
  %76 = load i32, ptr %9, align 4, !tbaa !5
  %77 = add nsw i32 %76, -1
  store i32 %77, ptr %9, align 4, !tbaa !5
  br label %78

78:                                               ; preds = %66
  br label %41, !llvm.loop !18

79:                                               ; preds = %51, %41
  store i32 0, ptr %12, align 4
  br label %80

80:                                               ; preds = %79, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  %81 = load i32, ptr %12, align 4
  switch i32 %81, label %88 [
    i32 0, label %82
  ]

82:                                               ; preds = %80
  br label %83

83:                                               ; preds = %82
  %84 = load i32, ptr %9, align 4, !tbaa !5
  %85 = icmp sgt i32 %84, 0
  br i1 %85, label %13, label %86, !llvm.loop !19

86:                                               ; preds = %83
  %87 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %87, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %88

88:                                               ; preds = %86, %80
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %89 = load i64, ptr %5, align 8
  ret i64 %89
}

declare i64 @pwritev(i32 noundef, ptr noundef, i32 noundef, i64 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !10, i64 8}
!16 = !{!"iovec", !12, i64 0, !10, i64 8}
!17 = !{!16, !12, 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_pwritev(i32 noundef %0, i64 noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #1 {
  br label %5

5:                                                ; preds = %24, %4
  %.028 = phi ptr [ %2, %4 ], [ %.12958, %24 ]
  %.026 = phi i32 [ %3, %4 ], [ %.12759, %24 ]
  %.025 = phi i64 [ 0, %4 ], [ %18, %24 ]
  %6 = add i64 %.025, %1
  br label %7

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

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

.critedge:                                        ; preds = %7
  %15 = icmp sgt i32 %9, -1
  br i1 %15, label %16, label %.critedge.thread

16:                                               ; preds = %.critedge
  %17 = and i64 %8, 2147483647
  %18 = add i64 %17, %.025
  %19 = icmp sgt i32 %.026, 0
  br i1 %19, label %.lr.ph, label %.critedge.thread

.lr.ph:                                           ; preds = %16, %29
  %.02460 = phi i64 [ %30, %29 ], [ %8, %16 ]
  %.12759 = phi i32 [ %32, %29 ], [ %.026, %16 ]
  %.12958 = phi ptr [ %31, %29 ], [ %.028, %16 ]
  %20 = getelementptr inbounds i8, ptr %.12958, i64 8
  %21 = load i64, ptr %20, align 8, !tbaa !11
  %sext = shl i64 %.02460, 32
  %22 = ashr exact i64 %sext, 32
  %23 = icmp ugt i64 %21, %22
  br i1 %23, label %24, label %29

24:                                               ; preds = %.lr.ph
  %25 = getelementptr inbounds i8, ptr %.12958, i64 8
  %26 = load ptr, ptr %.12958, align 8, !tbaa !15
  %27 = getelementptr inbounds i8, ptr %26, i64 %22
  store ptr %27, ptr %.12958, align 8, !tbaa !15
  %28 = sub i64 %21, %22
  store i64 %28, ptr %25, align 8, !tbaa !11
  br label %5, !llvm.loop !16

29:                                               ; preds = %.lr.ph
  %30 = sub i64 %22, %21
  %31 = getelementptr inbounds i8, ptr %.12958, i64 16
  %32 = add nsw i32 %.12759, -1
  %33 = icmp sgt i32 %.12759, 1
  br i1 %33, label %.lr.ph, label %.critedge.thread, !llvm.loop !17

.critedge.thread:                                 ; preds = %.critedge, %16, %11, %29
  %.233 = phi i64 [ %18, %29 ], [ -1, %11 ], [ -1, %.critedge ], [ %18, %16 ]
  ret i64 %.233
}

declare i64 @pwritev(i32 noundef, ptr noundef, i32 noundef, i64 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