; LLVM IR from freddiev4/dailyprogrammerchallenges :: Intermediate Challenges/Challenge 0219 Intermediate - To-do list (Part 2) (task)

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

@first = external dso_local global ptr, align 8
@last = external dso_local global ptr, align 8
@.str.23 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [15 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

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

; Function Attrs: nounwind uwtable
define dso_local void @remove_task() #2 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  %3 = call i32 (ptr, ...) @printf(ptr noundef @.str.28)
  %4 = call ptr @search(i32 noundef 1)
  store ptr %4, ptr %1, align 8, !tbaa !5
  %5 = icmp ne ptr %4, null
  br i1 %5, label %6, label %47

6:                                                ; preds = %0
  %7 = load ptr, ptr %1, align 8, !tbaa !5
  %8 = load ptr, ptr @first, align 8, !tbaa !5
  %9 = icmp eq ptr %7, %8
  br i1 %9, label %10, label %14

10:                                               ; preds = %6
  %11 = load ptr, ptr %1, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.node, ptr %11, i32 0, i32 2
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  store ptr %13, ptr @first, align 8, !tbaa !5
  br label %22

14:                                               ; preds = %6
  %15 = load ptr, ptr %1, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.node, ptr %15, i32 0, i32 2
  %17 = load ptr, ptr %16, align 8, !tbaa !9
  %18 = load ptr, ptr %1, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.node, ptr %18, i32 0, i32 3
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %21 = getelementptr inbounds %struct.node, ptr %20, i32 0, i32 2
  store ptr %17, ptr %21, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %14, %10
  %23 = load ptr, ptr %1, align 8, !tbaa !5
  %24 = load ptr, ptr @last, align 8, !tbaa !5
  %25 = icmp eq ptr %23, %24
  br i1 %25, label %26, label %30

26:                                               ; preds = %22
  %27 = load ptr, ptr %1, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.node, ptr %27, i32 0, i32 3
  %29 = load ptr, ptr %28, align 8, !tbaa !11
  store ptr %29, ptr @last, align 8, !tbaa !5
  br label %38

30:                                               ; preds = %22
  %31 = load ptr, ptr %1, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.node, ptr %31, i32 0, i32 3
  %33 = load ptr, ptr %32, align 8, !tbaa !11
  %34 = load ptr, ptr %1, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.node, ptr %34, i32 0, i32 2
  %36 = load ptr, ptr %35, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.node, ptr %36, i32 0, i32 3
  store ptr %33, ptr %37, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %30, %26
  %39 = load ptr, ptr %1, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.node, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !12
  call void @free(ptr noundef %41) #4
  %42 = load ptr, ptr %1, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.node, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !13
  call void @free(ptr noundef %44) #4
  %45 = load ptr, ptr %1, align 8, !tbaa !5
  call void @free(ptr noundef %45) #4
  %46 = call i32 (ptr, ...) @printf(ptr noundef @.str.29)
  store i32 1, ptr %2, align 4
  br label %50

47:                                               ; preds = %0
  %48 = call i32 (ptr, ...) @printf(ptr noundef @.str.23)
  br label %49

49:                                               ; preds = %47
  store i32 0, ptr %2, align 4
  br label %50

50:                                               ; preds = %49, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  %51 = load i32, ptr %2, align 4
  switch i32 %51, label %53 [
    i32 0, label %52
    i32 1, label %52
  ]

52:                                               ; preds = %50, %50
  ret void

53:                                               ; preds = %50
  unreachable
}

; 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
declare dso_local ptr @search(i32 noundef) #2

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

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


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

@first = external dso_local local_unnamed_addr global ptr, align 8
@last = external dso_local local_unnamed_addr global ptr, align 8
@.str.23 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [15 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local void @remove_task() local_unnamed_addr #1 {
  %1 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.28)
  %2 = tail call ptr @search(i32 noundef 1)
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %21, label %3

3:                                                ; preds = %0
  %4 = load ptr, ptr @first, align 8, !tbaa !5
  %5 = icmp eq ptr %2, %4
  %6 = getelementptr inbounds i8, ptr %2, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  br i1 %5, label %12, label %8

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %2, i64 24
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  br label %12

12:                                               ; preds = %3, %8
  %.sink = phi ptr [ %11, %8 ], [ @first, %3 ]
  store ptr %7, ptr %.sink, align 8, !tbaa !5
  %13 = load ptr, ptr @last, align 8, !tbaa !5
  %14 = icmp eq ptr %2, %13
  %15 = getelementptr inbounds i8, ptr %2, i64 24
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %7, i64 24
  %.sink11 = select i1 %14, ptr @last, ptr %17
  store ptr %16, ptr %.sink11, align 8, !tbaa !5
  %18 = load ptr, ptr %2, align 8, !tbaa !12
  tail call void @free(ptr noundef %18) #3
  %19 = getelementptr inbounds i8, ptr %2, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  tail call void @free(ptr noundef %20) #3
  tail call void @free(ptr noundef nonnull %2) #3
  br label %21

21:                                               ; preds = %0, %12
  %.str.23.sink = phi ptr [ @.str.29, %12 ], [ @.str.23, %0 ]
  %22 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %.str.23.sink)
  ret void
}

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

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

attributes #0 = { 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 #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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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, !6, i64 16}
!10 = !{!"node", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!10, !6, i64 24}
!12 = !{!10, !6, i64 0}
!13 = !{!10, !6, i64 8}


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