; LLVM IR from golems/ach :: src/achd (client)

; 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.achd_cx = type { %struct.achd_headers, i32, i32, i32, i32, ptr, i32, %struct.ach_channel, ptr }
%struct.achd_headers = type { ptr, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i64, ptr, ptr, i32, i32, ptr }
%struct.ach_channel = type { %union.anon }
%union.anon = type { [16 x i64] }
%struct.timespec = type { i64, i64 }

@cx = external global %struct.achd_cx, align 8
@ach_verbosity = external global i32, align 4
@.str.16 = external hidden unnamed_addr constant [28 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 @ach_log(i32 noundef, ptr noundef, ...) #1

; Function Attrs: nounwind
declare ptr @strerror(i32 noundef) #2

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #3

; Function Attrs: nounwind uwtable
define dso_local void @achd_sleep_till(ptr noundef %0, i64 noundef %1) #4 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca %struct.timespec, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.timespec, ptr %9, i32 0, i32 1
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = load i64, ptr %4, align 8, !tbaa !9
  %13 = add nsw i64 %11, %12
  store i64 %13, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #5
  %14 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 0
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.timespec, ptr %15, i32 0, i32 0
  %17 = load i64, ptr %16, align 8, !tbaa !13
  %18 = load i64, ptr %5, align 8, !tbaa !9
  %19 = sdiv i64 %18, 1000000000
  %20 = add nsw i64 %17, %19
  store i64 %20, ptr %14, align 8, !tbaa !13
  %21 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 1
  %22 = load i64, ptr %5, align 8, !tbaa !9
  %23 = srem i64 %22, 1000000000
  store i64 %23, ptr %21, align 8, !tbaa !11
  br label %24

24:                                               ; preds = %53, %2
  %25 = load i32, ptr getelementptr inbounds (%struct.achd_cx, ptr @cx, i32 0, i32 6), align 8, !tbaa !14
  %26 = icmp ne i32 %25, 0
  %27 = xor i1 %26, true
  br i1 %27, label %28, label %55

28:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %29 = call i32 @clock_nanosleep(i32 noundef 1, i32 noundef 1, ptr noundef %6, ptr noundef null)
  store i32 %29, ptr %7, align 4, !tbaa !19
  %30 = load i32, ptr %7, align 4, !tbaa !19
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %40

32:                                               ; preds = %28
  %33 = load i32, ptr getelementptr inbounds (%struct.achd_cx, ptr @cx, i32 0, i32 6), align 8, !tbaa !14
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %40, label %35

35:                                               ; preds = %32
  %36 = call ptr @__errno_location() #6
  %37 = load i32, ptr %36, align 4, !tbaa !19
  %38 = icmp eq i32 4, %37
  br i1 %38, label %39, label %40

39:                                               ; preds = %35
  store i32 2, ptr %8, align 4
  br label %53, !llvm.loop !20

40:                                               ; preds = %35, %32, %28
  %41 = load i32, ptr %7, align 4, !tbaa !19
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %44, label %43

43:                                               ; preds = %40
  store i32 3, ptr %8, align 4
  br label %53

44:                                               ; preds = %40
  %45 = load i32, ptr @ach_verbosity, align 4, !tbaa !19
  %46 = add nsw i32 5, %45
  %47 = icmp sle i32 3, %46
  br i1 %47, label %48, label %52

48:                                               ; preds = %44
  %49 = call ptr @__errno_location() #6
  %50 = load i32, ptr %49, align 4, !tbaa !19
  %51 = call ptr @strerror(i32 noundef %50) #5
  call void (i32, ptr, ...) @ach_log(i32 noundef 3, ptr noundef @.str.16, ptr noundef %51)
  br label %52

52:                                               ; preds = %48, %44
  store i32 3, ptr %8, align 4
  br label %53

53:                                               ; preds = %52, %43, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  %54 = load i32, ptr %8, align 4
  switch i32 %54, label %56 [
    i32 2, label %24
    i32 3, label %55
  ]

55:                                               ; preds = %53, %24
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret void

56:                                               ; preds = %53
  unreachable
}

