#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ blending(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                            int, int, int /*<<< orphan*/ *, int, int, int, int);
int /*<<< orphan*/ calculate_background_avg(int, int /*<<< orphan*/ *, int, int,
                                            int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *arg[]) {
  FILE *bg, *osd, *dst;
  int i, width = 1920, height = 1080, osd_height = 64;
  int row[11] = {0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
  uint8_t *bg_buf = malloc(width * height * 3 / 2);
  uint8_t *osd_buf = malloc(width * height * 4);
  uint8_t *bg_avg = malloc(width / 64);
  if ((bg = fopen("nv12.raw", "rb")) == NULL) {
    printf("failed to open src\n");
    return -1;
  }
  if ((osd = fopen("font.rgba", "rb")) == NULL) {
    printf("failed to open osd\n");
    return -1;
  }
  if ((dst = fopen("out.nv12", "wb")) == NULL) {
    printf("failed to open dst\n");
    return -1;
  }
  fread(bg_buf, 1, width * height * 3 / 2, bg);
  fread(osd_buf, 1, width * osd_height * 4, osd);
  for (i = 0; i < 11; i++) {
    calculate_background_avg(row[i], bg_buf, width, 64, 64, bg_avg);
    blending(row[i], bg_buf, bg_avg, width, height, osd_buf, width, osd_height,
             64, 64);
  }
  fwrite(bg_buf, 1, width * height * 3 / 2, dst);
  if (bg)
    fclose(bg);
  if (osd)
    fclose(osd);
  if (dst)
    fclose(dst);
  if (bg_buf)
    free(bg_buf);
  if (osd_buf)
    free(osd_buf);
  if (bg_avg)
    free(bg_avg);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x780,-0x30(%rbp)
movl   $0x438,-0x34(%rbp)
movl   $0x40,-0x38(%rbp)
lea    -0x70(%rbp),%rdi
lea    0xe27(%rip),%rsi
mov    $0x2c,%edx
call   1080 <memcpy@plt>
mov    -0x30(%rbp),%eax
imul   -0x34(%rbp),%eax
imul   $0x3,%eax,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x78(%rbp)
mov    -0x30(%rbp),%edi
imul   -0x34(%rbp),%edi
shl    $0x2,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x80(%rbp)
mov    -0x30(%rbp),%eax
mov    $0x40,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x88(%rbp)
lea    0xdf9(%rip),%rdi
lea    0xdfb(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1265 <main+0xc5>
lea    0xde4(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1449 <main+0x2a9>
lea    0xde0(%rip),%rdi
lea    0xdc2(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    129e <main+0xfe>
lea    0xdc9(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1449 <main+0x2a9>
lea    0xdc5(%rip),%rdi
lea    0xdc7(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12d7 <main+0x137>
lea    0xdb0(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1449 <main+0x2a9>
mov    -0x78(%rbp),%rdi
mov    -0x30(%rbp),%eax
imul   -0x34(%rbp),%eax
imul   $0x3,%eax,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x80(%rbp),%rdi
mov    -0x30(%rbp),%edx
imul   -0x38(%rbp),%edx
shl    $0x2,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0xb,-0x2c(%rbp)
jge    139e <main+0x1fe>
movslq -0x2c(%rbp),%rax
mov    -0x70(%rbp,%rax,4),%edi
mov    -0x78(%rbp),%rsi
mov    -0x30(%rbp),%edx
mov    -0x88(%rbp),%r9
mov    $0x40,%r8d
mov    %r8d,%ecx
call   1050 <calculate_background_avg@plt>
movslq -0x2c(%rbp),%rax
mov    -0x70(%rbp,%rax,4),%edi
mov    -0x78(%rbp),%rsi
mov    -0x88(%rbp),%rdx
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%r8d
mov    -0x80(%rbp),%r9
mov    -0x30(%rbp),%r10d
mov    -0x38(%rbp),%eax
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
movl   $0x40,0x10(%rsp)
movl   $0x40,0x18(%rsp)
call   1090 <blending@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1320 <main+0x180>
mov    -0x78(%rbp),%rdi
mov    -0x30(%rbp),%eax
imul   -0x34(%rbp),%eax
imul   $0x3,%eax,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
cmpq   $0x0,-0x18(%rbp)
je     13d8 <main+0x238>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
je     13ec <main+0x24c>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     1400 <main+0x260>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
cmpq   $0x0,-0x78(%rbp)
je     1414 <main+0x274>
mov    -0x78(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x80(%rbp)
je     1428 <main+0x288>
mov    -0x80(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x88(%rbp)
je     1442 <main+0x2a2>
mov    -0x88(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x2f7600,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    $0x7e9000,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    $0x1e,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
lea    0xe60(%rip),%rdi
lea    0xe62(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1326 <main+0x196>
mov    %rax,%rbp
lea    0xe4d(%rip),%rdi
lea    0xe43(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     132f <main+0x19f>
mov    %rax,%r12
lea    0xe38(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1338 <main+0x1a8>
mov    %rax,(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x2f7600,%edx
mov    %rbp,0x10(%rsp)
mov    %rbp,%rcx
call   1050 <fread@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x78000,%edx
mov    %r12,0x8(%rsp)
mov    %r12,%rcx
call   1050 <fread@plt>
xor    %r13d,%r13d
lea    0xd9f(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13,%r12,1),%ebp
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x780,%edx
mov    $0x40,%ecx
mov    $0x40,%r8d
mov    %r15,%r9
call   1060 <calculate_background_avg@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    $0x780,%ecx
mov    $0x438,%r8d
mov    %r14,%r9
push   $0x40
push   $0x40
push   $0x40
push   $0x780
call   1080 <blending@plt>
add    $0x20,%rsp
add    $0x4,%r13
cmp    $0x2c,%r13
jne    1270 <main+0xe0>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x2f7600,%edx
mov    (%rsp),%rbp
mov    %rbp,%rcx
call   10b0 <fwrite@plt>
mov    0x10(%rsp),%rdi
call   1070 <fclose@plt>
mov    0x8(%rsp),%rdi
call   1070 <fclose@plt>
mov    %rbp,%rdi
call   1070 <fclose@plt>
test   %rbx,%rbx
je     1308 <main+0x178>
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r14,%r14
je     1315 <main+0x185>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
test   %r15,%r15
je     1349 <main+0x1b9>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1349 <main+0x1b9>
lea    0xd47(%rip),%rdi
jmp    133f <main+0x1af>
lea    0xd2b(%rip),%rdi
jmp    133f <main+0x1af>
lea    0xd0f(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.row = [11 x i32] [i32 0, i32 100, i32 200, i32 300, i32 400, i32 500, i32 600, i32 700, i32 800, i32 900, i32 1000]
@.str = [9 x i8] c"nv12.raw\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [20 x i8] c"failed to open src\0A\00"
@.str.3 = [10 x i8] c"font.rgba\00"
@.str.4 = [20 x i8] c"failed to open osd\0A\00"
@.str.5 = [9 x i8] c"out.nv12\00"
@.str.6 = [3 x i8] c"wb\00"
@.str.7 = [20 x i8] c"failed to open dst\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [11 x i32]
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1920, i32* %10
store i32 1080, i32* %11
store i32 64, i32* %12
%17 = bitcast [11 x i32]* %13 to i8*
%18 = load i32, i32* %10
%19 = load i32, i32* %11
%20 = mul nsw i32 %18, %19
%21 = mul nsw i32 %20, 3
%22 = sdiv i32 %21, 2
%23 = call i32* @malloc(i32 %22)
store i32* %23, i32** %14
%24 = load i32, i32* %10
%25 = load i32, i32* %11
%26 = mul nsw i32 %24, %25
%27 = mul nsw i32 %26, 4
%28 = call i32* @malloc(i32 %27)
store i32* %28, i32** %15
%29 = load i32, i32* %10
%30 = sdiv i32 %29, 64
%31 = call i32* @malloc(i32 %30)
store i32* %31, i32** %16
%32 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %32, i32** %6
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %36
34: ; preds = %2
%35 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %134
36: ; preds = %2
%37 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %37, i32** %7
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %134
41: ; preds = %36
%42 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %42, i32** %8
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %134
46: ; preds = %41
%47 = load i32*, i32** %14
%48 = load i32, i32* %10
%49 = load i32, i32* %11
%50 = mul nsw i32 %48, %49
%51 = mul nsw i32 %50, 3
%52 = sdiv i32 %51, 2
%53 = load i32*, i32** %6
%54 = call i32 @fread(i32* %47, i32 1, i32 %52, i32* %53)
%55 = load i32*, i32** %15
%56 = load i32, i32* %10
%57 = load i32, i32* %12
%58 = mul nsw i32 %56, %57
%59 = mul nsw i32 %58, 4
%60 = load i32*, i32** %7
%61 = call i32 @fread(i32* %55, i32 1, i32 %59, i32* %60)
store i32 0, i32* %9
br label %62
62: ; preds = %86, %46
%63 = load i32, i32* %9
%64 = icmp slt i32 %63, 11
br i1 %64, label %65, label %89
65: ; preds = %62
%66 = load i32, i32* %9
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [11 x i32], [11 x i32]* %13, i64 0, i64 %67
%69 = load i32, i32* %68
%70 = load i32*, i32** %14
%71 = load i32, i32* %10
%72 = load i32*, i32** %16
%73 = call i32 @calculate_background_avg(i32 %69, i32* %70, i32 %71, i32 64, i32 64, i32* %72)
%74 = load i32, i32* %9
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds [11 x i32], [11 x i32]* %13, i64 0, i64 %75
%77 = load i32, i32* %76
%78 = load i32*, i32** %14
%79 = load i32*, i32** %16
%80 = load i32, i32* %10
%81 = load i32, i32* %11
%82 = load i32*, i32** %15
%83 = load i32, i32* %10
%84 = load i32, i32* %12
%85 = call i32 @blending(i32 %77, i32* %78, i32* %79, i32 %80, i32 %81, i32* %82, i32 %83, i32 %84, i32 64, i32 64)
br label %86
86: ; preds = %65
%87 = load i32, i32* %9
%88 = add nsw i32 %87, 1
store i32 %88, i32* %9
br label %62
89: ; preds = %62
%90 = load i32*, i32** %14
%91 = load i32, i32* %10
%92 = load i32, i32* %11
%93 = mul nsw i32 %91, %92
%94 = mul nsw i32 %93, 3
%95 = sdiv i32 %94, 2
%96 = load i32*, i32** %8
%97 = call i32 @fwrite(i32* %90, i32 1, i32 %95, i32* %96)
%98 = load i32*, i32** %6
%99 = icmp ne i32* %98, null
br i1 %99, label %100, label %103
100: ; preds = %89
%101 = load i32*, i32** %6
%102 = call i32 @fclose(i32* %101)
br label %103
103: ; preds = %100, %89
%104 = load i32*, i32** %7
%105 = icmp ne i32* %104, null
br i1 %105, label %106, label %109
106: ; preds = %103
%107 = load i32*, i32** %7
%108 = call i32 @fclose(i32* %107)
br label %109
109: ; preds = %106, %103
%110 = load i32*, i32** %8
%111 = icmp ne i32* %110, null
br i1 %111, label %112, label %115
112: ; preds = %109
%113 = load i32*, i32** %8
%114 = call i32 @fclose(i32* %113)
br label %115
115: ; preds = %112, %109
%116 = load i32*, i32** %14
%117 = icmp ne i32* %116, null
br i1 %117, label %118, label %121
118: ; preds = %115
%119 = load i32*, i32** %14
%120 = call i32 @free(i32* %119)
br label %121
121: ; preds = %118, %115
%122 = load i32*, i32** %15
%123 = icmp ne i32* %122, null
br i1 %123, label %124, label %127
124: ; preds = %121
%125 = load i32*, i32** %15
%126 = call i32 @free(i32* %125)
br label %127
127: ; preds = %124, %121
%128 = load i32*, i32** %16
%129 = icmp ne i32* %128, null
br i1 %129, label %130, label %133
130: ; preds = %127
%131 = load i32*, i32** %16
%132 = call i32 @free(i32* %131)
br label %133
133: ; preds = %130, %127
store i32 0, i32* %3
br label %134
134: ; preds = %133, %44, %39, %34
%135 = load i32, i32* %3
ret i32 %135
}

LLVM IR (-O2):
@__const.main.row = [11 x i32] [i32 0, i32 100, i32 200, i32 300, i32 400, i32 500, i32 600, i32 700, i32 800, i32 900, i32 1000]
@.str = [9 x i8] c"nv12.raw\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.3 = [10 x i8] c"font.rgba\00"
@.str.5 = [9 x i8] c"out.nv12\00"
@.str.6 = [3 x i8] c"wb\00"
@str = [19 x i8] c"failed to open dst\00"
@str.8 = [19 x i8] c"failed to open osd\00"
@str.9 = [19 x i8] c"failed to open src\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @malloc(i32 3110400)
%4 = tail call i32* @malloc(i32 8294400)
%5 = tail call i32* @malloc(i32 30)
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0))
br label %47
10: ; preds = %2
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.8, i64 0, i64 0))
br label %47
15: ; preds = %10
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %47
20: ; preds = %15
%21 = tail call i32 @fread(i32* %3, i32 1, i32 3110400, i32* %6)
%22 = tail call i32 @fread(i32* %4, i32 1, i32 491520, i32* %11)
br label %23
23: ; preds = %20, %23
%24 = phi i64 [ 0, %20 ], [ %29, %23 ]
%25 = getelementptr inbounds [11 x i32], [11 x i32]* @__const.main.row, i64 0, i64 %24
%26 = load i32, i32* %25
%27 = tail call i32 @calculate_background_avg(i32 %26, i32* %3, i32 1920, i32 64, i32 64, i32* %5)
%28 = tail call i32 @blending(i32 %26, i32* %3, i32* %5, i32 1920, i32 1080, i32* %4, i32 1920, i32 64, i32 64, i32 64)
%29 = add nuw nsw i64 %24, 1
%30 = icmp eq i64 %29, 11
br i1 %30, label %31, label %23
31: ; preds = %23
%32 = tail call i32 @fwrite(i32* %3, i32 1, i32 3110400, i32* %16)
%33 = tail call i32 @fclose(i32* %6)
%34 = tail call i32 @fclose(i32* %11)
%35 = tail call i32 @fclose(i32* %16)
%36 = icmp eq i32* %3, null
br i1 %36, label %39, label %37
37: ; preds = %31
%38 = tail call i32 @free(i32* %3)
br label %39
39: ; preds = %37, %31
%40 = icmp eq i32* %4, null
br i1 %40, label %43, label %41
41: ; preds = %39
%42 = tail call i32 @free(i32* %4)
br label %43
43: ; preds = %41, %39
%44 = icmp eq i32* %5, null
br i1 %44, label %47, label %45
45: ; preds = %43
%46 = tail call i32 @free(i32* %5)
br label %47
47: ; preds = %43, %45, %18, %13, %8
%48 = phi i32 [ -1, %8 ], [ -1, %13 ], [ -1, %18 ], [ 0, %45 ], [ 0, %43 ]
ret i32 %48
}