; LLVM IR from kala13x/libxutils :: src/sys (xfs)

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

; 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: nounwind uwtable
declare dso_local i32 @XFile_Open(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @XFile_Close(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @XFile_Write(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @XPath_Write(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca %struct.XFile, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br i1 %16, label %23, label %17

17:                                               ; preds = %4
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %23, label %20

20:                                               ; preds = %17
  %21 = load i64, ptr %8, align 8, !tbaa !9
  %22 = icmp ne i64 %21, 0
  br i1 %22, label %24, label %23

23:                                               ; preds = %20, %17, %4
  store i32 -1, ptr %5, align 4
  br label %61

24:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 48, ptr %10) #2
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = load ptr, ptr %9, align 8, !tbaa !5
  %27 = call i32 @XFile_Open(ptr noundef %10, ptr noundef %25, ptr noundef %26, ptr noundef null)
  %28 = icmp slt i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %24
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %60

30:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %31 = load i64, ptr %8, align 8, !tbaa !9
  %32 = trunc i64 %31 to i32
  store i32 %32, ptr %12, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 0, ptr %13, align 4, !tbaa !11
  br label %33

33:                                               ; preds = %56, %30
  %34 = load i32, ptr %12, align 4, !tbaa !11
  %35 = icmp sgt i32 %34, 0
  br i1 %35, label %36, label %57

36:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = load i32, ptr %13, align 4, !tbaa !11
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i8, ptr %37, i64 %39
  %41 = load i64, ptr %8, align 8, !tbaa !9
  %42 = call i32 @XFile_Write(ptr noundef %10, ptr noundef %40, i64 noundef %41)
  store i32 %42, ptr %14, align 4, !tbaa !11
  %43 = load i32, ptr %14, align 4, !tbaa !11
  %44 = icmp sle i32 %43, 0
  br i1 %44, label %45, label %47

45:                                               ; preds = %36
  call void @XFile_Close(ptr noundef %10)
  %46 = load i32, ptr %13, align 4, !tbaa !11
  store i32 %46, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %54

47:                                               ; preds = %36
  %48 = load i32, ptr %14, align 4, !tbaa !11
  %49 = load i32, ptr %13, align 4, !tbaa !11
  %50 = add nsw i32 %49, %48
  store i32 %50, ptr %13, align 4, !tbaa !11
  %51 = load i32, ptr %14, align 4, !tbaa !11
  %52 = load i32, ptr %12, align 4, !tbaa !11
  %53 = sub nsw i32 %52, %51
  store i32 %53, ptr %12, align 4, !tbaa !11
  store i32 0, ptr %11, align 4
  br label %54

54:                                               ; preds = %47, %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  %55 = load i32, ptr %11, align 4
  switch i32 %55, label %59 [
    i32 0, label %56
  ]

56:                                               ; preds = %54
  br label %33, !llvm.loop !13

57:                                               ; preds = %33
  call void @XFile_Close(ptr noundef %10)
  %58 = load i32, ptr %13, align 4, !tbaa !11
  store i32 %58, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %59

59:                                               ; preds = %57, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %60

60:                                               ; preds = %59, %29
  call void @llvm.lifetime.end.p0(i64 48, ptr %10) #2
  br label %61

61:                                               ; preds = %60, %23
  %62 = load i32, ptr %5, align 4
  ret i32 %62
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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

; 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: nounwind uwtable
declare dso_local i32 @XFile_Open(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @XFile_Close(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @XFile_Write(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @XPath_Write(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = alloca %struct.XFile, align 8
  %6 = icmp ne ptr %0, null
  %7 = icmp ne ptr %1, null
  %or.cond.not29 = and i1 %6, %7
  %8 = icmp ne i64 %2, 0
  %or.cond3 = and i1 %or.cond.not29, %8
  br i1 %or.cond3, label %9, label %24

9:                                                ; preds = %4
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %5) #2
  %10 = call i32 @XFile_Open(ptr noundef nonnull %5, ptr noundef nonnull %0, ptr noundef %3, ptr noundef null)
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %23, label %12

12:                                               ; preds = %9
  %13 = trunc i64 %2 to i32
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %.lr.ph, label %.sink.split

.lr.ph:                                           ; preds = %12, %19
  %.02135 = phi i32 [ %20, %19 ], [ 0, %12 ]
  %.02334 = phi i32 [ %21, %19 ], [ %13, %12 ]
  %15 = zext nneg i32 %.02135 to i64
  %16 = getelementptr inbounds i8, ptr %1, i64 %15
  %17 = call i32 @XFile_Write(ptr noundef nonnull %5, ptr noundef nonnull %16, i64 noundef %2)
  %18 = icmp sgt i32 %17, 0
  br i1 %18, label %19, label %.sink.split

19:                                               ; preds = %.lr.ph
  %20 = add nuw nsw i32 %17, %.02135
  %21 = sub nsw i32 %.02334, %17
  %22 = icmp sgt i32 %21, 0
  br i1 %22, label %.lr.ph, label %.sink.split, !llvm.loop !5

.sink.split:                                      ; preds = %19, %.lr.ph, %12
  %.3.ph = phi i32 [ 0, %12 ], [ %.02135, %.lr.ph ], [ %20, %19 ]
  call void @XFile_Close(ptr noundef nonnull %5)
  br label %23

23:                                               ; preds = %.sink.split, %9
  %.3 = phi i32 [ -1, %9 ], [ %.3.ph, %.sink.split ]
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %5) #2
  br label %24

24:                                               ; preds = %4, %23
  %.4 = phi i32 [ %.3, %23 ], [ -1, %4 ]
  ret i32 %.4
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass