; LLVM IR from facebookresearch/habitat-sim :: src/deps/basis-universal/encoder/jpgd.ll (ZN4jpgd24jpeg_decoder_file_stream4readEPhiPb)

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

%"class.jpgd::jpeg_decoder_file_stream" = type <{ %"class.jpgd::jpeg_decoder_stream", ptr, i8, i8, [6 x i8] }>
%"class.jpgd::jpeg_decoder_stream" = type { ptr }

; 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: mustprogress uwtable
define dso_local noundef i32 @_ZN4jpgd24jpeg_decoder_file_stream4readEPhiPb(ptr noundef nonnull align 8 dereferenceable(18) %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) unnamed_addr #1 align 2 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  %12 = load ptr, ptr %6, align 8
  %13 = getelementptr inbounds %"class.jpgd::jpeg_decoder_file_stream", ptr %12, i32 0, i32 1
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  %15 = icmp ne ptr %14, null
  br i1 %15, label %17, label %16

16:                                               ; preds = %4
  store i32 -1, ptr %5, align 4
  br label %52

17:                                               ; preds = %4
  %18 = getelementptr inbounds %"class.jpgd::jpeg_decoder_file_stream", ptr %12, i32 0, i32 2
  %19 = load i8, ptr %18, align 8, !tbaa !15, !range !16, !noundef !17
  %20 = trunc i8 %19 to i1
  br i1 %20, label %21, label %23

21:                                               ; preds = %17
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  store i8 1, ptr %22, align 1, !tbaa !18
  store i32 0, ptr %5, align 4
  br label %52

23:                                               ; preds = %17
  %24 = getelementptr inbounds %"class.jpgd::jpeg_decoder_file_stream", ptr %12, i32 0, i32 3
  %25 = load i8, ptr %24, align 1, !tbaa !19, !range !16, !noundef !17
  %26 = trunc i8 %25 to i1
  br i1 %26, label %27, label %28

27:                                               ; preds = %23
  store i32 -1, ptr %5, align 4
  br label %52

28:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds %"class.jpgd::jpeg_decoder_file_stream", ptr %12, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !11
  %34 = call i64 @fread(ptr noundef %29, i64 noundef 1, i64 noundef %31, ptr noundef %33)
  %35 = trunc i64 %34 to i32
  store i32 %35, ptr %10, align 4, !tbaa !9
  %36 = load i32, ptr %10, align 4, !tbaa !9
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = icmp slt i32 %36, %37
  br i1 %38, label %39, label %49

39:                                               ; preds = %28
  %40 = getelementptr inbounds %"class.jpgd::jpeg_decoder_file_stream", ptr %12, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  %42 = call i32 @ferror(ptr noundef %41) #4
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %46

44:                                               ; preds = %39
  %45 = getelementptr inbounds %"class.jpgd::jpeg_decoder_file_stream", ptr %12, i32 0, i32 3
  store i8 1, ptr %45, align 1, !tbaa !19
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %51

46:                                               ; preds = %39
  %47 = getelementptr inbounds %"class.jpgd::jpeg_decoder_file_stream", ptr %12, i32 0, i32 2
  store i8 1, ptr %47, align 8, !tbaa !15
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  store i8 1, ptr %48, align 1, !tbaa !18
  br label %49

49:                                               ; preds = %46, %28
  %50 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %50, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %51

51:                                               ; preds = %49, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %52

52:                                               ; preds = %51, %27, %21, %16
  %53 = load i32, ptr %5, align 4
  ret i32 %53
}

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

; Function Attrs: nounwind
declare i32 @ferror(ptr noundef) #3

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

!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 = !{!12, !6, i64 8}
!12 = !{!"_ZTSN4jpgd24jpeg_decoder_file_streamE", !13, i64 0, !6, i64 8, !14, i64 16, !14, i64 17}
!13 = !{!"_ZTSN4jpgd19jpeg_decoder_streamE"}
!14 = !{!"bool", !7, i64 0}
!15 = !{!12, !14, i64 16}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = !{!14, !14, i64 0}
!19 = !{!12, !14, i64 17}


; 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: mustprogress nofree nounwind uwtable
define dso_local noundef i32 @_ZN4jpgd24jpeg_decoder_file_stream4readEPhiPb(ptr nocapture noundef nonnull align 8 dereferenceable(18) %0, ptr nocapture noundef %1, i32 noundef %2, ptr nocapture noundef writeonly %3) unnamed_addr #0 align 2 {
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %24, label %7

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load i8, ptr %8, align 8, !tbaa !12, !range !13, !noundef !14
  %10 = trunc nuw i8 %9 to i1
  br i1 %10, label %.sink.split, label %11

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, ptr %0, i64 17
  %13 = load i8, ptr %12, align 1, !tbaa !15, !range !13, !noundef !14
  %14 = trunc nuw i8 %13 to i1
  br i1 %14, label %24, label %15

15:                                               ; preds = %11
  %16 = sext i32 %2 to i64
  %17 = tail call i64 @fread(ptr noundef %1, i64 noundef 1, i64 noundef %16, ptr noundef nonnull %6)
  %18 = trunc i64 %17 to i32
  %19 = icmp slt i32 %18, %2
  br i1 %19, label %20, label %24

20:                                               ; preds = %15
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = tail call i32 @ferror(ptr noundef %21) #3
  %.not9 = icmp eq i32 %22, 0
  br i1 %.not9, label %23, label %.sink.split

23:                                               ; preds = %20
  store i8 1, ptr %8, align 8, !tbaa !12
  br label %.sink.split

.sink.split:                                      ; preds = %20, %7, %23
  %.sink = phi ptr [ %3, %23 ], [ %3, %7 ], [ %12, %20 ]
  %.1.ph = phi i32 [ %18, %23 ], [ 0, %7 ], [ -1, %20 ]
  store i8 1, ptr %.sink, align 1, !tbaa !16
  br label %24

24:                                               ; preds = %.sink.split, %15, %11, %4
  %.1 = phi i32 [ -1, %4 ], [ -1, %11 ], [ %18, %15 ], [ %.1.ph, %.sink.split ]
  ret i32 %.1
}

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

; Function Attrs: nofree nounwind memory(read)
declare noundef i32 @ferror(ptr nocapture noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 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 #2 = { nofree nounwind memory(read) "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, !8, i64 8}
!6 = !{!"_ZTSN4jpgd24jpeg_decoder_file_streamE", !7, i64 0, !8, i64 8, !11, i64 16, !11, i64 17}
!7 = !{!"_ZTSN4jpgd19jpeg_decoder_streamE"}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"bool", !9, i64 0}
!12 = !{!6, !11, i64 16}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!6, !11, i64 17}
!16 = !{!11, !11, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass