; LLVM IR from tenok-rtos/tenok :: kernel/fs/wrapper.ll (fread)

; 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.__FILE = type { i32 }

; 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 uwtable
define dso_local i64 @_fread(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  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
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %17, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %18 = load i64, ptr %6, align 8, !tbaa !9
  %19 = load i64, ptr %7, align 8, !tbaa !9
  %20 = mul i64 %18, %19
  store i64 %20, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %21 = load i64, ptr %10, align 8, !tbaa !9
  %22 = sub i64 %21, 1
  %23 = udiv i64 %22, 100
  %24 = add i64 %23, 1
  %25 = trunc i64 %24 to i32
  store i32 %25, ptr %11, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i32 0, ptr %14, align 4, !tbaa !11
  br label %26

26:                                               ; preds = %67, %4
  %27 = load i32, ptr %14, align 4, !tbaa !11
  %28 = load i32, ptr %11, align 4, !tbaa !11
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  store i32 2, ptr %15, align 4
  br label %70

31:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  %32 = load i64, ptr %10, align 8, !tbaa !9
  %33 = icmp uge i64 %32, 100
  br i1 %33, label %34, label %35

34:                                               ; preds = %31
  br label %37

35:                                               ; preds = %31
  %36 = load i64, ptr %10, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %35, %34
  %38 = phi i64 [ 100, %34 ], [ %36, %35 ]
  %39 = trunc i64 %38 to i32
  store i32 %39, ptr %16, align 4, !tbaa !11
  %40 = load ptr, ptr %9, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.__FILE, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 4, !tbaa !13
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = ptrtoint ptr %43 to i64
  %45 = load i32, ptr %12, align 4, !tbaa !11
  %46 = sext i32 %45 to i64
  %47 = add i64 %44, %46
  %48 = inttoptr i64 %47 to ptr
  %49 = load i32, ptr %16, align 4, !tbaa !11
  %50 = sext i32 %49 to i64
  %51 = call i32 @read(i32 noundef %42, ptr noundef %48, i64 noundef %50)
  store i32 %51, ptr %13, align 4, !tbaa !11
  %52 = load i32, ptr %16, align 4, !tbaa !11
  %53 = load i32, ptr %12, align 4, !tbaa !11
  %54 = add nsw i32 %53, %52
  store i32 %54, ptr %12, align 4, !tbaa !11
  %55 = load i32, ptr %16, align 4, !tbaa !11
  %56 = sext i32 %55 to i64
  %57 = load i64, ptr %10, align 8, !tbaa !9
  %58 = sub i64 %57, %56
  store i64 %58, ptr %10, align 8, !tbaa !9
  %59 = load i32, ptr %13, align 4, !tbaa !11
  %60 = load i32, ptr %16, align 4, !tbaa !11
  %61 = icmp ne i32 %59, %60
  br i1 %61, label %62, label %63

62:                                               ; preds = %37
  store i32 2, ptr %15, align 4
  br label %64

63:                                               ; preds = %37
  store i32 0, ptr %15, align 4
  br label %64

64:                                               ; preds = %63, %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  %65 = load i32, ptr %15, align 4
  switch i32 %65, label %70 [
    i32 0, label %66
  ]

66:                                               ; preds = %64
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %14, align 4, !tbaa !11
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %14, align 4, !tbaa !11
  br label %26, !llvm.loop !15

70:                                               ; preds = %64, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  br label %71

71:                                               ; preds = %70
  %72 = load i32, ptr %12, align 4, !tbaa !11
  %73 = sext i32 %72 to i64
  store i32 1, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  ret i64 %73
}

declare i32 @read(i32 noundef, ptr noundef, i64 noundef) #2

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

!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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"", !12, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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: nounwind uwtable
define dso_local range(i64 -2147483648, 2147483648) i64 @_fread(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = mul i64 %2, %1
  %6 = add i64 %5, -1
  %7 = udiv i64 %6, 100
  %8 = trunc i64 %7 to i32
  %9 = icmp ult i32 %8, 2147483647
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %4
  %10 = ptrtoint ptr %0 to i64
  br label %11

11:                                               ; preds = %.lr.ph, %20
  %.025 = phi i64 [ %5, %.lr.ph ], [ %21, %20 ]
  %.02124 = phi i32 [ 0, %.lr.ph ], [ %22, %20 ]
  %.02223 = phi i32 [ 0, %.lr.ph ], [ %19, %20 ]
  %12 = tail call i64 @llvm.umin.i64(i64 %.025, i64 100)
  %13 = trunc nuw nsw i64 %12 to i32
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = zext nneg i32 %.02223 to i64
  %16 = add i64 %15, %10
  %17 = inttoptr i64 %16 to ptr
  %18 = tail call i32 @read(i32 noundef %14, ptr noundef %17, i64 noundef %12) #3
  %19 = add nuw nsw i32 %.02223, %13
  %.not = icmp eq i32 %18, %13
  br i1 %.not, label %20, label %._crit_edge.loopexit

20:                                               ; preds = %11
  %21 = sub i64 %.025, %12
  %22 = add nuw nsw i32 %.02124, 1
  %exitcond.not = icmp eq i32 %.02124, %8
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %11, !llvm.loop !10

._crit_edge.loopexit:                             ; preds = %20, %11
  %23 = zext nneg i32 %19 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %4
  %.1 = phi i64 [ 0, %4 ], [ %23, %._crit_edge.loopexit ]
  ret i64 %.1
}

declare i32 @read(i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #2

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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"", !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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