#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 conv_frame(void *, void *, int, int);
size_t fread(void *, int, int, int /*<<< orphan*/ *);
size_t fwrite(void *, int, int, int /*<<< orphan*/ *);
void *malloc(int);
int /*<<< orphan*/ printf(char *);
int conv_file(FILE *input, FILE *output, int width, int height) {
  void *inbuf;
  void *outbuf;
  int input_framesize = (width * height) + ((width * height) / 4);
  inbuf = malloc(input_framesize);
  if (inbuf == NULL) {
    printf("err malloc 1\n");
    return -1;
  }
  int output_framesize = ((width * height) * 2) + (((width * height) / 4) * 2);
  outbuf = malloc(output_framesize);
  if (outbuf == NULL) {
    printf("err malloc 2\n");
    return -1;
  }
  for (;;) {
    size_t readsize = fread(inbuf, input_framesize, 1, input);
    if (readsize != readsize) {
      printf("fread err\n");
      return -1;
    } else {
    }
    int ret = conv_frame(inbuf, outbuf, height, width);
    if (ret < 0) {
      printf("frame ng\n");
    }
    size_t writesize = fwrite(outbuf, output_framesize, 1, output);
    if (writesize != output_framesize) {
      printf("fwrite err\n");
      return -1;
    } else {
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<conv_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b8 <conv_file+0x68>
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d1 <conv_file+0x181>
mov    -0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x58(%rbp)
mov    -0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x58(%rbp),%eax
shl    $0x1,%ecx
add    %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1210 <conv_file+0xc0>
lea    0xe0f(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d1 <conv_file+0x181>
jmp    1215 <conv_file+0xc5>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x40(%rbp),%rax
je     1254 <conv_file+0x104>
lea    0xdd9(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d1 <conv_file+0x181>
jmp    1259 <conv_file+0x109>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   1030 <conv_frame@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1285 <conv_file+0x135>
lea    0xda7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
movslq -0x38(%rbp),%rcx
cmp    %rcx,%rax
je     12c7 <conv_file+0x177>
lea    0xd7b(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d1 <conv_file+0x181>
jmp    12cc <conv_file+0x17c>
jmp    1215 <conv_file+0xc5>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conv_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r13d
mov    %rsi,0x10(%rsp)
mov    %rdi,0x8(%rsp)
mov    %ecx,0x4(%rsp)
mov    %ecx,%eax
imul   %edx,%eax
lea    0x3(%rax),%ebx
test   %eax,%eax
cmovns %eax,%ebx
sar    $0x2,%ebx
add    %eax,%ebx
mov    %ebx,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     120b <conv_file+0xbb>
mov    %rax,%rbp
lea    (%rbx,%rbx,1),%r14d
mov    %r14d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1214 <conv_file+0xc4>
mov    %rax,%r15
movslq %r14d,%r12
jmp    11ca <conv_file+0x7a>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    0x10(%rsp),%rcx
call   1070 <fwrite@plt>
cmp    %r12,%rax
jne    1202 <conv_file+0xb2>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    0x8(%rsp),%rcx
call   1050 <fread@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    0x4(%rsp),%edx
mov    %r13d,%ecx
call   1030 <conv_frame@plt>
test   %eax,%eax
jns    11b0 <conv_file+0x60>
lea    0xe10(%rip),%rdi
call   1040 <puts@plt>
jmp    11b0 <conv_file+0x60>
lea    0xdf7(%rip),%rdi
jmp    121b <conv_file+0xcb>
lea    0xe0f(%rip),%rdi
jmp    121b <conv_file+0xcb>
lea    0xdf9(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@str = [11 x i8] c"fwrite err\00"
@str.5 = [9 x i8] c"frame ng\00"
@str.6 = [13 x i8] c"err malloc 2\00"
@str.7 = [13 x i8] c"err malloc 1\00"
define i32 @conv_file(i32* nocapture %0, i32* nocapture %1, i32 %2, i32 %3) {
%5 = mul nsw i32 %3, %2
%6 = sdiv i32 %5, 4
%7 = add nsw i32 %6, %5
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %25, label %10
10: ; preds = %4
%11 = shl i32 %7, 1
%12 = tail call i8* @malloc(i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %25, label %14
14: ; preds = %10
%15 = sext i32 %11 to i64
br label %16
16: ; preds = %14, %22
%17 = tail call i64 @fread(i8* %8, i32 %7, i32 1, i32* %0)
%18 = tail call i32 @conv_frame(i8* %8, i8* %12, i32 %3, i32 %2)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.5, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = tail call i64 @fwrite(i8* %12, i32 %11, i32 1, i32* %1)
%24 = icmp eq i64 %23, %15
br i1 %24, label %16, label %25
25: ; preds = %22, %10, %4
%26 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @str.7, i64 0, i64 0), %4 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @str.6, i64 0, i64 0), %10 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0), %22 ]
%27 = tail call i32 @puts(i8* dereferenceable(1) %26)
ret i32 -1
}