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

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @pcan_record_buffer_request(ptr noundef, i16 noundef zeroext) #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
declare dso_local void @pcan_record_buffer_commit(ptr noundef, i16 noundef zeroext) #0

declare zeroext i16 @pcan_timestamp_ticks() #2

; Function Attrs: nounwind uwtable
define dso_local void @pcan_timesync_event(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i8, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  %7 = alloca i16, align 2
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #3
  store i8 66, ptr %3, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  store i8 0, ptr %5, align 1, !tbaa !9
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = call ptr @pcan_record_buffer_request(ptr noundef %8, i16 noundef zeroext 5)
  store ptr %9, ptr %4, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %13, label %12

12:                                               ; preds = %1
  store i32 1, ptr %6, align 4
  br label %106

13:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #3
  %14 = call zeroext i16 @pcan_timestamp_ticks()
  store i16 %14, ptr %7, align 2, !tbaa !10
  %15 = load i8, ptr %3, align 1, !tbaa !9
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i64 0
  store i8 %15, ptr %17, align 1, !tbaa !9
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %18, i64 1
  store ptr %19, ptr %4, align 8, !tbaa !5
  %20 = load i8, ptr %5, align 1, !tbaa !9
  %21 = zext i8 %20 to i32
  %22 = add nsw i32 %21, 1
  %23 = trunc i32 %22 to i8
  store i8 %23, ptr %5, align 1, !tbaa !9
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i64 0
  store i8 4, ptr %25, align 1, !tbaa !9
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 1
  store ptr %27, ptr %4, align 8, !tbaa !5
  %28 = load i8, ptr %5, align 1, !tbaa !9
  %29 = zext i8 %28 to i32
  %30 = add nsw i32 %29, 1
  %31 = trunc i32 %30 to i8
  store i8 %31, ptr %5, align 1, !tbaa !9
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i64 0
  store i8 1, ptr %33, align 1, !tbaa !9
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %34, i64 1
  store ptr %35, ptr %4, align 8, !tbaa !5
  %36 = load i8, ptr %5, align 1, !tbaa !9
  %37 = zext i8 %36 to i32
  %38 = add nsw i32 %37, 1
  %39 = trunc i32 %38 to i8
  store i8 %39, ptr %5, align 1, !tbaa !9
  br label %40

40:                                               ; preds = %13
  %41 = load i16, ptr %7, align 2, !tbaa !10
  %42 = zext i16 %41 to i32
  %43 = and i32 %42, 255
  %44 = trunc i32 %43 to i8
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds i8, ptr %45, i64 0
  store i8 %44, ptr %46, align 1, !tbaa !9
  %47 = load i16, ptr %7, align 2, !tbaa !10
  %48 = zext i16 %47 to i32
  %49 = ashr i32 %48, 8
  %50 = and i32 %49, 255
  %51 = trunc i32 %50 to i8
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds i8, ptr %52, i64 1
  store i8 %51, ptr %53, align 1, !tbaa !9
  br label %54

54:                                               ; preds = %40
  br label %55

55:                                               ; preds = %54
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds i8, ptr %56, i64 2
  store ptr %57, ptr %4, align 8, !tbaa !5
  %58 = load i8, ptr %5, align 1, !tbaa !9
  %59 = zext i8 %58 to i32
  %60 = add nsw i32 %59, 2
  %61 = trunc i32 %60 to i8
  store i8 %61, ptr %5, align 1, !tbaa !9
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = load i8, ptr %5, align 1, !tbaa !9
  %64 = zext i8 %63 to i16
  call void @pcan_record_buffer_commit(ptr noundef %62, i16 noundef zeroext %64)
  store i8 0, ptr %5, align 1, !tbaa !9
  %65 = load ptr, ptr %2, align 8, !tbaa !5
  %66 = call ptr @pcan_record_buffer_request(ptr noundef %65, i16 noundef zeroext 3)
  store ptr %66, ptr %4, align 8, !tbaa !5
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = icmp ne ptr %67, null
  br i1 %68, label %70, label %69

69:                                               ; preds = %55
  store i32 1, ptr %6, align 4
  br label %105

70:                                               ; preds = %55
  %71 = load i8, ptr getelementptr inbounds (%struct.anon.0, ptr getelementptr inbounds (%struct.anon, ptr @pcan_device, i32 0, i32 5), i32 0, i32 8), align 4, !tbaa !12
  %72 = zext i8 %71 to i32
  %73 = load i8, ptr getelementptr inbounds (%struct.anon, ptr @pcan_device, i32 0, i32 5), align 4, !tbaa !16
  %74 = zext i8 %73 to i32
  %75 = and i32 %74, %72
  %76 = trunc i32 %75 to i8
  store i8 %76, ptr getelementptr inbounds (%struct.anon, ptr @pcan_device, i32 0, i32 5), align 4, !tbaa !16
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  %78 = getelementptr inbounds i8, ptr %77, i64 0
  store i8 64, ptr %78, align 1, !tbaa !9
  %79 = load ptr, ptr %4, align 8, !tbaa !5
  %80 = getelementptr inbounds i8, ptr %79, i64 1
  store ptr %80, ptr %4, align 8, !tbaa !5
  %81 = load i8, ptr %5, align 1, !tbaa !9
  %82 = zext i8 %81 to i32
  %83 = add nsw i32 %82, 1
  %84 = trunc i32 %83 to i8
  store i8 %84, ptr %5, align 1, !tbaa !9
  %85 = load ptr, ptr %4, align 8, !tbaa !5
  %86 = getelementptr inbounds i8, ptr %85, i64 0
  store i8 1, ptr %86, align 1, !tbaa !9
  %87 = load ptr, ptr %4, align 8, !tbaa !5
  %88 = getelementptr inbounds i8, ptr %87, i64 1
  store ptr %88, ptr %4, align 8, !tbaa !5
  %89 = load i8, ptr %5, align 1, !tbaa !9
  %90 = zext i8 %89 to i32
  %91 = add nsw i32 %90, 1
  %92 = trunc i32 %91 to i8
  store i8 %92, ptr %5, align 1, !tbaa !9
  %93 = load i8, ptr getelementptr inbounds (%struct.anon, ptr @pcan_device, i32 0, i32 5), align 4, !tbaa !16
  %94 = load ptr, ptr %4, align 8, !tbaa !5
  %95 = getelementptr inbounds i8, ptr %94, i64 0
  store i8 %93, ptr %95, align 1, !tbaa !9
  %96 = load ptr, ptr %4, align 8, !tbaa !5
  %97 = getelementptr inbounds i8, ptr %96, i64 1
  store ptr %97, ptr %4, align 8, !tbaa !5
  %98 = load i8, ptr %5, align 1, !tbaa !9
  %99 = zext i8 %98 to i32
  %100 = add nsw i32 %99, 1
  %101 = trunc i32 %100 to i8
  store i8 %101, ptr %5, align 1, !tbaa !9
  %102 = load ptr, ptr %2, align 8, !tbaa !5
  %103 = load i8, ptr %5, align 1, !tbaa !9
  %104 = zext i8 %103 to i16
  call void @pcan_record_buffer_commit(ptr noundef %102, i16 noundef zeroext %104)
  store i8 0, ptr getelementptr inbounds (%struct.anon, ptr @pcan_device, i32 0, i32 5), align 4, !tbaa !16
  store i32 0, ptr %6, align 4
  br label %105

105:                                              ; preds = %70, %69
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #3
  br label %106

106:                                              ; preds = %105, %12
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #3
  %107 = load i32, ptr %6, align 4
  switch i32 %107, label %109 [
    i32 0, label %108
    i32 1, label %108
  ]

108:                                              ; preds = %106, %106
  ret void

109:                                              ; preds = %106
  unreachable
}

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


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

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @pcan_record_buffer_request(ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @pcan_record_buffer_commit(ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

declare zeroext i16 @pcan_timestamp_ticks() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @pcan_timesync_event(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call ptr @pcan_record_buffer_request(ptr noundef %0, i16 noundef zeroext 5)
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %20, label %3

3:                                                ; preds = %1
  %4 = tail call zeroext i16 @pcan_timestamp_ticks() #2
  store i8 66, ptr %2, align 1, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %2, i64 1
  store i8 4, ptr %5, align 1, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %2, i64 2
  store i8 1, ptr %6, align 1, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %2, i64 3
  %8 = trunc i16 %4 to i8
  store i8 %8, ptr %7, align 1, !tbaa !5
  %9 = lshr i16 %4, 8
  %10 = trunc nuw i16 %9 to i8
  %11 = getelementptr inbounds i8, ptr %2, i64 4
  store i8 %10, ptr %11, align 1, !tbaa !5
  tail call void @pcan_record_buffer_commit(ptr noundef %0, i16 noundef zeroext 5)
  %12 = tail call ptr @pcan_record_buffer_request(ptr noundef %0, i16 noundef zeroext 3)
  %.not33 = icmp eq ptr %12, null
  br i1 %.not33, label %20, label %13

13:                                               ; preds = %3
  %14 = load i8, ptr getelementptr inbounds (i8, ptr @pcan_device, i64 16), align 4, !tbaa !8
  %15 = load i8, ptr getelementptr inbounds (i8, ptr @pcan_device, i64 8), align 4, !tbaa !13
  %16 = and i8 %15, %14
  store i8 %16, ptr getelementptr inbounds (i8, ptr @pcan_device, i64 8), align 4, !tbaa !13
  store i8 64, ptr %12, align 1, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %12, i64 1
  store i8 1, ptr %17, align 1, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %12, i64 2
  %19 = load i8, ptr getelementptr inbounds (i8, ptr @pcan_device, i64 8), align 4, !tbaa !13
  store i8 %19, ptr %18, align 1, !tbaa !5
  tail call void @pcan_record_buffer_commit(ptr noundef %0, i16 noundef zeroext 3)
  store i8 0, ptr getelementptr inbounds (i8, ptr @pcan_device, i64 8), align 4, !tbaa !13
  br label %20

20:                                               ; preds = %13, %3, %1
  ret void
}

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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !6, i64 16}
!9 = !{!"", !6, i64 0, !6, i64 1, !6, i64 2, !10, i64 4, !10, i64 6, !11, i64 8, !6, i64 24, !6, i64 30, !6, i64 31, !12, i64 32}
!10 = !{!"short", !6, i64 0}
!11 = !{!"", !6, i64 0, !6, i64 1, !6, i64 2, !6, i64 3, !6, i64 4, !6, i64 5, !6, i64 6, !6, i64 7, !6, i64 8, !12, i64 12}
!12 = !{!"int", !6, i64 0}
!13 = !{!9, !6, i64 8}


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