#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*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(unsigned char *);
int 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);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
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);
  do {
    if (fwrite(lum_tab, 1, w * h, f) != w * h) {
      fprintf(stderr, "%s\n", strerror(errno));
      exit(1);
    }
  } while (0);
  h2 = h / 2;
  w2 = w / 2;
  cb = cb_tab;
  cr = cr_tab;
  for (i = 0; i < h2; i++) {
    do {
      if (fwrite(cb, 1, w2, f) != w2) {
        fprintf(stderr, "%s\n", strerror(errno));
        exit(1);
      }
    } while (0);
    do {
      if (fwrite(cr, 1, w2, f) != w2) {
        fprintf(stderr, "%s\n", strerror(errno));
        exit(1);
      }
    } while (0);
    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    $0x70,%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    0xdee(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,%edi
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    0xdc4(%rip),%rsi
mov    $0xff,%r8d
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0xc(%rbp),%edx
imul   -0x10(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
mov    -0xc(%rbp),%ecx
imul   -0x10(%rbp),%ecx
cmp    %ecx,%eax
je     12ac <pgmyuv_save+0x11c>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
lea    0xd75(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    12b1 <pgmyuv_save+0x121>
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    13e0 <pgmyuv_save+0x250>
jmp    12f5 <pgmyuv_save+0x165>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
cmp    -0x2c(%rbp),%eax
je     134b <pgmyuv_save+0x1bb>
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
lea    0xcd6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1350 <pgmyuv_save+0x1c0>
jmp    1355 <pgmyuv_save+0x1c5>
mov    -0x40(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
cmp    -0x2c(%rbp),%eax
je     13ab <pgmyuv_save+0x21b>
mov    0x2c66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2c52(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xc76(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    13b0 <pgmyuv_save+0x220>
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    12e4 <pgmyuv_save+0x154>
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    $0x70,%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    0xdf8(%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    0xdda(%rip),%rsi
mov    %r15d,%edi
mov    %r13d,%edx
mov    $0xff,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    %ebp,%eax
jne    1300 <pgmyuv_save+0x170>
cmp    $0x2,%r14d
jl     12cc <pgmyuv_save+0x13c>
mov    %r14d,%r12d
shr    $0x1f,%r12d
add    %r14d,%r12d
sar    %r12d
mov    %r13d,%r14d
shr    $0x1f,%r14d
add    %r13d,%r14d
sar    %r14d
movslq %r14d,%r13
mov    0x18(%rsp),%rbx
mov    0x10(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    %r14d,%eax
jne    1300 <pgmyuv_save+0x170>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    %r14d,%eax
jne    1300 <pgmyuv_save+0x170>
add    %r13,%rbx
add    %r13,%rbp
add    $0xffffffff,%r12d
jne    1290 <pgmyuv_save+0x100>
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>
mov    0x2cd9(%rip),%rax
mov    (%rax),%ebx
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xcf2(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@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"
@stderr = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
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 = ptrtoint i32* %41 to i32
%43 = load i32, i32* %6
%44 = load i32, i32* %7
%45 = mul nsw i32 %44, 3
%46 = sdiv i32 %45, 2
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %43, i32 %46, i32 255)
br label %48
48: ; preds = %4
%49 = load i8*, i8** %15
%50 = load i32, i32* %6
%51 = load i32, i32* %7
%52 = mul nsw i32 %50, %51
%53 = load i32*, i32** %9
%54 = call i32 @fwrite(i8* %49, i32 1, i32 %52, i32* %53)
%55 = load i32, i32* %6
%56 = load i32, i32* %7
%57 = mul nsw i32 %55, %56
%58 = icmp ne i32 %54, %57
br i1 %58, label %59, label %65
59: ; preds = %48
%60 = load i32, i32* @stderr
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %62)
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %48
br label %66
66: ; preds = %65
%67 = load i32, i32* %7
%68 = sdiv i32 %67, 2
store i32 %68, i32* %11
%69 = load i32, i32* %6
%70 = sdiv i32 %69, 2
store i32 %70, i32* %12
%71 = load i8*, i8** %16
store i8* %71, i8** %13
%72 = load i8*, i8** %17
store i8* %72, i8** %14
store i32 0, i32* %10
br label %73
73: ; preds = %116, %66
%74 = load i32, i32* %10
%75 = load i32, i32* %11
%76 = icmp slt i32 %74, %75
br i1 %76, label %77, label %119
77: ; preds = %73
br label %78
78: ; preds = %77
%79 = load i8*, i8** %13
%80 = load i32, i32* %12
%81 = load i32*, i32** %9
%82 = call i32 @fwrite(i8* %79, i32 1, i32 %80, i32* %81)
%83 = load i32, i32* %12
%84 = icmp ne i32 %82, %83
br i1 %84, label %85, label %91
85: ; preds = %78
%86 = load i32, i32* @stderr
%87 = load i32, i32* @errno
%88 = call i8* @strerror(i32 %87)
%89 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %88)
%90 = call i32 @exit(i32 1)
unreachable
91: ; preds = %78
br label %92
92: ; preds = %91
br label %93
93: ; preds = %92
%94 = load i8*, i8** %14
%95 = load i32, i32* %12
%96 = load i32*, i32** %9
%97 = call i32 @fwrite(i8* %94, i32 1, i32 %95, i32* %96)
%98 = load i32, i32* %12
%99 = icmp ne i32 %97, %98
br i1 %99, label %100, label %106
100: ; preds = %93
%101 = load i32, i32* @stderr
%102 = load i32, i32* @errno
%103 = call i8* @strerror(i32 %102)
%104 = call i32 (i32, i8*, ...) @fprintf(i32 %101, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %103)
%105 = call i32 @exit(i32 1)
unreachable
106: ; preds = %93
br label %107
107: ; preds = %106
%108 = load i32, i32* %12
%109 = load i8*, i8** %13
%110 = sext i32 %108 to i64
%111 = getelementptr inbounds i8, i8* %109, i64 %110
store i8* %111, i8** %13
%112 = load i32, i32* %12
%113 = load i8*, i8** %14
%114 = sext i32 %112 to i64
%115 = getelementptr inbounds i8, i8* %113, i64 %114
store i8* %115, i8** %14
br label %116
116: ; preds = %107
%117 = load i32, i32* %10
%118 = add nsw i32 %117, 1
store i32 %118, i32* %10
br label %73
119: ; preds = %73
%120 = load i32*, i32** %9
%121 = call i32 @fclose(i32* %120)
%122 = load i8*, i8** %15
%123 = call i32 @free(i8* %122)
%124 = load i8*, i8** %16
%125 = call i32 @free(i8* %124)
%126 = load i8*, i8** %17
%127 = call i32 @free(i8* %126)
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"
@stderr = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
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 = ptrtoint i32* %11 to i64
%13 = trunc i64 %12 to i32
%14 = mul nsw i32 %2, 3
%15 = sdiv i32 %14, 2
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %15, i32 255)
%17 = tail call i32 @fwrite(i8* %6, i32 1, i32 %5, i32* %11)
%18 = icmp eq i32 %17, %5
br i1 %18, label %25, label %19
19: ; preds = %4
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %4
%26 = sdiv i32 %2, 2
%27 = sdiv i32 %1, 2
%28 = icmp sgt i32 %2, 1
br i1 %28, label %29, label %57
29: ; preds = %25
%30 = sext i32 %27 to i64
br label %31
31: ; preds = %29, %52
%32 = phi i32 [ 0, %29 ], [ %55, %52 ]
%33 = phi i8* [ %8, %29 ], [ %53, %52 ]
%34 = phi i8* [ %9, %29 ], [ %54, %52 ]
%35 = tail call i32 @fwrite(i8* %33, i32 1, i32 %27, i32* %11)
%36 = icmp eq i32 %35, %27
br i1 %36, label %43, label %37
37: ; preds = %31
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = tail call i8* @strerror(i32 %39)
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %31
%44 = tail call i32 @fwrite(i8* %34, i32 1, i32 %27, i32* %11)
%45 = icmp eq i32 %44, %27
br i1 %45, label %52, label %46
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = tail call i8* @strerror(i32 %48)
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %49)
%51 = tail call i32 @exit(i32 1)
unreachable
52: ; preds = %43
%53 = getelementptr inbounds i8, i8* %33, i64 %30
%54 = getelementptr inbounds i8, i8* %34, i64 %30
%55 = add nuw nsw i32 %32, 1
%56 = icmp eq i32 %55, %26
br i1 %56, label %57, label %31
57: ; preds = %52, %25
%58 = tail call i32 @fclose(i32* %11)
%59 = tail call i32 @free(i8* %6)
%60 = tail call i32 @free(i8* %8)
%61 = tail call i32 @free(i8* %9)
ret void
}