; LLVM IR from cruppstahl/upscaledb :: src/1os/os_posix.ll (ZN9upscaledb4File4openEPKcb)

; 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.upscaledb::File" = type { i32, i32, i32 }

@.str.2 = external hidden unnamed_addr constant [74 x i8], align 1
@_ZTIN9upscaledb9ExceptionE = external dso_local constant { ptr, ptr }, align 8
@__FUNCTION__._ZN9upscaledb4File4openEPKcb = external hidden unnamed_addr constant [5 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [43 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare i32 @__gxx_personality_v0(...)

declare ptr @__cxa_allocate_exception(i64)

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN9upscaledb9ExceptionC2Ei(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) unnamed_addr #1 align 2

declare void @__cxa_free_exception(ptr)

declare void @__cxa_throw(ptr, ptr, ptr)

declare void @_ZN9upscaledb11dbg_prepareEiPKciS1_S1_(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) #2

declare void @_ZN9upscaledb7dbg_logEPKcz(ptr noundef, ...) #2

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #3

; Function Attrs: nounwind
declare ptr @strerror(i32 noundef) #4

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

; Function Attrs: mustprogress uwtable
declare hidden void @_ZN9upscaledbL14lock_exclusiveEib(i32 noundef, i1 noundef zeroext) #5

; Function Attrs: mustprogress nounwind uwtable
declare hidden void @_ZN9upscaledbL16enable_largefileEi(i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN9upscaledb4File4openEPKcb(ptr noundef nonnull align 4 dereferenceable(12) %0, ptr noundef %1, i1 noundef zeroext %2) #5 align 2 personality ptr @__gxx_personality_v0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %11 = zext i1 %2 to i8
  store i8 %11, ptr %6, align 1, !tbaa !9
  %12 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  store i32 0, ptr %7, align 4, !tbaa !11
  %13 = load i8, ptr %6, align 1, !tbaa !9, !range !13, !noundef !14
  %14 = trunc i8 %13 to i1
  br i1 %14, label %15, label %18

15:                                               ; preds = %3
  %16 = load i32, ptr %7, align 4, !tbaa !11
  %17 = or i32 %16, 0
  store i32 %17, ptr %7, align 4, !tbaa !11
  br label %21

18:                                               ; preds = %3
  %19 = load i32, ptr %7, align 4, !tbaa !11
  %20 = or i32 %19, 2
  store i32 %20, ptr %7, align 4, !tbaa !11
  br label %21

21:                                               ; preds = %18, %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #6
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = load i32, ptr %7, align 4, !tbaa !11
  %24 = call i32 (ptr, i32, ...) @open64(ptr noundef %22, i32 noundef %23)
  store i32 %24, ptr %8, align 4, !tbaa !11
  %25 = load i32, ptr %8, align 4, !tbaa !11
  %26 = icmp slt i32 %25, 0
  br i1 %26, label %27, label %48

27:                                               ; preds = %21
  br label %28

28:                                               ; preds = %27
  call void @_ZN9upscaledb11dbg_prepareEiPKciS1_S1_(i32 noundef 1, ptr noundef @.str.2, i32 noundef 382, ptr noundef @__FUNCTION__._ZN9upscaledb4File4openEPKcb, ptr noundef null)
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = call ptr @__errno_location() #7
  %31 = load i32, ptr %30, align 4, !tbaa !11
  %32 = call ptr @__errno_location() #7
  %33 = load i32, ptr %32, align 4, !tbaa !11
  %34 = call ptr @strerror(i32 noundef %33) #6
  call void (ptr, ...) @_ZN9upscaledb7dbg_logEPKcz(ptr noundef @.str.10, ptr noundef %29, i32 noundef %31, ptr noundef %34)
  br label %35

35:                                               ; preds = %28
  br label %36

36:                                               ; preds = %35
  %37 = call ptr @__cxa_allocate_exception(i64 4) #6
  %38 = call ptr @__errno_location() #7
  %39 = load i32, ptr %38, align 4, !tbaa !11
  %40 = icmp eq i32 %39, 2
  %41 = zext i1 %40 to i64
  %42 = select i1 %40, i32 -21, i32 -18
  invoke void @_ZN9upscaledb9ExceptionC2Ei(ptr noundef nonnull align 4 dereferenceable(4) %37, i32 noundef %42)
          to label %43 unwind label %44

43:                                               ; preds = %36
  call void @__cxa_throw(ptr %37, ptr @_ZTIN9upscaledb9ExceptionE, ptr null) #8
  unreachable

44:                                               ; preds = %36
  %45 = landingpad { ptr, i32 }
          cleanup
  %46 = extractvalue { ptr, i32 } %45, 0
  store ptr %46, ptr %9, align 8
  %47 = extractvalue { ptr, i32 } %45, 1
  store i32 %47, ptr %10, align 4
  call void @__cxa_free_exception(ptr %37) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  br label %53

48:                                               ; preds = %21
  %49 = load i32, ptr %8, align 4, !tbaa !11
  call void @_ZN9upscaledbL14lock_exclusiveEib(i32 noundef %49, i1 noundef zeroext true)
  %50 = load i32, ptr %8, align 4, !tbaa !11
  call void @_ZN9upscaledbL16enable_largefileEi(i32 noundef %50)
  %51 = load i32, ptr %8, align 4, !tbaa !11
  %52 = getelementptr inbounds %"class.upscaledb::File", ptr %12, i32 0, i32 0
  store i32 %51, ptr %52, align 4, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  ret void

53:                                               ; preds = %44
  %54 = load ptr, ptr %9, align 8
  %55 = load i32, ptr %10, align 4
  %56 = insertvalue { ptr, i32 } poison, ptr %54, 0
  %57 = insertvalue { ptr, i32 } %56, i32 %55, 1
  resume { ptr, i32 } %57
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 willreturn memory(none) "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 "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 #5 = { 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 #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(none) }
attributes #8 = { 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 = !{!"bool", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!16, !12, i64 0}
!16 = !{!"_ZTSN9upscaledb4FileE", !12, i64 0, !12, i64 4, !12, i64 8}


; 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.2 = external hidden unnamed_addr constant [74 x i8], align 1
@_ZTIN9upscaledb9ExceptionE = external dso_local constant { ptr, ptr }, align 8
@__FUNCTION__._ZN9upscaledb4File4openEPKcb = external hidden unnamed_addr constant [5 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [43 x i8], align 1

declare i32 @__gxx_personality_v0(...)

declare ptr @__cxa_allocate_exception(i64) local_unnamed_addr

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN9upscaledb9ExceptionC2Ei(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) unnamed_addr #0 align 2

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

declare void @_ZN9upscaledb11dbg_prepareEiPKciS1_S1_(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @_ZN9upscaledb7dbg_logEPKcz(ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #2

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

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

; Function Attrs: mustprogress uwtable
declare hidden void @_ZN9upscaledbL14lock_exclusiveEib(i32 noundef, i1 noundef zeroext) local_unnamed_addr #5

; Function Attrs: mustprogress nounwind uwtable
declare hidden void @_ZN9upscaledbL16enable_largefileEi(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN9upscaledb4File4openEPKcb(ptr nocapture noundef nonnull writeonly align 4 dereferenceable(12) %0, ptr noundef %1, i1 noundef zeroext %2) local_unnamed_addr #5 align 2 personality ptr @__gxx_personality_v0 {
  %. = select i1 %2, i32 0, i32 2
  %4 = tail call i32 (ptr, i32, ...) @open64(ptr noundef %1, i32 noundef %.)
  %5 = icmp slt i32 %4, 0
  br i1 %5, label %6, label %14

6:                                                ; preds = %3
  tail call void @_ZN9upscaledb11dbg_prepareEiPKciS1_S1_(i32 noundef 1, ptr noundef nonnull @.str.2, i32 noundef 382, ptr noundef nonnull @__FUNCTION__._ZN9upscaledb4File4openEPKcb, ptr noundef null)
  %7 = tail call ptr @__errno_location() #6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = tail call ptr @strerror(i32 noundef %8) #7
  tail call void (ptr, ...) @_ZN9upscaledb7dbg_logEPKcz(ptr noundef nonnull @.str.10, ptr noundef %1, i32 noundef %8, ptr noundef %9)
  %10 = tail call ptr @__cxa_allocate_exception(i64 4) #7
  %11 = load i32, ptr %7, align 4, !tbaa !5
  %12 = icmp eq i32 %11, 2
  %13 = select i1 %12, i32 -21, i32 -18
  tail call void @_ZN9upscaledb9ExceptionC2Ei(ptr noundef nonnull align 4 dereferenceable(4) %10, i32 noundef %13)
  tail call void @__cxa_throw(ptr nonnull %10, ptr nonnull @_ZTIN9upscaledb9ExceptionE, ptr null) #8
  unreachable

14:                                               ; preds = %3
  tail call void @_ZN9upscaledbL14lock_exclusiveEib(i32 noundef %4, i1 noundef zeroext true)
  tail call void @_ZN9upscaledbL16enable_largefileEi(i32 noundef %4)
  store i32 %4, ptr %0, align 4, !tbaa !9
  ret void
}

attributes #0 = { mustprogress 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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 = { 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 #5 = { 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 #6 = { nounwind willreturn memory(none) }
attributes #7 = { nounwind }
attributes #8 = { 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 = !{!10, !6, i64 0}
!10 = !{!"_ZTSN9upscaledb4FileE", !6, i64 0, !6, i64 4, !6, i64 8}


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