#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*/ *);
int inputFrameSize(int, int);
void *malloc(int);
int outputFrameSize(int, int);
int /*<<< orphan*/ printf(char *, ...);
int conv_file(FILE *input, FILE *output, int width, int height) {
  void *inbuf;
  void *outbuf;
  int i;
  int input_framesize = inputFrameSize(width, height);
  inbuf = malloc(input_framesize);
  printf("i %d\n", input_framesize);
  if (inbuf == NULL) {
    printf("err malloc 1\n");
    return -1;
  }
  int output_framesize = outputFrameSize(width, height);
  printf("o %d\n", output_framesize);
  outbuf = malloc(output_framesize);
  if (outbuf == NULL) {
    printf("err malloc 2\n");
    return -1;
  }
  for (i = 0;; i++) {
    size_t readsize = fread(inbuf, 1, input_framesize, input);
    if (readsize == 0) {
      printf("OK end of file %d\n", i);
      return 0;
    } else if (readsize != input_framesize) {
      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, 1, output_framesize, output);
    if (writesize != output_framesize) {
      printf("fwrite err %d %d\n", writesize, output_framesize);
      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),%edi
mov    -0x20(%rbp),%esi
call   1070 <inputFrameSize@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11d6 <conv_file+0x66>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1334 <conv_file+0x1c4>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1060 <outputFrameSize@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x3c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1226 <conv_file+0xb6>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1334 <conv_file+0x1c4>
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    126e <conv_file+0xfe>
mov    -0x34(%rbp),%esi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1334 <conv_file+0x1c4>
mov    -0x48(%rbp),%rax
movslq -0x38(%rbp),%rcx
cmp    %rcx,%rax
je     1299 <conv_file+0x129>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1334 <conv_file+0x1c4>
jmp    129e <conv_file+0x12e>
jmp    12a3 <conv_file+0x133>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   1030 <conv_frame@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    12d1 <conv_file+0x161>
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
movslq -0x3c(%rbp),%rcx
cmp    %rcx,%rax
je     131c <conv_file+0x1ac>
mov    -0x58(%rbp),%rsi
mov    -0x3c(%rbp),%edx
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1334 <conv_file+0x1c4>
jmp    1321 <conv_file+0x1b1>
jmp    1326 <conv_file+0x1b6>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    122d <conv_file+0xbd>
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    $0x28,%rsp
mov    %ecx,%r13d
mov    %edx,%ebx
mov    %rsi,0x20(%rsp)
mov    %rdi,0x10(%rsp)
mov    %edx,%edi
mov    %ecx,%esi
call   1080 <inputFrameSize@plt>
mov    %eax,%r14d
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
lea    0xe46(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %rbp,%rbp
je     12aa <conv_file+0x12a>
mov    %ebx,0xc(%rsp)
mov    %ebx,%edi
mov    %r13d,%esi
call   1070 <outputFrameSize@plt>
mov    %eax,%ebx
lea    0xe22(%rip),%rdi
xor    %r15d,%r15d
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     12b3 <conv_file+0x133>
mov    %rax,%r12
mov    %r13d,0x8(%rsp)
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    0x10(%rsp),%rcx
call   1050 <fread@plt>
test   %rax,%rax
je     1295 <conv_file+0x115>
movslq %r14d,%r13
movslq %ebx,%rcx
mov    %rcx,0x18(%rsp)
xor    %r15d,%r15d
cmp    %r13,%rax
jne    12bc <conv_file+0x13c>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%ecx
call   1030 <conv_frame@plt>
test   %eax,%eax
jns    125c <conv_file+0xdc>
lea    0xdda(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    0x20(%rsp),%rcx
call   10a0 <fwrite@plt>
cmp    0x18(%rsp),%rax
jne    12de <conv_file+0x15e>
add    $0x1,%r15d
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    0x10(%rsp),%rcx
call   1050 <fread@plt>
test   %rax,%rax
jne    1230 <conv_file+0xb0>
lea    0xd70(%rip),%rdi
xor    %ebx,%ebx
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12cd <conv_file+0x14d>
lea    0xda0(%rip),%rdi
jmp    12c3 <conv_file+0x143>
lea    0xd8a(%rip),%rdi
jmp    12c3 <conv_file+0x143>
lea    0xd77(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3a(%rip),%rdi
mov    %rax,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12c8 <conv_file+0x148>

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

LLVM IR (-O2):
@.str = [6 x i8] c"i %d\0A\00"
@.str.2 = [6 x i8] c"o %d\0A\00"
@.str.4 = [19 x i8] c"OK end of file %d\0A\00"
@.str.7 = [18 x i8] c"fwrite err %d %d\0A\00"
@str = [9 x i8] c"frame ng\00"
@str.8 = [10 x i8] c"fread err\00"
@str.9 = [13 x i8] c"err malloc 2\00"
@str.10 = [13 x i8] c"err malloc 1\00"
define i32 @conv_file(i32* nocapture %0, i32* nocapture %1, i32 %2, i32 %3) {
%5 = tail call i32 @inputFrameSize(i32 %2, i32 %3)
%6 = tail call i8* @malloc(i32 %5)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5)
%8 = icmp eq i8* %6, null
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.10, i64 0, i64 0))
br label %47
11: ; preds = %4
%12 = tail call i32 @outputFrameSize(i32 %2, i32 %3)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = tail call i8* @malloc(i32 %12)
%15 = icmp eq i8* %14, null
br i1 %15, label %22, label %16
16: ; preds = %11
%17 = tail call i64 @fread(i8* %6, i32 1, i32 %5, i32* %0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = sext i32 %5 to i64
%21 = sext i32 %12 to i64
br label %27
22: ; preds = %11
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
br label %47
24: ; preds = %43, %16
%25 = phi i32 [ 0, %16 ], [ %44, %43 ]
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %25)
br label %47
27: ; preds = %19, %43
%28 = phi i64 [ %17, %19 ], [ %45, %43 ]
%29 = phi i32 [ 0, %19 ], [ %44, %43 ]
%30 = icmp eq i64 %28, %20
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.8, i64 0, i64 0))
br label %47
33: ; preds = %27
%34 = tail call i32 @conv_frame(i8* %6, i8* %14, i32 %3, i32 %2)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = tail call i64 @fwrite(i8* %14, i32 1, i32 %12, i32* %1)
%40 = icmp eq i64 %39, %21
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i64 %39, i32 %12)
br label %47
43: ; preds = %38
%44 = add nuw nsw i32 %29, 1
%45 = tail call i64 @fread(i8* %6, i32 1, i32 %5, i32* %0)
%46 = icmp eq i64 %45, 0
br i1 %46, label %24, label %27
47: ; preds = %41, %31, %24, %22, %9
%48 = phi i32 [ -1, %9 ], [ -1, %22 ], [ -1, %41 ], [ -1, %31 ], [ 0, %24 ]
ret i32 %48
}