; LLVM IR from ebiggers/libdeflate :: programs (prog_util)

; 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_stream = type { i32, ptr, i8, ptr, ptr, i64 }

@.str.1 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [26 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @msg_errno(ptr noundef, ...) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @xopen_for_read(ptr noundef %0, i1 noundef zeroext %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  %8 = zext i1 %1 to i8
  store i8 %8, ptr %6, align 1, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %9 = load ptr, ptr %7, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.file_stream, ptr %9, i32 0, i32 3
  store ptr null, ptr %10, align 8, !tbaa !11
  %11 = load ptr, ptr %7, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.file_stream, ptr %11, i32 0, i32 4
  store ptr null, ptr %12, align 8, !tbaa !15
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %22

15:                                               ; preds = %3
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.file_stream, ptr %16, i32 0, i32 2
  store i8 1, ptr %17, align 8, !tbaa !16
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.file_stream, ptr %18, i32 0, i32 1
  store ptr @.str.1, ptr %19, align 8, !tbaa !17
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.file_stream, ptr %20, i32 0, i32 0
  store i32 0, ptr %21, align 8, !tbaa !18
  store i32 0, ptr %4, align 4
  br label %61

22:                                               ; preds = %3
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.file_stream, ptr %23, i32 0, i32 2
  store i8 0, ptr %24, align 8, !tbaa !16
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = call ptr @quote_path(ptr noundef %25)
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.file_stream, ptr %27, i32 0, i32 1
  store ptr %26, ptr %28, align 8, !tbaa !17
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.file_stream, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !17
  %32 = icmp eq ptr %31, null
  br i1 %32, label %33, label %34

33:                                               ; preds = %22
  store i32 -1, ptr %4, align 4
  br label %61

34:                                               ; preds = %22
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = load i8, ptr %6, align 1, !tbaa !9, !range !19, !noundef !20
  %37 = trunc i8 %36 to i1
  %38 = zext i1 %37 to i64
  %39 = select i1 %37, i32 0, i32 131072
  %40 = or i32 2304, %39
  %41 = or i32 %40, 0
  %42 = call i32 (ptr, i32, ...) @open64(ptr noundef %35, i32 noundef %41)
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.file_stream, ptr %43, i32 0, i32 0
  store i32 %42, ptr %44, align 8, !tbaa !18
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.file_stream, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %46, align 8, !tbaa !18
  %48 = icmp slt i32 %47, 0
  br i1 %48, label %49, label %56

49:                                               ; preds = %34
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.file_stream, ptr %50, i32 0, i32 1
  %52 = load ptr, ptr %51, align 8, !tbaa !17
  call void (ptr, ...) @msg_errno(ptr noundef @.str.2, ptr noundef %52)
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.file_stream, ptr %53, i32 0, i32 1
  %55 = load ptr, ptr %54, align 8, !tbaa !17
  call void @free(ptr noundef %55) #3
  store i32 -1, ptr %4, align 4
  br label %61

56:                                               ; preds = %34
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.file_stream, ptr %57, i32 0, i32 0
  %59 = load i32, ptr %58, align 8, !tbaa !18
  %60 = call i32 @posix_fadvise64(i32 noundef %59, i64 noundef 0, i64 noundef 0, i32 noundef 2) #3
  store i32 0, ptr %4, align 4
  br label %61

61:                                               ; preds = %56, %49, %33, %15
  %62 = load i32, ptr %4, align 4
  ret i32 %62
}

; Function Attrs: nounwind uwtable
declare hidden ptr @quote_path(ptr noundef) #0

declare i32 @open64(ptr noundef, i32 noundef, ...) #1

; Function Attrs: nounwind
declare void @free(ptr noundef) #2

; Function Attrs: nounwind
declare i32 @posix_fadvise64(i32 noundef, i64 noundef, i64 noundef, i32 noundef) #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 = { 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 = { 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 = !{!"_Bool", !7, i64 0}
!11 = !{!12, !6, i64 24}
!12 = !{!"file_stream", !13, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !6, i64 32, !14, i64 40}
!13 = !{!"int", !7, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!12, !6, i64 32}
!16 = !{!12, !10, i64 16}
!17 = !{!12, !6, i64 8}
!18 = !{!12, !13, i64 0}
!19 = !{i8 0, i8 2}
!20 = !{}


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

@.str.1 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [26 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @msg_errno(ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @xopen_for_read(ptr noundef %0, i1 noundef zeroext %1, ptr nocapture noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %2, i64 24
  %5 = icmp eq ptr %0, null
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %4, i8 0, i64 16, i1 false)
  %6 = getelementptr inbounds i8, ptr %2, i64 16
  br i1 %5, label %7, label %9

7:                                                ; preds = %3
  store i8 1, ptr %6, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %2, i64 8
  store ptr @.str.1, ptr %8, align 8, !tbaa !13
  store i32 0, ptr %2, align 8, !tbaa !14
  br label %22

9:                                                ; preds = %3
  store i8 0, ptr %6, align 8, !tbaa !5
  %10 = tail call ptr @quote_path(ptr noundef nonnull %0)
  %11 = getelementptr inbounds i8, ptr %2, i64 8
  store ptr %10, ptr %11, align 8, !tbaa !13
  %12 = icmp eq ptr %10, null
  br i1 %12, label %22, label %13

13:                                               ; preds = %9
  %14 = select i1 %1, i32 2304, i32 133376
  %15 = tail call i32 (ptr, i32, ...) @open64(ptr noundef nonnull %0, i32 noundef %14) #5
  store i32 %15, ptr %2, align 8, !tbaa !14
  %16 = icmp slt i32 %15, 0
  br i1 %16, label %17, label %20

17:                                               ; preds = %13
  %18 = load ptr, ptr %11, align 8, !tbaa !13
  tail call void (ptr, ...) @msg_errno(ptr noundef nonnull @.str.2, ptr noundef %18)
  %19 = load ptr, ptr %11, align 8, !tbaa !13
  tail call void @free(ptr noundef %19) #5
  br label %22

20:                                               ; preds = %13
  %21 = tail call i32 @posix_fadvise64(i32 noundef %15, i64 noundef 0, i64 noundef 0, i32 noundef 2) #5
  br label %22

22:                                               ; preds = %9, %20, %17, %7
  %.0 = phi i32 [ 0, %7 ], [ -1, %17 ], [ 0, %20 ], [ -1, %9 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden ptr @quote_path(ptr noundef) local_unnamed_addr #0

; Function Attrs: nofree
declare noundef i32 @open64(ptr nocapture noundef readonly, i32 noundef, ...) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @posix_fadvise64(i32 noundef, i64 noundef, i64 noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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 = { 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 nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { 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, !11, i64 16}
!6 = !{!"file_stream", !7, i64 0, !10, i64 8, !11, i64 16, !10, i64 24, !10, i64 32, !12, i64 40}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!6, !7, i64 0}


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