; LLVM IR from tenok-rtos/tenok :: kernel (kfifo)

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

; 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 void @kfifo_in(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = call zeroext i1 @kfifo_is_full(ptr noundef %9)
  br i1 %10, label %11, label %43

11:                                               ; preds = %3
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.kfifo, ptr %12, i32 0, i32 3
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  %15 = ptrtoint ptr %14 to i64
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.kfifo, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8, !tbaa !14
  %19 = sext i32 %18 to i64
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.kfifo, ptr %20, i32 0, i32 7
  %22 = load i64, ptr %21, align 8, !tbaa !15
  %23 = mul i64 %19, %22
  %24 = add i64 %15, %23
  %25 = inttoptr i64 %24 to ptr
  store ptr %25, ptr %7, align 8, !tbaa !5
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = ptrtoint ptr %26 to i64
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.kfifo, ptr %28, i32 0, i32 6
  %30 = load i64, ptr %29, align 8, !tbaa !16
  %31 = add i64 %27, %30
  %32 = inttoptr i64 %31 to ptr
  store ptr %32, ptr %8, align 8, !tbaa !5
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %33, ptr align 1 %34, i64 %35, i1 false)
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.kfifo, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 8, !tbaa !14
  %40 = call i32 @kfifo_increase(ptr noundef %36, i32 noundef %39)
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.kfifo, ptr %41, i32 0, i32 0
  store i32 %40, ptr %42, align 8, !tbaa !14
  br label %72

43:                                               ; preds = %3
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.kfifo, ptr %44, i32 0, i32 3
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  %47 = ptrtoint ptr %46 to i64
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.kfifo, ptr %48, i32 0, i32 1
  %50 = load i32, ptr %49, align 4, !tbaa !17
  %51 = sext i32 %50 to i64
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.kfifo, ptr %52, i32 0, i32 7
  %54 = load i64, ptr %53, align 8, !tbaa !15
  %55 = mul i64 %51, %54
  %56 = add i64 %47, %55
  %57 = inttoptr i64 %56 to ptr
  store ptr %57, ptr %7, align 8, !tbaa !5
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = ptrtoint ptr %58 to i64
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.kfifo, ptr %60, i32 0, i32 6
  %62 = load i64, ptr %61, align 8, !tbaa !16
  %63 = add i64 %59, %62
  %64 = inttoptr i64 %63 to ptr
  store ptr %64, ptr %8, align 8, !tbaa !5
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %65, ptr align 1 %66, i64 %67, i1 false)
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.kfifo, ptr %68, i32 0, i32 2
  %70 = load i32, ptr %69, align 8, !tbaa !18
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %69, align 8, !tbaa !18
  br label %72

72:                                               ; preds = %43, %11
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.kfifo, ptr %73, i32 0, i32 5
  %75 = load i64, ptr %74, align 8, !tbaa !19
  %76 = icmp ugt i64 %75, 1
  br i1 %76, label %77, label %81

77:                                               ; preds = %72
  %78 = load i64, ptr %6, align 8, !tbaa !9
  %79 = trunc i64 %78 to i16
  %80 = load ptr, ptr %7, align 8, !tbaa !5
  store i16 %79, ptr %80, align 2, !tbaa !20
  br label %81

81:                                               ; preds = %77, %72
  %82 = load ptr, ptr %4, align 8, !tbaa !5
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.kfifo, ptr %83, i32 0, i32 1
  %85 = load i32, ptr %84, align 4, !tbaa !17
  %86 = call i32 @kfifo_increase(ptr noundef %82, i32 noundef %85)
  %87 = load ptr, ptr %4, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.kfifo, ptr %87, i32 0, i32 1
  store i32 %86, ptr %88, align 4, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @kfifo_is_full(ptr noundef) #1

; 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

; Function Attrs: nounwind uwtable
declare hidden i32 @kfifo_increase(ptr noundef, i32 noundef) #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 = { 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, !6, i64 16}
!12 = !{!"kfifo", !13, i64 0, !13, i64 4, !13, i64 8, !6, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 0}
!15 = !{!12, !10, i64 48}
!16 = !{!12, !10, i64 40}
!17 = !{!12, !13, i64 4}
!18 = !{!12, !13, i64 8}
!19 = !{!12, !10, i64 32}
!20 = !{!21, !21, i64 0}
!21 = !{!"short", !7, 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"

; Function Attrs: nounwind uwtable
define dso_local void @kfifo_in(ptr noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = tail call zeroext i1 @kfifo_is_full(ptr noundef %0)
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = ptrtoint ptr %6 to i64
  br i1 %4, label %8, label %21

8:                                                ; preds = %3
  %9 = load i32, ptr %0, align 8, !tbaa !12
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i8, ptr %0, i64 48
  %12 = load i64, ptr %11, align 8, !tbaa !13
  %13 = mul i64 %12, %10
  %14 = add i64 %13, %7
  %15 = getelementptr inbounds i8, ptr %0, i64 40
  %16 = load i64, ptr %15, align 8, !tbaa !14
  %17 = add i64 %14, %16
  %18 = inttoptr i64 %17 to ptr
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %18, ptr align 1 %1, i64 %2, i1 false)
  %19 = load i32, ptr %0, align 8, !tbaa !12
  %20 = tail call i32 @kfifo_increase(ptr noundef nonnull %0, i32 noundef %19)
  store i32 %20, ptr %0, align 8, !tbaa !12
  br label %36

21:                                               ; preds = %3
  %22 = getelementptr inbounds i8, ptr %0, i64 4
  %23 = load i32, ptr %22, align 4, !tbaa !15
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %0, i64 48
  %26 = load i64, ptr %25, align 8, !tbaa !13
  %27 = mul i64 %26, %24
  %28 = add i64 %27, %7
  %29 = getelementptr inbounds i8, ptr %0, i64 40
  %30 = load i64, ptr %29, align 8, !tbaa !14
  %31 = add i64 %28, %30
  %32 = inttoptr i64 %31 to ptr
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %32, ptr align 1 %1, i64 %2, i1 false)
  %33 = getelementptr inbounds i8, ptr %0, i64 8
  %34 = load i32, ptr %33, align 8, !tbaa !16
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %33, align 8, !tbaa !16
  br label %36

36:                                               ; preds = %21, %8
  %.0.in = phi i64 [ %14, %8 ], [ %28, %21 ]
  %37 = getelementptr inbounds i8, ptr %0, i64 32
  %38 = load i64, ptr %37, align 8, !tbaa !17
  %39 = icmp ugt i64 %38, 1
  br i1 %39, label %40, label %42

40:                                               ; preds = %36
  %.0 = inttoptr i64 %.0.in to ptr
  %41 = trunc i64 %2 to i16
  store i16 %41, ptr %.0, align 2, !tbaa !18
  br label %42

42:                                               ; preds = %40, %36
  %43 = getelementptr inbounds i8, ptr %0, i64 4
  %44 = load i32, ptr %43, align 4, !tbaa !15
  %45 = tail call i32 @kfifo_increase(ptr noundef nonnull %0, i32 noundef %44)
  store i32 %45, ptr %43, align 4, !tbaa !15
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @kfifo_is_full(ptr noundef) local_unnamed_addr #0

; 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: nounwind uwtable
declare hidden i32 @kfifo_increase(ptr noundef, i32 noundef) 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" }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !10, i64 16}
!6 = !{!"kfifo", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !11, i64 48}
!14 = !{!6, !11, i64 40}
!15 = !{!6, !7, i64 4}
!16 = !{!6, !7, i64 8}
!17 = !{!6, !11, i64 32}
!18 = !{!19, !19, i64 0}
!19 = !{!"short", !8, i64 0}


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