; LLVM IR from aap/pdp6 :: emu (rtc)

; 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.timespec = type { i64, i64 }
%struct.RtcMsg = type { i32, i32, ptr, i64 }
%struct.Clockchan = type { i32, ptr, %struct.timespec, %struct.timespec, %struct.timespec, i32, ptr }

@.str = external hidden unnamed_addr constant [4 x i8], align 1
@__const.rtcthread.slp = external hidden unnamed_addr constant %struct.timespec, align 8
@channels = external hidden global ptr, align 8
@rtcchan = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local { i64, i64 } @timeadd(i64, i64, i64, i64) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @timebefore(i64, i64, i64, i64) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @rtcthread(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca %struct.timespec, align 8
  %4 = alloca ptr, align 8
  %5 = alloca %struct.RtcMsg, align 8
  %6 = alloca i32, align 4
  %7 = alloca %struct.timespec, align 8
  %8 = alloca %struct.timespec, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %3) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 24, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  call void @threadname(ptr noundef @.str)
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %7, ptr align 8 @__const.rtcthread.slp, i64 16, i1 false)
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %93, %1
  %10 = call i32 @clock_gettime(i32 noundef 0, ptr noundef %3) #5
  %11 = load ptr, ptr @channels, align 8, !tbaa !5
  store ptr %11, ptr %4, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %70, %9
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %74

15:                                               ; preds = %12
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.Clockchan, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8, !tbaa !11
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %20, label %33

20:                                               ; preds = %15
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Clockchan, ptr %21, i32 0, i32 3
  %23 = getelementptr inbounds { i64, i64 }, ptr %22, i32 0, i32 0
  %24 = load i64, ptr %23, align 8
  %25 = getelementptr inbounds { i64, i64 }, ptr %22, i32 0, i32 1
  %26 = load i64, ptr %25, align 8
  %27 = getelementptr inbounds { i64, i64 }, ptr %3, i32 0, i32 0
  %28 = load i64, ptr %27, align 8
  %29 = getelementptr inbounds { i64, i64 }, ptr %3, i32 0, i32 1
  %30 = load i64, ptr %29, align 8
  %31 = call i32 @timebefore(i64 %24, i64 %26, i64 %28, i64 %30)
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %34

33:                                               ; preds = %20, %15
  br label %70

34:                                               ; preds = %20
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.Clockchan, ptr %35, i32 0, i32 1
  %37 = load ptr, ptr %36, align 8, !tbaa !15
  %38 = call i32 @channbsend(ptr noundef %37, ptr noundef %6)
  %39 = icmp eq i32 %38, 0
  br i1 %39, label %40, label %41

40:                                               ; preds = %34
  br label %41

41:                                               ; preds = %40, %34
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.Clockchan, ptr %42, i32 0, i32 5
  %44 = load i32, ptr %43, align 8, !tbaa !16
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %66

46:                                               ; preds = %41
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.Clockchan, ptr %47, i32 0, i32 3
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #5
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.Clockchan, ptr %49, i32 0, i32 3
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.Clockchan, ptr %51, i32 0, i32 4
  %53 = getelementptr inbounds { i64, i64 }, ptr %50, i32 0, i32 0
  %54 = load i64, ptr %53, align 8
  %55 = getelementptr inbounds { i64, i64 }, ptr %50, i32 0, i32 1
  %56 = load i64, ptr %55, align 8
  %57 = getelementptr inbounds { i64, i64 }, ptr %52, i32 0, i32 0
  %58 = load i64, ptr %57, align 8
  %59 = getelementptr inbounds { i64, i64 }, ptr %52, i32 0, i32 1
  %60 = load i64, ptr %59, align 8
  %61 = call { i64, i64 } @timeadd(i64 %54, i64 %56, i64 %58, i64 %60)
  %62 = getelementptr inbounds { i64, i64 }, ptr %8, i32 0, i32 0
  %63 = extractvalue { i64, i64 } %61, 0
  store i64 %63, ptr %62, align 8
  %64 = getelementptr inbounds { i64, i64 }, ptr %8, i32 0, i32 1
  %65 = extractvalue { i64, i64 } %61, 1
  store i64 %65, ptr %64, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %48, ptr align 8 %8, i64 16, i1 false), !tbaa.struct !17
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #5
  br label %69

66:                                               ; preds = %41
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.Clockchan, ptr %67, i32 0, i32 0
  store i32 0, ptr %68, align 8, !tbaa !11
  br label %69

69:                                               ; preds = %66, %46
  br label %70

70:                                               ; preds = %69, %33
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.Clockchan, ptr %71, i32 0, i32 6
  %73 = load ptr, ptr %72, align 8, !tbaa !19
  store ptr %73, ptr %4, align 8, !tbaa !5
  br label %12, !llvm.loop !20

74:                                               ; preds = %12
  %75 = load ptr, ptr @rtcchan, align 8, !tbaa !5
  %76 = call i32 @channbrecv(ptr noundef %75, ptr noundef %5)
  %77 = icmp eq i32 %76, 1
  br i1 %77, label %78, label %93

78:                                               ; preds = %74
  %79 = getelementptr inbounds %struct.RtcMsg, ptr %5, i32 0, i32 0
  %80 = load i32, ptr %79, align 8, !tbaa !22
  %81 = icmp eq i32 %80, 1
  br i1 %81, label %82, label %89

82:                                               ; preds = %78
  %83 = getelementptr inbounds %struct.RtcMsg, ptr %5, i32 0, i32 2
  %84 = load ptr, ptr %83, align 8, !tbaa !24
  %85 = getelementptr inbounds %struct.RtcMsg, ptr %5, i32 0, i32 3
  %86 = load i64, ptr %85, align 8, !tbaa !25
  %87 = getelementptr inbounds %struct.RtcMsg, ptr %5, i32 0, i32 1
  %88 = load i32, ptr %87, align 4, !tbaa !26
  call void @rtcstart(ptr noundef %84, i64 noundef %86, i32 noundef %88)
  br label %92

89:                                               ; preds = %78
  %90 = getelementptr inbounds %struct.RtcMsg, ptr %5, i32 0, i32 2
  %91 = load ptr, ptr %90, align 8, !tbaa !24
  call void @rtcstop(ptr noundef %91)
  br label %92

92:                                               ; preds = %89, %82
  br label %93

93:                                               ; preds = %92, %74
  %94 = call i32 @nanosleep(ptr noundef %7, ptr noundef null)
  br label %9
}

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

declare void @threadname(ptr noundef) #2

; 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) #3

; Function Attrs: nounwind
declare i32 @clock_gettime(i32 noundef, ptr noundef) #4

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

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

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

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

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

declare i32 @nanosleep(ptr noundef, ptr 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"Clockchan", !10, i64 0, !6, i64 8, !13, i64 16, !13, i64 32, !13, i64 48, !10, i64 64, !6, i64 72}
!13 = !{!"timespec", !14, i64 0, !14, i64 8}
!14 = !{!"long", !7, i64 0}
!15 = !{!12, !6, i64 8}
!16 = !{!12, !10, i64 64}
!17 = !{i64 0, i64 8, !18, i64 8, i64 8, !18}
!18 = !{!14, !14, i64 0}
!19 = !{!12, !6, i64 72}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!23, !10, i64 0}
!23 = !{!"RtcMsg", !10, i64 0, !10, i64 4, !6, i64 8, !14, i64 16}
!24 = !{!23, !6, i64 8}
!25 = !{!23, !14, i64 16}
!26 = !{!23, !10, i64 4}


; 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.timespec = type { i64, i64 }
%struct.RtcMsg = type { i32, i32, ptr, i64 }

@.str = external hidden unnamed_addr constant [4 x i8], align 1
@__const.rtcthread.slp = external hidden unnamed_addr constant %struct.timespec, align 8
@channels = external hidden local_unnamed_addr global ptr, align 8
@rtcchan = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local { i64, i64 } @timeadd(i64, i64, i64, i64) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @timebefore(i64, i64, i64, i64) local_unnamed_addr #0

; Function Attrs: noreturn nounwind uwtable
define dso_local noalias noundef nonnull ptr @rtcthread(ptr nocapture noundef readnone %0) local_unnamed_addr #1 {
  %2 = alloca %struct.timespec, align 8
  %3 = alloca %struct.RtcMsg, align 8
  %4 = alloca i32, align 4
  %5 = alloca %struct.timespec, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #6
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #6
  tail call void @threadname(ptr noundef nonnull @.str) #6
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5) #6
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %5, ptr noundef nonnull align 8 dereferenceable(16) @__const.rtcthread.slp, i64 16, i1 false)
  store i32 0, ptr %4, align 4, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %2, i64 8
  %7 = getelementptr inbounds i8, ptr %3, i64 8
  %8 = getelementptr inbounds i8, ptr %3, i64 16
  %9 = getelementptr inbounds i8, ptr %3, i64 4
  br label %10

