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

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

declare void @ach_log(i32 noundef, ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @server_connect(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @achd_reconnect(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.achd_conn, ptr %4, i32 0, i32 5
  %6 = load i32, ptr %5, align 8, !tbaa !9
  %7 = call i32 @close(i32 noundef %6)
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.achd_conn, ptr %8, i32 0, i32 6
  %10 = load i32, ptr %9, align 4, !tbaa !15
  %11 = call i32 @close(i32 noundef %10)
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.achd_conn, ptr %12, i32 0, i32 7
  %14 = load i32, ptr %13, align 8, !tbaa !16
  %15 = icmp sge i32 %14, 0
  br i1 %15, label %16, label %21

16:                                               ; preds = %1
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.achd_conn, ptr %17, i32 0, i32 7
  %19 = load i32, ptr %18, align 8, !tbaa !16
  %20 = call i32 @close(i32 noundef %19)
  br label %21

21:                                               ; preds = %16, %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 -1, ptr %3, align 4, !tbaa !17
  br label %22

22:                                               ; preds = %39, %21
  %23 = load i32, ptr getelementptr inbounds (%struct.achd_cx, ptr @cx, i32 0, i32 3), align 8, !tbaa !18
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %32

25:                                               ; preds = %22
  %26 = load i32, ptr %3, align 4, !tbaa !17
  %27 = icmp slt i32 %26, 0
  br i1 %27, label %28, label %32

28:                                               ; preds = %25
  %29 = load i32, ptr getelementptr inbounds (%struct.achd_cx, ptr @cx, i32 0, i32 6), align 8, !tbaa !21
  %30 = icmp ne i32 %29, 0
  %31 = xor i1 %30, true
  br label %32

32:                                               ; preds = %28, %25, %22
  %33 = phi i1 [ false, %25 ], [ false, %22 ], [ %31, %28 ]
  br i1 %33, label %34, label %44

34:                                               ; preds = %32
  %35 = load i32, ptr @ach_verbosity, align 4, !tbaa !17
  %36 = add nsw i32 5, %35
  %37 = icmp sle i32 7, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %34
  call void (i32, ptr, ...) @ach_log(i32 noundef 7, ptr noundef @.str.15)
  br label %39

39:                                               ; preds = %38, %34
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.achd_conn, ptr %40, i32 0, i32 1
  call void @achd_sleep_till(ptr noundef %41, i64 noundef 250000000)
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = call i32 @server_connect(ptr noundef %42)
  store i32 %43, ptr %3, align 4, !tbaa !17
  br label %22, !llvm.loop !22

44:                                               ; preds = %32
  %45 = load i32, ptr %3, align 4, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 %45
}

; Function Attrs: nounwind uwtable
declare dso_local void @achd_sleep_till(ptr noundef, i64 noundef) #2

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 }

!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, !11, i64 216}
!10 = !{!"achd_conn", !11, i64 0, !12, i64 8, !12, i64 24, !14, i64 40, !14, i64 128, !11, i64 216, !11, i64 220, !11, i64 224, !13, i64 232, !6, i64 240, !6, i64 248, !6, i64 256}
!11 = !{!"int", !7, i64 0}
!12 = !{!"timespec", !13, i64 0, !13, i64 8}
!13 = !{!"long", !7, i64 0}
!14 = !{!"achd_headers", !6, i64 0, !6, 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, !13, i64 48, !6, i64 56, !6, i64 64, !11, i64 72, !11, i64 76, !6, i64 80}
!15 = !{!10, !11, i64 220}
!16 = !{!10, !11, i64 224}
!17 = !{!11, !11, i64 0}
!18 = !{!19, !11, i64 96}
!19 = !{!"achd_cx", !14, i64 0, !11, i64 88, !11, i64 92, !11, i64 96, !11, i64 100, !6, i64 104, !11, i64 112, !20, i64 120, !6, i64 248}
!20 = !{!"ach_channel", !7, i64 0}
!21 = !{!19, !11, i64 112}
!22 = distinct !{!22, !23}
!23 = !{!"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] }

@cx = external local_unnamed_addr global %struct.achd_cx, align 8
@ach_verbosity = external local_unnamed_addr global i32, align 4
@.str.15 = external hidden unnamed_addr constant [19 x i8], align 1

declare i32 @close(i32 noundef) local_unnamed_addr #0

declare void @ach_log(i32 noundef, ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i32 @server_connect(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @achd_reconnect(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 216
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = tail call i32 @close(i32 noundef %3) #2
  %5 = getelementptr inbounds i8, ptr %0, i64 220
  %6 = load i32, ptr %5, align 4, !tbaa !14
  %7 = tail call i32 @close(i32 noundef %6) #2
  %8 = getelementptr inbounds i8, ptr %0, i64 224
  %9 = load i32, ptr %8, align 8, !tbaa !15
  %10 = icmp sgt i32 %9, -1
  br i1 %10, label %11, label %13

11:                                               ; preds = %1
  %12 = tail call i32 @close(i32 noundef %9) #2
  br label %13

13:                                               ; preds = %11, %1
  %14 = load i32, ptr getelementptr inbounds (i8, ptr @cx, i64 96), align 8, !tbaa !16
  %15 = icmp ne i32 %14, 0
  %16 = load i32, ptr getelementptr inbounds (i8, ptr @cx, i64 112), align 8
  %.not9 = icmp eq i32 %16, 0
  %17 = select i1 %15, i1 %.not9, i1 false
  br i1 %17, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %13
  %18 = getelementptr inbounds i8, ptr %0, i64 8
  br label %19

19:                                               ; preds = %.lr.ph, %23
  %20 = load i32, ptr @ach_verbosity, align 4, !tbaa !19
  %21 = icmp sgt i32 %20, 1
  br i1 %21, label %22, label %23

22:                                               ; preds = %19
  tail call void (i32, ptr, ...) @ach_log(i32 noundef 7, ptr noundef nonnull @.str.15) #2
  br label %23

23:                                               ; preds = %22, %19
  tail call void @achd_sleep_till(ptr noundef nonnull %18, i64 noundef 250000000)
  %24 = tail call i32 @server_connect(ptr noundef %0)
  %25 = load i32, ptr getelementptr inbounds (i8, ptr @cx, i64 96), align 8, !tbaa !16
  %26 = icmp ne i32 %25, 0
  %27 = icmp slt i32 %24, 0
  %or.cond = select i1 %26, i1 %27, i1 false
  %28 = load i32, ptr getelementptr inbounds (i8, ptr @cx, i64 112), align 8
  %.not = icmp eq i32 %28, 0
  %29 = select i1 %or.cond, i1 %.not, i1 false
  br i1 %29, label %19, label %._crit_edge, !llvm.loop !20

._crit_edge:                                      ; preds = %23, %13
  %.0.lcssa = phi i32 [ -1, %13 ], [ %24, %23 ]
  ret i32 %.0.lcssa
}

; Function Attrs: nounwind uwtable
declare dso_local void @achd_sleep_till(ptr noundef, i64 noundef) local_unnamed_addr #1

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


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