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

; 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.CodecJpegSave = type { %struct.jpeg_compress_struct, %struct.img_codec_jpeg_error_mgr, ptr, i32 }
%struct.jpeg_compress_struct = type { ptr, ptr, ptr, ptr, i32, i32, ptr, i32, i32, i32, i32, double, i32, i32, i32, i32, i32, i32, i32, ptr, [4 x ptr], [4 x i32], [4 x ptr], [4 x ptr], [16 x i8], [16 x i8], [16 x i8], i32, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i8, i8, i8, i16, i16, i32, i32, i32, i32, i32, i32, i32, i32, i32, [4 x ptr], i32, i32, i32, [10 x i32], i32, i32, i32, i32, i32, ptr, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32 }
%struct.img_codec_jpeg_error_mgr = type { %struct.jpeg_error_mgr, [1 x %struct.__jmp_buf_tag] }
%struct.jpeg_error_mgr = type { ptr, ptr, ptr, ptr, ptr, i32, %union.anon, i32, i64, ptr, i32, ptr, i32, i32 }
%union.anon = type { [8 x i32], [48 x i8] }
%struct.__jmp_buf_tag = type { [8 x i64], i32, %struct.__sigset_t }
%struct.__sigset_t = type { [16 x i64] }
%struct.CodecJpegText = type { ptr, ptr }

@g_allocator_dopt = external global ptr, align 8

; 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: inlinehint nounwind uwtable
declare hidden i32 @dynbuf_count(ptr noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @dynbuf_append(ptr noundef, i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @imgio_jpeg_save_value_set(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) #2 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !9
  %14 = load i32, ptr %9, align 4, !tbaa !9
  switch i32 %14, label %64 [
    i32 1, label %15
    i32 7, label %20
  ]

15:                                               ; preds = %5
  %16 = load ptr, ptr %10, align 8, !tbaa !5
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.CodecJpegSave, ptr %18, i32 0, i32 3
  store i32 %17, ptr %19, align 8, !tbaa !11
  br label %65

20:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %21 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %21, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %22 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %22, ptr %13, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %28, %20
  %24 = load ptr, ptr %13, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i32 1
  store ptr %25, ptr %13, align 8, !tbaa !5
  %26 = load i8, ptr %24, align 1, !tbaa !19
  %27 = icmp ne i8 %26, 0
  br i1 %27, label %28, label %29

28:                                               ; preds = %23
  br label %23, !llvm.loop !20

29:                                               ; preds = %23
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.CodecJpegSave, ptr %30, i32 0, i32 2
  %32 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !5
  %33 = call ptr @dynbuf_append(ptr noundef %31, i32 noundef 1, ptr noundef null, i64 noundef 16, i32 noundef 2, ptr noundef %32)
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.CodecJpegSave, ptr %34, i32 0, i32 2
  %36 = load ptr, ptr %35, align 8, !tbaa !22
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.CodecJpegSave, ptr %37, i32 0, i32 2
  %39 = load ptr, ptr %38, align 8, !tbaa !22
  %40 = call i32 @dynbuf_count(ptr noundef %39)
  %41 = sub i32 %40, 1
  %42 = sub i32 %41, 0
  %43 = zext i32 %42 to i64
  %44 = getelementptr inbounds %struct.CodecJpegText, ptr %36, i64 %43
  %45 = getelementptr inbounds %struct.CodecJpegText, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %12, align 8, !tbaa !5
  %47 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !5
  %48 = call i32 @dynstr_copyz(ptr noundef %45, ptr noundef %46, ptr noundef %47)
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.CodecJpegSave, ptr %49, i32 0, i32 2
  %51 = load ptr, ptr %50, align 8, !tbaa !22
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.CodecJpegSave, ptr %52, i32 0, i32 2
  %54 = load ptr, ptr %53, align 8, !tbaa !22
  %55 = call i32 @dynbuf_count(ptr noundef %54)
  %56 = sub i32 %55, 1
  %57 = sub i32 %56, 0
  %58 = zext i32 %57 to i64
  %59 = getelementptr inbounds %struct.CodecJpegText, ptr %51, i64 %58
  %60 = getelementptr inbounds %struct.CodecJpegText, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %13, align 8, !tbaa !5
  %62 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !5
  %63 = call i32 @dynstr_copyz(ptr noundef %60, ptr noundef %61, ptr noundef %62)
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %65

64:                                               ; preds = %5
  store i32 -785, ptr %6, align 4
  br label %66

65:                                               ; preds = %29, %15
  store i32 0, ptr %6, align 4
  br label %66

66:                                               ; preds = %65, %64
  %67 = load i32, ptr %6, align 4
  ret i32 %67
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @dynstr_copyz(ptr noundef, ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 }

!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 960}
!12 = !{!"CodecJpegSave", !13, i64 0, !16, i64 584, !6, i64 952, !10, i64 960}
!13 = !{!"jpeg_compress_struct", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 36, !6, i64 40, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !14, i64 64, !10, i64 72, !10, i64 76, !10, i64 80, !10, i64 84, !10, i64 88, !10, i64 92, !10, i64 96, !6, i64 104, !7, i64 112, !7, i64 144, !7, i64 160, !7, i64 192, !7, i64 224, !7, i64 240, !7, i64 256, !10, i64 272, !6, i64 280, !10, i64 288, !10, i64 292, !10, i64 296, !10, i64 300, !10, i64 304, !10, i64 308, !10, i64 312, !10, i64 316, !10, i64 320, !10, i64 324, !7, i64 328, !7, i64 329, !7, i64 330, !15, i64 332, !15, i64 334, !10, i64 336, !10, i64 340, !10, i64 344, !10, i64 348, !10, i64 352, !10, i64 356, !10, i64 360, !10, i64 364, !10, i64 368, !7, i64 376, !10, i64 408, !10, i64 412, !10, i64 416, !7, i64 420, !10, i64 460, !10, i64 464, !10, i64 468, !10, i64 472, !10, i64 476, !6, i64 480, !10, i64 488, !6, i64 496, !6, i64 504, !6, i64 512, !6, i64 520, !6, i64 528, !6, i64 536, !6, i64 544, !6, i64 552, !6, i64 560, !6, i64 568, !10, i64 576}
!14 = !{!"double", !7, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = !{!"img_codec_jpeg_error_mgr", !17, i64 0, !7, i64 168}
!17 = !{!"jpeg_error_mgr", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !10, i64 40, !7, i64 44, !10, i64 124, !18, i64 128, !6, i64 136, !10, i64 144, !6, i64 152, !10, i64 160, !10, i64 164}
!18 = !{!"long", !7, i64 0}
!19 = !{!7, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!12, !6, i64 952}


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

%struct.CodecJpegText = type { ptr, ptr }

@g_allocator_dopt = external local_unnamed_addr global ptr, align 8

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @dynbuf_count(ptr noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @dynbuf_append(ptr noundef, i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -785, 1) i32 @imgio_jpeg_save_value_set(ptr noundef %0, ptr nocapture noundef readnone %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) local_unnamed_addr #1 {
  switch i32 %2, label %29 [
    i32 1, label %6
    i32 7, label %.preheader
  ]

6:                                                ; preds = %5
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %0, i64 960
  store i32 %7, ptr %8, align 8, !tbaa !9
  br label %29

.preheader:                                       ; preds = %5, %.preheader
  %.0 = phi ptr [ %9, %.preheader ], [ %3, %5 ]
  %9 = getelementptr inbounds i8, ptr %.0, i64 1
  %10 = load i8, ptr %.0, align 1, !tbaa !18
  %.not = icmp eq i8 %10, 0
  br i1 %.not, label %11, label %.preheader, !llvm.loop !19

11:                                               ; preds = %.preheader
  %12 = getelementptr inbounds i8, ptr %0, i64 952
  %13 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !21
  %14 = tail call ptr @dynbuf_append(ptr noundef nonnull %12, i32 noundef 1, ptr noundef null, i64 noundef 16, i32 noundef 2, ptr noundef %13)
  %15 = load ptr, ptr %12, align 8, !tbaa !22
  %16 = tail call i32 @dynbuf_count(ptr noundef %15)
  %17 = add i32 %16, -1
  %18 = zext i32 %17 to i64
  %19 = getelementptr inbounds %struct.CodecJpegText, ptr %15, i64 %18
  %20 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !21
  %21 = tail call i32 @dynstr_copyz(ptr noundef %19, ptr noundef %3, ptr noundef %20)
  %22 = load ptr, ptr %12, align 8, !tbaa !22
  %23 = tail call i32 @dynbuf_count(ptr noundef %22)
  %24 = add i32 %23, -1
  %25 = zext i32 %24 to i64
  %26 = getelementptr inbounds %struct.CodecJpegText, ptr %22, i64 %25, i32 1
  %27 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !21
  %28 = tail call i32 @dynstr_copyz(ptr noundef nonnull %26, ptr noundef nonnull %9, ptr noundef %27)
  br label %29

29:                                               ; preds = %6, %11, %5
  %.013 = phi i32 [ -785, %5 ], [ 0, %11 ], [ 0, %6 ]
  ret i32 %.013
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @dynstr_copyz(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

attributes #0 = { 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 #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" }

!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, !6, i64 960}
!10 = !{!"CodecJpegSave", !11, i64 0, !15, i64 584, !12, i64 952, !6, i64 960}
!11 = !{!"jpeg_compress_struct", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !6, i64 32, !6, i64 36, !12, i64 40, !6, i64 48, !6, i64 52, !6, i64 56, !6, i64 60, !13, i64 64, !6, i64 72, !6, i64 76, !6, i64 80, !6, i64 84, !6, i64 88, !6, i64 92, !6, i64 96, !12, i64 104, !7, i64 112, !7, i64 144, !7, i64 160, !7, i64 192, !7, i64 224, !7, i64 240, !7, i64 256, !6, i64 272, !12, i64 280, !6, i64 288, !6, i64 292, !6, i64 296, !6, i64 300, !6, i64 304, !6, i64 308, !6, i64 312, !6, i64 316, !6, i64 320, !6, i64 324, !7, i64 328, !7, i64 329, !7, i64 330, !14, i64 332, !14, i64 334, !6, i64 336, !6, i64 340, !6, i64 344, !6, i64 348, !6, i64 352, !6, i64 356, !6, i64 360, !6, i64 364, !6, i64 368, !7, i64 376, !6, i64 408, !6, i64 412, !6, i64 416, !7, i64 420, !6, i64 460, !6, i64 464, !6, i64 468, !6, i64 472, !6, i64 476, !12, i64 480, !6, i64 488, !12, i64 496, !12, i64 504, !12, i64 512, !12, i64 520, !12, i64 528, !12, i64 536, !12, i64 544, !12, i64 552, !12, i64 560, !12, i64 568, !6, i64 576}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"double", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!"img_codec_jpeg_error_mgr", !16, i64 0, !7, i64 168}
!16 = !{!"jpeg_error_mgr", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !6, i64 40, !7, i64 44, !6, i64 124, !17, i64 128, !12, i64 136, !6, i64 144, !12, i64 152, !6, i64 160, !6, i64 164}
!17 = !{!"long", !7, i64 0}
!18 = !{!7, !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!12, !12, i64 0}
!22 = !{!10, !12, i64 952}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass