; LLVM IR from moonglow/pcan_cantact :: Src (pcan_protocol)

; 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.anon = type { i8, i8, i8, i16, i16, %struct.anon.0, [6 x i8], i8, i8, i32 }
%struct.anon.0 = type { i8, i8, i8, i8, i8, i8, i8, i8, i8, i32 }
%struct.t_m2h_fsm = type { i8, i8, ptr, i32, i32 }
%struct.PCAN_RECORD_BUFFER_EX = type { [896 x i8], i16, i16 }

@pcan_device = external hidden global %struct.anon, align 4
@data_fsm = external hidden global %struct.t_m2h_fsm, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @pcan_record_buffer_reset(ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @pcan_record_buffer_flush(ptr noundef %0) #0 {
  %2 = alloca i16, align 2
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #3
  %8 = call zeroext i16 @pcan_timestamp_ticks()
  store i16 %8, ptr %4, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %9, i32 0, i32 2
  %11 = load i16, ptr %10, align 2, !tbaa !11
  %12 = zext i16 %11 to i32
  %13 = icmp sle i32 %12, 2
  br i1 %13, label %14, label %34

14:                                               ; preds = %1
  %15 = load i16, ptr getelementptr inbounds (%struct.anon, ptr @pcan_device, i32 0, i32 4), align 2, !tbaa !13
  %16 = zext i16 %15 to i32
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %33

18:                                               ; preds = %14
  %19 = load i16, ptr %4, align 2, !tbaa !9
  %20 = zext i16 %19 to i32
  %21 = load i16, ptr getelementptr inbounds (%struct.anon, ptr @pcan_device, i32 0, i32 4), align 2, !tbaa !13
  %22 = zext i16 %21 to i32
  %23 = sub nsw i32 %20, %22
  %24 = trunc i32 %23 to i16
  %25 = zext i16 %24 to i32
  %26 = icmp uge i32 %25, 18
  br i1 %26, label %27, label %33

27:                                               ; preds = %18
  %28 = call i32 @pcan_flush_data(ptr noundef @data_fsm, ptr noundef null, i32 noundef 0)
  store i32 %28, ptr %5, align 4, !tbaa !17
  %29 = load i32, ptr %5, align 4, !tbaa !17
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %27
  store i16 0, ptr getelementptr inbounds (%struct.anon, ptr @pcan_device, i32 0, i32 4), align 2, !tbaa !13
  br label %32

32:                                               ; preds = %31, %27
  br label %33

33:                                               ; preds = %32, %18, %14
  store i16 0, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %52

34:                                               ; preds = %1
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %35, i32 0, i32 2
  %37 = load i16, ptr %36, align 2, !tbaa !11
  %38 = zext i16 %37 to i32
  %39 = add nsw i32 %38, 63
  %40 = and i32 %39, -64
  store i32 %40, ptr %6, align 4, !tbaa !17
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %41, i32 0, i32 0
  %43 = getelementptr inbounds [896 x i8], ptr %42, i64 0, i64 0
  %44 = load i32, ptr %6, align 4, !tbaa !17
  %45 = call i32 @pcan_flush_data(ptr noundef @data_fsm, ptr noundef %43, i32 noundef %44)
  store i32 %45, ptr %5, align 4, !tbaa !17
  %46 = load i32, ptr %5, align 4, !tbaa !17
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %48, label %51

48:                                               ; preds = %34
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  call void @pcan_record_buffer_reset(ptr noundef %49)
  %50 = load i16, ptr %4, align 2, !tbaa !9
  store i16 %50, ptr getelementptr inbounds (%struct.anon, ptr @pcan_device, i32 0, i32 4), align 2, !tbaa !13
  store i16 1, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %52

51:                                               ; preds = %34
  store i16 0, ptr %2, align 2
  store i32 1, ptr %7, align 4
  br label %52

52:                                               ; preds = %51, %48, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #3
  %53 = load i16, ptr %2, align 2
  ret i16 %53
}

declare zeroext i16 @pcan_timestamp_ticks() #2

declare i32 @pcan_flush_data(ptr noundef, ptr noundef, i32 noundef) #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 = { 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 }

!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 = !{!"short", !7, i64 0}
!11 = !{!12, !10, i64 898}
!12 = !{!"", !7, i64 0, !10, i64 896, !10, i64 898}
!13 = !{!14, !10, i64 6}
!14 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !10, i64 4, !10, i64 6, !15, i64 8, !7, i64 24, !7, i64 30, !7, i64 31, !16, i64 32}
!15 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !7, i64 8, !16, i64 12}
!16 = !{!"int", !7, i64 0}
!17 = !{!16, !16, 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"

%struct.anon = type { i8, i8, i8, i16, i16, %struct.anon.0, [6 x i8], i8, i8, i32 }
%struct.anon.0 = type { i8, i8, i8, i8, i8, i8, i8, i8, i8, i32 }
%struct.t_m2h_fsm = type { i8, i8, ptr, i32, i32 }

@pcan_device = external hidden local_unnamed_addr global %struct.anon, align 4
@data_fsm = external hidden global %struct.t_m2h_fsm, align 8

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

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i16 0, 2) i16 @pcan_record_buffer_flush(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call zeroext i16 @pcan_timestamp_ticks() #2
  %3 = getelementptr inbounds i8, ptr %0, i64 898
  %4 = load i16, ptr %3, align 2, !tbaa !5
  %5 = icmp ult i16 %4, 3
  br i1 %5, label %6, label %12

6:                                                ; preds = %1
  %7 = load i16, ptr getelementptr inbounds (i8, ptr @pcan_device, i64 6), align 2, !tbaa !10
  %.not11 = icmp ne i16 %7, 0
  %8 = sub i16 %2, %7
  %9 = icmp ugt i16 %8, 17
  %or.cond = select i1 %.not11, i1 %9, i1 false
  br i1 %or.cond, label %10, label %18

10:                                               ; preds = %6
  %11 = tail call i32 @pcan_flush_data(ptr noundef nonnull @data_fsm, ptr noundef null, i32 noundef 0) #2
  %.not12 = icmp eq i32 %11, 0
  br i1 %.not12, label %18, label %.sink.split

12:                                               ; preds = %1
  %13 = zext i16 %4 to i32
  %14 = add nuw nsw i32 %13, 63
  %15 = and i32 %14, 131008
  %16 = tail call i32 @pcan_flush_data(ptr noundef nonnull @data_fsm, ptr noundef nonnull %0, i32 noundef %15) #2
  %.not = icmp eq i32 %16, 0
  br i1 %.not, label %18, label %17

17:                                               ; preds = %12
  tail call void @pcan_record_buffer_reset(ptr noundef nonnull %0)
  br label %.sink.split

.sink.split:                                      ; preds = %10, %17
  %.sink = phi i16 [ %2, %17 ], [ 0, %10 ]
  %.0.ph = phi i16 [ 1, %17 ], [ 0, %10 ]
  store i16 %.sink, ptr getelementptr inbounds (i8, ptr @pcan_device, i64 6), align 2, !tbaa !10
  br label %18

18:                                               ; preds = %.sink.split, %12, %6, %10
  %.0 = phi i16 [ 0, %10 ], [ 0, %6 ], [ 0, %12 ], [ %.0.ph, %.sink.split ]
  ret i16 %.0
}

declare zeroext i16 @pcan_timestamp_ticks() local_unnamed_addr #1

declare i32 @pcan_flush_data(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #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 = { "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, !9, i64 898}
!6 = !{!"", !7, i64 0, !9, i64 896, !9, i64 898}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"short", !7, i64 0}
!10 = !{!11, !9, i64 6}
!11 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !9, i64 4, !9, i64 6, !12, i64 8, !7, i64 24, !7, i64 30, !7, i64 31, !13, i64 32}
!12 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !7, i64 8, !13, i64 12}
!13 = !{!"int", !7, i64 0}


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