; LLVM IR from MaJerle/lwpkt :: examples (example_lwpkt_evt)

; 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.lwrb = type { ptr, i64, i64, i64, ptr }
%struct.lwpkt = type { i32, [256 x i8], ptr, ptr, i32, ptr, i8, %struct.anon }
%struct.anon = type { i32, %struct.lwpkt_crc_t, i32, i32, i32, i32, i32, i64, i64 }
%struct.lwpkt_crc_t = type { i32 }

@.str = external hidden unnamed_addr constant [28 x i8], align 1
@pkt_tx_rb = external hidden global %struct.lwrb, align 8
@pkt_tx_rb_data = external hidden global [64 x i8], align 16
@pkt_rx_rb = external hidden global %struct.lwrb, align 8
@pkt_rx_rb_data = external hidden global [64 x i8], align 16
@pkt = external hidden global %struct.lwpkt, align 8
@data = external hidden global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @example_lwpkt_evt() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #4
  %4 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  %5 = call zeroext i8 @lwrb_init(ptr noundef @pkt_tx_rb, ptr noundef @pkt_tx_rb_data, i64 noundef 64)
  %6 = call zeroext i8 @lwrb_init(ptr noundef @pkt_rx_rb, ptr noundef @pkt_rx_rb_data, i64 noundef 64)
  %7 = call i32 @lwpkt_init(ptr noundef @pkt, ptr noundef @pkt_tx_rb, ptr noundef @pkt_rx_rb)
  %8 = call i32 @lwpkt_set_addr(ptr noundef @pkt, i32 noundef 18)
  %9 = load ptr, ptr @data, align 8, !tbaa !5
  %10 = load ptr, ptr @data, align 8, !tbaa !5
  %11 = call i64 @strlen(ptr noundef %10) #5
  %12 = call i32 @lwpkt_write(ptr noundef @pkt, i32 noundef 17, i32 noundef 305419896, i32 noundef 133, ptr noundef %9, i64 noundef %11)
  store i32 %12, ptr %1, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %16, %0
  %14 = call i64 @lwrb_read(ptr noundef @pkt_tx_rb, ptr noundef %3, i64 noundef 1)
  %15 = icmp eq i64 %14, 1
  br i1 %15, label %16, label %18

16:                                               ; preds = %13
  %17 = call i64 @lwrb_write(ptr noundef @pkt_rx_rb, ptr noundef %3, i64 noundef 1)
  br label %13, !llvm.loop !11

18:                                               ; preds = %13
  %19 = call i32 @lwpkt_set_evt_fn(ptr noundef @pkt, ptr noundef @my_lwpkt_evt_fn)
  store i32 100, ptr %2, align 4, !tbaa !9
  %20 = load i32, ptr %2, align 4, !tbaa !9
  %21 = call i32 @lwpkt_process(ptr noundef @pkt, i32 noundef %20)
  %22 = load i32, ptr %1, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  ret i32 0
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare i32 @printf(ptr noundef, ...) #2

declare zeroext i8 @lwrb_init(ptr noundef, ptr noundef, i64 noundef) #2

declare i32 @lwpkt_init(ptr noundef, ptr noundef, ptr noundef) #2

declare i32 @lwpkt_set_addr(ptr noundef, i32 noundef) #2

declare i32 @lwpkt_write(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #3

declare i64 @lwrb_read(ptr noundef, ptr noundef, i64 noundef) #2

declare i64 @lwrb_write(ptr noundef, ptr noundef, i64 noundef) #2

declare i32 @lwpkt_set_evt_fn(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @my_lwpkt_evt_fn(ptr noundef, i32 noundef) #0

declare i32 @lwpkt_process(ptr noundef, i32 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

!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 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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.lwrb = type { ptr, i64, i64, i64, ptr }
%struct.lwpkt = type { i32, [256 x i8], ptr, ptr, i32, ptr, i8, %struct.anon }
%struct.anon = type { i32, %struct.lwpkt_crc_t, i32, i32, i32, i32, i32, i64, i64 }
%struct.lwpkt_crc_t = type { i32 }

@.str = external hidden unnamed_addr constant [28 x i8], align 1
@pkt_tx_rb = external hidden global %struct.lwrb, align 8
@pkt_tx_rb_data = external hidden global [64 x i8], align 16
@pkt_rx_rb = external hidden global %struct.lwrb, align 8
@pkt_rx_rb_data = external hidden global [64 x i8], align 16
@pkt = external hidden global %struct.lwpkt, align 8
@data = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @example_lwpkt_evt() local_unnamed_addr #0 {
  %1 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %1) #5
  %2 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  %3 = tail call zeroext i8 @lwrb_init(ptr noundef nonnull @pkt_tx_rb, ptr noundef nonnull @pkt_tx_rb_data, i64 noundef 64) #5
  %4 = tail call zeroext i8 @lwrb_init(ptr noundef nonnull @pkt_rx_rb, ptr noundef nonnull @pkt_rx_rb_data, i64 noundef 64) #5
  %5 = tail call i32 @lwpkt_init(ptr noundef nonnull @pkt, ptr noundef nonnull @pkt_tx_rb, ptr noundef nonnull @pkt_rx_rb) #5
  %6 = tail call i32 @lwpkt_set_addr(ptr noundef nonnull @pkt, i32 noundef 18) #5
  %7 = load ptr, ptr @data, align 8, !tbaa !5
  %8 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %7) #6
  %9 = tail call i32 @lwpkt_write(ptr noundef nonnull @pkt, i32 noundef 17, i32 noundef 305419896, i32 noundef 133, ptr noundef %7, i64 noundef %8) #5
  %10 = call i64 @lwrb_read(ptr noundef nonnull @pkt_tx_rb, ptr noundef nonnull %1, i64 noundef 1) #5
  %11 = icmp eq i64 %10, 1
  br i1 %11, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %.lr.ph
  %12 = call i64 @lwrb_write(ptr noundef nonnull @pkt_rx_rb, ptr noundef nonnull %1, i64 noundef 1) #5
  %13 = call i64 @lwrb_read(ptr noundef nonnull @pkt_tx_rb, ptr noundef nonnull %1, i64 noundef 1) #5
  %14 = icmp eq i64 %13, 1
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %0
  %15 = call i32 @lwpkt_set_evt_fn(ptr noundef nonnull @pkt, ptr noundef nonnull @my_lwpkt_evt_fn) #5
  %16 = call i32 @lwpkt_process(ptr noundef nonnull @pkt, i32 noundef 100) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %1) #5
  ret i32 0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

declare zeroext i8 @lwrb_init(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #3

declare i32 @lwpkt_init(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare i32 @lwpkt_set_addr(ptr noundef, i32 noundef) local_unnamed_addr #3

declare i32 @lwpkt_write(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #4

declare i64 @lwrb_read(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #3

declare i64 @lwrb_write(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #3

declare i32 @lwpkt_set_evt_fn(ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden void @my_lwpkt_evt_fn(ptr noundef, i32 noundef) #0

declare i32 @lwpkt_process(ptr noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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 nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass