; LLVM IR from JayXon/Leanify :: fileio_linux.ll (ZN4FileC2EPKc)

; 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.stat = type { i64, i64, i64, i32, i32, i32, i32, i64, i64, i64, i64, %struct.timespec, %struct.timespec, %struct.timespec, [3 x i64] }
%struct.timespec = type { i64, i64 }
%class.File = type { i32, ptr, i64 }

@.str.1 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [15 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 void @perror(ptr noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4FileC2EPKc(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef %1) unnamed_addr #2 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca %struct.stat, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8
  %8 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 0
  store i32 -1, ptr %8, align 8, !tbaa !9
  %9 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 1
  store ptr null, ptr %9, align 8, !tbaa !13
  %10 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 2
  store i64 0, ptr %10, align 8, !tbaa !14
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = call i32 (ptr, i32, ...) @open(ptr noundef %11, i32 noundef 2)
  %13 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 0
  store i32 %12, ptr %13, align 8, !tbaa !9
  %14 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 0
  %15 = load i32, ptr %14, align 8, !tbaa !9
  %16 = icmp eq i32 %15, -1
  br i1 %16, label %17, label %18

17:                                               ; preds = %2
  call void @perror(ptr noundef @.str.1)
  br label %42

18:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 144, ptr %5) #4
  %19 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 0
  %20 = load i32, ptr %19, align 8, !tbaa !9
  %21 = call i32 @fstat(i32 noundef %20, ptr noundef %5) #4
  %22 = icmp eq i32 %21, -1
  br i1 %22, label %23, label %24

23:                                               ; preds = %18
  call void @perror(ptr noundef @.str.2)
  store i32 1, ptr %6, align 4
  br label %40

24:                                               ; preds = %18
  %25 = getelementptr inbounds %struct.stat, ptr %5, i32 0, i32 8
  %26 = load i64, ptr %25, align 8, !tbaa !15
  %27 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 2
  store i64 %26, ptr %27, align 8, !tbaa !14
  %28 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 2
  %29 = load i64, ptr %28, align 8, !tbaa !14
  %30 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 0
  %31 = load i32, ptr %30, align 8, !tbaa !9
  %32 = call ptr @mmap(ptr noundef null, i64 noundef %29, i32 noundef 3, i32 noundef 1, i32 noundef %31, i64 noundef 0) #4
  %33 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 1
  store ptr %32, ptr %33, align 8, !tbaa !13
  %34 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = icmp eq ptr %35, inttoptr (i64 -1 to ptr)
  br i1 %36, label %37, label %39

37:                                               ; preds = %24
  call void @perror(ptr noundef @.str.3)
  %38 = getelementptr inbounds %class.File, ptr %7, i32 0, i32 1
  store ptr null, ptr %38, align 8, !tbaa !13
  br label %39

39:                                               ; preds = %37, %24
  store i32 0, ptr %6, align 4
  br label %40

40:                                               ; preds = %39, %23
  call void @llvm.lifetime.end.p0(i64 144, ptr %5) #4
  %41 = load i32, ptr %6, align 4
  switch i32 %41, label %43 [
    i32 0, label %42
    i32 1, label %42
  ]

42:                                               ; preds = %40, %40, %17
  ret void

43:                                               ; preds = %40
  unreachable
}

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

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

; Function Attrs: nounwind
declare ptr @mmap(ptr noundef, i64 noundef, i32 noundef, i32 noundef, i32 noundef, i64 noundef) #3

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

!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, !11, i64 0}
!10 = !{!"_ZTS4File", !11, i64 0, !6, i64 8, !12, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!10, !6, i64 8}
!14 = !{!10, !12, i64 16}
!15 = !{!16, !12, i64 48}
!16 = !{!"_ZTS4stat", !12, i64 0, !12, i64 8, !12, i64 16, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !17, i64 72, !17, i64 88, !17, i64 104, !7, i64 120}
!17 = !{!"_ZTS8timespec", !12, i64 0, !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"

%struct.stat = type { i64, i64, i64, i32, i32, i32, i32, i64, i64, i64, i64, %struct.timespec, %struct.timespec, %struct.timespec, [3 x i64] }
%struct.timespec = type { i64, i64 }

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

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

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

; Function Attrs: nofree nounwind
declare void @perror(ptr nocapture noundef readonly) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4FileC2EPKc(ptr nocapture noundef nonnull align 8 dereferenceable(24) %0, ptr nocapture noundef readonly %1) unnamed_addr #2 align 2 {
  %3 = alloca %struct.stat, align 8
  store i32 -1, ptr %0, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %4, i8 0, i64 16, i1 false)
  %6 = tail call i32 (ptr, i32, ...) @open(ptr noundef %1, i32 noundef 2)
  store i32 %6, ptr %0, align 8, !tbaa !5
  %7 = icmp eq i32 %6, -1
  br i1 %7, label %8, label %9

8:                                                ; preds = %2
  tail call void @perror(ptr noundef nonnull @.str.1) #6
  br label %21

9:                                                ; preds = %2
  call void @llvm.lifetime.start.p0(i64 144, ptr nonnull %3) #7
  %10 = call i32 @fstat(i32 noundef %6, ptr noundef nonnull %3) #7
  %11 = icmp eq i32 %10, -1
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  tail call void @perror(ptr noundef nonnull @.str.2) #6
  br label %20

13:                                               ; preds = %9
  %14 = getelementptr inbounds i8, ptr %3, i64 48
  %15 = load i64, ptr %14, align 8, !tbaa !12
  store i64 %15, ptr %5, align 8, !tbaa !15
  %16 = load i32, ptr %0, align 8, !tbaa !5
  %17 = tail call ptr @mmap(ptr noundef null, i64 noundef %15, i32 noundef 3, i32 noundef 1, i32 noundef %16, i64 noundef 0) #7
  store ptr %17, ptr %4, align 8, !tbaa !16
  %18 = icmp eq ptr %17, inttoptr (i64 -1 to ptr)
  br i1 %18, label %19, label %20

19:                                               ; preds = %13
  tail call void @perror(ptr noundef nonnull @.str.3) #6
  store ptr null, ptr %4, align 8, !tbaa !16
  br label %20

20:                                               ; preds = %13, %19, %12
  call void @llvm.lifetime.end.p0(i64 144, ptr nonnull %3) #7
  br label %21

21:                                               ; preds = %20, %8
  ret void
}

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

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

; Function Attrs: nounwind
declare ptr @mmap(ptr noundef, i64 noundef, i32 noundef, i32 noundef, i32 noundef, i64 noundef) local_unnamed_addr #4

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { 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 #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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #6 = { cold }
attributes #7 = { 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, !7, i64 0}
!6 = !{!"_ZTS4File", !7, i64 0, !10, i64 8, !11, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!13, !11, i64 48}
!13 = !{!"_ZTS4stat", !11, i64 0, !11, i64 8, !11, i64 16, !7, i64 24, !7, i64 28, !7, i64 32, !7, i64 36, !11, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !14, i64 72, !14, i64 88, !14, i64 104, !8, i64 120}
!14 = !{!"_ZTS8timespec", !11, i64 0, !11, i64 8}
!15 = !{!6, !11, i64 16}
!16 = !{!6, !10, i64 8}


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