; LLVM IR from attractivechaos/klib :: bgzf (bgzf_dopen)

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

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

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

; Function Attrs: nounwind willreturn memory(read)
declare ptr @strchr(ptr noundef, i32 noundef) #1

; 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
declare hidden ptr @bgzf_write_init(i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @mode2level(ptr noalias noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @bgzf_dopen(i32 noundef %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store ptr null, ptr %6, align 8, !tbaa !9
  %10 = load ptr, ptr %5, align 8, !tbaa !9
  %11 = call ptr @strchr(ptr noundef %10, i32 noundef 114) #5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %17, label %13

13:                                               ; preds = %2
  %14 = load ptr, ptr %5, align 8, !tbaa !9
  %15 = call ptr @strchr(ptr noundef %14, i32 noundef 82) #5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %30

17:                                               ; preds = %13, %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = call noalias ptr @fdopen(i32 noundef %18, ptr noundef @.str) #4
  store ptr %19, ptr %7, align 8, !tbaa !9
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %22

21:                                               ; preds = %17
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %27

22:                                               ; preds = %17
  %23 = call ptr @bgzf_read_init()
  store ptr %23, ptr %6, align 8, !tbaa !9
  %24 = load ptr, ptr %7, align 8, !tbaa !9
  %25 = load ptr, ptr %6, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.BGZF, ptr %25, i32 0, i32 8
  store ptr %24, ptr %26, align 8, !tbaa !11
  store i32 0, ptr %8, align 4
  br label %27

27:                                               ; preds = %22, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  %28 = load i32, ptr %8, align 4
  switch i32 %28, label %56 [
    i32 0, label %29
  ]

29:                                               ; preds = %27
  br label %54

30:                                               ; preds = %13
  %31 = load ptr, ptr %5, align 8, !tbaa !9
  %32 = call ptr @strchr(ptr noundef %31, i32 noundef 119) #5
  %33 = icmp ne ptr %32, null
  br i1 %33, label %38, label %34

34:                                               ; preds = %30
  %35 = load ptr, ptr %5, align 8, !tbaa !9
  %36 = call ptr @strchr(ptr noundef %35, i32 noundef 87) #5
  %37 = icmp ne ptr %36, null
  br i1 %37, label %38, label %53

38:                                               ; preds = %34, %30
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = call noalias ptr @fdopen(i32 noundef %39, ptr noundef @.str.1) #4
  store ptr %40, ptr %9, align 8, !tbaa !9
  %41 = icmp eq ptr %40, null
  br i1 %41, label %42, label %43

42:                                               ; preds = %38
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %50

43:                                               ; preds = %38
  %44 = load ptr, ptr %5, align 8, !tbaa !9
  %45 = call i32 @mode2level(ptr noundef %44)
  %46 = call ptr @bgzf_write_init(i32 noundef %45)
  store ptr %46, ptr %6, align 8, !tbaa !9
  %47 = load ptr, ptr %9, align 8, !tbaa !9
  %48 = load ptr, ptr %6, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.BGZF, ptr %48, i32 0, i32 8
  store ptr %47, ptr %49, align 8, !tbaa !11
  store i32 0, ptr %8, align 4
  br label %50

50:                                               ; preds = %43, %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  %51 = load i32, ptr %8, align 4
  switch i32 %51, label %56 [
    i32 0, label %52
  ]

52:                                               ; preds = %50
  br label %53

53:                                               ; preds = %52, %34
  br label %54

54:                                               ; preds = %53, %29
  %55 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %55, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %56

56:                                               ; preds = %54, %50, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %57 = load ptr, ptr %3, align 8
  ret ptr %57
}

; Function Attrs: nounwind
declare noalias ptr @fdopen(i32 noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden ptr @bgzf_read_init() #2

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

!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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 48}
!12 = !{!"", !6, i64 0, !6, i64 1, !6, i64 2, !6, i64 4, !6, i64 8, !6, i64 12, !13, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!13 = !{!"long", !7, i64 0}


; 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 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare ptr @strchr(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @bgzf_write_init(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @mode2level(ptr noalias noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @bgzf_dopen(i32 noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = tail call ptr @strchr(ptr noundef nonnull dereferenceable(1) %1, i32 noundef 114) #3
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %4, label %6

4:                                                ; preds = %2
  %5 = tail call ptr @strchr(ptr noundef nonnull dereferenceable(1) %1, i32 noundef 82) #3
  %.not21 = icmp eq ptr %5, null
  br i1 %.not21, label %10, label %6

6:                                                ; preds = %4, %2
  %7 = tail call noalias ptr @fdopen(i32 noundef %0, ptr noundef nonnull @.str) #4
  %.not25 = icmp eq ptr %7, null
  br i1 %.not25, label %.thread, label %8

8:                                                ; preds = %6
  %9 = tail call ptr @bgzf_read_init()
  br label %.thread.sink.split

10:                                               ; preds = %4
  %11 = tail call ptr @strchr(ptr noundef nonnull dereferenceable(1) %1, i32 noundef 119) #3
  %.not22 = icmp eq ptr %11, null
  br i1 %.not22, label %12, label %14

12:                                               ; preds = %10
  %13 = tail call ptr @strchr(ptr noundef nonnull dereferenceable(1) %1, i32 noundef 87) #3
  %.not23 = icmp eq ptr %13, null
  br i1 %.not23, label %.thread, label %14

14:                                               ; preds = %12, %10
  %15 = tail call noalias ptr @fdopen(i32 noundef %0, ptr noundef nonnull @.str.1) #4
  %.not24 = icmp eq ptr %15, null
  br i1 %.not24, label %.thread, label %16

16:                                               ; preds = %14
  %17 = tail call i32 @mode2level(ptr noundef %1)
  %18 = tail call ptr @bgzf_write_init(i32 noundef %17)
  br label %.thread.sink.split

.thread.sink.split:                               ; preds = %16, %8
  %.sink30 = phi ptr [ %9, %8 ], [ %18, %16 ]
  %.sink = phi ptr [ %7, %8 ], [ %15, %16 ]
  %19 = getelementptr inbounds i8, ptr %.sink30, i64 48
  store ptr %.sink, ptr %19, align 8, !tbaa !5
  br label %.thread

.thread:                                          ; preds = %.thread.sink.split, %14, %6, %12
  %.2 = phi ptr [ null, %12 ], [ null, %6 ], [ null, %14 ], [ %.sink30, %.thread.sink.split ]
  ret ptr %.2
}

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fdopen(i32 noundef, ptr nocapture noundef readonly) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden ptr @bgzf_read_init() local_unnamed_addr #1

attributes #0 = { mustprogress nofree nounwind willreturn memory(argmem: 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 #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 = { 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 #3 = { nounwind willreturn memory(read) }
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, !11, i64 48}
!6 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 4, !7, i64 8, !7, i64 12, !10, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}


; Effective O3 passes: GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass