; LLVM IR from freakout42/macrocalc :: mc/mex (buffer)

; 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.BUFFER = type { ptr, ptr, i32, ptr, i32, ptr, i32, i32, [128 x i8], [128 x i8] }
%struct.LINE = type { ptr, ptr, i16, i16, [32768 x i8] }

@bheadp = external global ptr, align 8
@.str.13 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [1 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 ptr @lalloc(...) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

declare void @mlwrite(ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @bfind(ptr noundef %0, i32 noundef %1, i32 noundef %2) #3 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  %11 = load ptr, ptr @bheadp, align 8, !tbaa !5
  store ptr %11, ptr %8, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %24, %3
  %13 = load ptr, ptr %8, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %28

15:                                               ; preds = %12
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.BUFFER, ptr %17, i32 0, i32 9
  %19 = getelementptr inbounds [128 x i8], ptr %18, i64 0, i64 0
  %20 = call i32 @strcmp(ptr noundef %16, ptr noundef %19) #7
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %24

22:                                               ; preds = %15
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %23, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %79

24:                                               ; preds = %15
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.BUFFER, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !11
  store ptr %27, ptr %8, align 8, !tbaa !5
  br label %12, !llvm.loop !13

28:                                               ; preds = %12
  %29 = load i32, ptr %6, align 4, !tbaa !9
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %78

31:                                               ; preds = %28
  %32 = call noalias ptr @malloc(i64 noundef 312) #8
  store ptr %32, ptr %8, align 8, !tbaa !5
  %33 = icmp eq ptr %32, null
  br i1 %33, label %34, label %35

34:                                               ; preds = %31
  call void (ptr, ...) @mlwrite(ptr noundef @.str.13)
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %79

35:                                               ; preds = %31
  %36 = call ptr (i32, ...) @lalloc(i32 noundef 0)
  store ptr %36, ptr %9, align 8, !tbaa !5
  %37 = icmp eq ptr %36, null
  br i1 %37, label %38, label %40

38:                                               ; preds = %35
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  call void @free(ptr noundef %39) #6
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %79

40:                                               ; preds = %35
  %41 = load ptr, ptr @bheadp, align 8, !tbaa !5
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.BUFFER, ptr %42, i32 0, i32 0
  store ptr %41, ptr %43, align 8, !tbaa !11
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %44, ptr @bheadp, align 8, !tbaa !5
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.BUFFER, ptr %46, i32 0, i32 1
  store ptr %45, ptr %47, align 8, !tbaa !15
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.BUFFER, ptr %48, i32 0, i32 2
  store i32 0, ptr %49, align 8, !tbaa !16
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.BUFFER, ptr %50, i32 0, i32 3
  store ptr null, ptr %51, align 8, !tbaa !17
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.BUFFER, ptr %52, i32 0, i32 4
  store i32 0, ptr %53, align 8, !tbaa !18
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.BUFFER, ptr %55, i32 0, i32 7
  store i32 %54, ptr %56, align 4, !tbaa !19
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.BUFFER, ptr %57, i32 0, i32 6
  store i32 0, ptr %58, align 8, !tbaa !20
  %59 = load ptr, ptr %9, align 8, !tbaa !5
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.BUFFER, ptr %60, i32 0, i32 5
  store ptr %59, ptr %61, align 8, !tbaa !21
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.BUFFER, ptr %62, i32 0, i32 8
  %64 = getelementptr inbounds [128 x i8], ptr %63, i64 0, i64 0
  %65 = call ptr @strcpy(ptr noundef %64, ptr noundef @.str.14) #6
  %66 = load ptr, ptr %8, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.BUFFER, ptr %66, i32 0, i32 9
  %68 = getelementptr inbounds [128 x i8], ptr %67, i64 0, i64 0
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = call ptr @strcpy(ptr noundef %68, ptr noundef %69) #6
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  %72 = load ptr, ptr %9, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.LINE, ptr %72, i32 0, i32 0
  store ptr %71, ptr %73, align 8, !tbaa !22
  %74 = load ptr, ptr %9, align 8, !tbaa !5
  %75 = load ptr, ptr %9, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.LINE, ptr %75, i32 0, i32 1
  store ptr %74, ptr %76, align 8, !tbaa !25
  %77 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %77, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %79

78:                                               ; preds = %28
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %79

79:                                               ; preds = %78, %40, %38, %34, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  %80 = load ptr, ptr %4, align 8
  ret ptr %80
}

; Function Attrs: nounwind willreturn memory(read)
declare i32 @strcmp(ptr noundef, ptr noundef) #4

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #5

; Function Attrs: nounwind
declare ptr @strcpy(ptr noundef, ptr noundef) #2

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 "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 willreturn memory(read) "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 allocsize(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 #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(read) }
attributes #8 = { nounwind allocsize(0) }

!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, !6, i64 0}
!12 = !{!"BUFFER", !6, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !10, i64 32, !6, i64 40, !10, i64 48, !10, i64 52, !7, i64 56, !7, i64 184}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!12, !6, i64 8}
!16 = !{!12, !10, i64 16}
!17 = !{!12, !6, i64 24}
!18 = !{!12, !10, i64 32}
!19 = !{!12, !10, i64 52}
!20 = !{!12, !10, i64 48}
!21 = !{!12, !6, i64 40}
!22 = !{!23, !6, i64 0}
!23 = !{!"LINE", !6, i64 0, !6, i64 8, !24, i64 16, !24, i64 18, !7, i64 20}
!24 = !{!"short", !7, i64 0}
!25 = !{!23, !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"

@bheadp = external local_unnamed_addr global ptr, align 8
@.str.13 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [1 x i8], align 1

declare ptr @lalloc(...) local_unnamed_addr #0

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

declare void @mlwrite(ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @bfind(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #2 {
  br label %4

4:                                                ; preds = %5, %3
  %.027.in = phi ptr [ @bheadp, %3 ], [ %.027, %5 ]
  %.027 = load ptr, ptr %.027.in, align 8, !tbaa !5
  %.not = icmp eq ptr %.027, null
  br i1 %.not, label %9, label %5

5:                                                ; preds = %4
  %6 = getelementptr inbounds i8, ptr %.027, i64 184
  %7 = tail call i32 @strcmp(ptr noundef nonnull dereferenceable(1) %0, ptr noundef nonnull dereferenceable(1) %6) #6
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %.loopexit, label %4, !llvm.loop !9

9:                                                ; preds = %4
  %.not30 = icmp eq i32 %1, 0
  br i1 %.not30, label %.loopexit, label %10

10:                                               ; preds = %9
  %11 = tail call noalias dereferenceable_or_null(312) ptr @malloc(i64 noundef 312) #7
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %14

13:                                               ; preds = %10
  tail call void (ptr, ...) @mlwrite(ptr noundef nonnull @.str.13) #8
  br label %.loopexit

14:                                               ; preds = %10
  %15 = tail call ptr (i32, ...) @lalloc(i32 noundef 0) #8
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %14
  tail call void @free(ptr noundef nonnull %11) #8
  br label %.loopexit

18:                                               ; preds = %14
  %19 = load ptr, ptr @bheadp, align 8, !tbaa !5
  store ptr %19, ptr %11, align 8, !tbaa !11
  store ptr %11, ptr @bheadp, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %11, i64 8
  store ptr %15, ptr %20, align 8, !tbaa !14
  %21 = getelementptr inbounds i8, ptr %11, i64 16
  store i32 0, ptr %21, align 8, !tbaa !15
  %22 = getelementptr inbounds i8, ptr %11, i64 24
  store ptr null, ptr %22, align 8, !tbaa !16
  %23 = getelementptr inbounds i8, ptr %11, i64 32
  store i32 0, ptr %23, align 8, !tbaa !17
  %24 = getelementptr inbounds i8, ptr %11, i64 52
  store i32 %2, ptr %24, align 4, !tbaa !18
  %25 = getelementptr inbounds i8, ptr %11, i64 48
  store i32 0, ptr %25, align 8, !tbaa !19
  %26 = getelementptr inbounds i8, ptr %11, i64 40
  store ptr %15, ptr %26, align 8, !tbaa !20
  %27 = getelementptr inbounds i8, ptr %11, i64 56
  %28 = tail call ptr @strcpy(ptr noundef nonnull dereferenceable(1) %27, ptr noundef nonnull dereferenceable(1) @.str.14) #8
  %29 = getelementptr inbounds i8, ptr %11, i64 184
  %30 = tail call ptr @strcpy(ptr noundef nonnull dereferenceable(1) %29, ptr noundef nonnull dereferenceable(1) %0) #8
  store ptr %15, ptr %15, align 8, !tbaa !21
  %31 = getelementptr inbounds i8, ptr %15, i64 8
  store ptr %15, ptr %31, align 8, !tbaa !24
  br label %.loopexit

.loopexit:                                        ; preds = %5, %9, %18, %17, %13
  %.0 = phi ptr [ null, %13 ], [ null, %17 ], [ %11, %18 ], [ null, %9 ], [ %.027, %5 ]
  ret ptr %.0
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i32 @strcmp(ptr nocapture noundef, ptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strcpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly) local_unnamed_addr #5

attributes #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 #1 = { 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 #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 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #5 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite) "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 #6 = { nounwind willreturn memory(read) }
attributes #7 = { nounwind allocsize(0) }
attributes #8 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !6, i64 0}
!12 = !{!"BUFFER", !6, i64 0, !6, i64 8, !13, i64 16, !6, i64 24, !13, i64 32, !6, i64 40, !13, i64 48, !13, i64 52, !7, i64 56, !7, i64 184}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !6, i64 8}
!15 = !{!12, !13, i64 16}
!16 = !{!12, !6, i64 24}
!17 = !{!12, !13, i64 32}
!18 = !{!12, !13, i64 52}
!19 = !{!12, !13, i64 48}
!20 = !{!12, !6, i64 40}
!21 = !{!22, !6, i64 0}
!22 = !{!"LINE", !6, i64 0, !6, i64 8, !23, i64 16, !23, i64 18, !7, i64 20}
!23 = !{!"short", !7, i64 0}
!24 = !{!22, !6, i64 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass