#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int, int);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ rgb24_to_yuv420p(unsigned char *, unsigned char *,
                                    unsigned char *, unsigned char *, int, int);
void pgmyuv_save(const char *filename, int w, int h, unsigned char *rgb_tab) {
  FILE *f;
  int i, h2, w2;
  unsigned char *cb, *cr;
  unsigned char *lum_tab, *cb_tab, *cr_tab;
  lum_tab = malloc(w * h);
  cb_tab = malloc(w * h / 4);
  cr_tab = malloc(w * h / 4);
  rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
  f = fopen(filename, "wb");
  fprintf(f, "P5\n%d %d\n%d\n", w, h * 3 / 2, 255);
  fwrite(lum_tab, 1, w * h, f);
  h2 = h / 2;
  w2 = w / 2;
  cb = cb_tab;
  cr = cr_tab;
  for (i = 0; i < h2; i++) {
    fwrite(cb, 1, w2, f);
    fwrite(cr, 1, w2, f);
    cb += w2;
    cr += w2;
  }
  fclose(f);
  free(lum_tab);
  free(cb_tab);
  free(cr_tab);
}


x86 assembly (clang, -O0):
<pgmyuv_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0xc(%rbp),%edi
imul   -0x10(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x10(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x10(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0xc(%rbp),%r8d
mov    -0x10(%rbp),%r9d
call   1070 <rgb24_to_yuv420p@plt>
mov    -0x8(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%eax
mov    %eax,-0x5c(%rbp)
imul   $0x3,-0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x5c(%rbp),%edx
mov    %eax,%ecx
lea    0xde6(%rip),%rsi
mov    $0xff,%r8d
call   1050 <fprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0xc(%rbp),%edx
imul   -0x10(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    12da <pgmyuv_save+0x16a>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x40(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1274 <pgmyuv_save+0x104>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgmyuv_save>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x20(%rsp)
mov    %edx,%r14d
mov    %esi,%r13d
mov    %rdi,0x8(%rsp)
mov    %edx,%ebp
imul   %esi,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0x3(%rbp),%r12d
test   %ebp,%ebp
cmovns %ebp,%r12d
sar    $0x2,%r12d
mov    %r12d,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %r12d,%edi
call   1060 <malloc@plt>
mov    %rbx,%rdi
mov    %r15,0x18(%rsp)
mov    %r15,%rsi
mov    %rax,0x10(%rsp)
mov    %rax,%rdx
mov    0x20(%rsp),%rcx
mov    %r13d,%r8d
mov    %r14d,%r9d
call   1070 <rgb24_to_yuv420p@plt>
lea    0xe18(%rip),%rsi
mov    0x8(%rsp),%rdi
call   1080 <fopen@plt>
mov    %rax,%r15
lea    (%r14,%r14,2),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
lea    0xdfa(%rip),%rsi
mov    %r15,%rdi
mov    %r13d,%edx
mov    $0xff,%r8d
call   1050 <fprintf@plt>
mov    %rbx,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1090 <fwrite@plt>
cmp    $0x2,%r14d
jl     1292 <pgmyuv_save+0x122>
mov    %r14d,%r12d
shr    $0x1f,%r12d
add    %r14d,%r12d
sar    %r12d
mov    %r13d,%eax
shr    $0x1f,%eax
add    %r13d,%eax
sar    %eax
movslq %eax,%r14
mov    0x18(%rsp),%rbx
mov    0x10(%rsp),%rbp
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r15,%rcx
call   1090 <fwrite@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r15,%rcx
call   1090 <fwrite@plt>
add    %r14,%rbx
add    %r14,%rbp
add    $0xffffffff,%r12d
jne    1260 <pgmyuv_save+0xf0>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [13 x i8] c"P5\0A%d %d\0A%d\0A\00"
define void @pgmyuv_save(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%18 = load i32, i32* %6
%19 = load i32, i32* %7
%20 = mul nsw i32 %18, %19
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %15
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = mul nsw i32 %22, %23
%25 = sdiv i32 %24, 4
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %16
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = mul nsw i32 %27, %28
%30 = sdiv i32 %29, 4
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %17
%32 = load i8*, i8** %15
%33 = load i8*, i8** %16
%34 = load i8*, i8** %17
%35 = load i8*, i8** %8
%36 = load i32, i32* %6
%37 = load i32, i32* %7
%38 = call i32 @rgb24_to_yuv420p(i8* %32, i8* %33, i8* %34, i8* %35, i32 %36, i32 %37)
%39 = load i8*, i8** %5
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %40, i32** %9
%41 = load i32*, i32** %9
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = mul nsw i32 %43, 3
%45 = sdiv i32 %44, 2
%46 = call i32 @fprintf(i32* %41, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %42, i32 %45, i32 255)
%47 = load i8*, i8** %15
%48 = load i32, i32* %6
%49 = load i32, i32* %7
%50 = mul nsw i32 %48, %49
%51 = load i32*, i32** %9
%52 = call i32 @fwrite(i8* %47, i32 1, i32 %50, i32* %51)
%53 = load i32, i32* %7
%54 = sdiv i32 %53, 2
store i32 %54, i32* %11
%55 = load i32, i32* %6
%56 = sdiv i32 %55, 2
store i32 %56, i32* %12
%57 = load i8*, i8** %16
store i8* %57, i8** %13
%58 = load i8*, i8** %17
store i8* %58, i8** %14
store i32 0, i32* %10
br label %59
59: ; preds = %80, %4
%60 = load i32, i32* %10
%61 = load i32, i32* %11
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %83
63: ; preds = %59
%64 = load i8*, i8** %13
%65 = load i32, i32* %12
%66 = load i32*, i32** %9
%67 = call i32 @fwrite(i8* %64, i32 1, i32 %65, i32* %66)
%68 = load i8*, i8** %14
%69 = load i32, i32* %12
%70 = load i32*, i32** %9
%71 = call i32 @fwrite(i8* %68, i32 1, i32 %69, i32* %70)
%72 = load i32, i32* %12
%73 = load i8*, i8** %13
%74 = sext i32 %72 to i64
%75 = getelementptr inbounds i8, i8* %73, i64 %74
store i8* %75, i8** %13
%76 = load i32, i32* %12
%77 = load i8*, i8** %14
%78 = sext i32 %76 to i64
%79 = getelementptr inbounds i8, i8* %77, i64 %78
store i8* %79, i8** %14
br label %80
80: ; preds = %63
%81 = load i32, i32* %10
%82 = add nsw i32 %81, 1
store i32 %82, i32* %10
br label %59
83: ; preds = %59
%84 = load i32*, i32** %9
%85 = call i32 @fclose(i32* %84)
%86 = load i8*, i8** %15
%87 = call i32 @free(i8* %86)
%88 = load i8*, i8** %16
%89 = call i32 @free(i8* %88)
%90 = load i8*, i8** %17
%91 = call i32 @free(i8* %90)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [13 x i8] c"P5\0A%d %d\0A%d\0A\00"
define void @pgmyuv_save(i8* nocapture %0, i32 %1, i32 %2, i8* %3) {
%5 = mul nsw i32 %2, %1
%6 = tail call i8* @malloc(i32 %5)
%7 = sdiv i32 %5, 4
%8 = tail call i8* @malloc(i32 %7)
%9 = tail call i8* @malloc(i32 %7)
%10 = tail call i32 @rgb24_to_yuv420p(i8* %6, i8* %8, i8* %9, i8* %3, i32 %1, i32 %2)
%11 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = mul nsw i32 %2, 3
%13 = sdiv i32 %12, 2
%14 = tail call i32 @fprintf(i32* %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %13, i32 255)
%15 = tail call i32 @fwrite(i8* %6, i32 1, i32 %5, i32* %11)
%16 = sdiv i32 %2, 2
%17 = sdiv i32 %1, 2
%18 = icmp sgt i32 %2, 1
br i1 %18, label %19, label %31
19: ; preds = %4
%20 = sext i32 %17 to i64
br label %21
21: ; preds = %19, %21
%22 = phi i32 [ 0, %19 ], [ %29, %21 ]
%23 = phi i8* [ %8, %19 ], [ %27, %21 ]
%24 = phi i8* [ %9, %19 ], [ %28, %21 ]
%25 = tail call i32 @fwrite(i8* %23, i32 1, i32 %17, i32* %11)
%26 = tail call i32 @fwrite(i8* %24, i32 1, i32 %17, i32* %11)
%27 = getelementptr inbounds i8, i8* %23, i64 %20
%28 = getelementptr inbounds i8, i8* %24, i64 %20
%29 = add nuw nsw i32 %22, 1
%30 = icmp eq i32 %29, %16
br i1 %30, label %31, label %21
31: ; preds = %21, %4
%32 = tail call i32 @fclose(i32* %11)
%33 = tail call i32 @free(i8* %6)
%34 = tail call i32 @free(i8* %8)
%35 = tail call i32 @free(i8* %9)
ret void
}