; LLVM IR from xdevelnet/libsdb :: libsdb (sdb_insert_fileno)

; 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.sdb_dbo_s = type { i32, ptr, ptr, i64, i64, i64, i64, %struct.sdb_defun }
%struct.sdb_defun = type { ptr, ptr, ptr, ptr, ptr }

@enomem_flag = external dso_local global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @sdb_insert_fileno(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca [4096 x i8], align 16
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4096, ptr %8) #7
  call void @llvm.memset.p0.i64(ptr align 16 %8, i8 0, i64 4096, i1 false)
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.sdb_dbo_s, ptr %13, i32 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds [4096 x i8], ptr %8, i64 0, i64 0
  %18 = call zeroext i1 @prepare_path(ptr noundef %15, ptr noundef %16, ptr noundef %17)
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #7
  %19 = getelementptr inbounds [4096 x i8], ptr %8, i64 0, i64 0
  %20 = call i32 (ptr, i32, ...) @open(ptr noundef %19, i32 noundef 193, i32 noundef 438)
  store i32 %20, ptr %9, align 4, !tbaa !14
  %21 = load i32, ptr %9, align 4, !tbaa !14
  %22 = icmp slt i32 %21, 0
  br i1 %22, label %23, label %29

23:                                               ; preds = %3
  %24 = call ptr @__errno_location() #8
  %25 = load i32, ptr %24, align 4, !tbaa !14
  %26 = icmp eq i32 %25, 12
  br i1 %26, label %27, label %28

27:                                               ; preds = %23
  store i32 1, ptr @enomem_flag, align 4, !tbaa !14
  br label %28

28:                                               ; preds = %27, %23
  store i1 false, ptr %4, align 1
  store i32 1, ptr %10, align 4
  br label %76

29:                                               ; preds = %3
  %30 = call ptr @__errno_location() #8
  store i32 0, ptr %30, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #7
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = call i64 @strlen(ptr noundef %31) #9
  store i64 %32, ptr %11, align 8, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #7
  %33 = load i32, ptr %9, align 4, !tbaa !14
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = load i64, ptr %11, align 8, !tbaa !15
  %36 = call i64 @write(i32 noundef %33, ptr noundef %34, i64 noundef %35)
  store i64 %36, ptr %12, align 8, !tbaa !15
  %37 = load i64, ptr %12, align 8, !tbaa !15
  %38 = icmp slt i64 %37, 0
  br i1 %38, label %39, label %55

39:                                               ; preds = %29
  %40 = call ptr @__errno_location() #8
  %41 = load i32, ptr %40, align 4, !tbaa !14
  %42 = icmp eq i32 %41, 12
  br i1 %42, label %51, label %43

43:                                               ; preds = %39
  %44 = call ptr @__errno_location() #8
  %45 = load i32, ptr %44, align 4, !tbaa !14
  %46 = icmp eq i32 %45, 27
  br i1 %46, label %51, label %47

47:                                               ; preds = %43
  %48 = call ptr @__errno_location() #8
  %49 = load i32, ptr %48, align 4, !tbaa !14
  %50 = icmp eq i32 %49, 28
  br i1 %50, label %51, label %52

51:                                               ; preds = %47, %43, %39
  store i32 1, ptr @enomem_flag, align 4, !tbaa !14
  br label %52

52:                                               ; preds = %51, %47
  %53 = load i32, ptr %9, align 4, !tbaa !14
  %54 = call i32 @close(i32 noundef %53)
  store i1 false, ptr %4, align 1
  store i32 1, ptr %10, align 4
  br label %75

55:                                               ; preds = %29
  %56 = call ptr @__errno_location() #8
  %57 = load i32, ptr %56, align 4, !tbaa !14
  %58 = icmp eq i32 %57, 12
  br i1 %58, label %71, label %59

59:                                               ; preds = %55
  %60 = call ptr @__errno_location() #8
  %61 = load i32, ptr %60, align 4, !tbaa !14
  %62 = icmp eq i32 %61, 27
  br i1 %62, label %71, label %63

63:                                               ; preds = %59
  %64 = call ptr @__errno_location() #8
  %65 = load i32, ptr %64, align 4, !tbaa !14
  %66 = icmp eq i32 %65, 28
  br i1 %66, label %71, label %67

67:                                               ; preds = %63
  %68 = load i64, ptr %12, align 8, !tbaa !15
  %69 = load i64, ptr %11, align 8, !tbaa !15
  %70 = icmp ult i64 %68, %69
  br i1 %70, label %71, label %72

71:                                               ; preds = %67, %63, %59, %55
  store i32 1, ptr @enomem_flag, align 4, !tbaa !14
  br label %72

72:                                               ; preds = %71, %67
  %73 = load i32, ptr %9, align 4, !tbaa !14
  %74 = call i32 @close(i32 noundef %73)
  store i1 true, ptr %4, align 1
  store i32 1, ptr %10, align 4
  br label %75

75:                                               ; preds = %72, %52
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #7
  br label %76

76:                                               ; preds = %75, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #7
  call void @llvm.lifetime.end.p0(i64 4096, ptr %8) #7
  %77 = load i1, ptr %4, align 1
  ret i1 %77
}

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @prepare_path(ptr noundef, ptr noundef, ptr noundef) #3

declare i32 @open(ptr noundef, i32 noundef, ...) #4

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #6

declare i64 @write(i32 noundef, ptr noundef, i64 noundef) #4

declare i32 @close(i32 noundef) #4

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { inlinehint 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 #4 = { "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 = { 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 #6 = { 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 #7 = { nounwind }
attributes #8 = { nounwind willreturn memory(none) }
attributes #9 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"sdb_dbo_s", !11, i64 0, !6, i64 8, !6, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !13, i64 56}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!14 = !{!11, !11, i64 0}
!15 = !{!12, !12, 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"

@enomem_flag = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @sdb_insert_fileno(ptr nocapture noundef readonly %0, ptr noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = alloca [4096 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 4096, ptr nonnull %4) #8
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(4096) %4, i8 0, i64 4096, i1 false)
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = call zeroext i1 @prepare_path(ptr noundef %6, ptr noundef %1, ptr noundef nonnull %4)
  %8 = call i32 (ptr, i32, ...) @open(ptr noundef nonnull %4, i32 noundef 193, i32 noundef 438) #8
  %9 = icmp slt i32 %8, 0
  %10 = tail call ptr @__errno_location() #9
  br i1 %9, label %11, label %15

11:                                               ; preds = %3
  %12 = load i32, ptr %10, align 4, !tbaa !13
  %13 = icmp eq i32 %12, 12
  br i1 %13, label %14, label %30

14:                                               ; preds = %11
  store i32 1, ptr @enomem_flag, align 4, !tbaa !13
  br label %30

15:                                               ; preds = %3
  store i32 0, ptr %10, align 4, !tbaa !13
  %16 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %2) #10
  %17 = call i64 @write(i32 noundef %8, ptr noundef %2, i64 noundef %16) #8
  %18 = icmp sgt i64 %17, -1
  %19 = load i32, ptr %10, align 4, !tbaa !13
  br i1 %18, label %24, label %20

20:                                               ; preds = %15
  switch i32 %19, label %22 [
    i32 12, label %21
    i32 27, label %21
    i32 28, label %21
  ]

21:                                               ; preds = %20, %20, %20
  store i32 1, ptr @enomem_flag, align 4, !tbaa !13
  br label %22

22:                                               ; preds = %20, %21
  %23 = call i32 @close(i32 noundef %8) #8
  br label %30

24:                                               ; preds = %15
  switch i32 %19, label %25 [
    i32 12, label %27
    i32 27, label %27
    i32 28, label %27
  ]

25:                                               ; preds = %24
  %26 = icmp ult i64 %17, %16
  br i1 %26, label %27, label %28

27:                                               ; preds = %24, %24, %24, %25
  store i32 1, ptr @enomem_flag, align 4, !tbaa !13
  br label %28

28:                                               ; preds = %27, %25
  %29 = call i32 @close(i32 noundef %8) #8
  br label %30

30:                                               ; preds = %22, %28, %11, %14
  %.1 = phi i1 [ false, %14 ], [ false, %11 ], [ true, %28 ], [ false, %22 ]
  call void @llvm.lifetime.end.p0(i64 4096, ptr nonnull %4) #8
  ret i1 %.1
}

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @prepare_path(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

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

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #6

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

declare i32 @close(i32 noundef) local_unnamed_addr #7

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

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


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