; LLVM IR from bartobri/data-structures-c :: deque/doubly-linked-list (QUEUE)

; 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.QUEUEnode = type { i32, ptr, ptr }

@head = external hidden global ptr, align 8
@tail = external hidden global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local ptr @NEW(i32 noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @QUEUEputhead(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %3 = load ptr, ptr @head, align 8, !tbaa !9
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %11

5:                                                ; preds = %1
  %6 = load ptr, ptr @tail, align 8, !tbaa !9
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %11

8:                                                ; preds = %5
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = call ptr @NEW(i32 noundef %9, ptr noundef null, ptr noundef null)
  store ptr %10, ptr @head, align 8, !tbaa !9
  store ptr %10, ptr @tail, align 8, !tbaa !9
  br label %20

11:                                               ; preds = %5, %1
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = load ptr, ptr @head, align 8, !tbaa !9
  %14 = call ptr @NEW(i32 noundef %12, ptr noundef %13, ptr noundef null)
  %15 = load ptr, ptr @head, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.QUEUEnode, ptr %15, i32 0, i32 2
  store ptr %14, ptr %16, align 8, !tbaa !11
  %17 = load ptr, ptr @head, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.QUEUEnode, ptr %17, i32 0, i32 2
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  store ptr %19, ptr @head, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %11, %8
  ret void
}

attributes #0 = { 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" }

!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, !10, i64 16}
!12 = !{!"QUEUEnode", !6, i64 0, !10, i64 8, !10, 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"

@head = external hidden local_unnamed_addr global ptr, align 8
@tail = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local ptr @NEW(i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @QUEUEputhead(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @head, align 8, !tbaa !5
  %3 = icmp eq ptr %2, null
  %4 = load ptr, ptr @tail, align 8
  %5 = icmp eq ptr %4, null
  %or.cond = select i1 %3, i1 %5, i1 false
  br i1 %or.cond, label %6, label %8

6:                                                ; preds = %1
  %7 = tail call ptr @NEW(i32 noundef %0, ptr noundef null, ptr noundef null)
  store ptr %7, ptr @tail, align 8, !tbaa !5
  br label %12

8:                                                ; preds = %1
  %9 = tail call ptr @NEW(i32 noundef %0, ptr noundef %2, ptr noundef null)
  %10 = load ptr, ptr @head, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  store ptr %9, ptr %11, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %8, %6
  %.sink = phi ptr [ %7, %6 ], [ %9, %8 ]
  store ptr %.sink, ptr @head, align 8
  ret void
}

attributes #0 = { 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" }

!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, !6, i64 16}
!10 = !{!"QUEUEnode", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InstCombinePass,MergedLoadStoreMotionPass,SROAPass,SimplifyCFGPass,TailCallElimPass