; LLVM IR from nelhage/reptyr :: attach (wait_for_stop)

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

@.str.4 = external hidden unnamed_addr constant [34 x i8], align 1

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

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

declare void @error(ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
define dso_local void @wait_for_stop(i32 noundef %0, i32 noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca %struct.timeval, align 8
  %6 = alloca %struct.timeval, align 8
  %7 = alloca %struct.timespec, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #4
  %8 = call i32 @gettimeofday(ptr noundef %5, ptr noundef null) #4
  br label %9

9:                                                ; preds = %37, %2
  br label %10

10:                                               ; preds = %9
  %11 = call i32 @gettimeofday(ptr noundef %6, ptr noundef null) #4
  %12 = getelementptr inbounds %struct.timeval, ptr %6, i32 0, i32 0
  %13 = load i64, ptr %12, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.timeval, ptr %5, i32 0, i32 0
  %15 = load i64, ptr %14, align 8, !tbaa !9
  %16 = icmp sgt i64 %13, %15
  br i1 %16, label %17, label %23

17:                                               ; preds = %10
  %18 = getelementptr inbounds %struct.timeval, ptr %6, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !12
  %20 = getelementptr inbounds %struct.timeval, ptr %5, i32 0, i32 1
  %21 = load i64, ptr %20, align 8, !tbaa !12
  %22 = icmp sgt i64 %19, %21
  br i1 %22, label %30, label %23

23:                                               ; preds = %17, %10
  %24 = getelementptr inbounds %struct.timeval, ptr %6, i32 0, i32 0
  %25 = load i64, ptr %24, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.timeval, ptr %5, i32 0, i32 0
  %27 = load i64, ptr %26, align 8, !tbaa !9
  %28 = sub nsw i64 %25, %27
  %29 = icmp sgt i64 %28, 1
  br i1 %29, label %30, label %31

30:                                               ; preds = %23, %17
  call void (ptr, ...) @error(ptr noundef @.str.4)
  br label %41

31:                                               ; preds = %23
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = call i32 @check_proc_stopped(i32 noundef %32, i32 noundef %33)
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %31
  br label %41

37:                                               ; preds = %31
  %38 = getelementptr inbounds %struct.timespec, ptr %7, i32 0, i32 0
  store i64 0, ptr %38, align 8, !tbaa !13
  %39 = getelementptr inbounds %struct.timespec, ptr %7, i32 0, i32 1
  store i64 10000000, ptr %39, align 8, !tbaa !15
  %40 = call i32 @nanosleep(ptr noundef %7, ptr noundef null)
  br label %9

41:                                               ; preds = %36, %30
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #4
  ret void
}

; Function Attrs: nounwind
declare i32 @gettimeofday(ptr noundef, ptr noundef) #3

declare i32 @check_proc_stopped(i32 noundef, i32 noundef) #1

declare i32 @nanosleep(ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #3 = { 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 #4 = { 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, !11, i64 0}
!10 = !{!"timeval", !11, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!14, !11, i64 0}
!14 = !{!"timespec", !11, i64 0, !11, i64 8}
!15 = !{!14, !11, 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.timeval = type { i64, i64 }
%struct.timespec = type { i64, i64 }

@.str.4 = external hidden unnamed_addr constant [34 x i8], align 1

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

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

declare void @error(ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @wait_for_stop(i32 noundef %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = alloca %struct.timeval, align 8
  %4 = alloca %struct.timeval, align 8
  %5 = alloca %struct.timespec, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5) #4
  %6 = call i32 @gettimeofday(ptr noundef nonnull %3, ptr noundef null) #4
  %7 = load i64, ptr %3, align 8
  %8 = getelementptr inbounds i8, ptr %4, i64 8
  %9 = getelementptr inbounds i8, ptr %3, i64 8
  %10 = load i64, ptr %9, align 8
  %11 = getelementptr inbounds i8, ptr %5, i64 8
  br label %12

12:                                               ; preds = %25, %2
  %13 = call i32 @gettimeofday(ptr noundef nonnull %4, ptr noundef null) #4
  %14 = load i64, ptr %4, align 8, !tbaa !5
  %15 = icmp sgt i64 %14, %7
  br i1 %15, label %16, label %21

16:                                               ; preds = %12
  %17 = load i64, ptr %8, align 8, !tbaa !10
  %18 = icmp sgt i64 %17, %10
  %19 = sub nsw i64 %14, %7
  %20 = icmp sgt i64 %19, 1
  %or.cond = select i1 %18, i1 true, i1 %20
  br i1 %or.cond, label %22, label %23

21:                                               ; preds = %12
  %.old = sub nsw i64 %14, %7
  %.old2 = icmp sgt i64 %.old, 1
  br i1 %.old2, label %22, label %23

22:                                               ; preds = %21, %16
  call void (ptr, ...) @error(ptr noundef nonnull @.str.4) #4
  br label %.loopexit

23:                                               ; preds = %16, %21
  %24 = call i32 @check_proc_stopped(i32 noundef %0, i32 noundef %1) #4
  %.not = icmp eq i32 %24, 0
  br i1 %.not, label %25, label %.loopexit

25:                                               ; preds = %23
  store i64 0, ptr %5, align 8, !tbaa !11
  store i64 10000000, ptr %11, align 8, !tbaa !13
  %26 = call i32 @nanosleep(ptr noundef nonnull %5, ptr noundef null) #4
  br label %12

.loopexit:                                        ; preds = %23, %22
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #4
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @gettimeofday(ptr nocapture noundef, ptr nocapture noundef) local_unnamed_addr #3

declare i32 @check_proc_stopped(i32 noundef, i32 noundef) local_unnamed_addr #1

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #3 = { 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 #4 = { 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, !7, i64 0}
!6 = !{!"timeval", !7, i64 0, !7, i64 8}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!12, !7, i64 0}
!12 = !{!"timespec", !7, i64 0, !7, i64 8}
!13 = !{!12, !7, i64 8}


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