; LLVM IR from chromia/lichtenberg :: src/core.ll (ZNK11lichtenberg10CellList2D4saveEPKc)

; 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.lichtenberg::CellList2D" = type { ptr, i32, i32 }

@.str = external hidden unnamed_addr constant [3 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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK11lichtenberg10CellList2D8is_validEv(ptr noundef nonnull align 8 dereferenceable(16)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZNK11lichtenberg10CellList2D4saveEPKc(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef %1) #2 align 2 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8
  %11 = call noundef zeroext i1 @_ZNK11lichtenberg10CellList2D8is_validEv(ptr noundef nonnull align 8 dereferenceable(16) %10)
  br i1 %11, label %13, label %12

12:                                               ; preds = %2
  store i1 false, ptr %3, align 1
  br label %62

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = call noalias ptr @fopen(ptr noundef %14, ptr noundef @.str)
  store ptr %15, ptr %6, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %19, label %18

18:                                               ; preds = %13
  store i1 false, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %61

19:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 1, ptr %8, align 4, !tbaa !9
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = call i64 @fwrite(ptr noundef %8, i64 noundef 4, i64 noundef 1, ptr noundef %20)
  %22 = icmp ne i64 %21, 1
  br i1 %22, label %23, label %24

23:                                               ; preds = %19
  br label %57

24:                                               ; preds = %19
  %25 = getelementptr inbounds %"class.lichtenberg::CellList2D", ptr %10, i32 0, i32 1
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = call i64 @fwrite(ptr noundef %25, i64 noundef 4, i64 noundef 1, ptr noundef %26)
  %28 = icmp ne i64 %27, 1
  br i1 %28, label %29, label %30

29:                                               ; preds = %24
  br label %57

30:                                               ; preds = %24
  %31 = getelementptr inbounds %"class.lichtenberg::CellList2D", ptr %10, i32 0, i32 2
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = call i64 @fwrite(ptr noundef %31, i64 noundef 4, i64 noundef 1, ptr noundef %32)
  %34 = icmp ne i64 %33, 1
  br i1 %34, label %35, label %36

35:                                               ; preds = %30
  br label %57

36:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %37 = getelementptr inbounds %"class.lichtenberg::CellList2D", ptr %10, i32 0, i32 1
  %38 = load i32, ptr %37, align 8, !tbaa !11
  %39 = getelementptr inbounds %"class.lichtenberg::CellList2D", ptr %10, i32 0, i32 2
  %40 = load i32, ptr %39, align 4, !tbaa !13
  %41 = mul nsw i32 %38, %40
  %42 = sext i32 %41 to i64
  store i64 %42, ptr %9, align 8, !tbaa !14
  %43 = getelementptr inbounds %"class.lichtenberg::CellList2D", ptr %10, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !16
  %45 = load i64, ptr %9, align 8, !tbaa !14
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = call i64 @fwrite(ptr noundef %44, i64 noundef 12, i64 noundef %45, ptr noundef %46)
  %48 = load i64, ptr %9, align 8, !tbaa !14
  %49 = icmp ne i64 %47, %48
  br i1 %49, label %50, label %51

50:                                               ; preds = %36
  store i32 2, ptr %7, align 4
  br label %52

51:                                               ; preds = %36
  store i32 0, ptr %7, align 4
  br label %52

52:                                               ; preds = %51, %50
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  %53 = load i32, ptr %7, align 4
  switch i32 %53, label %60 [
    i32 0, label %54
    i32 2, label %57
  ]

54:                                               ; preds = %52
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = call i32 @fclose(ptr noundef %55)
  store i1 true, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %60

57:                                               ; preds = %52, %35, %29, %23
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = call i32 @fclose(ptr noundef %58)
  store i1 false, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %60

60:                                               ; preds = %57, %54, %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %61

61:                                               ; preds = %60, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %62

62:                                               ; preds = %61, %12
  %63 = load i1, ptr %3, align 1
  ret i1 %63
}

declare noalias ptr @fopen(ptr noundef, ptr noundef) #3

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

declare i32 @fclose(ptr noundef) #3

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 = { 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 = { "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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_ZTSN11lichtenberg10CellList2DE", !6, i64 0, !10, i64 8, !10, i64 12}
!13 = !{!12, !10, i64 12}
!14 = !{!15, !15, i64 0}
!15 = !{!"long", !7, i64 0}
!16 = !{!12, !6, 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 [3 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: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK11lichtenberg10CellList2D8is_validEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZNK11lichtenberg10CellList2D4saveEPKc(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr nocapture noundef readonly %1) local_unnamed_addr #1 align 2 {
  %3 = alloca i32, align 4
  %4 = tail call noundef zeroext i1 @_ZNK11lichtenberg10CellList2D8is_validEv(ptr noundef nonnull align 8 dereferenceable(16) %0)
  br i1 %4, label %5, label %25

5:                                                ; preds = %2
  %6 = tail call noalias ptr @fopen(ptr noundef %1, ptr noundef nonnull @.str)
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %25, label %7

7:                                                ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  store i32 1, ptr %3, align 4, !tbaa !5
  %8 = call i64 @fwrite(ptr noundef nonnull %3, i64 noundef 4, i64 noundef 1, ptr noundef nonnull %6)
  %.not14 = icmp eq i64 %8, 1
  br i1 %.not14, label %9, label %22

9:                                                ; preds = %7
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = tail call i64 @fwrite(ptr noundef nonnull %10, i64 noundef 4, i64 noundef 1, ptr noundef nonnull %6)
  %.not15 = icmp eq i64 %11, 1
  br i1 %.not15, label %12, label %22

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %0, i64 12
  %14 = tail call i64 @fwrite(ptr noundef nonnull %13, i64 noundef 4, i64 noundef 1, ptr noundef nonnull %6)
  %.not16 = icmp eq i64 %14, 1
  br i1 %.not16, label %15, label %22

15:                                               ; preds = %12
  %16 = load i32, ptr %10, align 8, !tbaa !9
  %17 = load i32, ptr %13, align 4, !tbaa !12
  %18 = mul nsw i32 %17, %16
  %19 = sext i32 %18 to i64
  %20 = load ptr, ptr %0, align 8, !tbaa !13
  %21 = tail call i64 @fwrite(ptr noundef %20, i64 noundef 12, i64 noundef %19, ptr noundef nonnull %6)
  %.not17 = icmp eq i64 %21, %19
  br i1 %.not17, label %23, label %22

22:                                               ; preds = %15, %12, %9, %7
  br label %23

23:                                               ; preds = %15, %22
  %.0 = phi i1 [ false, %22 ], [ true, %15 ]
  %24 = tail call i32 @fclose(ptr noundef nonnull %6)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  br label %25

25:                                               ; preds = %23, %5, %2
  %.2 = phi i1 [ false, %2 ], [ %.0, %23 ], [ false, %5 ]
  ret i1 %.2
}

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

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { 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 }

!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 8}
!10 = !{!"_ZTSN11lichtenberg10CellList2DE", !11, i64 0, !6, i64 8, !6, i64 12}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !6, i64 12}
!13 = !{!10, !11, i64 0}


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