; LLVM IR from m-boutaleb/LZS :: src (buffer_service)

; 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"

@buffer_size = external hidden global i16, align 2
@buffer = external hidden global 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: nounwind uwtable
define dso_local zeroext i8 @read_1byte() #1 {
  %1 = alloca i8, align 1
  %2 = alloca i8, align 1
  %3 = alloca i32, align 4
  %4 = alloca i16, align 2
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #2
  store i8 0, ptr %2, align 1, !tbaa !5
  %5 = load i16, ptr @buffer_size, align 2, !tbaa !8
  %6 = zext i16 %5 to i32
  %7 = icmp eq i32 %6, 8
  br i1 %7, label %8, label %11

8:                                                ; preds = %0
  %9 = load i8, ptr @buffer, align 1, !tbaa !5
  store i8 %9, ptr %2, align 1, !tbaa !5
  call void @read_buffer()
  %10 = load i8, ptr %2, align 1, !tbaa !5
  store i8 %10, ptr %1, align 1
  store i32 1, ptr %3, align 4
  br label %39

11:                                               ; preds = %0
  %12 = load i16, ptr @buffer_size, align 2, !tbaa !8
  %13 = zext i16 %12 to i32
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %18

15:                                               ; preds = %11
  call void @read_buffer()
  %16 = load i8, ptr @buffer, align 1, !tbaa !5
  store i8 %16, ptr %2, align 1, !tbaa !5
  call void @reset_buffer()
  %17 = load i8, ptr %2, align 1, !tbaa !5
  store i8 %17, ptr %1, align 1
  store i32 1, ptr %3, align 4
  br label %39

18:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  %19 = load i16, ptr @buffer_size, align 2, !tbaa !8
  store i16 %19, ptr %4, align 2, !tbaa !8
  %20 = load i8, ptr @buffer, align 1, !tbaa !5
  store i8 %20, ptr %2, align 1, !tbaa !5
  call void @read_buffer()
  %21 = load i8, ptr @buffer, align 1, !tbaa !5
  %22 = zext i8 %21 to i32
  %23 = load i16, ptr %4, align 2, !tbaa !8
  %24 = zext i16 %23 to i32
  %25 = ashr i32 %22, %24
  %26 = load i8, ptr %2, align 1, !tbaa !5
  %27 = zext i8 %26 to i32
  %28 = or i32 %27, %25
  %29 = trunc i32 %28 to i8
  store i8 %29, ptr %2, align 1, !tbaa !5
  %30 = load i16, ptr %4, align 2, !tbaa !8
  %31 = zext i16 %30 to i32
  %32 = sub nsw i32 8, %31
  %33 = load i8, ptr @buffer, align 1, !tbaa !5
  %34 = zext i8 %33 to i32
  %35 = shl i32 %34, %32
  %36 = trunc i32 %35 to i8
  store i8 %36, ptr @buffer, align 1, !tbaa !5
  %37 = load i16, ptr %4, align 2, !tbaa !8
  store i16 %37, ptr @buffer_size, align 2, !tbaa !8
  %38 = load i8, ptr %2, align 1, !tbaa !5
  store i8 %38, ptr %1, align 1
  store i32 1, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  br label %39

39:                                               ; preds = %18, %15, %8
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #2
  %40 = load i8, ptr %1, align 1
  ret i8 %40
}

; Function Attrs: nounwind uwtable
declare hidden void @read_buffer() #1

; Function Attrs: nounwind uwtable
declare hidden void @reset_buffer() #1

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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"short", !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"

@buffer_size = external hidden local_unnamed_addr global i16, align 2
@buffer = external hidden local_unnamed_addr global i8, align 1

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @read_1byte() local_unnamed_addr #0 {
  %1 = load i16, ptr @buffer_size, align 2, !tbaa !5
  switch i16 %1, label %6 [
    i16 8, label %2
    i16 0, label %4
  ]

2:                                                ; preds = %0
  %3 = load i8, ptr @buffer, align 1, !tbaa !9
  tail call void @read_buffer()
  br label %17

4:                                                ; preds = %0
  tail call void @read_buffer()
  %5 = load i8, ptr @buffer, align 1, !tbaa !9
  tail call void @reset_buffer()
  br label %17

6:                                                ; preds = %0
  %7 = zext i16 %1 to i32
  %8 = load i8, ptr @buffer, align 1, !tbaa !9
  tail call void @read_buffer()
  %9 = load i8, ptr @buffer, align 1, !tbaa !9
  %10 = zext i8 %9 to i32
  %11 = lshr i32 %10, %7
  %12 = trunc nuw i32 %11 to i8
  %13 = or i8 %8, %12
  %14 = sub nsw i32 8, %7
  %15 = shl nuw nsw i32 %10, %14
  %16 = trunc i32 %15 to i8
  store i8 %16, ptr @buffer, align 1, !tbaa !9
  store i16 %1, ptr @buffer_size, align 2, !tbaa !5
  br label %17

17:                                               ; preds = %6, %4, %2
  %.0 = phi i8 [ %3, %2 ], [ %5, %4 ], [ %13, %6 ]
  ret i8 %.0
}

; Function Attrs: nounwind uwtable
declare hidden void @read_buffer() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @reset_buffer() local_unnamed_addr #0

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" }

!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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}


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