#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 fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int check_magic(FILE *input_file, FILE *output_file) {
  int i, count;
  char *dynamic_array = NULL;
  dynamic_array = (char *)malloc(1 * sizeof(char));
  for (i = 0; i <= 2; i++) {
    count = fread(dynamic_array, sizeof(char), 1, input_file);
    if (count <= 0)
      return 4;
    if (i == 0) {
      if (dynamic_array[0] != 'P') {
        free(dynamic_array);
        return 4;
      }
      fwrite(dynamic_array, sizeof(char), 1, output_file);
    }
    if (i == 1) {
      if (dynamic_array[0] != '6') {
        free(dynamic_array);
        return 4;
      }
      fwrite(dynamic_array, sizeof(char), 1, output_file);
    }
    if (i == 2) {
      if ((int)dynamic_array[0] != 10) {
        free(dynamic_array);
        return 4;
      }
      fwrite(dynamic_array, sizeof(char), 1, output_file);
    }
  }
  free(dynamic_array);
  return 0;
}


x86 assembly (clang, -O0):
<check_magic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jg     1280 <check_magic+0x140>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     11a4 <check_magic+0x64>
movl   $0x4,-0x4(%rbp)
jmp    1290 <check_magic+0x150>
cmpl   $0x0,-0x1c(%rbp)
jne    11e7 <check_magic+0xa7>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x50,%eax
je     11d3 <check_magic+0x93>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x4,-0x4(%rbp)
jmp    1290 <check_magic+0x150>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1060 <fwrite@plt>
cmpl   $0x1,-0x1c(%rbp)
jne    122a <check_magic+0xea>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x36,%eax
je     1216 <check_magic+0xd6>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x4,-0x4(%rbp)
jmp    1290 <check_magic+0x150>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1060 <fwrite@plt>
cmpl   $0x2,-0x1c(%rbp)
jne    126d <check_magic+0x12d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     1259 <check_magic+0x119>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x4,-0x4(%rbp)
jmp    1290 <check_magic+0x150>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1060 <fwrite@plt>
jmp    1272 <check_magic+0x132>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116d <check_magic+0x2d>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_magic>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    $0x4,%ebp
test   %eax,%eax
jle    11f4 <check_magic+0xb4>
cmpb   $0x50,(%rbx)
jne    11ec <check_magic+0xac>
mov    $0x50,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    $0x4,%ebp
test   %eax,%eax
jle    11f4 <check_magic+0xb4>
cmpb   $0x36,(%rbx)
jne    11ec <check_magic+0xac>
mov    $0x36,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    $0x4,%ebp
test   %eax,%eax
jle    11f4 <check_magic+0xb4>
cmpb   $0xa,(%rbx)
jne    11ec <check_magic+0xac>
mov    $0xa,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @check_magic(i32* nocapture %0, i32* nocapture %1) {
%3 = tail call i64 @malloc(i32 1)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @fread(i8* %4, i32 1, i32 1, i32* %0)
%6 = icmp slt i32 %5, 1
br i1 %6, label %29, label %7
7: ; preds = %2
%8 = load i8, i8* %4
%9 = icmp eq i8 %8, 80
br i1 %9, label %10, label %26
10: ; preds = %7
%11 = tail call i32 @fputc(i32 80, i32* %1)
%12 = tail call i32 @fread(i8* %4, i32 1, i32 1, i32* %0)
%13 = icmp slt i32 %12, 1
br i1 %13, label %29, label %14
14: ; preds = %10
%15 = load i8, i8* %4
%16 = icmp eq i8 %15, 54
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = tail call i32 @fputc(i32 54, i32* %1)
%19 = tail call i32 @fread(i8* %4, i32 1, i32 1, i32* %0)
%20 = icmp slt i32 %19, 1
br i1 %20, label %29, label %21
21: ; preds = %17
%22 = load i8, i8* %4
%23 = icmp eq i8 %22, 10
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @fputc(i32 10, i32* %1)
br label %26
26: ; preds = %21, %14, %7, %24
%27 = phi i32 [ 0, %24 ], [ 4, %7 ], [ 4, %14 ], [ 4, %21 ]
%28 = tail call i32 @free(i8* %4)
br label %29
29: ; preds = %26, %2, %10, %17
%30 = phi i32 [ 4, %17 ], [ 4, %10 ], [ 4, %2 ], [ %27, %26 ]
ret i32 %30
}