10:                                               ; preds = %51, %1
  %11 = call i32 @clock_gettime(i32 noundef 0, ptr noundef nonnull %2) #6
  %.013 = load ptr, ptr @channels, align 8, !tbaa !9
  %.not14 = icmp eq ptr %.013, null
  br i1 %.not14, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %10, %38
  %.015 = phi ptr [ %.0, %38 ], [ %.013, %10 ]
  %12 = load i32, ptr %.015, align 8, !tbaa !11
  %.not10 = icmp eq i32 %12, 0
  br i1 %.not10, label %38, label %13

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %.015, i64 32
  %15 = load i64, ptr %14, align 8
  %16 = getelementptr inbounds i8, ptr %.015, i64 40
  %17 = load i64, ptr %16, align 8
  %18 = load i64, ptr %2, align 8
  %19 = load i64, ptr %6, align 8
  %20 = call i32 @timebefore(i64 %15, i64 %17, i64 %18, i64 %19)
  %.not11 = icmp eq i32 %20, 0
  br i1 %.not11, label %21, label %38

21:                                               ; preds = %13
  %22 = getelementptr inbounds i8, ptr %.015, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !15
  %24 = call i32 @channbsend(ptr noundef %23, ptr noundef nonnull %4) #6
  %25 = getelementptr inbounds i8, ptr %.015, i64 64
  %26 = load i32, ptr %25, align 8, !tbaa !16
  %.not12 = icmp eq i32 %26, 0
  br i1 %.not12, label %37, label %27

27:                                               ; preds = %21
  %28 = getelementptr inbounds i8, ptr %.015, i64 48
  %29 = load i64, ptr %14, align 8
  %30 = load i64, ptr %16, align 8
  %31 = load i64, ptr %28, align 8
  %32 = getelementptr inbounds i8, ptr %.015, i64 56
  %33 = load i64, ptr %32, align 8
  %34 = call { i64, i64 } @timeadd(i64 %29, i64 %30, i64 %31, i64 %33)
  %35 = extractvalue { i64, i64 } %34, 0
  %36 = extractvalue { i64, i64 } %34, 1
  store i64 %35, ptr %14, align 8, !tbaa !17
  store i64 %36, ptr %16, align 8, !tbaa !17
  br label %38

37:                                               ; preds = %21
  store i32 0, ptr %.015, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %27, %37, %.lr.ph, %13
  %39 = getelementptr inbounds i8, ptr %.015, i64 72
  %.0 = load ptr, ptr %39, align 8, !tbaa !9
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !18

._crit_edge:                                      ; preds = %38, %10
  %40 = load ptr, ptr @rtcchan, align 8, !tbaa !9
  %41 = call i32 @channbrecv(ptr noundef %40, ptr noundef nonnull %3) #6
  %42 = icmp eq i32 %41, 1
  br i1 %42, label %43, label %51

43:                                               ; preds = %._crit_edge
  %44 = load i32, ptr %3, align 8, !tbaa !20
  %45 = icmp eq i32 %44, 1
  %46 = load ptr, ptr %7, align 8, !tbaa !22
  br i1 %45, label %47, label %50

47:                                               ; preds = %43
  %48 = load i64, ptr %8, align 8, !tbaa !23
  %49 = load i32, ptr %9, align 4, !tbaa !24
  call void @rtcstart(ptr noundef %46, i64 noundef %48, i32 noundef %49)
  br label %51

50:                                               ; preds = %43
  call void @rtcstop(ptr noundef %46)
  br label %51

51:                                               ; preds = %47, %50, %._crit_edge
  %52 = call i32 @nanosleep(ptr noundef nonnull %5, ptr noundef null) #6
  br label %10
}

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

declare void @threadname(ptr noundef) local_unnamed_addr #3

; 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) #4

; Function Attrs: nounwind
declare i32 @clock_gettime(i32 noundef, ptr noundef) local_unnamed_addr #5

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

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

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

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

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

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 = { noreturn 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"Clockchan", !6, i64 0, !10, i64 8, !13, i64 16, !13, i64 32, !13, i64 48, !6, i64 64, !10, i64 72}
!13 = !{!"timespec", !14, i64 0, !14, i64 8}
!14 = !{!"long", !7, i64 0}
!15 = !{!12, !10, i64 8}
!16 = !{!12, !6, i64 64}
!17 = !{!14, !14, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!21, !6, i64 0}
!21 = !{!"RtcMsg", !6, i64 0, !6, i64 4, !10, i64 8, !14, i64 16}
!22 = !{!21, !10, i64 8}
!23 = !{!21, !14, i64 16}
!24 = !{!21, !6, i64 4}


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