; 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.PCAN_RECORD_BUFFER_EX = type { [896 x i8], i16, i16 }

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

; Function Attrs: nounwind uwtable
define dso_local ptr @pcan_record_buffer_request(ptr noundef %0, i16 noundef zeroext %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i16 %1, ptr %5, align 2, !tbaa !9
  %9 = load i16, ptr %5, align 2, !tbaa !9
  %10 = zext i16 %9 to i32
  %11 = icmp sgt i32 %10, 62
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  br label %82

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #3
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %14, i32 0, i32 2
  %16 = load i16, ptr %15, align 2, !tbaa !11
  %17 = zext i16 %16 to i32
  %18 = and i32 %17, 63
  %19 = sub nsw i32 64, %18
  %20 = trunc i32 %19 to i16
  store i16 %20, ptr %6, align 2, !tbaa !9
  %21 = load i16, ptr %6, align 2, !tbaa !9
  %22 = zext i16 %21 to i32
  %23 = load i16, ptr %5, align 2, !tbaa !9
  %24 = zext i16 %23 to i32
  %25 = icmp slt i32 %22, %24
  br i1 %25, label %26, label %63

26:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #3
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %27, i32 0, i32 2
  %29 = load i16, ptr %28, align 2, !tbaa !11
  %30 = zext i16 %29 to i32
  %31 = add nsw i32 %30, 63
  %32 = and i32 %31, -64
  %33 = trunc i32 %32 to i8
  store i8 %33, ptr %7, align 1, !tbaa !13
  %34 = load i8, ptr %7, align 1, !tbaa !13
  %35 = zext i8 %34 to i32
  %36 = add nsw i32 %35, 2
  %37 = icmp sge i32 %36, 896
  br i1 %37, label %38, label %39

38:                                               ; preds = %26
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %60

39:                                               ; preds = %26
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %40, i32 0, i32 0
  %42 = load i8, ptr %7, align 1, !tbaa !13
  %43 = zext i8 %42 to i64
  %44 = getelementptr inbounds [896 x i8], ptr %41, i64 0, i64 %43
  call void @llvm.memset.p0.i64(ptr align 1 %44, i8 0, i64 64, i1 false)
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %45, i32 0, i32 0
  %47 = load i8, ptr %7, align 1, !tbaa !13
  %48 = zext i8 %47 to i64
  %49 = getelementptr inbounds [896 x i8], ptr %46, i64 0, i64 %48
  call void @pcan_record_write_header(ptr noundef %49)
  %50 = load i8, ptr %7, align 1, !tbaa !13
  %51 = zext i8 %50 to i32
  %52 = add nsw i32 %51, 2
  %53 = trunc i32 %52 to i8
  store i8 %53, ptr %7, align 1, !tbaa !13
  %54 = load i8, ptr %7, align 1, !tbaa !13
  %55 = zext i8 %54 to i16
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %56, i32 0, i32 2
  store i16 %55, ptr %57, align 2, !tbaa !11
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %58, i32 0, i32 1
  store i16 0, ptr %59, align 2, !tbaa !14
  store i32 0, ptr %8, align 4
  br label %60

60:                                               ; preds = %39, %38
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #3
  %61 = load i32, ptr %8, align 4
  switch i32 %61, label %81 [
    i32 0, label %62
  ]

62:                                               ; preds = %60
  br label %63

63:                                               ; preds = %62, %13
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %64, i32 0, i32 2
  %66 = load i16, ptr %65, align 2, !tbaa !11
  %67 = zext i16 %66 to i32
  %68 = load i16, ptr %5, align 2, !tbaa !9
  %69 = zext i16 %68 to i32
  %70 = add nsw i32 %67, %69
  %71 = icmp sgt i32 %70, 896
  br i1 %71, label %72, label %73

72:                                               ; preds = %63
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %81

73:                                               ; preds = %63
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %74, i32 0, i32 0
  %76 = load ptr, ptr %4, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.PCAN_RECORD_BUFFER_EX, ptr %76, i32 0, i32 2
  %78 = load i16, ptr %77, align 2, !tbaa !11
  %79 = zext i16 %78 to i64
  %80 = getelementptr inbounds [896 x i8], ptr %75, i64 0, i64 %79
  store ptr %80, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %81

81:                                               ; preds = %73, %72, %60
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #3
  br label %82

82:                                               ; preds = %81, %12
  %83 = load ptr, ptr %3, align 8
  ret ptr %83
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!7, !7, i64 0}
!14 = !{!12, !10, i64 896}


; 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 dso_local void @pcan_record_write_header(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @pcan_record_buffer_request(ptr noundef %0, i16 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = zext i16 %1 to i32
  %4 = icmp ugt i16 %1, 62
  br i1 %4, label %27, label %5

5:                                                ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 898
  %7 = load i16, ptr %6, align 2, !tbaa !5
  %8 = and i16 %7, 63
  %9 = sub nuw nsw i16 64, %8
  %10 = icmp ult i16 %9, %1
  br i1 %10, label %11, label %20

11:                                               ; preds = %5
  %12 = trunc i16 %7 to i8
  %13 = add i8 %12, 63
  %14 = and i8 %13, -64
  %15 = or disjoint i8 %14, 2
  %16 = zext i8 %14 to i64
  %17 = getelementptr inbounds [896 x i8], ptr %0, i64 0, i64 %16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(64) %17, i8 0, i64 64, i1 false)
  tail call void @pcan_record_write_header(ptr noundef %17)
  %18 = zext i8 %15 to i16
  store i16 %18, ptr %6, align 2, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %0, i64 896
  store i16 0, ptr %19, align 2, !tbaa !10
  br label %20

20:                                               ; preds = %11, %5
  %21 = phi i16 [ %18, %11 ], [ %7, %5 ]
  %22 = zext i16 %21 to i32
  %23 = add nuw nsw i32 %22, %3
  %24 = icmp ugt i32 %23, 896
  %25 = zext i16 %21 to i64
  %26 = getelementptr inbounds [896 x i8], ptr %0, i64 0, i64 %25
  %.1 = select i1 %24, ptr null, ptr %26
  br label %27

27:                                               ; preds = %2, %20
  %.2 = phi ptr [ %.1, %20 ], [ null, %2 ]
  ret ptr %.2
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 nounwind willreturn memory(argmem: write) }

!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 = !{!6, !9, i64 896}


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