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

; 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.22 = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @cpyfname(ptr noundef, ptr noundef) #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

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

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

declare i32 @ctrlg(...) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @filewrite(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca [128 x i8], align 16
  %8 = alloca ptr, align 8
  %9 = 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 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 128, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %10 = getelementptr inbounds [128 x i8], ptr %7, i64 0, i64 0
  %11 = call i32 @choosefile(ptr noundef @.str.22, ptr noundef %10, i32 noundef 1)
  %12 = icmp ne i32 %11, 1
  br i1 %12, label %13, label %15

13:                                               ; preds = %2
  %14 = call i32 (...) @ctrlg()
  store i32 %14, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %50

15:                                               ; preds = %2
  %16 = getelementptr inbounds [128 x i8], ptr %7, i64 0, i64 0
  %17 = call i32 @writeout(ptr noundef %16)
  store i32 %17, ptr %6, align 4, !tbaa !5
  %18 = icmp eq i32 %17, 1
  br i1 %18, label %19, label %48

19:                                               ; preds = %15
  %20 = load ptr, ptr @curbp, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.BUFFER, ptr %20, i32 0, i32 8
  %22 = getelementptr inbounds [128 x i8], ptr %21, i64 0, i64 0
  %23 = getelementptr inbounds [128 x i8], ptr %7, i64 0, i64 0
  call void @cpyfname(ptr noundef %22, ptr noundef %23)
  %24 = load ptr, ptr @curbp, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.BUFFER, ptr %24, i32 0, i32 7
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = and i32 %26, -3
  store i32 %27, ptr %25, align 4, !tbaa !11
  %28 = load ptr, ptr @wheadp, align 8, !tbaa !9
  store ptr %28, ptr %8, align 8, !tbaa !9
  br label %29

29:                                               ; preds = %43, %19
  %30 = load ptr, ptr %8, align 8, !tbaa !9
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %47

32:                                               ; preds = %29
  %33 = load ptr, ptr %8, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.WINDOW, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = load ptr, ptr @curbp, align 8, !tbaa !9
  %37 = icmp eq ptr %35, %36
  br i1 %37, label %38, label %43

38:                                               ; preds = %32
  %39 = load ptr, ptr %8, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.WINDOW, ptr %39, i32 0, i32 11
  %41 = load i32, ptr %40, align 4, !tbaa !15
  %42 = or i32 %41, 16
  store i32 %42, ptr %40, align 4, !tbaa !15
  br label %43

43:                                               ; preds = %38, %32
  %44 = load ptr, ptr %8, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.WINDOW, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !16
  store ptr %46, ptr %8, align 8, !tbaa !9
  br label %29, !llvm.loop !17

47:                                               ; preds = %29
  br label %48

48:                                               ; preds = %47, %15
  %49 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %49, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %50

50:                                               ; preds = %48, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 128, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %51 = load i32, ptr %3, align 4
  ret i32 %51
}

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = !{!12, !6, i64 52}
!12 = !{!"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}
!13 = !{!14, !10, i64 8}
!14 = !{!"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}
!15 = !{!14, !6, i64 68}
!16 = !{!14, !10, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; 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.22 = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @cpyfname(ptr noundef, ptr noundef) 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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @filewrite(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca [128 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %3) #3
  %4 = call i32 @choosefile(ptr noundef nonnull @.str.22, ptr noundef nonnull %3, i32 noundef 1)
  %.not = icmp eq i32 %4, 1
  br i1 %.not, label %7, label %5

5:                                                ; preds = %2
  %6 = call i32 (...) @ctrlg() #3
  br label %.loopexit

7:                                                ; preds = %2
  %8 = call i32 @writeout(ptr noundef nonnull %3)
  %9 = icmp eq i32 %8, 1
  br i1 %9, label %10, label %.loopexit

10:                                               ; preds = %7
  %11 = load ptr, ptr @curbp, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 56
  call void @cpyfname(ptr noundef nonnull %12, ptr noundef nonnull %3)
  %13 = load ptr, ptr @curbp, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 52
  %15 = load i32, ptr %14, align 4, !tbaa !9
  %16 = and i32 %15, -3
  store i32 %16, ptr %14, align 4, !tbaa !9
  %.07 = load ptr, ptr @wheadp, align 8, !tbaa !5
  %.not68 = icmp eq ptr %.07, null
  br i1 %.not68, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %10, %24
  %.09 = phi ptr [ %.0, %24 ], [ %.07, %10 ]
  %17 = getelementptr inbounds i8, ptr %.09, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !12
  %19 = icmp eq ptr %18, %13
  br i1 %19, label %20, label %24

20:                                               ; preds = %.lr.ph
  %21 = getelementptr inbounds i8, ptr %.09, i64 68
  %22 = load i32, ptr %21, align 4, !tbaa !14
  %23 = or i32 %22, 16
  store i32 %23, ptr %21, align 4, !tbaa !14
  br label %24

24:                                               ; preds = %20, %.lr.ph
  %.0 = load ptr, ptr %.09, align 8, !tbaa !5
  %.not6 = icmp eq ptr %.0, null
  br i1 %.not6, label %.loopexit, label %.lr.ph, !llvm.loop !15

.loopexit:                                        ; preds = %24, %10, %7, %5
  %.05 = phi i32 [ %6, %5 ], [ %8, %7 ], [ 1, %10 ], [ 1, %24 ]
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %3) #3
  ret i32 %.05
}

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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, !11, i64 52}
!10 = !{!"BUFFER", !6, i64 0, !6, i64 8, !11, i64 16, !6, i64 24, !11, i64 32, !6, i64 40, !11, i64 48, !11, i64 52, !7, i64 56, !7, i64 184}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !6, i64 8}
!13 = !{!"WINDOW", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !6, i64 40, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !11, i64 64, !11, i64 68}
!14 = !{!13, !11, i64 68}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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