; LLVM IR from ohler55/agoo :: ext/agoo (text)

; 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._agooText = type { ptr, i64, i64, %struct._agooAtom, i8, [8 x i8] }
%struct._agooAtom = type { ptr, %union.pthread_mutex_t }
%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: 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

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @agoo_text_append_char(ptr noundef %0, i8 noundef signext %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = icmp eq ptr null, %9
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  br label %66

12:                                               ; preds = %2
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct._agooText, ptr %13, i32 0, i32 2
  %15 = load i64, ptr %14, align 8, !tbaa !10
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct._agooText, ptr %16, i32 0, i32 1
  %18 = load i64, ptr %17, align 8, !tbaa !15
  %19 = add nsw i64 %18, 1
  %20 = icmp sle i64 %15, %19
  br i1 %20, label %21, label %46

21:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct._agooText, ptr %22, i32 0, i32 2
  %24 = load i64, ptr %23, align 8, !tbaa !10
  %25 = add nsw i64 %24, 1
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct._agooText, ptr %26, i32 0, i32 2
  %28 = load i64, ptr %27, align 8, !tbaa !10
  %29 = sdiv i64 %28, 2
  %30 = add nsw i64 %25, %29
  store i64 %30, ptr %6, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %31 = load i64, ptr %6, align 8, !tbaa !16
  %32 = add i64 80, %31
  %33 = add i64 %32, 1
  store i64 %33, ptr %7, align 8, !tbaa !16
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = load i64, ptr %7, align 8, !tbaa !16
  %36 = call ptr @realloc(ptr noundef %34, i64 noundef %35) #4
  store ptr %36, ptr %4, align 8, !tbaa !5
  %37 = icmp eq ptr null, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %21
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %43

39:                                               ; preds = %21
  %40 = load i64, ptr %6, align 8, !tbaa !16
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct._agooText, ptr %41, i32 0, i32 2
  store i64 %40, ptr %42, align 8, !tbaa !10
  store i32 0, ptr %8, align 4
  br label %43

43:                                               ; preds = %39, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %44 = load i32, ptr %8, align 4
  switch i32 %44, label %68 [
    i32 0, label %45
    i32 1, label %66
  ]

45:                                               ; preds = %43
  br label %46

46:                                               ; preds = %45, %12
  %47 = load i8, ptr %5, align 1, !tbaa !9
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct._agooText, ptr %48, i32 0, i32 5
  %50 = getelementptr inbounds [8 x i8], ptr %49, i64 0, i64 0
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct._agooText, ptr %51, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !15
  %54 = getelementptr inbounds i8, ptr %50, i64 %53
  store i8 %47, ptr %54, align 1, !tbaa !9
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct._agooText, ptr %55, i32 0, i32 1
  %57 = load i64, ptr %56, align 8, !tbaa !15
  %58 = add nsw i64 %57, 1
  store i64 %58, ptr %56, align 8, !tbaa !15
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct._agooText, ptr %59, i32 0, i32 5
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct._agooText, ptr %61, i32 0, i32 1
  %63 = load i64, ptr %62, align 8, !tbaa !15
  %64 = getelementptr inbounds [8 x i8], ptr %60, i64 0, i64 %63
  store i8 0, ptr %64, align 1, !tbaa !9
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %65, ptr %3, align 8
  br label %66

66:                                               ; preds = %46, %43, %11
  %67 = load ptr, ptr %3, align 8
  ret ptr %67

68:                                               ; preds = %43
  unreachable
}

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

!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 = !{!7, !7, i64 0}
!10 = !{!11, !12, i64 16}
!11 = !{!"_agooText", !6, i64 0, !12, i64 8, !12, i64 16, !13, i64 24, !14, i64 72, !7, i64 73}
!12 = !{!"long", !7, i64 0}
!13 = !{!"_agooAtom", !6, i64 0, !7, i64 8}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!11, !12, i64 8}
!16 = !{!12, !12, i64 0}


; 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: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn uwtable
define dso_local noundef ptr @agoo_text_append_char(ptr noundef %0, i8 noundef signext %1) local_unnamed_addr #1 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %26, label %4

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load i64, ptr %7, align 8, !tbaa !13
  %9 = add nsw i64 %8, 1
  %.not = icmp sgt i64 %6, %9
  br i1 %.not, label %18, label %10

10:                                               ; preds = %4
  %11 = add nsw i64 %6, 1
  %12 = sdiv i64 %6, 2
  %13 = add nsw i64 %11, %12
  %14 = add i64 %13, 81
  %15 = tail call ptr @realloc(ptr noundef nonnull %0, i64 noundef %14) #2
  %16 = icmp eq ptr %15, null
  br i1 %16, label %26, label %.thread

.thread:                                          ; preds = %10
  %17 = getelementptr inbounds i8, ptr %15, i64 16
  store i64 %13, ptr %17, align 8, !tbaa !5
  %.phi.trans.insert = getelementptr inbounds i8, ptr %15, i64 8
  %.pre = load i64, ptr %.phi.trans.insert, align 8, !tbaa !13
  br label %18

18:                                               ; preds = %.thread, %4
  %19 = phi i64 [ %8, %4 ], [ %.pre, %.thread ]
  %.019 = phi ptr [ %0, %4 ], [ %15, %.thread ]
  %20 = getelementptr inbounds i8, ptr %.019, i64 73
  %21 = getelementptr inbounds i8, ptr %.019, i64 8
  %22 = getelementptr inbounds i8, ptr %20, i64 %19
  store i8 %1, ptr %22, align 1, !tbaa !14
  %23 = load i64, ptr %21, align 8, !tbaa !13
  %24 = add nsw i64 %23, 1
  store i64 %24, ptr %21, align 8, !tbaa !13
  %25 = getelementptr inbounds [8 x i8], ptr %20, i64 0, i64 %24
  store i8 0, ptr %25, align 1, !tbaa !14
  br label %26

26:                                               ; preds = %10, %2, %18
  %.1 = phi ptr [ %.019, %18 ], [ null, %2 ], [ null, %10 ]
  ret ptr %.1
}

attributes #0 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) 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 #1 = { mustprogress nounwind willreturn 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 allocsize(1) }

!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 16}
!6 = !{!"_agooText", !7, i64 0, !10, i64 8, !10, i64 16, !11, i64 24, !12, i64 72, !8, i64 73}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"_agooAtom", !7, i64 0, !8, i64 8}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!8, !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass