; LLVM IR from petecoup/argon :: src/codec (ppm_codec)

; 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.arRGBBitmap = type { i32, i32, ptr }

@.str.16 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [14 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [43 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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @ar_write_ppm_file(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca [32 x i8], align 16
  %8 = alloca i32, align 4
  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 8, ptr %5) #5
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = call noalias ptr @fopen(ptr noundef %9, ptr noundef @.str.16)
  store ptr %10, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr %7) #5
  %11 = getelementptr inbounds [32 x i8], ptr %7, i64 0, i64 0
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.arRGBBitmap, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 8, !tbaa !9
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.arRGBBitmap, ptr %15, i32 0, i32 1
  %17 = load i32, ptr %16, align 4, !tbaa !12
  %18 = call i32 (ptr, ptr, ...) @sprintf(ptr noundef %11, ptr noundef @.str.17, i32 noundef %14, i32 noundef %17) #5
  %19 = getelementptr inbounds [32 x i8], ptr %7, i64 0, i64 0
  %20 = getelementptr inbounds [32 x i8], ptr %7, i64 0, i64 0
  %21 = call i64 @strlen(ptr noundef %20) #6
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = call i64 @fwrite(ptr noundef %19, i64 noundef 1, i64 noundef %21, ptr noundef %22)
  store i64 %23, ptr %6, align 8, !tbaa !13
  %24 = load i64, ptr %6, align 8, !tbaa !13
  %25 = getelementptr inbounds [32 x i8], ptr %7, i64 0, i64 0
  %26 = call i64 @strlen(ptr noundef %25) #6
  %27 = icmp ne i64 %24, %26
  br i1 %27, label %28, label %35

28:                                               ; preds = %2
  %29 = load i64, ptr %6, align 8, !tbaa !13
  %30 = trunc i64 %29 to i32
  %31 = getelementptr inbounds [32 x i8], ptr %7, i64 0, i64 0
  %32 = call i64 @strlen(ptr noundef %31) #6
  %33 = trunc i64 %32 to i32
  %34 = call i32 (ptr, ...) @printf(ptr noundef @.str.18, i32 noundef %30, i32 noundef %33)
  store i32 1, ptr %8, align 4
  br label %74

35:                                               ; preds = %2
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.arRGBBitmap, ptr %36, i32 0, i32 2
  %38 = load ptr, ptr %37, align 8, !tbaa !15
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.arRGBBitmap, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %40, align 8, !tbaa !9
  %42 = mul i32 3, %41
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.arRGBBitmap, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 4, !tbaa !12
  %46 = mul i32 %42, %45
  %47 = zext i32 %46 to i64
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = call i64 @fwrite(ptr noundef %38, i64 noundef 1, i64 noundef %47, ptr noundef %48)
  store i64 %49, ptr %6, align 8, !tbaa !13
  %50 = load i64, ptr %6, align 8, !tbaa !13
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.arRGBBitmap, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 8, !tbaa !9
  %54 = mul i32 3, %53
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.arRGBBitmap, ptr %55, i32 0, i32 1
  %57 = load i32, ptr %56, align 4, !tbaa !12
  %58 = mul i32 %54, %57
  %59 = zext i32 %58 to i64
  %60 = icmp ne i64 %50, %59
  br i1 %60, label %61, label %73

61:                                               ; preds = %35
  %62 = load i64, ptr %6, align 8, !tbaa !13
  %63 = trunc i64 %62 to i32
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.arRGBBitmap, ptr %64, i32 0, i32 0
  %66 = load i32, ptr %65, align 8, !tbaa !9
  %67 = mul i32 3, %66
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.arRGBBitmap, ptr %68, i32 0, i32 1
  %70 = load i32, ptr %69, align 4, !tbaa !12
  %71 = mul i32 %67, %70
  %72 = call i32 (ptr, ...) @printf(ptr noundef @.str.18, i32 noundef %63, i32 noundef %71)
  store i32 1, ptr %8, align 4
  br label %74

73:                                               ; preds = %35
  store i32 0, ptr %8, align 4
  br label %74

74:                                               ; preds = %73, %61, %28
  call void @llvm.lifetime.end.p0(i64 32, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  %75 = load i32, ptr %8, align 4
  switch i32 %75, label %77 [
    i32 0, label %76
    i32 1, label %76
  ]

76:                                               ; preds = %74, %74
  ret void

77:                                               ; preds = %74
  unreachable
}

; Function Attrs: nounwind
declare i32 @sprintf(ptr noundef, ptr noundef, ...) #3

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 "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(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(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, !11, i64 0}
!10 = !{!"arRGBBitmap", !11, i64 0, !11, i64 4, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 4}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!10, !6, i64 8}


; 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.16 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [14 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [43 x i8], align 1

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

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

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

; 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 uwtable
define dso_local void @ar_write_ppm_file(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1) local_unnamed_addr #2 {
  %3 = alloca [32 x i8], align 16
  %4 = tail call noalias ptr @fopen(ptr noundef %1, ptr noundef nonnull @.str.16)
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %3) #4
  %5 = load i32, ptr %0, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %8 = call i32 (ptr, ptr, ...) @sprintf(ptr noundef nonnull dereferenceable(1) %3, ptr noundef nonnull dereferenceable(1) @.str.17, i32 noundef %5, i32 noundef %7) #4
  %9 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %3) #5
  %10 = call i64 @fwrite(ptr noundef nonnull %3, i64 noundef 1, i64 noundef %9, ptr noundef %4)
  %11 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %3) #5
  %.not = icmp eq i64 %10, %11
  br i1 %.not, label %16, label %12

12:                                               ; preds = %2
  %13 = trunc i64 %10 to i32
  %14 = trunc i64 %11 to i32
  %15 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.18, i32 noundef %13, i32 noundef %14)
  br label %33

16:                                               ; preds = %2
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !12
  %19 = load i32, ptr %0, align 8, !tbaa !5
  %20 = mul i32 %19, 3
  %21 = load i32, ptr %6, align 4, !tbaa !11
  %22 = mul i32 %20, %21
  %23 = zext i32 %22 to i64
  %24 = tail call i64 @fwrite(ptr noundef %18, i64 noundef 1, i64 noundef %23, ptr noundef %4)
  %25 = load i32, ptr %0, align 8, !tbaa !5
  %26 = mul i32 %25, 3
  %27 = load i32, ptr %6, align 4, !tbaa !11
  %28 = mul i32 %26, %27
  %29 = zext i32 %28 to i64
  %.not17 = icmp eq i64 %24, %29
  br i1 %.not17, label %33, label %30

30:                                               ; preds = %16
  %31 = trunc i64 %24 to i32
  %32 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.18, i32 noundef %31, i32 noundef %28)
  br label %33

33:                                               ; preds = %16, %30, %12
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %3) #4
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @sprintf(ptr noalias nocapture noundef writeonly, 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

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nofree 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 = { 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 #4 = { nounwind }
attributes #5 = { 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, !7, i64 0}
!6 = !{!"arRGBBitmap", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !10, i64 8}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass