; LLVM IR from goldshtn/shmemq-blog :: main (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.message = type { i32, [252 x i8] }

@.str = external hidden unnamed_addr constant [28 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [28 x i8], align 1
@stdout = external global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [49 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [2 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

declare ptr @shmemq_new(ptr noundef, i64 noundef, i32 noundef) #2

declare void @perror(ptr noundef) #2

declare i32 @printf(ptr noundef, ...) #2

declare zeroext i1 @shmemq_try_dequeue(ptr noundef, ptr noundef, i32 noundef) #2

declare zeroext i1 @shmemq_try_enqueue(ptr noundef, ptr noundef, i32 noundef) #2

declare i32 @fflush(ptr noundef) #2

declare void @shmemq_destroy(ptr noundef, i32 noundef) #2

; 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 void @client(ptr noundef %0, ptr noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca %struct.message, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 256, ptr %8) #4
  call void @llvm.memset.p0.i64(ptr align 4 %8, i8 0, i64 256, i1 false)
  %9 = getelementptr inbounds %struct.message, ptr %8, i32 0, i32 0
  store i32 42, ptr %9, align 4
  %10 = getelementptr inbounds %struct.message, ptr %8, i32 0, i32 1
  %11 = getelementptr inbounds [252 x i8], ptr %10, i32 0, i32 0
  store i8 72, ptr %11, align 4
  %12 = getelementptr inbounds [252 x i8], ptr %10, i32 0, i32 1
  store i8 101, ptr %12, align 1
  %13 = getelementptr inbounds [252 x i8], ptr %10, i32 0, i32 2
  store i8 108, ptr %13, align 2
  %14 = getelementptr inbounds [252 x i8], ptr %10, i32 0, i32 3
  store i8 108, ptr %14, align 1
  %15 = getelementptr inbounds [252 x i8], ptr %10, i32 0, i32 4
  store i8 111, ptr %15, align 4
  store ptr null, ptr %6, align 8, !tbaa !5
  store ptr null, ptr %5, align 8, !tbaa !5
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = call ptr @shmemq_new(ptr noundef %16, i64 noundef 1000, i32 noundef 256)
  store ptr %17, ptr %5, align 8, !tbaa !5
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %21, label %20

20:                                               ; preds = %2
  call void @perror(ptr noundef @.str)
  br label %59

21:                                               ; preds = %2
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = call ptr @shmemq_new(ptr noundef %22, i64 noundef 1000, i32 noundef 256)
  store ptr %23, ptr %6, align 8, !tbaa !5
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %27, label %26

26:                                               ; preds = %21
  call void @perror(ptr noundef @.str.1)
  br label %59

27:                                               ; preds = %21
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, ptr noundef %28, ptr noundef %29)
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %55, %27
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = icmp slt i32 %32, 100000
  br i1 %33, label %34, label %58

34:                                               ; preds = %31
  br label %35

35:                                               ; preds = %39, %34
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = call zeroext i1 @shmemq_try_enqueue(ptr noundef %36, ptr noundef %8, i32 noundef 256)
  %38 = xor i1 %37, true
  br i1 %38, label %39, label %40

39:                                               ; preds = %35
  br label %35, !llvm.loop !11

40:                                               ; preds = %35
  br label %41

41:                                               ; preds = %45, %40
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = call zeroext i1 @shmemq_try_dequeue(ptr noundef %42, ptr noundef %8, i32 noundef 256)
  %44 = xor i1 %43, true
  br i1 %44, label %45, label %46

45:                                               ; preds = %41
  br label %41, !llvm.loop !13

46:                                               ; preds = %41
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = srem i32 %47, 10000
  %49 = icmp eq i32 %48, 0
  br i1 %49, label %50, label %54

50:                                               ; preds = %46
  %51 = call i32 (ptr, ...) @printf(ptr noundef @.str.5)
  %52 = load ptr, ptr @stdout, align 8, !tbaa !5
  %53 = call i32 @fflush(ptr noundef %52)
  br label %54

54:                                               ; preds = %50, %46
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %7, align 4, !tbaa !9
  br label %31, !llvm.loop !14

58:                                               ; preds = %31
  br label %59

59:                                               ; preds = %58, %26, %20
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = icmp ne ptr %60, null
  br i1 %61, label %62, label %64

62:                                               ; preds = %59
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  call void @shmemq_destroy(ptr noundef %63, i32 noundef 0)
  br label %64

64:                                               ; preds = %62, %59
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = icmp ne ptr %65, null
  br i1 %66, label %67, label %69

67:                                               ; preds = %64
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  call void @shmemq_destroy(ptr noundef %68, i32 noundef 0)
  br label %69

69:                                               ; preds = %67, %64
  call void @llvm.lifetime.end.p0(i64 256, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { 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 #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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


; 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.message = type { i32, [252 x i8] }

@.str = external hidden unnamed_addr constant [28 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [28 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [49 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [2 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

declare ptr @shmemq_new(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare void @perror(ptr nocapture noundef readonly) local_unnamed_addr #3

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

declare zeroext i1 @shmemq_try_dequeue(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare zeroext i1 @shmemq_try_enqueue(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

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

declare void @shmemq_destroy(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress 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 void @client(ptr noundef %0, ptr noundef %1) local_unnamed_addr #4 {
  %3 = alloca %struct.message, align 4
  call void @llvm.lifetime.start.p0(i64 256, ptr nonnull %3) #5
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(256) %4, i8 0, i64 248, i1 false)
  store i32 42, ptr %3, align 4
  %5 = getelementptr inbounds i8, ptr %3, i64 4
  store <4 x i8> <i8 72, i8 101, i8 108, i8 108>, ptr %5, align 4
  %6 = getelementptr inbounds i8, ptr %3, i64 8
  store i8 111, ptr %6, align 4
  %7 = tail call ptr @shmemq_new(ptr noundef %1, i64 noundef 1000, i32 noundef 256) #5
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %.thread25, label %8

.thread25:                                        ; preds = %2
  tail call void @perror(ptr noundef nonnull @.str) #6
  br label %24

8:                                                ; preds = %2
  %9 = tail call ptr @shmemq_new(ptr noundef %0, i64 noundef 1000, i32 noundef 256) #5
  %.not18 = icmp eq ptr %9, null
  br i1 %.not18, label %.loopexit.thread, label %10

.loopexit.thread:                                 ; preds = %8
  tail call void @perror(ptr noundef nonnull @.str.1) #6
  tail call void @shmemq_destroy(ptr noundef nonnull %7, i32 noundef 0) #5
  br label %24

10:                                               ; preds = %8
  %11 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, ptr noundef %0, ptr noundef %1)
  br label %.preheader28

.preheader28:                                     ; preds = %10, %22
  %.029 = phi i32 [ 0, %10 ], [ %23, %22 ]
  br label %12

12:                                               ; preds = %.preheader28, %12
  %13 = call zeroext i1 @shmemq_try_enqueue(ptr noundef nonnull %7, ptr noundef nonnull %3, i32 noundef 256) #5
  br i1 %13, label %.preheader, label %12, !llvm.loop !5

.preheader:                                       ; preds = %12, %.preheader
  %14 = call zeroext i1 @shmemq_try_dequeue(ptr noundef nonnull %9, ptr noundef nonnull %3, i32 noundef 256) #5
  br i1 %14, label %15, label %.preheader, !llvm.loop !7

15:                                               ; preds = %.preheader
  %16 = urem i32 %.029, 10000
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %22

18:                                               ; preds = %15
  %19 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5)
  %20 = load ptr, ptr @stdout, align 8, !tbaa !8
  %21 = call i32 @fflush(ptr noundef %20)
  br label %22

22:                                               ; preds = %15, %18
  %23 = add nuw nsw i32 %.029, 1
  %exitcond.not = icmp eq i32 %23, 100000
  br i1 %exitcond.not, label %.loopexit, label %.preheader28, !llvm.loop !12

.loopexit:                                        ; preds = %22
  call void @shmemq_destroy(ptr noundef nonnull %7, i32 noundef 0) #5
  call void @shmemq_destroy(ptr noundef nonnull %9, i32 noundef 0) #5
  br label %24

24:                                               ; preds = %.loopexit.thread, %.thread25, %.loopexit
  call void @llvm.lifetime.end.p0(i64 256, ptr nonnull %3) #5
  ret void
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { 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 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 = { cold }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = distinct !{!12, !6}


; Effective O3 passes: ConstraintEliminationPass,CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass