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

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

@cx = external global %struct.achd_cx, align 8

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @achd_write(i32 noundef %0, ptr noundef %1, i64 noundef %2) #2 {
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !11
  br label %11

11:                                               ; preds = %48, %46, %3
  %12 = load i32, ptr getelementptr inbounds (%struct.achd_cx, ptr @cx, i32 0, i32 6), align 8, !tbaa !13
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %18, label %14

14:                                               ; preds = %11
  %15 = load i64, ptr %8, align 8, !tbaa !11
  %16 = load i64, ptr %7, align 8, !tbaa !11
  %17 = icmp ult i64 %15, %16
  br label %18

18:                                               ; preds = %14, %11
  %19 = phi i1 [ false, %11 ], [ %17, %14 ]
  br i1 %19, label %20, label %49

20:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = load ptr, ptr %6, align 8, !tbaa !9
  %23 = load i64, ptr %8, align 8, !tbaa !11
  %24 = getelementptr inbounds i8, ptr %22, i64 %23
  %25 = load i64, ptr %7, align 8, !tbaa !11
  %26 = load i64, ptr %8, align 8, !tbaa !11
  %27 = sub i64 %25, %26
  %28 = call i64 @write(i32 noundef %21, ptr noundef %24, i64 noundef %27)
  store i64 %28, ptr %9, align 8, !tbaa !11
  %29 = load i64, ptr %9, align 8, !tbaa !11
  %30 = icmp sgt i64 %29, 0
  br i1 %30, label %31, label %35

31:                                               ; preds = %20
  %32 = load i64, ptr %9, align 8, !tbaa !11
  %33 = load i64, ptr %8, align 8, !tbaa !11
  %34 = add i64 %33, %32
  store i64 %34, ptr %8, align 8, !tbaa !11
  br label %45

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

39:                                               ; preds = %35
  %40 = load i32, ptr getelementptr inbounds (%struct.achd_cx, ptr @cx, i32 0, i32 6), align 8, !tbaa !13
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %43, label %42

42:                                               ; preds = %39
  store i32 2, ptr %10, align 4
  br label %46, !llvm.loop !17

43:                                               ; preds = %39, %35
  %44 = load i64, ptr %9, align 8, !tbaa !11
  store i64 %44, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %46

45:                                               ; preds = %31
  store i32 0, ptr %10, align 4
  br label %46

46:                                               ; preds = %45, %43, %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  %47 = load i32, ptr %10, align 4
  switch i32 %47, label %57 [
    i32 0, label %48
    i32 2, label %11
  ]

48:                                               ; preds = %46
  br label %11, !llvm.loop !17

49:                                               ; preds = %18
  br label %50

50:                                               ; preds = %54, %49
  %51 = load i64, ptr %8, align 8, !tbaa !11
  %52 = load i64, ptr %7, align 8, !tbaa !11
  %53 = icmp ult i64 %51, %52
  br i1 %53, label %54, label %55

54:                                               ; preds = %50
  br label %50, !llvm.loop !19

55:                                               ; preds = %50
  %56 = load i64, ptr %7, align 8, !tbaa !11
  store i64 %56, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %57

57:                                               ; preds = %55, %46
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %58 = load i64, ptr %4, align 8
  ret i64 %58
}

declare i64 @write(i32 noundef, ptr noundef, i64 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { "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 }
attributes #5 = { 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 = !{!"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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 112}
!14 = !{!"achd_cx", !15, i64 0, !6, i64 88, !6, i64 92, !6, i64 96, !6, i64 100, !10, i64 104, !6, i64 112, !16, i64 120, !10, i64 248}
!15 = !{!"achd_headers", !10, i64 0, !10, i64 8, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !6, i64 44, !12, i64 48, !10, i64 56, !10, i64 64, !6, i64 72, !6, i64 76, !10, i64 80}
!16 = !{!"ach_channel", !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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

@cx = external local_unnamed_addr global %struct.achd_cx, align 8

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

; Function Attrs: nofree nounwind uwtable
define dso_local noundef i64 @achd_write(i32 noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = load i32, ptr getelementptr inbounds (i8, ptr @cx, i64 112), align 8, !tbaa !5
  %.not28 = icmp eq i32 %4, 0
  %5 = icmp ne i64 %2, 0
  %6 = and i1 %.not28, %5
  br i1 %6, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %19, %3
  %.lcssa = phi i1 [ %5, %3 ], [ %21, %19 ]
  br i1 %.lcssa, label %.preheader.split, label %.thread, !llvm.loop !14

.lr.ph:                                           ; preds = %3, %19
  %.01729 = phi i64 [ %.118, %19 ], [ 0, %3 ]
  %7 = getelementptr inbounds i8, ptr %1, i64 %.01729
  %8 = sub i64 %2, %.01729
  %9 = tail call i64 @write(i32 noundef %0, ptr noundef %7, i64 noundef %8) #3
  %10 = icmp sgt i64 %9, 0
  br i1 %10, label %11, label %13

11:                                               ; preds = %.lr.ph
  %12 = add i64 %9, %.01729
  %.pre = load i32, ptr getelementptr inbounds (i8, ptr @cx, i64 112), align 8, !tbaa !5
  br label %19

13:                                               ; preds = %.lr.ph
  %14 = tail call ptr @__errno_location() #4
  %15 = load i32, ptr %14, align 4, !tbaa !16
  %16 = icmp ne i32 %15, 4
  %17 = load i32, ptr getelementptr inbounds (i8, ptr @cx, i64 112), align 8
  %18 = icmp ne i32 %17, 0
  %or.cond = select i1 %16, i1 true, i1 %18
  br i1 %or.cond, label %.thread, label %19

19:                                               ; preds = %13, %11
  %20 = phi i32 [ %.pre, %11 ], [ 0, %13 ]
  %.118 = phi i64 [ %12, %11 ], [ %.01729, %13 ]
  %.not = icmp eq i32 %20, 0
  %21 = icmp ult i64 %.118, %2
  %22 = and i1 %.not, %21
  br i1 %22, label %.lr.ph, label %.preheader

.preheader.split:                                 ; preds = %.preheader, %.preheader.split
  br label %.preheader.split

.thread:                                          ; preds = %13, %.preheader
  %.2 = phi i64 [ %2, %.preheader ], [ %9, %13 ]
  ret i64 %.2
}

; Function Attrs: nofree
declare noundef i64 @write(i32 noundef, ptr nocapture noundef readonly, i64 noundef) local_unnamed_addr #2

attributes #0 = { 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 #1 = { nofree 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 = { nofree "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 }
attributes #4 = { 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, !11, i64 112}
!6 = !{!"achd_cx", !7, i64 0, !11, i64 88, !11, i64 92, !11, i64 96, !11, i64 100, !8, i64 104, !11, i64 112, !13, i64 120, !8, i64 248}
!7 = !{!"achd_headers", !8, i64 0, !8, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !12, i64 48, !8, i64 56, !8, i64 64, !11, i64 72, !11, i64 76, !8, i64 80}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"long", !9, i64 0}
!13 = !{!"ach_channel", !9, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!11, !11, i64 0}


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