; LLVM IR from aagdev/mlimgsynth :: src/ccommon (image_io_pnm)

; 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.ImageIO = type { ptr, ptr, ptr, ptr, i32, i32 }
%struct.Image = type { ptr, i32, i32, i32, i32, i32, i32 }

@.str = external hidden unnamed_addr constant [14 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [14 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [68 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: nounwind uwtable
define dso_local i32 @imgio_pnm_save(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.ImageIO, ptr %12, i32 0, i32 1
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  %15 = call i32 @stream_write_prep(ptr noundef %14, i64 noundef 0)
  %16 = icmp slt i32 %15, 8
  br i1 %16, label %17, label %18

17:                                               ; preds = %3
  store i32 -780, ptr %4, align 4
  br label %106

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !12
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.Image, ptr %19, i32 0, i32 5
  %21 = load i32, ptr %20, align 8, !tbaa !14
  switch i32 %21, label %69 [
    i32 1, label %22
    i32 258, label %37
    i32 771, label %53
  ]

22:                                               ; preds = %18
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.Image, ptr %23, i32 0, i32 1
  %25 = load i32, ptr %24, align 8, !tbaa !16
  %26 = zext i32 %25 to i64
  store i64 %26, ptr %8, align 8, !tbaa !12
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.ImageIO, ptr %27, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !9
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Image, ptr %30, i32 0, i32 1
  %32 = load i32, ptr %31, align 8, !tbaa !16
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Image, ptr %33, i32 0, i32 2
  %35 = load i32, ptr %34, align 4, !tbaa !17
  %36 = call i32 (ptr, ptr, ...) @stream_printf(ptr noundef %29, ptr noundef @.str, i32 noundef %32, i32 noundef %35)
  br label %70

37:                                               ; preds = %18
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.Image, ptr %38, i32 0, i32 1
  %40 = load i32, ptr %39, align 8, !tbaa !16
  %41 = mul i32 %40, 3
  %42 = zext i32 %41 to i64
  store i64 %42, ptr %8, align 8, !tbaa !12
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.ImageIO, ptr %43, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !9
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.Image, ptr %46, i32 0, i32 1
  %48 = load i32, ptr %47, align 8, !tbaa !16
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.Image, ptr %49, i32 0, i32 2
  %51 = load i32, ptr %50, align 4, !tbaa !17
  %52 = call i32 (ptr, ptr, ...) @stream_printf(ptr noundef %45, ptr noundef @.str.1, i32 noundef %48, i32 noundef %51)
  br label %70

53:                                               ; preds = %18
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.Image, ptr %54, i32 0, i32 1
  %56 = load i32, ptr %55, align 8, !tbaa !16
  %57 = mul i32 %56, 4
  %58 = zext i32 %57 to i64
  store i64 %58, ptr %8, align 8, !tbaa !12
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.ImageIO, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !9
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.Image, ptr %62, i32 0, i32 1
  %64 = load i32, ptr %63, align 8, !tbaa !16
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.Image, ptr %65, i32 0, i32 2
  %67 = load i32, ptr %66, align 4, !tbaa !17
  %68 = call i32 (ptr, ptr, ...) @stream_printf(ptr noundef %61, ptr noundef @.str.2, i32 noundef %64, i32 noundef %67)
  br label %70

69:                                               ; preds = %18
  store i32 -776, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %105

70:                                               ; preds = %53, %37, %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.Image, ptr %71, i32 0, i32 0
  %73 = load ptr, ptr %72, align 8, !tbaa !18
  store ptr %73, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !19
  br label %74

74:                                               ; preds = %98, %70
  %75 = load i32, ptr %11, align 4, !tbaa !19
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.Image, ptr %76, i32 0, i32 2
  %78 = load i32, ptr %77, align 4, !tbaa !17
  %79 = icmp ult i32 %75, %78
  br i1 %79, label %81, label %80

80:                                               ; preds = %74
  store i32 3, ptr %9, align 4
  br label %101

81:                                               ; preds = %74
  %82 = load ptr, ptr %6, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.ImageIO, ptr %82, i32 0, i32 1
  %84 = load ptr, ptr %83, align 8, !tbaa !9
  %85 = load i64, ptr %8, align 8, !tbaa !12
  %86 = load ptr, ptr %10, align 8, !tbaa !5
  %87 = call i64 @stream_write(ptr noundef %84, i64 noundef %85, ptr noundef %86)
  %88 = load i64, ptr %8, align 8, !tbaa !12
  %89 = icmp ne i64 %87, %88
  br i1 %89, label %90, label %91

90:                                               ; preds = %81
  store i32 -780, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %101

91:                                               ; preds = %81
  %92 = load ptr, ptr %7, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.Image, ptr %92, i32 0, i32 3
  %94 = load i32, ptr %93, align 8, !tbaa !20
  %95 = load ptr, ptr %10, align 8, !tbaa !5
  %96 = zext i32 %94 to i64
  %97 = getelementptr inbounds i8, ptr %95, i64 %96
  store ptr %97, ptr %10, align 8, !tbaa !5
  br label %98

98:                                               ; preds = %91
  %99 = load i32, ptr %11, align 4, !tbaa !19
  %100 = add i32 %99, 1
  store i32 %100, ptr %11, align 4, !tbaa !19
  br label %74, !llvm.loop !21

101:                                              ; preds = %90, %80
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  %102 = load i32, ptr %9, align 4
  switch i32 %102, label %104 [
    i32 3, label %103
  ]

103:                                              ; preds = %101
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %104

104:                                              ; preds = %103, %101
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %105

105:                                              ; preds = %104, %69
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %106

106:                                              ; preds = %105, %17
  %107 = load i32, ptr %4, align 4
  ret i32 %107
}

declare i32 @stream_write_prep(ptr noundef, i64 noundef) #2

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @stream_write(ptr noalias noundef, i64 noundef, ptr noalias noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { inlinehint 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 #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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"ImageIO", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !11, i64 36}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !11, i64 24}
!15 = !{!"Image", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28}
!16 = !{!15, !11, i64 8}
!17 = !{!15, !11, i64 12}
!18 = !{!15, !6, i64 0}
!19 = !{!11, !11, i64 0}
!20 = !{!15, !11, i64 16}
!21 = distinct !{!21, !22}
!22 = !{!"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"

@.str = external hidden unnamed_addr constant [14 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [14 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [68 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -780, 1) i32 @imgio_pnm_save(ptr nocapture noundef readnone %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %1, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = tail call i32 @stream_write_prep(ptr noundef %5, i64 noundef 0) #3
  %7 = icmp slt i32 %6, 8
  br i1 %7, label %.loopexit, label %8

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %2, i64 24
  %10 = load i32, ptr %9, align 8, !tbaa !11
  switch i32 %10, label %.loopexit [
    i32 1, label %11
    i32 258, label %18
    i32 771, label %26
  ]

11:                                               ; preds = %8
  %12 = getelementptr inbounds i8, ptr %2, i64 8
  %13 = load i32, ptr %12, align 8, !tbaa !13
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %2, i64 12
  %16 = load i32, ptr %15, align 4, !tbaa !14
  %17 = tail call i32 (ptr, ptr, ...) @stream_printf(ptr noundef %14, ptr noundef nonnull @.str, i32 noundef %13, i32 noundef %16) #3
  br label %34

18:                                               ; preds = %8
  %19 = getelementptr inbounds i8, ptr %2, i64 8
  %20 = load i32, ptr %19, align 8, !tbaa !13
  %21 = mul i32 %20, 3
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %2, i64 12
  %24 = load i32, ptr %23, align 4, !tbaa !14
  %25 = tail call i32 (ptr, ptr, ...) @stream_printf(ptr noundef %22, ptr noundef nonnull @.str.1, i32 noundef %20, i32 noundef %24) #3
  br label %34

26:                                               ; preds = %8
  %27 = getelementptr inbounds i8, ptr %2, i64 8
  %28 = load i32, ptr %27, align 8, !tbaa !13
  %29 = shl i32 %28, 2
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %2, i64 12
  %32 = load i32, ptr %31, align 4, !tbaa !14
  %33 = tail call i32 (ptr, ptr, ...) @stream_printf(ptr noundef %30, ptr noundef nonnull @.str.2, i32 noundef %28, i32 noundef %32) #3
  br label %34

34:                                               ; preds = %26, %18, %11
  %.027.in = phi i32 [ %29, %26 ], [ %21, %18 ], [ %13, %11 ]
  %.027 = zext i32 %.027.in to i64
  %35 = getelementptr inbounds i8, ptr %2, i64 12
  %36 = load i32, ptr %35, align 4, !tbaa !14
  %.not2930.not = icmp eq i32 %36, 0
  br i1 %.not2930.not, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %34
  %37 = load ptr, ptr %2, align 8, !tbaa !15
  %38 = getelementptr inbounds i8, ptr %2, i64 16
  br label %39

39:                                               ; preds = %.lr.ph, %42
  %.032 = phi i32 [ 0, %.lr.ph ], [ %46, %42 ]
  %.02531 = phi ptr [ %37, %.lr.ph ], [ %45, %42 ]
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = tail call i64 @stream_write(ptr noundef %40, i64 noundef %.027, ptr noundef %.02531)
  %.not = icmp eq i64 %41, %.027
  br i1 %.not, label %42, label %.loopexit

42:                                               ; preds = %39
  %43 = load i32, ptr %38, align 8, !tbaa !16
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %.02531, i64 %44
  %46 = add nuw i32 %.032, 1
  %47 = load i32, ptr %35, align 4, !tbaa !14
  %.not29 = icmp ult i32 %46, %47
  br i1 %.not29, label %39, label %.loopexit, !llvm.loop !17

.loopexit:                                        ; preds = %39, %42, %34, %8, %3
  %.3 = phi i32 [ -780, %3 ], [ -776, %8 ], [ 0, %34 ], [ -780, %39 ], [ 0, %42 ]
  ret i32 %.3
}

declare i32 @stream_write_prep(ptr noundef, i64 noundef) local_unnamed_addr #1

declare i32 @stream_printf(ptr noundef, ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @stream_write(ptr noalias noundef, i64 noundef, ptr noalias noundef) local_unnamed_addr #2

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 = { "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 = { inlinehint 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, !7, i64 8}
!6 = !{!"ImageIO", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !10, i64 36}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !10, i64 24}
!12 = !{!"Image", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 12}
!15 = !{!12, !7, i64 0}
!16 = !{!12, !10, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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