#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 fp_move_forward(int /*<<< orphan*/ *, int);
int fread(char *, int, int, int /*<<< orphan*/ *);
int read_tiff(int /*<<< orphan*/ *, int);
scalar_t__ strcmp(char *, char *);
int read_app1(FILE *fp, int clen) {
  char buf[6];
  int ret;
  if (clen < 14)
    return 0;
  if (fread(buf, 1, 6, fp) != 6)
    return 0;
  clen -= 6;
  if (buf[4] != 0 || buf[5] != 0)
    return 0;
  if (strcmp(buf, "Exif") != 0)
    return 0;
  ret = read_tiff(fp, clen);
  if (!ret || ret > clen)
    return 0;
  clen -= ret;
  return fp_move_forward(fp, clen);
}


x86 assembly (clang, -O0):
<read_app1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0xe,-0x14(%rbp)
jge    1165 <read_app1+0x25>
movl   $0x0,-0x4(%rbp)
jmp    1231 <read_app1+0xf1>
lea    -0x1a(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x6,%edx
call   1040 <fread@plt>
cmp    $0x6,%eax
je     1191 <read_app1+0x51>
movl   $0x0,-0x4(%rbp)
jmp    1231 <read_app1+0xf1>
mov    -0x14(%rbp),%eax
sub    $0x6,%eax
mov    %eax,-0x14(%rbp)
movsbl -0x16(%rbp),%eax
cmp    $0x0,%eax
jne    11b4 <read_app1+0x74>
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
je     11c0 <read_app1+0x80>
movl   $0x0,-0x4(%rbp)
jmp    1231 <read_app1+0xf1>
lea    -0x1a(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11e6 <read_app1+0xa6>
movl   $0x0,-0x4(%rbp)
jmp    1231 <read_app1+0xf1>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <read_tiff@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     120b <read_app1+0xcb>
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1217 <read_app1+0xd7>
movl   $0x0,-0x4(%rbp)
jmp    1231 <read_app1+0xf1>
mov    -0x20(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <fp_move_forward@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_app1>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
cmp    $0xe,%esi
jl     11b1 <read_app1+0x71>
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xa(%rsp),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    $0x6,%eax
jne    11b1 <read_app1+0x71>
mov    0xe(%rsp),%al
or     0xf(%rsp),%al
jne    11b1 <read_app1+0x71>
lea    0xe7f(%rip),%rsi
lea    0xa(%rsp),%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11b1 <read_app1+0x71>
add    $0xfffffffa,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <read_tiff@plt>
test   %eax,%eax
je     11b1 <read_app1+0x71>
sub    %eax,%ebp
jl     11b1 <read_app1+0x71>
mov    %r14,%rdi
mov    %ebp,%esi
call   1060 <fp_move_forward@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"Exif\00"
define i32 @read_app1(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [6 x i8]
%7 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 14
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 0, i32* %3
br label %53
11: ; preds = %2
%12 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%13 = load i32*, i32** %4
%14 = call i32 @fread(i8* %12, i32 1, i32 6, i32* %13)
%15 = icmp ne i32 %14, 6
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 0, i32* %3
br label %53
17: ; preds = %11
%18 = load i32, i32* %5
%19 = sub nsw i32 %18, 6
store i32 %19, i32* %5
%20 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 4
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %17
%25 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 5
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24, %17
store i32 0, i32* %3
br label %53
30: ; preds = %24
%31 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%32 = call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 0, i32* %3
br label %53
35: ; preds = %30
%36 = load i32*, i32** %4
%37 = load i32, i32* %5
%38 = call i32 @read_tiff(i32* %36, i32 %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %35
%42 = load i32, i32* %7
%43 = load i32, i32* %5
%44 = icmp sgt i32 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %41, %35
store i32 0, i32* %3
br label %53
46: ; preds = %41
%47 = load i32, i32* %7
%48 = load i32, i32* %5
%49 = sub nsw i32 %48, %47
store i32 %49, i32* %5
%50 = load i32*, i32** %4
%51 = load i32, i32* %5
%52 = call i32 @fp_move_forward(i32* %50, i32 %51)
store i32 %52, i32* %3
br label %53
53: ; preds = %46, %45, %34, %29, %16, %10
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [5 x i8] c"Exif\00"
define i32 @read_app1(i32* %0, i32 %1) {
%3 = alloca [6 x i8]
%4 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %1, 14
br i1 %5, label %29, label %6
6: ; preds = %2
%7 = call i32 @fread(i8* %4, i32 1, i32 6, i32* %0)
%8 = icmp eq i32 %7, 6
br i1 %8, label %9, label %29
9: ; preds = %6
%10 = add nsw i32 %1, -6
%11 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 4
%12 = load i8, i8* %11
%13 = icmp ne i8 %12, 0
%14 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 5
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
%17 = select i1 %13, i1 true, i1 %16
br i1 %17, label %29, label %18
18: ; preds = %9
%19 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = call i32 @read_tiff(i32* %0, i32 %10)
%23 = icmp eq i32 %22, 0
%24 = icmp slt i32 %10, %22
%25 = select i1 %23, i1 true, i1 %24
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = sub nsw i32 %10, %22
%28 = call i32 @fp_move_forward(i32* %0, i32 %27)
br label %29
29: ; preds = %21, %18, %9, %6, %2, %26
%30 = phi i32 [ %28, %26 ], [ 0, %2 ], [ 0, %6 ], [ 0, %9 ], [ 0, %18 ], [ 0, %21 ]
ret i32 %30
}