; LLVM IR from gem5/gem5 :: util/m5/src/command/readfile.test.ll (Z17test_m5_read_filePvmm)

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

@test_read_file_size = external dso_local global i64, align 8
@test_max_buf_size = external dso_local global i64, align 8
@test_total_read = external dso_local global i64, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z17test_m5_read_filePvmm(ptr noundef %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = load i64, ptr @test_read_file_size, align 8, !tbaa !9
  %18 = icmp uge i64 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %3
  store i64 0, ptr %4, align 8
  br label %128

20:                                               ; preds = %3
  %21 = load i64, ptr %7, align 8, !tbaa !9
  %22 = load i64, ptr %6, align 8, !tbaa !9
  %23 = add i64 %21, %22
  %24 = load i64, ptr @test_read_file_size, align 8, !tbaa !9
  %25 = icmp ugt i64 %23, %24
  br i1 %25, label %26, label %30

26:                                               ; preds = %20
  %27 = load i64, ptr @test_read_file_size, align 8, !tbaa !9
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = sub i64 %27, %28
  store i64 %29, ptr %6, align 8, !tbaa !9
  br label %30

30:                                               ; preds = %26, %20
  %31 = load i64, ptr @test_max_buf_size, align 8, !tbaa !9
  %32 = icmp ne i64 %31, 0
  br i1 %32, label %33, label %39

33:                                               ; preds = %30
  %34 = load i64, ptr %6, align 8, !tbaa !9
  %35 = load i64, ptr @test_max_buf_size, align 8, !tbaa !9
  %36 = icmp ugt i64 %34, %35
  br i1 %36, label %37, label %39

37:                                               ; preds = %33
  %38 = load i64, ptr @test_max_buf_size, align 8, !tbaa !9
  store i64 %38, ptr %6, align 8, !tbaa !9
  br label %39

39:                                               ; preds = %37, %33, %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #6
  store i32 4, ptr %8, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  %40 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %40, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #6
  %41 = load i32, ptr %8, align 4, !tbaa !11
  %42 = sext i32 %41 to i64
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = load i32, ptr %8, align 4, !tbaa !11
  %45 = sext i32 %44 to i64
  %46 = urem i64 %43, %45
  %47 = sub i64 %42, %46
  %48 = trunc i64 %47 to i32
  store i32 %48, ptr %10, align 4, !tbaa !11
  %49 = load i32, ptr %10, align 4, !tbaa !11
  %50 = sext i32 %49 to i64
  %51 = load i64, ptr %6, align 8, !tbaa !9
  %52 = icmp ugt i64 %50, %51
  br i1 %52, label %53, label %56

53:                                               ; preds = %39
  %54 = load i64, ptr %6, align 8, !tbaa !9
  %55 = trunc i64 %54 to i32
  store i32 %55, ptr %10, align 4, !tbaa !11
  br label %56

56:                                               ; preds = %53, %39
  %57 = load i32, ptr %10, align 4, !tbaa !11
  %58 = sext i32 %57 to i64
  %59 = load i64, ptr %9, align 8, !tbaa !9
  %60 = sub i64 %59, %58
  store i64 %60, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #6
  %61 = load i64, ptr %9, align 8, !tbaa !9
  %62 = load i32, ptr %8, align 4, !tbaa !11
  %63 = sext i32 %62 to i64
  %64 = urem i64 %61, %63
  %65 = trunc i64 %64 to i32
  store i32 %65, ptr %11, align 4, !tbaa !11
  %66 = load i32, ptr %11, align 4, !tbaa !11
  %67 = sext i32 %66 to i64
  %68 = load i64, ptr %9, align 8, !tbaa !9
  %69 = sub i64 %68, %67
  store i64 %69, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %70 = load i64, ptr %9, align 8, !tbaa !9
  %71 = load i32, ptr %8, align 4, !tbaa !11
  %72 = sext i32 %71 to i64
  %73 = udiv i64 %70, %72
  %74 = load i32, ptr %10, align 4, !tbaa !11
  %75 = icmp ne i32 %74, 0
  %76 = zext i1 %75 to i64
  %77 = select i1 %75, i32 1, i32 0
  %78 = sext i32 %77 to i64
  %79 = add i64 %73, %78
  %80 = load i32, ptr %11, align 4, !tbaa !11
  %81 = icmp ne i32 %80, 0
  %82 = zext i1 %81 to i64
  %83 = select i1 %81, i32 1, i32 0
  %84 = sext i32 %83 to i64
  %85 = add i64 %79, %84
  store i64 %85, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #6
  %86 = load i64, ptr %12, align 8, !tbaa !9
  %87 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %86, i64 4)
  %88 = extractvalue { i64, i1 } %87, 1
  %89 = extractvalue { i64, i1 } %87, 0
  %90 = select i1 %88, i64 -1, i64 %89
  %91 = call noalias noundef nonnull ptr @_Znam(i64 noundef %90) #7
  store ptr %91, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #6
  %92 = load i64, ptr %7, align 8, !tbaa !9
  %93 = load i32, ptr %8, align 4, !tbaa !11
  %94 = sext i32 %93 to i64
  %95 = udiv i64 %92, %94
  %96 = trunc i64 %95 to i32
  store i32 %96, ptr %14, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #6
  store i64 0, ptr %15, align 8, !tbaa !9
  br label %97