declare i32 @clock_nanosleep(i32 noundef, i32 noundef, 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 "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 willreturn memory(none) "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 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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(none) }

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"timespec", !10, i64 0, !10, i64 8}
!13 = !{!12, !10, i64 0}
!14 = !{!15, !17, i64 112}
!15 = !{!"achd_cx", !16, i64 0, !17, i64 88, !17, i64 92, !17, i64 96, !17, i64 100, !6, i64 104, !17, i64 112, !18, i64 120, !6, i64 248}
!16 = !{!"achd_headers", !6, i64 0, !6, i64 8, !17, i64 16, !17, i64 20, !17, i64 24, !17, i64 28, !17, i64 32, !17, i64 36, !17, i64 40, !17, i64 44, !10, i64 48, !6, i64 56, !6, i64 64, !17, i64 72, !17, i64 76, !6, i64 80}
!17 = !{!"int", !7, i64 0}
!18 = !{!"ach_channel", !7, i64 0}
!19 = !{!17, !17, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; 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.achd_cx = type { %struct.achd_headers, i32, i32, i32, i32, ptr, i32, %struct.ach_channel, ptr }
%struct.achd_headers = type { ptr, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i64, ptr, ptr, i32, i32, ptr }
%struct.ach_channel = type { %union.anon }
%union.anon = type { [16 x i64] }
%struct.timespec = type { i64, i64 }

@cx = external local_unnamed_addr global %struct.achd_cx, align 8
@ach_verbosity = external local_unnamed_addr global i32, align 4
@.str.16 = external hidden unnamed_addr constant [28 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 @ach_log(i32 noundef, ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: nounwind
declare ptr @strerror(i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local void @achd_sleep_till(ptr nocapture noundef readonly %0, i64 noundef %1) local_unnamed_addr #4 {
  %3 = alloca %struct.timespec, align 8
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = add nsw i64 %5, %1
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #5
  %7 = load i64, ptr %0, align 8, !tbaa !10
  %8 = sdiv i64 %6, 1000000000
  %9 = add nsw i64 %7, %8
  store i64 %9, ptr %3, align 8, !tbaa !10
  %10 = getelementptr inbounds i8, ptr %3, i64 8
  %11 = srem i64 %6, 1000000000
  store i64 %11, ptr %10, align 8, !tbaa !5
  %.old = load i32, ptr getelementptr inbounds (i8, ptr @cx, i64 112), align 8, !tbaa !11
  %.not.old = icmp eq i32 %.old, 0
  br i1 %.not.old, label %.preheader, label %.thread

.preheader:                                       ; preds = %2, %16
  %12 = call i32 @clock_nanosleep(i32 noundef 1, i32 noundef 1, ptr noundef nonnull %3, ptr noundef null) #5
  %13 = icmp eq i32 %12, 0
  %14 = load i32, ptr getelementptr inbounds (i8, ptr @cx, i64 112), align 8
  %15 = icmp ne i32 %14, 0
  %or.cond = select i1 %13, i1 true, i1 %15
  br i1 %or.cond, label %20, label %16

16:                                               ; preds = %.preheader
  %17 = tail call ptr @__errno_location() #6
  %18 = load i32, ptr %17, align 4, !tbaa !17
  %19 = icmp eq i32 %18, 4
  br i1 %19, label %.preheader, label %20, !llvm.loop !18

20:                                               ; preds = %16, %.preheader
  %.not8 = icmp ne i32 %12, 0
  %21 = load i32, ptr @ach_verbosity, align 4
  %22 = icmp sgt i32 %21, -3
  %or.cond12 = select i1 %.not8, i1 %22, i1 false
  br i1 %or.cond12, label %23, label %.thread

23:                                               ; preds = %20
  %24 = tail call ptr @__errno_location() #6
  %25 = load i32, ptr %24, align 4, !tbaa !17
  %26 = call ptr @strerror(i32 noundef %25) #5
  call void (i32, ptr, ...) @ach_log(i32 noundef 3, ptr noundef nonnull @.str.16, ptr noundef %26) #5
  br label %.thread

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

declare i32 @clock_nanosleep(i32 noundef, i32 noundef, 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 "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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(none) }

!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 8}
!6 = !{!"timespec", !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 0}
!11 = !{!12, !15, i64 112}
!12 = !{!"achd_cx", !13, i64 0, !15, i64 88, !15, i64 92, !15, i64 96, !15, i64 100, !14, i64 104, !15, i64 112, !16, i64 120, !14, i64 248}
!13 = !{!"achd_headers", !14, i64 0, !14, i64 8, !15, i64 16, !15, i64 20, !15, i64 24, !15, i64 28, !15, i64 32, !15, i64 36, !15, i64 40, !15, i64 44, !7, i64 48, !14, i64 56, !14, i64 64, !15, i64 72, !15, i64 76, !14, i64 80}
!14 = !{!"any pointer", !8, i64 0}
!15 = !{!"int", !8, i64 0}
!16 = !{!"ach_channel", !8, i64 0}
!17 = !{!15, !15, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass