; LLVM IR from Wilson-ZheLin/Introduction-to-Digital-Image-Processing :: 1. Average Filter and Median Filter/src/main.ll (Z12SavePNMImageP5ImagePc)

; 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.Image = type { i32, i32, i32, ptr, [15 x [100 x i8]], i32 }

@.str.17 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.22 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.23 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.24 = external hidden unnamed_addr constant [32 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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z12SavePNMImageP5ImagePc(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = call i32 (ptr, ...) @printf(ptr noundef @.str.17, ptr noundef %9)
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = call noalias ptr @fopen(ptr noundef %11, ptr noundef @.str.18)
  store ptr %12, ptr %8, align 8, !tbaa !5
  %13 = load ptr, ptr %8, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %17, label %15

15:                                               ; preds = %2
  %16 = call i32 (ptr, ...) @printf(ptr noundef @.str.19)
  call void @exit(i32 noundef 0) #6
  unreachable

17:                                               ; preds = %2
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.Image, ptr %18, i32 0, i32 2
  %20 = load i32, ptr %19, align 8, !tbaa !9
  %21 = icmp eq i32 %20, 1
  br i1 %21, label %22, label %32

22:                                               ; preds = %17
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %23, ptr noundef @.str.20) #5
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.Image, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 8, !tbaa !12
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.Image, ptr %28, i32 0, i32 1
  %30 = load i32, ptr %29, align 4, !tbaa !13
  %31 = mul nsw i32 %27, %30
  store i32 %31, ptr %7, align 4, !tbaa !14
  br label %49

32:                                               ; preds = %17
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Image, ptr %33, i32 0, i32 2
  %35 = load i32, ptr %34, align 8, !tbaa !9
  %36 = icmp eq i32 %35, 2
  br i1 %36, label %37, label %48

37:                                               ; preds = %32
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %38, ptr noundef @.str.21) #5
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.Image, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !12
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.Image, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 4, !tbaa !13
  %46 = mul nsw i32 %42, %45
  %47 = mul nsw i32 %46, 3
  store i32 %47, ptr %7, align 4, !tbaa !14
  br label %48

48:                                               ; preds = %37, %32
  br label %49

49:                                               ; preds = %48, %22
  store i32 0, ptr %6, align 4, !tbaa !14
  br label %50

50:                                               ; preds = %65, %49
  %51 = load i32, ptr %6, align 4, !tbaa !14
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.Image, ptr %52, i32 0, i32 5
  %54 = load i32, ptr %53, align 4, !tbaa !15
  %55 = icmp slt i32 %51, %54
  br i1 %55, label %56, label %68

56:                                               ; preds = %50
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.Image, ptr %58, i32 0, i32 4
  %60 = load i32, ptr %6, align 4, !tbaa !14
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [15 x [100 x i8]], ptr %59, i64 0, i64 %61
  %63 = getelementptr inbounds [100 x i8], ptr %62, i64 0, i64 0
  %64 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %57, ptr noundef @.str.22, ptr noundef %63) #5
  br label %65

65:                                               ; preds = %56
  %66 = load i32, ptr %6, align 4, !tbaa !14
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %6, align 4, !tbaa !14
  br label %50, !llvm.loop !16

68:                                               ; preds = %50
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.Image, ptr %70, i32 0, i32 0
  %72 = load i32, ptr %71, align 8, !tbaa !12
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.Image, ptr %73, i32 0, i32 1
  %75 = load i32, ptr %74, align 4, !tbaa !13
  %76 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %69, ptr noundef @.str.23, i32 noundef %72, i32 noundef %75, i32 noundef 255) #5
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.Image, ptr %77, i32 0, i32 3
  %79 = load ptr, ptr %78, align 8, !tbaa !18
  %80 = load i32, ptr %7, align 4, !tbaa !14
  %81 = sext i32 %80 to i64
  %82 = load ptr, ptr %8, align 8, !tbaa !5
  %83 = call i64 @fwrite(ptr noundef %79, i64 noundef 1, i64 noundef %81, ptr noundef %82)
  %84 = trunc i64 %83 to i32
  store i32 %84, ptr %5, align 4, !tbaa !14
  %85 = load i32, ptr %5, align 4, !tbaa !14
  %86 = load i32, ptr %7, align 4, !tbaa !14
  %87 = icmp ne i32 %85, %86
  br i1 %87, label %88, label %90

88:                                               ; preds = %68
  %89 = call i32 (ptr, ...) @printf(ptr noundef @.str.24)
  call void @exit(i32 noundef 0) #6
  unreachable

90:                                               ; preds = %68
  %91 = load ptr, ptr %8, align 8, !tbaa !5
  %92 = call i32 @fclose(ptr noundef %91)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  ret void
}

declare noalias ptr @fopen(ptr noundef, ptr noundef) #2

declare i32 @printf(ptr noundef, ...) #2

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #3

declare i32 @fclose(ptr noundef) #2

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #4

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 #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 = { noreturn 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 "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 = { noreturn 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++ TBAA"}
!9 = !{!10, !11, i64 8}
!10 = !{!"_ZTS5Image", !11, i64 0, !11, i64 4, !11, i64 8, !6, i64 16, !7, i64 24, !11, i64 1524}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!10, !11, i64 4}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !11, i64 1524}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!10, !6, i64 16}


; 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.17 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.22 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.23 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.24 = external hidden unnamed_addr constant [32 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z12SavePNMImageP5ImagePc(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.17, ptr noundef %1)
  %4 = tail call noalias ptr @fopen(ptr noundef %1, ptr noundef nonnull @.str.18)
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %5, label %7

5:                                                ; preds = %2
  %6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.19)
  tail call void @exit(i32 noundef 0) #3
  unreachable

7:                                                ; preds = %2
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  %9 = load i32, ptr %8, align 8, !tbaa !5
  switch i32 %9, label %23 [
    i32 1, label %10
    i32 2, label %16
  ]

10:                                               ; preds = %7
  %11 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef nonnull %4, ptr noundef nonnull @.str.20) #4
  %12 = load i32, ptr %0, align 8, !tbaa !11
  %13 = getelementptr inbounds i8, ptr %0, i64 4
  %14 = load i32, ptr %13, align 4, !tbaa !12
  %15 = mul nsw i32 %14, %12
  br label %23

16:                                               ; preds = %7
  %17 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef nonnull %4, ptr noundef nonnull @.str.21) #4
  %18 = load i32, ptr %0, align 8, !tbaa !11
  %19 = getelementptr inbounds i8, ptr %0, i64 4
  %20 = load i32, ptr %19, align 4, !tbaa !12
  %21 = mul i32 %18, 3
  %22 = mul i32 %21, %20
  br label %23

23:                                               ; preds = %7, %16, %10
  %.025 = phi i32 [ %15, %10 ], [ %22, %16 ], [ undef, %7 ]
  %24 = getelementptr inbounds i8, ptr %0, i64 1524
  %25 = load i32, ptr %24, align 4, !tbaa !13
  %26 = icmp sgt i32 %25, 0
  br i1 %26, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %23
  %27 = getelementptr inbounds i8, ptr %0, i64 24
  br label %28

28:                                               ; preds = %.lr.ph, %28
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %28 ]
  %29 = getelementptr inbounds [15 x [100 x i8]], ptr %27, i64 0, i64 %indvars.iv
  %30 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef nonnull %4, ptr noundef nonnull @.str.22, ptr noundef nonnull %29) #4
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %31 = load i32, ptr %24, align 4, !tbaa !13
  %32 = sext i32 %31 to i64
  %33 = icmp slt i64 %indvars.iv.next, %32
  br i1 %33, label %28, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %28, %23
  %34 = load i32, ptr %0, align 8, !tbaa !11
  %35 = getelementptr inbounds i8, ptr %0, i64 4
  %36 = load i32, ptr %35, align 4, !tbaa !12
  %37 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef nonnull %4, ptr noundef nonnull @.str.23, i32 noundef %34, i32 noundef %36, i32 noundef 255) #4
  %38 = getelementptr inbounds i8, ptr %0, i64 16
  %39 = load ptr, ptr %38, align 8, !tbaa !16
  %40 = sext i32 %.025 to i64
  %41 = tail call i64 @fwrite(ptr noundef %39, i64 noundef 1, i64 noundef %40, ptr noundef nonnull %4)
  %42 = trunc i64 %41 to i32
  %.not28 = icmp eq i32 %.025, %42
  br i1 %.not28, label %45, label %43

43:                                               ; preds = %._crit_edge
  %44 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.24)
  tail call void @exit(i32 noundef 0) #3
  unreachable

45:                                               ; preds = %._crit_edge
  %46 = tail call i32 @fclose(ptr noundef nonnull %4)
  ret void
}

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #2

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

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { 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 #2 = { noreturn 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 = { noreturn nounwind }
attributes #4 = { 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 8}
!6 = !{!"_ZTS5Image", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16, !8, i64 24, !7, i64 1524}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!6, !7, i64 4}
!13 = !{!6, !7, i64 1524}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !10, i64 16}


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