97:                                               ; preds = %108, %56
  %98 = load i64, ptr %15, align 8, !tbaa !9
  %99 = load i64, ptr %12, align 8, !tbaa !9
  %100 = icmp ult i64 %98, %99
  br i1 %100, label %102, label %101

101:                                              ; preds = %97
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #6
  br label %111

102:                                              ; preds = %97
  %103 = load i32, ptr %14, align 4, !tbaa !11
  %104 = add i32 %103, 1
  store i32 %104, ptr %14, align 4, !tbaa !11
  %105 = load ptr, ptr %13, align 8, !tbaa !5
  %106 = load i64, ptr %15, align 8, !tbaa !9
  %107 = getelementptr inbounds i32, ptr %105, i64 %106
  store i32 %103, ptr %107, align 4, !tbaa !11
  br label %108

108:                                              ; preds = %102
  %109 = load i64, ptr %15, align 8, !tbaa !9
  %110 = add i64 %109, 1
  store i64 %110, ptr %15, align 8, !tbaa !9
  br label %97, !llvm.loop !13

111:                                              ; preds = %101
  %112 = load ptr, ptr %5, align 8, !tbaa !5
  %113 = load ptr, ptr %13, align 8, !tbaa !5
  %114 = load i32, ptr %8, align 4, !tbaa !11
  %115 = load i32, ptr %10, align 4, !tbaa !11
  %116 = sub nsw i32 %114, %115
  %117 = sext i32 %116 to i64
  %118 = getelementptr inbounds i8, ptr %113, i64 %117
  %119 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %112, ptr align 1 %118, i64 %119, i1 false)
  %120 = load ptr, ptr %13, align 8, !tbaa !5
  %121 = icmp eq ptr %120, null
  br i1 %121, label %123, label %122

122:                                              ; preds = %111
  call void @_ZdaPv(ptr noundef %120) #8
  br label %123

123:                                              ; preds = %122, %111
  %124 = load i64, ptr %6, align 8, !tbaa !9
  %125 = load i64, ptr @test_total_read, align 8, !tbaa !9
  %126 = add i64 %125, %124
  store i64 %126, ptr @test_total_read, align 8, !tbaa !9
  %127 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %127, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  br label %128

128:                                              ; preds = %123, %19
  %129 = load i64, ptr %4, align 8
  ret i64 %129
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare { i64, i1 } @llvm.umul.with.overflow.i64(i64, i64) #2

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #3

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

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

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) #5

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nobuiltin allocsize(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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nobuiltin 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 #6 = { nounwind }
attributes #7 = { builtin allocsize(0) }
attributes #8 = { builtin 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !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"

@test_read_file_size = external dso_local local_unnamed_addr global i64, align 8
@test_max_buf_size = external dso_local local_unnamed_addr global i64, align 8
@test_total_read = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z17test_m5_read_filePvmm(ptr nocapture noundef writeonly %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = load i64, ptr @test_read_file_size, align 8, !tbaa !5
  %.not = icmp ugt i64 %4, %2
  br i1 %.not, label %5, label %42

5:                                                ; preds = %3
  %6 = add i64 %2, %1
  %7 = icmp ugt i64 %6, %4
  %8 = sub i64 %4, %2
  %spec.select = select i1 %7, i64 %8, i64 %1
  %9 = load i64, ptr @test_max_buf_size, align 8, !tbaa !5
  %.not46.not = icmp eq i64 %9, 0
  %10 = tail call i64 @llvm.umin.i64(i64 %spec.select, i64 %9)
  %.1 = select i1 %.not46.not, i64 %spec.select, i64 %10
  %11 = trunc i64 %2 to i32
  %12 = and i32 %11, 3
  %13 = sub nuw nsw i32 4, %12
  %14 = zext nneg i32 %13 to i64
  %15 = icmp ult i64 %.1, %14
  %16 = trunc nuw nsw i64 %.1 to i32
  %.042 = select i1 %15, i32 %16, i32 %13
  %17 = zext nneg i32 %.042 to i64
  %18 = sub i64 %.1, %17
  %19 = lshr i64 %18, 2
  %.not47 = icmp ne i32 %.042, 0
  %20 = zext i1 %.not47 to i64
  %21 = add nuw nsw i64 %19, %20
  %22 = and i64 %18, 3
  %.not48 = icmp ne i64 %22, 0
  %23 = zext i1 %.not48 to i64
  %24 = add nuw nsw i64 %21, %23
  %25 = icmp ugt i64 %24, 4611686018427387903
  %26 = shl i64 %24, 2
  %27 = select i1 %25, i64 -1, i64 %26
  %28 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %27) #5
  %.not51 = icmp eq i64 %24, 0
  br i1 %.not51, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %5
  %29 = lshr i64 %2, 2
  %30 = trunc i64 %29 to i32
  %min.iters.check = icmp ult i64 %24, 8
  br i1 %min.iters.check, label %.lr.ph.preheader54, label %vector.ph

.lr.ph.preheader54:                               ; preds = %middle.block, %.lr.ph.preheader
  %.050.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.03949.ph = phi i32 [ %30, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %24, 9223372036854775800
  %.cast = trunc i64 %n.vec to i32
  %ind.end = add i32 %30, %.cast
  %.splatinsert = insertelement <4 x i32> poison, i32 %30, i64 0
  %.splat = shufflevector <4 x i32> %.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %induction = add <4 x i32> %.splat, <i32 0, i32 1, i32 2, i32 3>
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <4 x i32> [ %induction, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %step.add = add <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
  %31 = getelementptr inbounds i32, ptr %28, i64 %index
  %32 = getelementptr inbounds i8, ptr %31, i64 16
  store <4 x i32> %vec.ind, ptr %31, align 4, !tbaa !9
  store <4 x i32> %step.add, ptr %32, align 4, !tbaa !9
  %index.next = add nuw i64 %index, 8
  %vec.ind.next = add <4 x i32> %vec.ind, <i32 8, i32 8, i32 8, i32 8>
  %33 = icmp eq i64 %index.next, %n.vec
  br i1 %33, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %24, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader54

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %5
  %34 = sub nuw nsw i32 4, %.042
  %35 = zext nneg i32 %34 to i64
  %36 = getelementptr inbounds i8, ptr %28, i64 %35
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %0, ptr nonnull align 1 %36, i64 %.1, i1 false)
  tail call void @_ZdaPv(ptr noundef nonnull %28) #6
  %37 = load i64, ptr @test_total_read, align 8, !tbaa !5
  %38 = add i64 %37, %.1
  store i64 %38, ptr @test_total_read, align 8, !tbaa !5
  br label %42

.lr.ph:                                           ; preds = %.lr.ph.preheader54, %.lr.ph
  %.050 = phi i64 [ %41, %.lr.ph ], [ %.050.ph, %.lr.ph.preheader54 ]
  %.03949 = phi i32 [ %39, %.lr.ph ], [ %.03949.ph, %.lr.ph.preheader54 ]
  %39 = add i32 %.03949, 1
  %40 = getelementptr inbounds i32, ptr %28, i64 %.050
  store i32 %.03949, ptr %40, align 4, !tbaa !9
  %41 = add nuw nsw i64 %.050, 1
  %exitcond.not = icmp eq i64 %41, %24
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15

42:                                               ; preds = %3, %._crit_edge
  %.040 = phi i64 [ %.1, %._crit_edge ], [ 0, %3 ]
  ret i64 %.040
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #1

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

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) local_unnamed_addr #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #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 = { nobuiltin allocsize(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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nobuiltin 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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { builtin allocsize(0) }
attributes #6 = { builtin 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12, !14, !13}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass