; LLVM IR from 0intro/wmii :: lib/libbio (bgetrune)

; 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.Biobuf = type { i32, i32, i32, i32, i32, i32, i32, i64, i32, ptr, ptr, ptr, [8196 x i8] }

; Function Attrs: nounwind uwtable
define dso_local i64 @Bgetrune(ptr noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca [4 x i8], align 1
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = call i32 @Bgetc(ptr noundef %9)
  store i32 %10, ptr %4, align 4, !tbaa !9
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = icmp slt i32 %11, 128
  br i1 %12, label %13, label %18

13:                                               ; preds = %1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.Biobuf, ptr %14, i32 0, i32 3
  store i32 1, ptr %15, align 4, !tbaa !11
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  store i64 %17, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %61

18:                                               ; preds = %1
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = trunc i32 %19 to i8
  %21 = getelementptr inbounds [4 x i8], ptr %7, i64 0, i64 0
  store i8 %20, ptr %21, align 1, !tbaa !14
  store i32 1, ptr %5, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %60, %18
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = call i32 @Bgetc(ptr noundef %23)
  store i32 %24, ptr %4, align 4, !tbaa !9
  %25 = load i32, ptr %4, align 4, !tbaa !9
  %26 = icmp slt i32 %25, 0
  br i1 %26, label %27, label %30

27:                                               ; preds = %22
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  store i64 %29, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %61

30:                                               ; preds = %22
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = trunc i32 %31 to i8
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %5, align 4, !tbaa !9
  %35 = sext i32 %33 to i64
  %36 = getelementptr inbounds [4 x i8], ptr %7, i64 0, i64 %35
  store i8 %32, ptr %36, align 1, !tbaa !14
  %37 = getelementptr inbounds [4 x i8], ptr %7, i64 0, i64 0
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = call i32 @fullrune(ptr noundef %37, i32 noundef %38)
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %41, label %60

41:                                               ; preds = %30
  %42 = getelementptr inbounds [4 x i8], ptr %7, i64 0, i64 0
  %43 = call i32 @chartorune(ptr noundef %6, ptr noundef %42)
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Biobuf, ptr %44, i32 0, i32 3
  store i32 %43, ptr %45, align 4, !tbaa !11
  br label %46

46:                                               ; preds = %52, %41
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Biobuf, ptr %48, i32 0, i32 3
  %50 = load i32, ptr %49, align 4, !tbaa !11
  %51 = icmp sgt i32 %47, %50
  br i1 %51, label %52, label %57

52:                                               ; preds = %46
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = call i32 @Bungetc(ptr noundef %53)
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = add nsw i32 %55, -1
  store i32 %56, ptr %5, align 4, !tbaa !9
  br label %46, !llvm.loop !15

57:                                               ; preds = %46
  %58 = load i32, ptr %6, align 4, !tbaa !9
  %59 = zext i32 %58 to i64
  store i64 %59, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %61

60:                                               ; preds = %30
  br label %22

61:                                               ; preds = %57, %27, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %62 = load i64, ptr %2, align 8
  ret i64 %62
}

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

declare i32 @Bgetc(ptr noundef) #2

declare i32 @fullrune(ptr noundef, i32 noundef) #2

declare i32 @chartorune(ptr noundef, ptr noundef) #2

declare i32 @Bungetc(ptr noundef) #2

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

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 = !{!"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, !10, i64 12}
!12 = !{!"Biobuf", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !13, i64 32, !10, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !7, i64 72}
!13 = !{!"long", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

; Function Attrs: nounwind uwtable
define dso_local range(i64 -2147483648, 4294967296) i64 @Bgetrune(ptr noundef %0) local_unnamed_addr #0 {
  %2 = alloca i32, align 4
  %3 = alloca [4 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  %4 = tail call i32 @Bgetc(ptr noundef %0) #3
  %5 = icmp slt i32 %4, 128
  br i1 %5, label %6, label %9

6:                                                ; preds = %1
  %7 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 1, ptr %7, align 4, !tbaa !5
  %8 = sext i32 %4 to i64
  br label %31

9:                                                ; preds = %1
  %10 = trunc i32 %4 to i8
  store i8 %10, ptr %3, align 1, !tbaa !12
  br label %11

11:                                               ; preds = %16, %9
  %indvars.iv = phi i64 [ %indvars.iv.next, %16 ], [ 1, %9 ]
  %12 = call i32 @Bgetc(ptr noundef %0) #3
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %14, label %16

14:                                               ; preds = %11
  %15 = sext i32 %12 to i64
  br label %31

16:                                               ; preds = %11
  %17 = trunc i32 %12 to i8
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %18 = getelementptr inbounds [4 x i8], ptr %3, i64 0, i64 %indvars.iv
  store i8 %17, ptr %18, align 1, !tbaa !12
  %19 = trunc nuw i64 %indvars.iv.next to i32
  %20 = call i32 @fullrune(ptr noundef nonnull %3, i32 noundef %19) #3
  %.not = icmp eq i32 %20, 0
  br i1 %.not, label %11, label %21

21:                                               ; preds = %16
  %22 = trunc nuw nsw i64 %indvars.iv to i32
  %23 = call i32 @chartorune(ptr noundef nonnull %2, ptr noundef nonnull %3) #3
  %24 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 %23, ptr %24, align 4, !tbaa !5
  %.not24 = icmp sgt i32 %23, %22
  br i1 %.not24, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %21, %.lr.ph
  %.123 = phi i32 [ %26, %.lr.ph ], [ %19, %21 ]
  %25 = call i32 @Bungetc(ptr noundef nonnull %0) #3
  %26 = add nsw i32 %.123, -1
  %27 = load i32, ptr %24, align 4, !tbaa !5
  %28 = icmp sgt i32 %26, %27
  br i1 %28, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %21
  %29 = load i32, ptr %2, align 4, !tbaa !15
  %30 = zext i32 %29 to i64
  br label %31

31:                                               ; preds = %._crit_edge, %14, %6
  %.016 = phi i64 [ %8, %6 ], [ %15, %14 ], [ %30, %._crit_edge ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  ret i64 %.016
}

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

declare i32 @Bgetc(ptr noundef) local_unnamed_addr #2

declare i32 @fullrune(ptr noundef, i32 noundef) local_unnamed_addr #2

declare i32 @chartorune(ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @Bungetc(ptr noundef) local_unnamed_addr #2

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

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, !7, i64 12}
!6 = !{!"Biobuf", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !7, i64 24, !10, i64 32, !7, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !8, i64 72}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!8, !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!7, !7, i64 0}


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