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

@curbp = external global ptr, align 8
@wheadp = external global ptr, align 8
@.str.15 = external hidden unnamed_addr constant [16 x i8], align 1

declare i32 @ctrlg(...) #0

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

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

declare i32 @mlreply(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @renambuf(i32 noundef %0, i32 noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i8, align 1
  %10 = alloca i32, align 4
  %11 = alloca [128 x i8], align 16
  %12 = alloca [128 x i8], align 16
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 128, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 128, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store ptr null, ptr %13, align 8, !tbaa !9
  %15 = load ptr, ptr @curbp, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.BUFFER, ptr %15, i32 0, i32 8
  %17 = getelementptr inbounds [128 x i8], ptr %16, i64 0, i64 0
  %18 = load i8, ptr %17, align 8, !tbaa !11
  %19 = sext i8 %18 to i32
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %27

21:                                               ; preds = %2
  %22 = getelementptr inbounds [128 x i8], ptr %12, i64 0, i64 0
  %23 = load ptr, ptr @curbp, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.BUFFER, ptr %23, i32 0, i32 8
  %25 = getelementptr inbounds [128 x i8], ptr %24, i64 0, i64 0
  call void @makename(ptr noundef %22, ptr noundef %25)
  %26 = getelementptr inbounds [128 x i8], ptr %12, i64 0, i64 0
  store ptr %26, ptr %13, align 8, !tbaa !9
  br label %27

27:                                               ; preds = %21, %2
  %28 = load ptr, ptr %13, align 8, !tbaa !9
  %29 = getelementptr inbounds [128 x i8], ptr %11, i64 0, i64 0
  %30 = call i32 @mlreply(ptr noundef @.str.15, ptr noundef %28, ptr noundef %29, i32 noundef 128, i32 noundef 0)
  store i32 %30, ptr %10, align 4, !tbaa !5
  %31 = load i32, ptr %10, align 4, !tbaa !5
  %32 = icmp ne i32 %31, 1
  br i1 %32, label %33, label %38

33:                                               ; preds = %27
  %34 = load i32, ptr %10, align 4, !tbaa !5
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %38

36:                                               ; preds = %33
  %37 = call i32 (...) @ctrlg()
  store i32 %37, ptr %3, align 4
  store i32 1, ptr %14, align 4
  br label %92

38:                                               ; preds = %33, %27
  %39 = load ptr, ptr @curbp, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.BUFFER, ptr %39, i32 0, i32 7
  %41 = load i32, ptr %40, align 4, !tbaa !12
  %42 = and i32 %41, -2
  store i32 %42, ptr %40, align 4, !tbaa !12
  %43 = getelementptr inbounds [128 x i8], ptr %11, i64 0, i64 0
  store ptr %43, ptr %7, align 8, !tbaa !9
  %44 = load ptr, ptr @curbp, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.BUFFER, ptr %44, i32 0, i32 9
  %46 = getelementptr inbounds [128 x i8], ptr %45, i64 0, i64 0
  store ptr %46, ptr %8, align 8, !tbaa !9
  br label %47

47:                                               ; preds = %67, %38
  %48 = load ptr, ptr %7, align 8, !tbaa !9
  %49 = getelementptr inbounds i8, ptr %48, i32 1
  store ptr %49, ptr %7, align 8, !tbaa !9
  %50 = load i8, ptr %48, align 1, !tbaa !11
  store i8 %50, ptr %9, align 1, !tbaa !11
  %51 = load i8, ptr %9, align 1, !tbaa !11
  %52 = sext i8 %51 to i32
  %53 = icmp sge i32 %52, 65
  br i1 %53, label %54, label %63

54:                                               ; preds = %47
  %55 = load i8, ptr %9, align 1, !tbaa !11
  %56 = sext i8 %55 to i32
  %57 = icmp sle i32 %56, 90
  br i1 %57, label %58, label %63

58:                                               ; preds = %54
  %59 = load i8, ptr %9, align 1, !tbaa !11
  %60 = sext i8 %59 to i32
  %61 = add nsw i32 %60, 32
  %62 = trunc i32 %61 to i8
  store i8 %62, ptr %9, align 1, !tbaa !11
  br label %63

63:                                               ; preds = %58, %54, %47
  %64 = load i8, ptr %9, align 1, !tbaa !11
  %65 = load ptr, ptr %8, align 8, !tbaa !9
  %66 = getelementptr inbounds i8, ptr %65, i32 1
  store ptr %66, ptr %8, align 8, !tbaa !9
  store i8 %64, ptr %65, align 1, !tbaa !11
  br label %67

67:                                               ; preds = %63
  %68 = load i8, ptr %9, align 1, !tbaa !11
  %69 = sext i8 %68 to i32
  %70 = icmp ne i32 %69, 0
  br i1 %70, label %47, label %71, !llvm.loop !14

71:                                               ; preds = %67
  %72 = load ptr, ptr @wheadp, align 8, !tbaa !9
  store ptr %72, ptr %6, align 8, !tbaa !9
  br label %73

73:                                               ; preds = %87, %71
  %74 = load ptr, ptr %6, align 8, !tbaa !9
  %75 = icmp ne ptr %74, null
  br i1 %75, label %76, label %91

76:                                               ; preds = %73
  %77 = load ptr, ptr %6, align 8, !tbaa !9
  %78 = getelementptr inbounds %struct.WINDOW, ptr %77, i32 0, i32 1
  %79 = load ptr, ptr %78, align 8, !tbaa !16
  %80 = load ptr, ptr @curbp, align 8, !tbaa !9
  %81 = icmp eq ptr %79, %80
  br i1 %81, label %82, label %87

82:                                               ; preds = %76
  %83 = load ptr, ptr %6, align 8, !tbaa !9
  %84 = getelementptr inbounds %struct.WINDOW, ptr %83, i32 0, i32 11
  %85 = load i32, ptr %84, align 4, !tbaa !18
  %86 = or i32 %85, 16
  store i32 %86, ptr %84, align 4, !tbaa !18
  br label %87

87:                                               ; preds = %82, %76
  %88 = load ptr, ptr %6, align 8, !tbaa !9
  %89 = getelementptr inbounds %struct.WINDOW, ptr %88, i32 0, i32 0
  %90 = load ptr, ptr %89, align 8, !tbaa !19
  store ptr %90, ptr %6, align 8, !tbaa !9
  br label %73, !llvm.loop !20

91:                                               ; preds = %73
  store i32 1, ptr %3, align 4
  store i32 1, ptr %14, align 4
  br label %92

92:                                               ; preds = %91, %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 128, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 128, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %93 = load i32, ptr %3, align 4
  ret i32 %93
}

declare void @makename(ptr noundef, ptr noundef) #0

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 = { nocallback nofree nosync 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 = !{!"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 = !{!7, !7, i64 0}
!12 = !{!13, !6, i64 52}
!13 = !{!"BUFFER", !10, i64 0, !10, i64 8, !6, i64 16, !10, i64 24, !6, i64 32, !10, i64 40, !6, i64 48, !6, i64 52, !7, i64 56, !7, i64 184}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !10, i64 8}
!17 = !{!"WINDOW", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !6, i64 32, !10, i64 40, !6, i64 48, !6, i64 52, !6, i64 56, !6, i64 60, !6, i64 64, !6, i64 68}
!18 = !{!17, !6, i64 68}
!19 = !{!17, !10, i64 0}
!20 = distinct !{!20, !15}


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

@curbp = external local_unnamed_addr global ptr, align 8
@wheadp = external local_unnamed_addr global ptr, align 8
@.str.15 = external hidden unnamed_addr constant [16 x i8], align 1

declare i32 @ctrlg(...) local_unnamed_addr #0

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

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

declare i32 @mlreply(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @renambuf(i32 noundef %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = alloca [128 x i8], align 16
  %4 = alloca [128 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %4) #3
  %5 = load ptr, ptr @curbp, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 56
  %7 = load i8, ptr %6, align 8, !tbaa !9
  %.not = icmp eq i8 %7, 0
  br i1 %.not, label %9, label %8

8:                                                ; preds = %2
  call void @makename(ptr noundef nonnull %4, ptr noundef nonnull %6) #3
  br label %9

9:                                                ; preds = %8, %2
  %.0 = phi ptr [ %4, %8 ], [ null, %2 ]
  %10 = call i32 @mlreply(ptr noundef nonnull @.str.15, ptr noundef %.0, ptr noundef nonnull %3, i32 noundef 128, i32 noundef 0) #3
  %or.cond = icmp ugt i32 %10, 1
  br i1 %or.cond, label %11, label %13

11:                                               ; preds = %9
  %12 = call i32 (...) @ctrlg() #3
  br label %.loopexit

13:                                               ; preds = %9
  %14 = load ptr, ptr @curbp, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 52
  %16 = load i32, ptr %15, align 4, !tbaa !10
  %17 = and i32 %16, -2
  store i32 %17, ptr %15, align 4, !tbaa !10
  %18 = getelementptr inbounds i8, ptr %14, i64 184
  br label %19

19:                                               ; preds = %19, %13
  %.021 = phi ptr [ %3, %13 ], [ %20, %19 ]
  %.020 = phi ptr [ %18, %13 ], [ %24, %19 ]
  %20 = getelementptr inbounds i8, ptr %.021, i64 1
  %21 = load i8, ptr %.021, align 1, !tbaa !9
  %22 = add i8 %21, -65
  %or.cond4 = icmp ult i8 %22, 26
  %23 = or disjoint i8 %21, 32
  %spec.select = select i1 %or.cond4, i8 %23, i8 %21
  %24 = getelementptr inbounds i8, ptr %.020, i64 1
  store i8 %spec.select, ptr %.020, align 1, !tbaa !9
  %.not24 = icmp eq i8 %spec.select, 0
  br i1 %.not24, label %.preheader, label %19, !llvm.loop !13

.preheader:                                       ; preds = %19
  %.02226 = load ptr, ptr @wheadp, align 8, !tbaa !5
  %.not2527 = icmp eq ptr %.02226, null
  br i1 %.not2527, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %25 = load ptr, ptr @curbp, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %.lr.ph, %34
  %.02228 = phi ptr [ %.02226, %.lr.ph ], [ %.022, %34 ]
  %27 = getelementptr inbounds i8, ptr %.02228, i64 8
  %28 = load ptr, ptr %27, align 8, !tbaa !15
  %29 = icmp eq ptr %28, %25
  br i1 %29, label %30, label %34

30:                                               ; preds = %26
  %31 = getelementptr inbounds i8, ptr %.02228, i64 68
  %32 = load i32, ptr %31, align 4, !tbaa !17
  %33 = or i32 %32, 16
  store i32 %33, ptr %31, align 4, !tbaa !17
  br label %34

34:                                               ; preds = %30, %26
  %.022 = load ptr, ptr %.02228, align 8, !tbaa !5
  %.not25 = icmp eq ptr %.022, null
  br i1 %.not25, label %.loopexit, label %26, !llvm.loop !18

.loopexit:                                        ; preds = %34, %.preheader, %11
  %.018 = phi i32 [ %12, %11 ], [ 1, %.preheader ], [ 1, %34 ]
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %3) #3
  ret i32 %.018
}

declare void @makename(ptr noundef, ptr noundef) local_unnamed_addr #0

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 nocallback nofree nosync 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 = !{!7, !7, i64 0}
!10 = !{!11, !12, i64 52}
!11 = !{!"BUFFER", !6, i64 0, !6, i64 8, !12, i64 16, !6, i64 24, !12, i64 32, !6, i64 40, !12, i64 48, !12, i64 52, !7, i64 56, !7, i64 184}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !6, i64 8}
!16 = !{!"WINDOW", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !12, i64 32, !6, i64 40, !12, i64 48, !12, i64 52, !12, i64 56, !12, i64 60, !12, i64 64, !12, i64 68}
!17 = !{!16, !12, i64 68}
!18 = distinct !{!18, !14}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass