; LLVM IR from kkumatani/distant_speech_recognition :: btk20_src/common/mach_ind_io.ll (Z9read_intsP8_IO_FILEPii)

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

%union.unionT = type { float }

@_ZL7machine = external hidden global i32, align 4
@.str.3 = external hidden unnamed_addr constant [32 x i8], align 1
@_ZTI9jio_error = external dso_local constant { ptr, ptr, ptr }, align 8

; 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

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #1

declare ptr @__cxa_allocate_exception(i64)

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9jio_errorC2EPKcz(ptr noundef nonnull align 8 dereferenceable(44), ptr noundef, ...) unnamed_addr #2 align 2

declare i32 @__gxx_personality_v0(...)

declare void @__cxa_free_exception(ptr)

; Function Attrs: nounwind
declare void @_ZN7j_errorD2Ev(ptr noundef nonnull align 8 dereferenceable(44)) unnamed_addr #3

declare void @__cxa_throw(ptr, ptr, ptr)

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z9read_intsP8_IO_FILEPii(ptr noundef %0, ptr noundef %1, i32 noundef %2) #2 personality ptr @__gxx_personality_v0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca %union.unionT, align 4
  %8 = alloca %union.unionT, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %14, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = call i64 @fread(ptr noundef %15, i64 noundef 4, i64 noundef %17, ptr noundef %18)
  %20 = trunc i64 %19 to i32
  store i32 %20, ptr %11, align 4, !tbaa !9
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %60, %3
  %22 = load i32, ptr %10, align 4, !tbaa !9
  %23 = load i32, ptr %11, align 4, !tbaa !9
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %25, label %63

25:                                               ; preds = %21
  %26 = load ptr, ptr %9, align 8, !tbaa !5
  %27 = load i32, ptr %26, align 4, !tbaa !9
  store i32 %27, ptr %8, align 4, !tbaa !11
  %28 = load i32, ptr @_ZL7machine, align 4, !tbaa !9
  switch i32 %28, label %47 [
    i32 1, label %29
    i32 2, label %31
    i32 3, label %31
  ]

29:                                               ; preds = %25
  %30 = load i32, ptr %8, align 4, !tbaa !11
  store i32 %30, ptr %7, align 4, !tbaa !11
  br label %55

31:                                               ; preds = %25, %25
  %32 = load i32, ptr %8, align 4, !tbaa !11
  %33 = shl i32 %32, 24
  %34 = and i32 %33, -16777216
  %35 = load i32, ptr %8, align 4, !tbaa !11
  %36 = shl i32 %35, 8
  %37 = and i32 %36, 16711680
  %38 = or i32 %34, %37
  %39 = load i32, ptr %8, align 4, !tbaa !11
  %40 = ashr i32 %39, 8
  %41 = and i32 %40, 65280
  %42 = or i32 %38, %41
  %43 = load i32, ptr %8, align 4, !tbaa !11
  %44 = ashr i32 %43, 24
  %45 = and i32 %44, 255
  %46 = or i32 %42, %45
  store i32 %46, ptr %7, align 4, !tbaa !11
  br label %55

47:                                               ; preds = %25
  %48 = call ptr @__cxa_allocate_exception(i64 48) #4
  %49 = load i32, ptr @_ZL7machine, align 4, !tbaa !9
  invoke void (ptr, ptr, ...) @_ZN9jio_errorC2EPKcz(ptr noundef nonnull align 8 dereferenceable(44) %48, ptr noundef @.str.3, i32 noundef %49)
          to label %50 unwind label %51

50:                                               ; preds = %47
  call void @__cxa_throw(ptr %48, ptr @_ZTI9jio_error, ptr @_ZN7j_errorD2Ev) #5
  unreachable

51:                                               ; preds = %47
  %52 = landingpad { ptr, i32 }
          cleanup
  %53 = extractvalue { ptr, i32 } %52, 0
  store ptr %53, ptr %12, align 8
  %54 = extractvalue { ptr, i32 } %52, 1
  store i32 %54, ptr %13, align 4
  call void @__cxa_free_exception(ptr %48) #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 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %65

55:                                               ; preds = %31, %29
  %56 = load i32, ptr %7, align 4, !tbaa !11
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %56, ptr %57, align 4, !tbaa !9
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = getelementptr inbounds i32, ptr %58, i32 1
  store ptr %59, ptr %9, align 8, !tbaa !5
  br label %60

60:                                               ; preds = %55
  %61 = load i32, ptr %10, align 4, !tbaa !9
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %10, align 4, !tbaa !9
  br label %21, !llvm.loop !12

63:                                               ; preds = %21
  %64 = load i32, ptr %11, align 4, !tbaa !9
  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 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  ret i32 %64

65:                                               ; preds = %51
  %66 = load ptr, ptr %12, align 8
  %67 = load i32, ptr %13, align 4
  %68 = insertvalue { ptr, i32 } poison, ptr %66, 0
  %69 = insertvalue { ptr, i32 } %68, i32 %67, 1
  resume { ptr, i32 } %69
}

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

!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 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@_ZL7machine = external hidden local_unnamed_addr global i32, align 4
@.str.3 = external hidden unnamed_addr constant [32 x i8], align 1
@_ZTI9jio_error = external dso_local constant { ptr, ptr, ptr }, align 8

; Function Attrs: nofree nounwind
declare noundef i64 @fread(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #0

declare ptr @__cxa_allocate_exception(i64) local_unnamed_addr

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9jio_errorC2EPKcz(ptr noundef nonnull align 8 dereferenceable(44), ptr noundef, ...) unnamed_addr #1 align 2

declare i32 @__gxx_personality_v0(...)

declare void @__cxa_free_exception(ptr) local_unnamed_addr

; Function Attrs: nounwind
declare void @_ZN7j_errorD2Ev(ptr noundef nonnull align 8 dereferenceable(44)) unnamed_addr #2

declare void @__cxa_throw(ptr, ptr, ptr) local_unnamed_addr

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z9read_intsP8_IO_FILEPii(ptr nocapture noundef %0, ptr nocapture noundef %1, i32 noundef %2) local_unnamed_addr #1 personality ptr @__gxx_personality_v0 {
  %4 = sext i32 %2 to i64
  %5 = tail call i64 @fread(ptr noundef %1, i64 noundef 4, i64 noundef %4, ptr noundef %0)
  %6 = trunc i64 %5 to i32
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %18
  %.020 = phi ptr [ %19, %18 ], [ %1, %3 ]
  %.01719 = phi i32 [ %20, %18 ], [ 0, %3 ]
  %8 = load i32, ptr %.020, align 4, !tbaa !5
  %9 = load i32, ptr @_ZL7machine, align 4, !tbaa !5
  switch i32 %9, label %12 [
    i32 1, label %18
    i32 2, label %10
    i32 3, label %10
  ]

10:                                               ; preds = %.lr.ph, %.lr.ph
  %11 = tail call i32 @llvm.bswap.i32(i32 %8)
  br label %18

12:                                               ; preds = %.lr.ph
  %13 = tail call ptr @__cxa_allocate_exception(i64 48) #4
  %14 = load i32, ptr @_ZL7machine, align 4, !tbaa !5
  invoke void (ptr, ptr, ...) @_ZN9jio_errorC2EPKcz(ptr noundef nonnull align 8 dereferenceable(44) %13, ptr noundef nonnull @.str.3, i32 noundef %14)
          to label %15 unwind label %16

15:                                               ; preds = %12
  tail call void @__cxa_throw(ptr nonnull %13, ptr nonnull @_ZTI9jio_error, ptr nonnull @_ZN7j_errorD2Ev) #5
  unreachable

16:                                               ; preds = %12
  %17 = landingpad { ptr, i32 }
          cleanup
  tail call void @__cxa_free_exception(ptr %13) #4
  resume { ptr, i32 } %17

18:                                               ; preds = %.lr.ph, %10
  %.sroa.012.0 = phi i32 [ %11, %10 ], [ %8, %.lr.ph ]
  store i32 %.sroa.012.0, ptr %.020, align 4, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %.020, i64 4
  %20 = add nuw nsw i32 %.01719, 1
  %exitcond.not = icmp eq i32 %20, %6
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %18, %3
  ret i32 %6
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.bswap.i32(i32) #3

attributes #0 = { nofree 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 #1 = { 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 #2 = { 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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind }
attributes #5 = { noreturn }

!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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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