; LLVM IR from ramsdell/lineqpp :: lua (lzio)

; 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.Zio = type { i64, ptr, ptr, ptr, ptr }

; 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 hidden i32 @luaZ_lookahead(ptr noundef) #1

; Function Attrs: nounwind uwtable
define hidden i64 @luaZ_read(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  br label %10

10:                                               ; preds = %56, %3
  %11 = load i64, ptr %7, align 8, !tbaa !9
  %12 = icmp ne i64 %11, 0
  br i1 %12, label %13, label %57

13:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = call i32 @luaZ_lookahead(ptr noundef %14)
  %16 = icmp eq i32 %15, -1
  br i1 %16, label %17, label %19

17:                                               ; preds = %13
  %18 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %18, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %54

19:                                               ; preds = %13
  %20 = load i64, ptr %7, align 8, !tbaa !9
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Zio, ptr %21, i32 0, i32 0
  %23 = load i64, ptr %22, align 8, !tbaa !11
  %24 = icmp ule i64 %20, %23
  br i1 %24, label %25, label %27

25:                                               ; preds = %19
  %26 = load i64, ptr %7, align 8, !tbaa !9
  br label %31

27:                                               ; preds = %19
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.Zio, ptr %28, i32 0, i32 0
  %30 = load i64, ptr %29, align 8, !tbaa !11
  br label %31

31:                                               ; preds = %27, %25
  %32 = phi i64 [ %26, %25 ], [ %30, %27 ]
  store i64 %32, ptr %8, align 8, !tbaa !9
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.Zio, ptr %34, i32 0, i32 1
  %36 = load ptr, ptr %35, align 8, !tbaa !13
  %37 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %33, ptr align 1 %36, i64 %37, i1 false)
  %38 = load i64, ptr %8, align 8, !tbaa !9
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.Zio, ptr %39, i32 0, i32 0
  %41 = load i64, ptr %40, align 8, !tbaa !11
  %42 = sub i64 %41, %38
  store i64 %42, ptr %40, align 8, !tbaa !11
  %43 = load i64, ptr %8, align 8, !tbaa !9
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Zio, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !13
  %47 = getelementptr inbounds i8, ptr %46, i64 %43
  store ptr %47, ptr %45, align 8, !tbaa !13
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = load i64, ptr %8, align 8, !tbaa !9
  %50 = getelementptr inbounds i8, ptr %48, i64 %49
  store ptr %50, ptr %6, align 8, !tbaa !5
  %51 = load i64, ptr %8, align 8, !tbaa !9
  %52 = load i64, ptr %7, align 8, !tbaa !9
  %53 = sub i64 %52, %51
  store i64 %53, ptr %7, align 8, !tbaa !9
  store i32 0, ptr %9, align 4
  br label %54

54:                                               ; preds = %31, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %55 = load i32, ptr %9, align 4
  switch i32 %55, label %60 [
    i32 0, label %56
    i32 1, label %58
  ]

56:                                               ; preds = %54
  br label %10, !llvm.loop !14

57:                                               ; preds = %10
  store i64 0, ptr %4, align 8
  br label %58

58:                                               ; preds = %57, %54
  %59 = load i64, ptr %4, align 8
  ret i64 %59

60:                                               ; preds = %54
  unreachable
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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, !10, i64 0}
!12 = !{!"Zio", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!13 = !{!12, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"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"

; Function Attrs: nounwind uwtable
declare hidden i32 @luaZ_lookahead(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define hidden i64 @luaZ_read(ptr noundef %0, ptr nocapture noundef writeonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not30 = icmp eq i64 %2, 0
  br i1 %.not30, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  br label %5

5:                                                ; preds = %.lr.ph, %8
  %.02032 = phi i64 [ %2, %.lr.ph ], [ %16, %8 ]
  %.02231 = phi ptr [ %1, %.lr.ph ], [ %15, %8 ]
  %6 = tail call i32 @luaZ_lookahead(ptr noundef %0)
  %7 = icmp eq i32 %6, -1
  br i1 %7, label %.thread, label %8

8:                                                ; preds = %5
  %9 = load i64, ptr %0, align 8, !tbaa !5
  %..020 = tail call i64 @llvm.umin.i64(i64 %.02032, i64 %9)
  %10 = load ptr, ptr %4, align 8, !tbaa !11
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.02231, ptr align 1 %10, i64 %..020, i1 false)
  %11 = load i64, ptr %0, align 8, !tbaa !5
  %12 = sub i64 %11, %..020
  store i64 %12, ptr %0, align 8, !tbaa !5
  %13 = load ptr, ptr %4, align 8, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %13, i64 %..020
  store ptr %14, ptr %4, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %.02231, i64 %..020
  %16 = sub i64 %.02032, %..020
  %.not = icmp eq i64 %16, 0
  br i1 %.not, label %.thread, label %5

.thread:                                          ; preds = %8, %5, %3
  %.020.lcssa = phi i64 [ 0, %3 ], [ %.02032, %5 ], [ 0, %8 ]
  ret i64 %.020.lcssa
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #2

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

!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 = !{!"Zio", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass