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

; 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 @Bread(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 4, 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
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %12, ptr %8, align 8, !tbaa !5
  %13 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %13, ptr %7, align 8, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.Biobuf, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !11
  store i32 %16, ptr %11, align 4, !tbaa !14
  br label %17

17:                                               ; preds = %105, %102, %3
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = icmp sgt i64 %18, 0
  br i1 %19, label %20, label %126

20:                                               ; preds = %17
  %21 = load i32, ptr %11, align 4, !tbaa !14
  %22 = sub nsw i32 0, %21
  store i32 %22, ptr %10, align 4, !tbaa !14
  %23 = load i32, ptr %10, align 4, !tbaa !14
  %24 = sext i32 %23 to i64
  %25 = load i64, ptr %7, align 8, !tbaa !9
  %26 = icmp sgt i64 %24, %25
  br i1 %26, label %27, label %30

27:                                               ; preds = %20
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = trunc i64 %28 to i32
  store i32 %29, ptr %10, align 4, !tbaa !14
  br label %30

30:                                               ; preds = %27, %20
  %31 = load i32, ptr %10, align 4, !tbaa !14
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %33, label %105

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

38:                                               ; preds = %33
  br label %126

39:                                               ; preds = %33
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.Biobuf, ptr %40, i32 0, i32 5
  %42 = load i32, ptr %41, align 4, !tbaa !16
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.Biobuf, ptr %43, i32 0, i32 9
  %45 = load ptr, ptr %44, align 8, !tbaa !17
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.Biobuf, ptr %46, i32 0, i32 8
  %48 = load i32, ptr %47, align 8, !tbaa !18
  %49 = sext i32 %48 to i64
  %50 = call i64 @read(i32 noundef %42, ptr noundef %45, i64 noundef %49)
  %51 = trunc i64 %50 to i32
  store i32 %51, ptr %9, align 4, !tbaa !14
  %52 = load i32, ptr %9, align 4, !tbaa !14
  %53 = icmp sle i32 %52, 0
  br i1 %53, label %54, label %63

54:                                               ; preds = %39
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.Biobuf, ptr %55, i32 0, i32 4
  store i32 3, ptr %56, align 8, !tbaa !15
  %57 = load i32, ptr %9, align 4, !tbaa !14
  %58 = icmp slt i32 %57, 0
  br i1 %58, label %59, label %62

59:                                               ; preds = %54
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.Biobuf, ptr %60, i32 0, i32 4
  store i32 0, ptr %61, align 8, !tbaa !15
  br label %62

62:                                               ; preds = %59, %54
  br label %126

63:                                               ; preds = %39
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.Biobuf, ptr %64, i32 0, i32 9
  %66 = load ptr, ptr %65, align 8, !tbaa !17
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.Biobuf, ptr %67, i32 0, i32 11
  store ptr %66, ptr %68, align 8, !tbaa !19
  %69 = load i32, ptr %9, align 4, !tbaa !14
  %70 = sext i32 %69 to i64
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.Biobuf, ptr %71, i32 0, i32 7
  %73 = load i64, ptr %72, align 8, !tbaa !20
  %74 = add nsw i64 %73, %70
  store i64 %74, ptr %72, align 8, !tbaa !20
  %75 = load i32, ptr %9, align 4, !tbaa !14
  %76 = load ptr, ptr %4, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.Biobuf, ptr %76, i32 0, i32 8
  %78 = load i32, ptr %77, align 8, !tbaa !18
  %79 = icmp slt i32 %75, %78
  br i1 %79, label %80, label %102

80:                                               ; preds = %63
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.Biobuf, ptr %81, i32 0, i32 10
  %83 = load ptr, ptr %82, align 8, !tbaa !21
  %84 = load i32, ptr %9, align 4, !tbaa !14
  %85 = sext i32 %84 to i64
  %86 = sub i64 0, %85
  %87 = getelementptr inbounds i8, ptr %83, i64 %86
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.Biobuf, ptr %88, i32 0, i32 9
  %90 = load ptr, ptr %89, align 8, !tbaa !17
  %91 = load i32, ptr %9, align 4, !tbaa !14
  %92 = sext i32 %91 to i64
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %87, ptr align 1 %90, i64 %92, i1 false)
  %93 = load ptr, ptr %4, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.Biobuf, ptr %93, i32 0, i32 10
  %95 = load ptr, ptr %94, align 8, !tbaa !21
  %96 = load i32, ptr %9, align 4, !tbaa !14
  %97 = sext i32 %96 to i64
  %98 = sub i64 0, %97
  %99 = getelementptr inbounds i8, ptr %95, i64 %98
  %100 = load ptr, ptr %4, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.Biobuf, ptr %100, i32 0, i32 11
  store ptr %99, ptr %101, align 8, !tbaa !19
  br label %102

102:                                              ; preds = %80, %63
  %103 = load i32, ptr %9, align 4, !tbaa !14
  %104 = sub nsw i32 0, %103
  store i32 %104, ptr %11, align 4, !tbaa !14
  br label %17, !llvm.loop !22

105:                                              ; preds = %30
  %106 = load ptr, ptr %8, align 8, !tbaa !5
  %107 = load ptr, ptr %4, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.Biobuf, ptr %107, i32 0, i32 10
  %109 = load ptr, ptr %108, align 8, !tbaa !21
  %110 = load i32, ptr %11, align 4, !tbaa !14
  %111 = sext i32 %110 to i64
  %112 = getelementptr inbounds i8, ptr %109, i64 %111
  %113 = load i32, ptr %10, align 4, !tbaa !14
  %114 = sext i32 %113 to i64
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %106, ptr align 1 %112, i64 %114, i1 false)
  %115 = load i32, ptr %10, align 4, !tbaa !14
  %116 = sext i32 %115 to i64
  %117 = load i64, ptr %7, align 8, !tbaa !9
  %118 = sub nsw i64 %117, %116
  store i64 %118, ptr %7, align 8, !tbaa !9
  %119 = load i32, ptr %10, align 4, !tbaa !14
  %120 = load i32, ptr %11, align 4, !tbaa !14
  %121 = add nsw i32 %120, %119
  store i32 %121, ptr %11, align 4, !tbaa !14
  %122 = load i32, ptr %10, align 4, !tbaa !14
  %123 = load ptr, ptr %8, align 8, !tbaa !5
  %124 = sext i32 %122 to i64
  %125 = getelementptr inbounds i8, ptr %123, i64 %124
  store ptr %125, ptr %8, align 8, !tbaa !5
  br label %17, !llvm.loop !22

126:                                              ; preds = %62, %38, %17
  %127 = load i32, ptr %11, align 4, !tbaa !14
  %128 = load ptr, ptr %4, align 8, !tbaa !5
  %129 = getelementptr inbounds %struct.Biobuf, ptr %128, i32 0, i32 0
  store i32 %127, ptr %129, align 8, !tbaa !11
  %130 = load i64, ptr %6, align 8, !tbaa !9
  %131 = load i64, ptr %7, align 8, !tbaa !9
  %132 = sub nsw i64 %130, %131
  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 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret i64 %132
}

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

declare i64 @read(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 0}
!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, !6, i64 64}
!20 = !{!12, !10, i64 32}
!21 = !{!12, !6, i64 56}
!22 = distinct !{!22, !23}
!23 = !{!"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"

; Function Attrs: nofree nounwind uwtable
define dso_local i64 @Bread(ptr nocapture noundef %0, ptr nocapture noundef writeonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = load i32, ptr %0, align 8, !tbaa !5
  %5 = icmp sgt i64 %2, 0
  br i1 %5, label %.lr.ph.lr.ph, label %.loopexit

.lr.ph.lr.ph:                                     ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = getelementptr inbounds i8, ptr %0, i64 20
  %8 = getelementptr inbounds i8, ptr %0, i64 48
  %9 = getelementptr inbounds i8, ptr %0, i64 40
  %10 = getelementptr inbounds i8, ptr %0, i64 64
  %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.ph103 = phi i32 [ %4, %.lr.ph.lr.ph ], [ %47, %.outer ]
  %.045.ph101 = phi i64 [ %2, %.lr.ph.lr.ph ], [ %46, %.outer ]
  %.046.ph100 = phi ptr [ %1, %.lr.ph.lr.ph ], [ %48, %.outer ]
  %13 = trunc nuw nsw i64 %.045.ph101 to i32
  %14 = sub nsw i32 0, %.0.ph103
  %15 = sext i32 %14 to i64
  %16 = icmp slt i64 %.045.ph101, %15
  %spec.select.us95 = select i1 %16, i32 %13, i32 %14
  %17 = icmp eq i32 %spec.select.us95, 0
  br i1 %17, label %.lr.ph97, label %.outer

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

19:                                               ; preds = %.lr.ph97
  %20 = load i32, ptr %7, align 4, !tbaa !13
  %21 = load ptr, ptr %8, align 8, !tbaa !14
  %22 = load i32, ptr %9, align 8, !tbaa !15
  %23 = sext i32 %22 to i64
  %24 = tail call i64 @read(i32 noundef %20, ptr noundef %21, i64 noundef %23) #3
  %25 = trunc i64 %24 to i32
  %26 = icmp slt i32 %25, 1
  br i1 %26, label %.split67.us, label %27

27:                                               ; preds = %19
  %28 = load ptr, ptr %8, align 8, !tbaa !14
  store ptr %28, ptr %10, align 8, !tbaa !16
  %29 = and i64 %24, 2147483647
  %30 = load i64, ptr %11, align 8, !tbaa !17
  %31 = add nsw i64 %30, %29
  store i64 %31, ptr %11, align 8, !tbaa !17
  %32 = load i32, ptr %9, align 8, !tbaa !15
  %33 = icmp sgt i32 %32, %25
  br i1 %33, label %34, label %..split.us_crit_edge

34:                                               ; preds = %27
  %35 = load ptr, ptr %12, align 8, !tbaa !18
  %36 = sub nsw i64 0, %29
  %37 = getelementptr inbounds i8, ptr %35, i64 %36
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %37, ptr align 1 %28, i64 %29, i1 false)
  %38 = load ptr, ptr %12, align 8, !tbaa !18
  %39 = getelementptr inbounds i8, ptr %38, i64 %36
  store ptr %39, ptr %10, align 8, !tbaa !16
  br label %..split.us_crit_edge

..split.us_crit_edge:                             ; preds = %34, %27
  %40 = sub nsw i32 0, %25
  %41 = icmp ult i64 %.045.ph101, %29
  %spec.select.us = select i1 %41, i32 %13, i32 %25
  br label %.outer

.split67.us:                                      ; preds = %19
  %.not48 = icmp eq i32 %25, 0
  %spec.store.select = select i1 %.not48, i32 3, i32 0
  store i32 %spec.store.select, ptr %6, align 8
  br label %.loopexit

.outer:                                           ; preds = %.lr.ph.split.us, %..split.us_crit_edge
  %.us-phi = phi i32 [ %spec.select.us, %..split.us_crit_edge ], [ %spec.select.us95, %.lr.ph.split.us ]
  %.us-phi63 = phi i32 [ %40, %..split.us_crit_edge ], [ %.0.ph103, %.lr.ph.split.us ]
  %42 = load ptr, ptr %12, align 8, !tbaa !18
  %43 = sext i32 %.us-phi63 to i64
  %44 = getelementptr inbounds i8, ptr %42, i64 %43
  %45 = sext i32 %.us-phi to i64
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %.046.ph100, ptr align 1 %44, i64 %45, i1 false)
  %46 = sub nsw i64 %.045.ph101, %45
  %47 = add nsw i32 %.us-phi, %.us-phi63
  %48 = getelementptr inbounds i8, ptr %.046.ph100, i64 %45
  %49 = icmp sgt i64 %46, 0
  br i1 %49, label %.lr.ph.split.us, label %.loopexit, !llvm.loop !19

.loopexit:                                        ; preds = %.outer, %.lr.ph97, %3, %.split67.us
  %.045.ph55 = phi i64 [ %.045.ph101, %.split67.us ], [ %2, %3 ], [ %46, %.outer ], [ %.045.ph101, %.lr.ph97 ]
  %.051 = phi i32 [ %.0.ph103, %.split67.us ], [ %4, %3 ], [ %47, %.outer ], [ %.0.ph103, %.lr.ph97 ]
  store i32 %.051, ptr %0, align 8, !tbaa !5
  %50 = sub nsw i64 %2, %.045.ph55
  ret i64 %50
}

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, 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 0}
!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, !11, i64 64}
!17 = !{!6, !10, i64 32}
!18 = !{!6, !11, i64 56}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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