; LLVM IR from 0intro/wmii :: lib/libbio (bwrite)

; 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.Biobuf = type { i32, i32, i32, i32, i32, i32, i32, i64, i32, ptr, ptr, ptr, [8196 x i8] }

; Function Attrs: nounwind uwtable
define dso_local i64 @Bwrite(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %14, ptr %9, align 8, !tbaa !5
  %15 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %15, ptr %8, align 8, !tbaa !9
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.Biobuf, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !11
  store i32 %18, ptr %12, align 4, !tbaa !14
  br label %19

19:                                               ; preds = %73, %62, %3
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = icmp sgt i64 %20, 0
  br i1 %21, label %22, label %94

22:                                               ; preds = %19
  %23 = load i32, ptr %12, align 4, !tbaa !14
  %24 = sub nsw i32 0, %23
  store i32 %24, ptr %11, align 4, !tbaa !14
  %25 = load i32, ptr %11, align 4, !tbaa !14
  %26 = sext i32 %25 to i64
  %27 = load i64, ptr %8, align 8, !tbaa !9
  %28 = icmp sgt i64 %26, %27
  br i1 %28, label %29, label %32

29:                                               ; preds = %22
  %30 = load i64, ptr %8, align 8, !tbaa !9
  %31 = trunc i64 %30 to i32
  store i32 %31, ptr %11, align 4, !tbaa !14
  br label %32

32:                                               ; preds = %29, %22
  %33 = load i32, ptr %11, align 4, !tbaa !14
  %34 = icmp eq i32 %33, 0
  br i1 %34, label %35, label %73

35:                                               ; preds = %32
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.Biobuf, ptr %36, i32 0, i32 4
  %38 = load i32, ptr %37, align 8, !tbaa !15
  %39 = icmp ne i32 %38, 2
  br i1 %39, label %40, label %41

40:                                               ; preds = %35
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %101

41:                                               ; preds = %35
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.Biobuf, ptr %42, i32 0, i32 5
  %44 = load i32, ptr %43, align 4, !tbaa !16
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.Biobuf, ptr %45, i32 0, i32 9
  %47 = load ptr, ptr %46, align 8, !tbaa !17
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Biobuf, ptr %48, i32 0, i32 8
  %50 = load i32, ptr %49, align 8, !tbaa !18
  %51 = sext i32 %50 to i64
  %52 = call i64 @write(i32 noundef %44, ptr noundef %47, i64 noundef %51)
  %53 = trunc i64 %52 to i32
  store i32 %53, ptr %10, align 4, !tbaa !14
  %54 = load i32, ptr %10, align 4, !tbaa !14
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.Biobuf, ptr %55, i32 0, i32 8
  %57 = load i32, ptr %56, align 8, !tbaa !18
  %58 = icmp ne i32 %54, %57
  br i1 %58, label %59, label %62

59:                                               ; preds = %41
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.Biobuf, ptr %60, i32 0, i32 4
  store i32 0, ptr %61, align 8, !tbaa !15
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %101

62:                                               ; preds = %41
  %63 = load i32, ptr %10, align 4, !tbaa !14
  %64 = sext i32 %63 to i64
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.Biobuf, ptr %65, i32 0, i32 7
  %67 = load i64, ptr %66, align 8, !tbaa !19
  %68 = add nsw i64 %67, %64
  store i64 %68, ptr %66, align 8, !tbaa !19
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.Biobuf, ptr %69, i32 0, i32 8
  %71 = load i32, ptr %70, align 8, !tbaa !18
  %72 = sub nsw i32 0, %71
  store i32 %72, ptr %12, align 4, !tbaa !14
  br label %19, !llvm.loop !20

73:                                               ; preds = %32
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.Biobuf, ptr %74, i32 0, i32 10
  %76 = load ptr, ptr %75, align 8, !tbaa !22
  %77 = load i32, ptr %12, align 4, !tbaa !14
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds i8, ptr %76, i64 %78
  %80 = load ptr, ptr %9, align 8, !tbaa !5
  %81 = load i32, ptr %11, align 4, !tbaa !14
  %82 = sext i32 %81 to i64
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %79, ptr align 1 %80, i64 %82, i1 false)
  %83 = load i32, ptr %11, align 4, !tbaa !14
  %84 = load i32, ptr %12, align 4, !tbaa !14
  %85 = add nsw i32 %84, %83
  store i32 %85, ptr %12, align 4, !tbaa !14
  %86 = load i32, ptr %11, align 4, !tbaa !14
  %87 = sext i32 %86 to i64
  %88 = load i64, ptr %8, align 8, !tbaa !9
  %89 = sub nsw i64 %88, %87
  store i64 %89, ptr %8, align 8, !tbaa !9
  %90 = load i32, ptr %11, align 4, !tbaa !14
  %91 = load ptr, ptr %9, align 8, !tbaa !5
  %92 = sext i32 %90 to i64
  %93 = getelementptr inbounds i8, ptr %91, i64 %92
  store ptr %93, ptr %9, align 8, !tbaa !5
  br label %19, !llvm.loop !20

94:                                               ; preds = %19
  %95 = load i32, ptr %12, align 4, !tbaa !14
  %96 = load ptr, ptr %5, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.Biobuf, ptr %96, i32 0, i32 1
  store i32 %95, ptr %97, align 4, !tbaa !11
  %98 = load i64, ptr %7, align 8, !tbaa !9
  %99 = load i64, ptr %8, align 8, !tbaa !9
  %100 = sub nsw i64 %98, %99
  store i64 %100, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %101

101:                                              ; preds = %94, %59, %40
  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
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %102 = load i64, ptr %4, align 8
  ret i64 %102
}

; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #3

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

attributes #0 = { 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 #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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!6 = !{!"any pointer", !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, !13, i64 4}
!12 = !{!"Biobuf", !13, i64 0, !13, i64 4, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20, !13, i64 24, !10, i64 32, !13, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !7, i64 72}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!12, !13, i64 16}
!16 = !{!12, !13, i64 20}
!17 = !{!12, !6, i64 48}
!18 = !{!12, !13, i64 40}
!19 = !{!12, !10, i64 32}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!12, !6, i64 56}


; 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: nofree nounwind uwtable
define dso_local i64 @Bwrite(ptr nocapture noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = icmp sgt i64 %2, 0
  br i1 %6, label %.lr.ph.lr.ph, label %.outer._crit_edge

.lr.ph.lr.ph:                                     ; preds = %3
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = getelementptr inbounds i8, ptr %0, i64 20
  %9 = getelementptr inbounds i8, ptr %0, i64 48
  %10 = getelementptr inbounds i8, ptr %0, i64 40
  %11 = getelementptr inbounds i8, ptr %0, i64 32
  %12 = getelementptr inbounds i8, ptr %0, i64 56
  br label %.lr.ph.split.us

.lr.ph.split.us:                                  ; preds = %.outer, %.lr.ph.lr.ph
  %.0.ph78 = phi i32 [ %5, %.lr.ph.lr.ph ], [ %39, %.outer ]
  %.035.ph77 = phi ptr [ %1, %.lr.ph.lr.ph ], [ %41, %.outer ]
  %.036.ph75 = phi i64 [ %2, %.lr.ph.lr.ph ], [ %40, %.outer ]
  %13 = trunc nuw nsw i64 %.036.ph75 to i32
  %14 = sub nsw i32 0, %.0.ph78
  %15 = sext i32 %14 to i64
  %16 = icmp slt i64 %.036.ph75, %15
  %spec.select.us71 = select i1 %16, i32 %13, i32 %14
  %17 = icmp eq i32 %spec.select.us71, 0
  br i1 %17, label %.lr.ph72, label %.outer

.lr.ph72:                                         ; preds = %.lr.ph.split.us, %27
  %18 = load i32, ptr %7, align 8, !tbaa !12
  %.not.us = icmp eq i32 %18, 2
  br i1 %.not.us, label %19, label %.loopexit

19:                                               ; preds = %.lr.ph72
  %20 = load i32, ptr %8, align 4, !tbaa !13
  %21 = load ptr, ptr %9, align 8, !tbaa !14
  %22 = load i32, ptr %10, align 8, !tbaa !15
  %23 = sext i32 %22 to i64
  %24 = tail call i64 @write(i32 noundef %20, ptr noundef %21, i64 noundef %23) #3
  %25 = trunc i64 %24 to i32
  %26 = load i32, ptr %10, align 8, !tbaa !15
  %.not38.us = icmp eq i32 %26, %25
  br i1 %.not38.us, label %27, label %.split53.us

27:                                               ; preds = %19
  %sext.us = shl i64 %24, 32
  %28 = ashr exact i64 %sext.us, 32
  %29 = load i64, ptr %11, align 8, !tbaa !16
  %30 = add nsw i64 %29, %28
  store i64 %30, ptr %11, align 8, !tbaa !16
  %sext = shl i64 %24, 32
  %31 = ashr exact i64 %sext, 32
  %32 = icmp slt i64 %.036.ph75, %31
  %spec.select.us = select i1 %32, i32 %13, i32 %25
  %33 = icmp eq i32 %spec.select.us, 0
  br i1 %33, label %.lr.ph72, label %.outer.loopexit

.split53.us:                                      ; preds = %19
  store i32 0, ptr %7, align 8, !tbaa !12
  br label %.loopexit

.outer.loopexit:                                  ; preds = %27
  %34 = sub nsw i32 0, %25
  br label %.outer

.outer:                                           ; preds = %.outer.loopexit, %.lr.ph.split.us
  %.us-phi = phi i32 [ %spec.select.us71, %.lr.ph.split.us ], [ %spec.select.us, %.outer.loopexit ]
  %.us-phi51 = phi i32 [ %.0.ph78, %.lr.ph.split.us ], [ %34, %.outer.loopexit ]
  %35 = load ptr, ptr %12, align 8, !tbaa !17
  %36 = sext i32 %.us-phi51 to i64
  %37 = getelementptr inbounds i8, ptr %35, i64 %36
  %38 = sext i32 %.us-phi to i64
  tail call void @llvm.memmove.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %37, ptr noundef nonnull align 1 dereferenceable(1) %.035.ph77, i64 %38, i1 false)
  %39 = add nsw i32 %.us-phi, %.us-phi51
  %40 = sub nsw i64 %.036.ph75, %38
  %41 = getelementptr inbounds i8, ptr %.035.ph77, i64 %38
  %42 = icmp sgt i64 %40, 0
  br i1 %42, label %.lr.ph.split.us, label %.outer._crit_edge, !llvm.loop !18

.outer._crit_edge:                                ; preds = %.outer, %3
  %.036.ph.lcssa46 = phi i64 [ %2, %3 ], [ %40, %.outer ]
  %.0.lcssa = phi i32 [ %5, %3 ], [ %39, %.outer ]
  store i32 %.0.lcssa, ptr %4, align 4, !tbaa !5
  %43 = sub nsw i64 %2, %.036.ph.lcssa46
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph72, %.outer._crit_edge, %.split53.us
  %.034 = phi i64 [ -1, %.split53.us ], [ %43, %.outer._crit_edge ], [ -1, %.lr.ph72 ]
  ret i64 %.034
}

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #2

attributes #0 = { nofree 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 #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 nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 4}
!6 = !{!"Biobuf", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !7, i64 24, !10, i64 32, !7, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !8, i64 72}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !7, i64 16}
!13 = !{!6, !7, i64 20}
!14 = !{!6, !11, i64 48}
!15 = !{!6, !7, i64 40}
!16 = !{!6, !10, i64 32}
!17 = !{!6, !11, i64 56}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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