#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 const *, int,
                                    int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdout;
char *strerror(int /*<<< orphan*/);
void pgmyuv_save(const char *filename, int w, int h,
                 const 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);
  if (filename) {
    f = fopen(filename, "wb");
    fprintf(f, "P5\n%d %d\n%d\n", w, h * 3 / 2, 255);
  } else {
    f = stdout;
  }
  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;
  if (filename) {
    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);
  } else {
    for (i = 0; i < h2; i++) {
      do {
        if (fwrite(cb, 1, w2, f) != w2) {
          fprintf(stderr, "%s\n", strerror(errno));
          exit(1);
        }
      } while (0);
      cb += w2;
    }
    for (i = 0; i < h2; i++) {
      do {
        if (fwrite(cr, 1, w2, f) != w2) {
          fprintf(stderr, "%s\n", strerror(errno));
          exit(1);
        }
      } while (0);
      cr += w2;
    }
  }
  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>
cmpq   $0x0,-0x8(%rbp)
je     125c <pgmyuv_save+0xcc>
mov    -0x8(%rbp),%rdi
lea    0xde3(%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    0xdb9(%rip),%rsi
mov    $0xff,%r8d
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    126a <pgmyuv_save+0xda>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    126f <pgmyuv_save+0xdf>
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     12cf <pgmyuv_save+0x13f>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
lea    0xd52(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    12d4 <pgmyuv_save+0x144>
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)
cmpq   $0x0,-0x8(%rbp)
je     141c <pgmyuv_save+0x28c>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    140e <pgmyuv_save+0x27e>
jmp    1323 <pgmyuv_save+0x193>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
cmp    -0x2c(%rbp),%eax
je     1379 <pgmyuv_save+0x1e9>
mov    0x2c98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
lea    0xca8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    137e <pgmyuv_save+0x1ee>
jmp    1383 <pgmyuv_save+0x1f3>
mov    -0x40(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
cmp    -0x2c(%rbp),%eax
je     13d9 <pgmyuv_save+0x249>
mov    0x2c38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2c24(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xc48(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    13de <pgmyuv_save+0x24e>
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    1312 <pgmyuv_save+0x182>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1545 <pgmyuv_save+0x3b5>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    14ae <pgmyuv_save+0x31e>
jmp    1434 <pgmyuv_save+0x2a4>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
cmp    -0x2c(%rbp),%eax
je     148a <pgmyuv_save+0x2fa>
mov    0x2b87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    0x2b73(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    %rax,%rdx
lea    0xb97(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    148f <pgmyuv_save+0x2ff>
mov    -0x2c(%rbp),%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1423 <pgmyuv_save+0x293>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1540 <pgmyuv_save+0x3b0>
jmp    14c6 <pgmyuv_save+0x336>
mov    -0x40(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
cmp    -0x2c(%rbp),%eax
je     151c <pgmyuv_save+0x38c>
mov    0x2af5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    0x2ae1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x70(%rbp),%edi
mov    %rax,%rdx
lea    0xb05(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1521 <pgmyuv_save+0x391>
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    14b5 <pgmyuv_save+0x325>
jmp    1545 <pgmyuv_save+0x3b5>
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,0x10(%rsp)
mov    %edx,%r12d
mov    %esi,%r15d
mov    %rdi,0x8(%rsp)
mov    %edx,%r14d
imul   %esi,%r14d
mov    %r14d,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
lea    0x3(%r14),%ebx
test   %r14d,%r14d
cmovns %r14d,%ebx
sar    $0x2,%ebx
mov    %ebx,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %ebx,%edi
mov    %r12,%rbx
call   1060 <malloc@plt>
mov    %r13,%rdi
mov    %rbp,0x20(%rsp)
mov    %rbp,%rsi
mov    0x8(%rsp),%rbp
mov    %rax,0x18(%rsp)
mov    %rax,%rdx
mov    0x10(%rsp),%rcx
mov    %r15d,%r8d
mov    %ebx,%r9d
call   1070 <rgb24_to_yuv420p@plt>
test   %rbp,%rbp
je     1249 <pgmyuv_save+0xb9>
lea    0xdea(%rip),%rsi
mov    %rbp,%rdi
call   1080 <fopen@plt>
mov    %rax,%r12
lea    (%rbx,%rbx,2),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
lea    0xdcf(%rip),%rsi
mov    %r12d,%edi
mov    %r15d,%edx
mov    $0xff,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1253 <pgmyuv_save+0xc3>
mov    0x2d80(%rip),%rax
mov    (%rax),%r12
mov    %r13,0x10(%rsp)
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
cmp    %r14d,%eax
jne    139d <pgmyuv_save+0x20d>
mov    %ebx,%r13d
shr    $0x1f,%r13d
add    %ebx,%r13d
sar    %r13d
mov    %r15d,%r14d
shr    $0x1f,%r14d
add    %r15d,%r14d
sar    %r14d
test   %rbp,%rbp
je     12fe <pgmyuv_save+0x16e>
cmp    $0x2,%ebx
jl     12f4 <pgmyuv_save+0x164>
movslq %r14d,%r15
mov    0x20(%rsp),%rbx
mov    0x18(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
cmp    %r14d,%eax
jne    139d <pgmyuv_save+0x20d>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
cmp    %r14d,%eax
jne    139d <pgmyuv_save+0x20d>
add    %r15,%rbx
add    %r15,%rbp
add    $0xffffffff,%r13d
jne    12b0 <pgmyuv_save+0x120>
mov    %r12,%rdi
call   1040 <fclose@plt>
jmp    1371 <pgmyuv_save+0x1e1>
cmp    $0x2,%ebx
jl     1371 <pgmyuv_save+0x1e1>
mov    %rbx,0x8(%rsp)
movslq %r14d,%r15
mov    %r13d,%ebp
mov    0x20(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
cmp    %r14d,%eax
jne    139d <pgmyuv_save+0x20d>
add    %r15,%rbx
add    $0xffffffff,%ebp
jne    1320 <pgmyuv_save+0x190>
cmpl   $0x2,0x8(%rsp)
jl     1371 <pgmyuv_save+0x1e1>
mov    0x18(%rsp),%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
cmp    %r14d,%eax
jne    139d <pgmyuv_save+0x20d>
add    %r15,%rbx
add    $0xffffffff,%r13d
jne    1350 <pgmyuv_save+0x1c0>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x20(%rsp),%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    0x2c3c(%rip),%rax
mov    (%rax),%ebx
mov    0x2c2b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xc55(%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"
@stdout = global i32* null
@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 = icmp ne i8* %39, null
br i1 %40, label %41, label %51
41: ; preds = %4
%42 = load i8*, i8** %5
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %43, i32** %9
%44 = load i32*, i32** %9
%45 = ptrtoint i32* %44 to i32
%46 = load i32, i32* %6
%47 = load i32, i32* %7
%48 = mul nsw i32 %47, 3
%49 = sdiv i32 %48, 2
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %46, i32 %49, i32 255)
br label %53
51: ; preds = %4
%52 = load i32*, i32** @stdout
store i32* %52, i32** %9
br label %53
53: ; preds = %51, %41
br label %54
54: ; preds = %53
%55 = load i8*, i8** %15
%56 = load i32, i32* %6
%57 = load i32, i32* %7
%58 = mul nsw i32 %56, %57
%59 = load i32*, i32** %9
%60 = call i32 @fwrite(i8* %55, i32 1, i32 %58, i32* %59)
%61 = load i32, i32* %6
%62 = load i32, i32* %7
%63 = mul nsw i32 %61, %62
%64 = icmp ne i32 %60, %63
br i1 %64, label %65, label %71
65: ; preds = %54
%66 = load i32, i32* @stderr
%67 = load i32, i32* @errno
%68 = call i8* @strerror(i32 %67)
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %68)
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %54
br label %72
72: ; preds = %71
%73 = load i32, i32* %7
%74 = sdiv i32 %73, 2
store i32 %74, i32* %11
%75 = load i32, i32* %6
%76 = sdiv i32 %75, 2
store i32 %76, i32* %12
%77 = load i8*, i8** %16
store i8* %77, i8** %13
%78 = load i8*, i8** %17
store i8* %78, i8** %14
%79 = load i8*, i8** %5
%80 = icmp ne i8* %79, null
br i1 %80, label %81, label %131
81: ; preds = %72
store i32 0, i32* %10
br label %82
82: ; preds = %125, %81
%83 = load i32, i32* %10
%84 = load i32, i32* %11
%85 = icmp slt i32 %83, %84
br i1 %85, label %86, label %128
86: ; preds = %82
br label %87
87: ; preds = %86
%88 = load i8*, i8** %13
%89 = load i32, i32* %12
%90 = load i32*, i32** %9
%91 = call i32 @fwrite(i8* %88, i32 1, i32 %89, i32* %90)
%92 = load i32, i32* %12
%93 = icmp ne i32 %91, %92
br i1 %93, label %94, label %100
94: ; preds = %87
%95 = load i32, i32* @stderr
%96 = load i32, i32* @errno
%97 = call i8* @strerror(i32 %96)
%98 = call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %97)
%99 = call i32 @exit(i32 1)
unreachable
100: ; preds = %87
br label %101
101: ; preds = %100
br label %102
102: ; preds = %101
%103 = load i8*, i8** %14
%104 = load i32, i32* %12
%105 = load i32*, i32** %9
%106 = call i32 @fwrite(i8* %103, i32 1, i32 %104, i32* %105)
%107 = load i32, i32* %12
%108 = icmp ne i32 %106, %107
br i1 %108, label %109, label %115
109: ; preds = %102
%110 = load i32, i32* @stderr
%111 = load i32, i32* @errno
%112 = call i8* @strerror(i32 %111)
%113 = call i32 (i32, i8*, ...) @fprintf(i32 %110, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %112)
%114 = call i32 @exit(i32 1)
unreachable
115: ; preds = %102
br label %116
116: ; preds = %115
%117 = load i32, i32* %12
%118 = load i8*, i8** %13
%119 = sext i32 %117 to i64
%120 = getelementptr inbounds i8, i8* %118, i64 %119
store i8* %120, i8** %13
%121 = load i32, i32* %12
%122 = load i8*, i8** %14
%123 = sext i32 %121 to i64
%124 = getelementptr inbounds i8, i8* %122, i64 %123
store i8* %124, i8** %14
br label %125
125: ; preds = %116
%126 = load i32, i32* %10
%127 = add nsw i32 %126, 1
store i32 %127, i32* %10
br label %82
128: ; preds = %82
%129 = load i32*, i32** %9
%130 = call i32 @fclose(i32* %129)
br label %188
131: ; preds = %72
store i32 0, i32* %10
br label %132
132: ; preds = %156, %131
%133 = load i32, i32* %10
%134 = load i32, i32* %11
%135 = icmp slt i32 %133, %134
br i1 %135, label %136, label %159
136: ; preds = %132
br label %137
137: ; preds = %136
%138 = load i8*, i8** %13
%139 = load i32, i32* %12
%140 = load i32*, i32** %9
%141 = call i32 @fwrite(i8* %138, i32 1, i32 %139, i32* %140)
%142 = load i32, i32* %12
%143 = icmp ne i32 %141, %142
br i1 %143, label %144, label %150
144: ; preds = %137
%145 = load i32, i32* @stderr
%146 = load i32, i32* @errno
%147 = call i8* @strerror(i32 %146)
%148 = call i32 (i32, i8*, ...) @fprintf(i32 %145, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %147)
%149 = call i32 @exit(i32 1)
unreachable
150: ; preds = %137
br label %151
151: ; preds = %150
%152 = load i32, i32* %12
%153 = load i8*, i8** %13
%154 = sext i32 %152 to i64
%155 = getelementptr inbounds i8, i8* %153, i64 %154
store i8* %155, i8** %13
br label %156
156: ; preds = %151
%157 = load i32, i32* %10
%158 = add nsw i32 %157, 1
store i32 %158, i32* %10
br label %132
159: ; preds = %132
store i32 0, i32* %10
br label %160
160: ; preds = %184, %159
%161 = load i32, i32* %10
%162 = load i32, i32* %11
%163 = icmp slt i32 %161, %162
br i1 %163, label %164, label %187
164: ; preds = %160
br label %165
165: ; preds = %164
%166 = load i8*, i8** %14
%167 = load i32, i32* %12
%168 = load i32*, i32** %9
%169 = call i32 @fwrite(i8* %166, i32 1, i32 %167, i32* %168)
%170 = load i32, i32* %12
%171 = icmp ne i32 %169, %170
br i1 %171, label %172, label %178
172: ; preds = %165
%173 = load i32, i32* @stderr
%174 = load i32, i32* @errno
%175 = call i8* @strerror(i32 %174)
%176 = call i32 (i32, i8*, ...) @fprintf(i32 %173, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %175)
%177 = call i32 @exit(i32 1)
unreachable
178: ; preds = %165
br label %179
179: ; preds = %178
%180 = load i32, i32* %12
%181 = load i8*, i8** %14
%182 = sext i32 %180 to i64
%183 = getelementptr inbounds i8, i8* %181, i64 %182
store i8* %183, i8** %14
br label %184
184: ; preds = %179
%185 = load i32, i32* %10
%186 = add nsw i32 %185, 1
store i32 %186, i32* %10
br label %160
187: ; preds = %160
br label %188
188: ; preds = %187, %128
%189 = load i8*, i8** %15
%190 = call i32 @free(i8* %189)
%191 = load i8*, i8** %16
%192 = call i32 @free(i8* %191)
%193 = load i8*, i8** %17
%194 = call i32 @free(i8* %193)
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"
@stdout = global i32* null
@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 = 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 = icmp eq i8* %0, null
br i1 %11, label %19, label %12
12: ; preds = %4
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = ptrtoint i32* %13 to i64
%15 = trunc i64 %14 to i32
%16 = mul nsw i32 %2, 3
%17 = sdiv i32 %16, 2
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %17, i32 255)
br label %21
19: ; preds = %4
%20 = load i32*, i32** @stdout
br label %21
21: ; preds = %12, %19
%22 = phi i32* [ %13, %12 ], [ %20, %19 ]
%23 = tail call i32 @fwrite(i8* %6, i32 1, i32 %5, i32* %22)
%24 = icmp eq i32 %23, %5
br i1 %24, label %31, label %25
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %21
%32 = sdiv i32 %2, 2
%33 = sdiv i32 %1, 2
%34 = icmp sgt i32 %2, 1
br i1 %11, label %38, label %35
35: ; preds = %31
br i1 %34, label %36, label %67
36: ; preds = %35
%37 = sext i32 %33 to i64
br label %41
38: ; preds = %31
br i1 %34, label %39, label %102
39: ; preds = %38
%40 = sext i32 %33 to i64
br label %72
41: ; preds = %36, %62
%42 = phi i32 [ 0, %36 ], [ %65, %62 ]
%43 = phi i8* [ %8, %36 ], [ %63, %62 ]
%44 = phi i8* [ %9, %36 ], [ %64, %62 ]
%45 = tail call i32 @fwrite(i8* %43, i32 1, i32 %33, i32* %22)
%46 = icmp eq i32 %45, %33
br i1 %46, label %53, label %47
47: ; preds = %41
%48 = load i32, i32* @stderr
%49 = load i32, i32* @errno
%50 = tail call i8* @strerror(i32 %49)
%51 = tail call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = tail call i32 @exit(i32 1)
unreachable
53: ; preds = %41
%54 = tail call i32 @fwrite(i8* %44, i32 1, i32 %33, i32* %22)
%55 = icmp eq i32 %54, %33
br i1 %55, label %62, label %56
56: ; preds = %53
%57 = load i32, i32* @stderr
%58 = load i32, i32* @errno
%59 = tail call i8* @strerror(i32 %58)
%60 = tail call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %59)
%61 = tail call i32 @exit(i32 1)
unreachable
62: ; preds = %53
%63 = getelementptr inbounds i8, i8* %43, i64 %37
%64 = getelementptr inbounds i8, i8* %44, i64 %37
%65 = add nuw nsw i32 %42, 1
%66 = icmp eq i32 %65, %32
br i1 %66, label %67, label %41
67: ; preds = %62, %35
%68 = tail call i32 @fclose(i32* %22)
br label %102
69: ; preds = %83
br i1 %34, label %70, label %102
70: ; preds = %69
%71 = sext i32 %33 to i64
br label %87
72: ; preds = %39, %83
%73 = phi i32 [ 0, %39 ], [ %85, %83 ]
%74 = phi i8* [ %8, %39 ], [ %84, %83 ]
%75 = tail call i32 @fwrite(i8* %74, i32 1, i32 %33, i32* %22)
%76 = icmp eq i32 %75, %33
br i1 %76, label %83, label %77
77: ; preds = %72
%78 = load i32, i32* @stderr
%79 = load i32, i32* @errno
%80 = tail call i8* @strerror(i32 %79)
%81 = tail call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %80)
%82 = tail call i32 @exit(i32 1)
unreachable
83: ; preds = %72
%84 = getelementptr inbounds i8, i8* %74, i64 %40
%85 = add nuw nsw i32 %73, 1
%86 = icmp eq i32 %85, %32
br i1 %86, label %69, label %72
87: ; preds = %70, %98
%88 = phi i32 [ 0, %70 ], [ %100, %98 ]
%89 = phi i8* [ %9, %70 ], [ %99, %98 ]
%90 = tail call i32 @fwrite(i8* %89, i32 1, i32 %33, i32* %22)
%91 = icmp eq i32 %90, %33
br i1 %91, label %98, label %92
92: ; preds = %87
%93 = load i32, i32* @stderr
%94 = load i32, i32* @errno
%95 = tail call i8* @strerror(i32 %94)
%96 = tail call i32 (i32, i8*, ...) @fprintf(i32 %93, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %95)
%97 = tail call i32 @exit(i32 1)
unreachable
98: ; preds = %87
%99 = getelementptr inbounds i8, i8* %89, i64 %71
%100 = add nuw nsw i32 %88, 1
%101 = icmp eq i32 %100, %32
br i1 %101, label %102, label %87
102: ; preds = %98, %38, %69, %67
%103 = tail call i32 @free(i8* %6)
%104 = tail call i32 @free(i8* %8)
%105 = tail call i32 @free(i8* %9)
ret void
}