; LLVM IR from goldshtn/shmemq-blog :: shmemq (shmemq_try_dequeue)

; 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._shmemq = type { i64, i32, i64, ptr, i32, i64, ptr }
%struct.shmemq_info = type { %union.pthread_mutex_t, i64, i64, [1 x i8] }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) #0

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @shmemq_try_dequeue(ptr noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct._shmemq, ptr %9, i32 0, i32 1
  %11 = load i32, ptr %10, align 8, !tbaa !11
  %12 = icmp ne i32 %8, %11
  br i1 %12, label %13, label %14

13:                                               ; preds = %3
  store i1 false, ptr %4, align 1
  br label %70

14:                                               ; preds = %3
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct._shmemq, ptr %15, i32 0, i32 6
  %17 = load ptr, ptr %16, align 8, !tbaa !14
  %18 = getelementptr inbounds %struct.shmemq_info, ptr %17, i32 0, i32 0
  %19 = call i32 @pthread_mutex_lock(ptr noundef %18) #3
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct._shmemq, ptr %20, i32 0, i32 6
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %23 = getelementptr inbounds %struct.shmemq_info, ptr %22, i32 0, i32 1
  %24 = load i64, ptr %23, align 8, !tbaa !15
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct._shmemq, ptr %25, i32 0, i32 6
  %27 = load ptr, ptr %26, align 8, !tbaa !14
  %28 = getelementptr inbounds %struct.shmemq_info, ptr %27, i32 0, i32 2
  %29 = load i64, ptr %28, align 8, !tbaa !17
  %30 = icmp uge i64 %24, %29
  br i1 %30, label %31, label %37

31:                                               ; preds = %14
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct._shmemq, ptr %32, i32 0, i32 6
  %34 = load ptr, ptr %33, align 8, !tbaa !14
  %35 = getelementptr inbounds %struct.shmemq_info, ptr %34, i32 0, i32 0
  %36 = call i32 @pthread_mutex_unlock(ptr noundef %35) #3
  store i1 false, ptr %4, align 1
  br label %70

37:                                               ; preds = %14
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct._shmemq, ptr %39, i32 0, i32 6
  %41 = load ptr, ptr %40, align 8, !tbaa !14
  %42 = getelementptr inbounds %struct.shmemq_info, ptr %41, i32 0, i32 3
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct._shmemq, ptr %43, i32 0, i32 6
  %45 = load ptr, ptr %44, align 8, !tbaa !14
  %46 = getelementptr inbounds %struct.shmemq_info, ptr %45, i32 0, i32 1
  %47 = load i64, ptr %46, align 8, !tbaa !15
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct._shmemq, ptr %48, i32 0, i32 2
  %50 = load i64, ptr %49, align 8, !tbaa !18
  %51 = urem i64 %47, %50
  %52 = getelementptr inbounds [1 x i8], ptr %42, i64 0, i64 %51
  %53 = load i32, ptr %7, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %38, ptr align 1 %52, i64 %54, i1 false)
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct._shmemq, ptr %55, i32 0, i32 1
  %57 = load i32, ptr %56, align 8, !tbaa !11
  %58 = zext i32 %57 to i64
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct._shmemq, ptr %59, i32 0, i32 6
  %61 = load ptr, ptr %60, align 8, !tbaa !14
  %62 = getelementptr inbounds %struct.shmemq_info, ptr %61, i32 0, i32 1
  %63 = load i64, ptr %62, align 8, !tbaa !15
  %64 = add i64 %63, %58
  store i64 %64, ptr %62, align 8, !tbaa !15
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct._shmemq, ptr %65, i32 0, i32 6
  %67 = load ptr, ptr %66, align 8, !tbaa !14
  %68 = getelementptr inbounds %struct.shmemq_info, ptr %67, i32 0, i32 0
  %69 = call i32 @pthread_mutex_unlock(ptr noundef %68) #3
  store i1 true, ptr %4, align 1
  br label %70

70:                                               ; preds = %37, %31, %13
  %71 = load i1, ptr %4, align 1
  ret i1 %71
}

attributes #0 = { 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 #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_shmemq", !13, i64 0, !10, i64 8, !13, i64 16, !6, i64 24, !10, i64 32, !13, i64 40, !6, i64 48}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !6, i64 48}
!15 = !{!16, !13, i64 40}
!16 = !{!"shmemq_info", !7, i64 0, !13, i64 40, !13, i64 48, !7, i64 56}
!17 = !{!16, !13, i64 48}
!18 = !{!12, !13, i64 16}


; 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"

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @shmemq_try_dequeue(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %.not = icmp eq i32 %5, %2
  br i1 %.not, label %6, label %31

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %0, i64 48
  %8 = load ptr, ptr %7, align 8, !tbaa !12
  %9 = tail call i32 @pthread_mutex_lock(ptr noundef %8) #3
  %10 = load ptr, ptr %7, align 8, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %10, i64 40
  %12 = load i64, ptr %11, align 8, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %10, i64 48
  %14 = load i64, ptr %13, align 8, !tbaa !15
  %.not16 = icmp ult i64 %12, %14
  br i1 %.not16, label %17, label %15

15:                                               ; preds = %6
  %16 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %10) #3
  br label %31

17:                                               ; preds = %6
  %18 = getelementptr inbounds i8, ptr %10, i64 56
  %19 = getelementptr inbounds i8, ptr %0, i64 16
  %20 = load i64, ptr %19, align 8, !tbaa !16
  %21 = urem i64 %12, %20
  %22 = getelementptr inbounds [1 x i8], ptr %18, i64 0, i64 %21
  %23 = sext i32 %2 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %1, ptr nonnull align 1 %22, i64 %23, i1 false)
  %24 = load i32, ptr %4, align 8, !tbaa !5
  %25 = zext i32 %24 to i64
  %26 = load ptr, ptr %7, align 8, !tbaa !12
  %27 = getelementptr inbounds i8, ptr %26, i64 40
  %28 = load i64, ptr %27, align 8, !tbaa !13
  %29 = add i64 %28, %25
  store i64 %29, ptr %27, align 8, !tbaa !13
  %30 = tail call i32 @pthread_mutex_unlock(ptr noundef %26) #3
  br label %31

31:                                               ; preds = %3, %17, %15
  %.0 = phi i1 [ false, %15 ], [ true, %17 ], [ false, %3 ]
  ret i1 %.0
}

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 8}
!6 = !{!"_shmemq", !7, i64 0, !10, i64 8, !7, i64 16, !11, i64 24, !10, i64 32, !7, i64 40, !11, i64 48}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !11, i64 48}
!13 = !{!14, !7, i64 40}
!14 = !{!"shmemq_info", !8, i64 0, !7, i64 40, !7, i64 48, !8, i64 56}
!15 = !{!14, !7, i64 48}
!16 = !{!6, !7, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass