; LLVM IR from AllenDowney/ExercisesInC :: exercises/ex11 (ikkp-server)

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

@.str.7 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @error(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 willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

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

12:                                               ; preds = %28, %3
  %13 = call ptr @__ctype_b_loc() #7
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  %15 = load ptr, ptr %7, align 8, !tbaa !9
  %16 = call i32 @fgetc(ptr noundef %15)
  store i32 %16, ptr %9, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i16, ptr %14, i64 %17
  %19 = load i16, ptr %18, align 2, !tbaa !11
  %20 = zext i16 %19 to i32
  %21 = and i32 %20, 8192
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %26

23:                                               ; preds = %12
  %24 = load i32, ptr %9, align 4, !tbaa !5
  %25 = icmp ne i32 %24, -1
  br label %26

26:                                               ; preds = %23, %12
  %27 = phi i1 [ false, %12 ], [ %25, %23 ]
  br i1 %27, label %28, label %29

28:                                               ; preds = %26
  br label %12, !llvm.loop !13

29:                                               ; preds = %26
  %30 = load ptr, ptr %7, align 8, !tbaa !9
  %31 = call i32 @ferror(ptr noundef %30) #6
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %34

33:                                               ; preds = %29
  call void @error(ptr noundef @.str.8)
  br label %34

34:                                               ; preds = %33, %29
  br label %35

35:                                               ; preds = %55, %34
  %36 = load i32, ptr %9, align 4, !tbaa !5
  %37 = icmp ne i32 %36, 10
  br i1 %37, label %38, label %41

38:                                               ; preds = %35
  %39 = load i32, ptr %9, align 4, !tbaa !5
  %40 = icmp ne i32 %39, -1
  br label %41

41:                                               ; preds = %38, %35
  %42 = phi i1 [ false, %35 ], [ %40, %38 ]
  br i1 %42, label %43, label %58

43:                                               ; preds = %41
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %47, label %55

47:                                               ; preds = %43
  %48 = load i32, ptr %9, align 4, !tbaa !5
  %49 = trunc i32 %48 to i8
  %50 = load ptr, ptr %5, align 8, !tbaa !9
  %51 = load i32, ptr %8, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %8, align 4, !tbaa !5
  %53 = sext i32 %51 to i64
  %54 = getelementptr inbounds i8, ptr %50, i64 %53
  store i8 %49, ptr %54, align 1, !tbaa !15
  br label %55

55:                                               ; preds = %47, %43
  %56 = load ptr, ptr %7, align 8, !tbaa !9
  %57 = call i32 @fgetc(ptr noundef %56)
  store i32 %57, ptr %9, align 4, !tbaa !5
  br label %35, !llvm.loop !16

58:                                               ; preds = %41
  %59 = load ptr, ptr %7, align 8, !tbaa !9
  %60 = call i32 @ferror(ptr noundef %59) #6
  %61 = icmp ne i32 %60, 0
  br i1 %61, label %62, label %63

62:                                               ; preds = %58
  call void @error(ptr noundef @.str.8)
  br label %63

63:                                               ; preds = %62, %58
  br label %64

64:                                               ; preds = %80, %63
  %65 = call ptr @__ctype_b_loc() #7
  %66 = load ptr, ptr %65, align 8, !tbaa !9
  %67 = load ptr, ptr %5, align 8, !tbaa !9
  %68 = load i32, ptr %8, align 4, !tbaa !5
  %69 = add nsw i32 %68, -1
  store i32 %69, ptr %8, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds i8, ptr %67, i64 %70
  %72 = load i8, ptr %71, align 1, !tbaa !15
  %73 = sext i8 %72 to i32
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds i16, ptr %66, i64 %74
  %76 = load i16, ptr %75, align 2, !tbaa !11
  %77 = zext i16 %76 to i32
  %78 = and i32 %77, 8192
  %79 = icmp ne i32 %78, 0
  br i1 %79, label %80, label %85

80:                                               ; preds = %64
  %81 = load ptr, ptr %5, align 8, !tbaa !9
  %82 = load i32, ptr %8, align 4, !tbaa !5
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds i8, ptr %81, i64 %83
  store i8 0, ptr %84, align 1, !tbaa !15
  br label %64, !llvm.loop !17

85:                                               ; preds = %64
  %86 = load ptr, ptr %5, align 8, !tbaa !9
  %87 = call i64 @strlen(ptr noundef %86) #8
  %88 = trunc i64 %87 to i32
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  ret i32 %88
}

; Function Attrs: nounwind
declare noalias ptr @fdopen(i32 noundef, ptr noundef) #3

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() #4

declare i32 @fgetc(ptr noundef) #5

; Function Attrs: nounwind
declare i32 @ferror(ptr noundef) #3

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 = { 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 #3 = { 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 #4 = { nounwind willreturn memory(none) "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 = { "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(none) }
attributes #8 = { nounwind willreturn memory(read) }

!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, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !14}
!17 = distinct !{!17, !14}


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

@.str.7 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [6 x i8], align 1

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @read_in(i32 noundef %0, ptr nocapture noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = tail call noalias ptr @fdopen(i32 noundef %0, ptr noundef nonnull @.str.7) #5
  %5 = tail call ptr @__ctype_b_loc() #6
  br label %6

6:                                                ; preds = %6, %3
  %7 = load ptr, ptr %5, align 8, !tbaa !5
  %8 = tail call i32 @fgetc(ptr noundef %4)
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds i16, ptr %7, i64 %9
  %11 = load i16, ptr %10, align 2, !tbaa !9
  %12 = and i16 %11, 8192
  %13 = icmp ne i16 %12, 0
  %14 = icmp ne i32 %8, -1
  %15 = and i1 %14, %13
  br i1 %15, label %6, label %16, !llvm.loop !11

16:                                               ; preds = %6
  %17 = tail call i32 @ferror(ptr noundef %4) #5
  %.not = icmp eq i32 %17, 0
  br i1 %.not, label %.preheader, label %18

18:                                               ; preds = %16
  tail call void @error(ptr noundef nonnull @.str.8)
  br label %.preheader

.preheader:                                       ; preds = %18, %16
  br label %19

19:                                               ; preds = %.preheader, %27
  %.017 = phi i32 [ %.1, %27 ], [ 0, %.preheader ]
  %.0 = phi i32 [ %28, %27 ], [ %8, %.preheader ]
  switch i32 %.0, label %20 [
    i32 -1, label %29
    i32 10, label %29
  ]

20:                                               ; preds = %19
  %21 = icmp slt i32 %.017, %2
  br i1 %21, label %22, label %27

22:                                               ; preds = %20
  %23 = trunc i32 %.0 to i8
  %24 = add nsw i32 %.017, 1
  %25 = sext i32 %.017 to i64
  %26 = getelementptr inbounds i8, ptr %1, i64 %25
  store i8 %23, ptr %26, align 1, !tbaa !13
  br label %27

27:                                               ; preds = %22, %20
  %.1 = phi i32 [ %24, %22 ], [ %.017, %20 ]
  %28 = tail call i32 @fgetc(ptr noundef %4)
  br label %19, !llvm.loop !14

29:                                               ; preds = %19, %19
  %30 = tail call i32 @ferror(ptr noundef %4) #5
  %.not18 = icmp eq i32 %30, 0
  br i1 %.not18, label %32, label %31

31:                                               ; preds = %29
  tail call void @error(ptr noundef nonnull @.str.8)
  br label %32

32:                                               ; preds = %31, %29
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = add nsw i32 %.017, -1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i8, ptr %1, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !13
  %38 = sext i8 %37 to i64
  %39 = getelementptr inbounds i16, ptr %33, i64 %38
  %40 = load i16, ptr %39, align 2, !tbaa !9
  %41 = and i16 %40, 8192
  %.not1921 = icmp eq i16 %41, 0
  br i1 %.not1921, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %32, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %35, %32 ]
  %42 = phi ptr [ %44, %.lr.ph ], [ %36, %32 ]
  store i8 0, ptr %42, align 1, !tbaa !13
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %44 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.next
  %45 = load i8, ptr %44, align 1, !tbaa !13
  %46 = sext i8 %45 to i64
  %47 = getelementptr inbounds i16, ptr %43, i64 %46
  %48 = load i16, ptr %47, align 2, !tbaa !9
  %49 = and i16 %48, 8192
  %.not19 = icmp eq i16 %49, 0
  br i1 %.not19, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %32
  %50 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #7
  %51 = trunc i64 %50 to i32
  ret i32 %51
}

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fdopen(i32 noundef, ptr nocapture noundef readonly) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @fgetc(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind memory(read)
declare noundef i32 @ferror(ptr nocapture noundef) local_unnamed_addr #4

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 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 #2 = { nofree 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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 = { nofree nounwind 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 }
attributes #6 = { nounwind willreturn memory(none) }
attributes #7 = { nounwind willreturn memory(read) }

!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 = !{!"short", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !12}
!15 = distinct !{!15, !12}


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