#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 EOF;
int /*<<< orphan*/ SEEK_CUR;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ stderr;
int print_jpeg_metadata_size(char *filename) {
  FILE *f = fopen(filename, "r");
  if (!f) {
    fprintf(stderr, "error: cannot open file %s\n", filename);
    return errno;
  }
  int c;
  int lsb = 0;
  int err = 0;
  while ((c = fgetc(f)) != EOF) {
    if (lsb) {
      int pos = ftell(f) - 2;
      int length;
      int d;
      if (c != 0xd8) {
        if ((c & 0xf0) != 0xe0) {
          printf("%d %s\n", ftell(f) - 2, filename);
          break;
        }
        length = 0;
        if ((d = fgetc(f)) == EOF)
          break;
        length += d << 8;
        if ((d = fgetc(f)) == EOF)
          break;
        length += d;
        if (length < 2) {
          fprintf(stderr, "error: invalid segment length in %s\n", filename);
          err = 1;
          break;
        }
        if (length && fseek(f, length - 2, SEEK_CUR) < 0) {
          fprintf(stderr, "error: i/o error with %s\n", filename);
          err = errno;
          break;
        }
      }
      lsb = 0;
    } else if (c == 0xff) {
      lsb = 1;
    }
  }
  fclose(f);
  return err;
}


x86 assembly (clang, -O0):
<print_jpeg_metadata_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c5 <print_jpeg_metadata_size+0x55>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe53(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1369 <print_jpeg_metadata_size+0x1f9>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%eax
je     135a <print_jpeg_metadata_size+0x1ea>
cmpl   $0x0,-0x20(%rbp)
je     133c <print_jpeg_metadata_size+0x1cc>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
sub    $0x2,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0xd8,-0x1c(%rbp)
je     1330 <print_jpeg_metadata_size+0x1c0>
mov    -0x1c(%rbp),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
je     124a <print_jpeg_metadata_size+0xda>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,%esi
sub    $0x2,%esi
mov    -0x10(%rbp),%rdx
lea    0xdde(%rip),%rdi
call   1040 <printf@plt>
jmp    135a <print_jpeg_metadata_size+0x1ea>
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x30(%rbp)
mov    0x2d7c(%rip),%rcx
cmp    (%rcx),%eax
jne    1271 <print_jpeg_metadata_size+0x101>
jmp    135a <print_jpeg_metadata_size+0x1ea>
mov    -0x30(%rbp),%eax
shl    $0x8,%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x30(%rbp)
mov    0x2d50(%rip),%rcx
cmp    (%rcx),%eax
jne    129d <print_jpeg_metadata_size+0x12d>
jmp    135a <print_jpeg_metadata_size+0x1ea>
mov    -0x30(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x2,-0x2c(%rbp)
jge    12d5 <print_jpeg_metadata_size+0x165>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd61(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
jmp    135a <print_jpeg_metadata_size+0x1ea>
cmpl   $0x0,-0x2c(%rbp)
je     132b <print_jpeg_metadata_size+0x1bb>
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
sub    $0x2,%esi
mov    0x2cf8(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
cmp    $0x0,%rax
jge    132b <print_jpeg_metadata_size+0x1bb>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd35(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2ca7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    135a <print_jpeg_metadata_size+0x1ea>
jmp    1330 <print_jpeg_metadata_size+0x1c0>
movl   $0x0,-0x20(%rbp)
jmp    1355 <print_jpeg_metadata_size+0x1e5>
cmpl   $0xff,-0x1c(%rbp)
jne    1350 <print_jpeg_metadata_size+0x1e0>
movl   $0x1,-0x20(%rbp)
jmp    1355 <print_jpeg_metadata_size+0x1e5>
jmp    11d3 <print_jpeg_metadata_size+0x63>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_jpeg_metadata_size>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe7b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     127d <print_jpeg_metadata_size+0x10d>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <fgetc@plt>
mov    0x2e3b(%rip),%r12
xor    %ebp,%ebp
cmp    (%r12),%eax
je     12b9 <print_jpeg_metadata_size+0x149>
mov    %eax,%ebx
xor    %ebp,%ebp
mov    0x2e2c(%rip),%r13
jmp    11e0 <print_jpeg_metadata_size+0x70>
xchg   %ax,%ax
xor    %ebp,%ebp
cmp    $0xff,%ebx
sete   %bpl
mov    %r15,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebx
cmp    (%r12),%eax
je     12b7 <print_jpeg_metadata_size+0x147>
test   %ebp,%ebp
je     11c0 <print_jpeg_metadata_size+0x50>
mov    %r15,%rdi
call   1070 <ftell@plt>
xor    %ebp,%ebp
cmp    $0xd8,%ebx
je     11cc <print_jpeg_metadata_size+0x5c>
and    $0xf0,%ebx
mov    %r15,%rdi
cmp    $0xe0,%ebx
jne    12a0 <print_jpeg_metadata_size+0x130>
call   1050 <fgetc@plt>
xor    %ebp,%ebp
cmp    (%r12),%eax
je     12b9 <print_jpeg_metadata_size+0x149>
mov    %eax,%ebx
mov    %r15,%rdi
call   1050 <fgetc@plt>
cmp    (%r12),%eax
je     12b9 <print_jpeg_metadata_size+0x149>
shl    $0x8,%ebx
add    %ebx,%eax
cmp    $0x1,%eax
jle    12d2 <print_jpeg_metadata_size+0x162>
add    $0xfffffffe,%eax
mov    0x0(%r13),%edx
mov    %r15,%rdi
mov    %eax,%esi
call   1080 <fseek@plt>
xor    %ebp,%ebp
test   %rax,%rax
jns    11cc <print_jpeg_metadata_size+0x5c>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%ebp
jmp    12b9 <print_jpeg_metadata_size+0x149>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xd75(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%ebp
jmp    12c1 <print_jpeg_metadata_size+0x151>
call   1070 <ftell@plt>
lea    -0x2(%rax),%esi
lea    0xd6f(%rip),%rdi
mov    %r14,%rdx
call   1040 <printf@plt>
xor    %ebp,%ebp
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xd43(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%ebp
jmp    12b9 <print_jpeg_metadata_size+0x149>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"error: cannot open file %s\0A\00"
@errno = global i32 0
@EOF = global i32 0
@.str.2 = [7 x i8] c"%d %s\0A\00"
@.str.3 = [37 x i8] c"error: invalid segment length in %s\0A\00"
@SEEK_CUR = global i32 0
@.str.4 = [26 x i8] c"error: i/o error with %s\0A\00"
define i32 @print_jpeg_metadata_size(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %20, label %15
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %3
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @errno
store i32 %19, i32* %2
br label %98
20: ; preds = %1
store i32 0, i32* %6
store i32 0, i32* %7
br label %21
21: ; preds = %93, %20
%22 = load i32*, i32** %4
%23 = call i32 @fgetc(i32* %22)
store i32 %23, i32* %5
%24 = load i32, i32* @EOF
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %94
26: ; preds = %21
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %88
29: ; preds = %26
%30 = load i32*, i32** %4
%31 = call i32 @ftell(i32* %30)
%32 = sub nsw i32 %31, 2
store i32 %32, i32* %8
%33 = load i32, i32* %5
%34 = icmp ne i32 %33, 216
br i1 %34, label %35, label %87
35: ; preds = %29
%36 = load i32, i32* %5
%37 = and i32 %36, 240
%38 = icmp ne i32 %37, 224
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = load i32*, i32** %4
%41 = call i32 @ftell(i32* %40)
%42 = sub nsw i32 %41, 2
%43 = load i8*, i8** %3
%44 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %42, i8* %43)
br label %94
45: ; preds = %35
store i32 0, i32* %9
%46 = load i32*, i32** %4
%47 = call i32 @fgetc(i32* %46)
store i32 %47, i32* %10
%48 = load i32, i32* @EOF
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %45
br label %94
51: ; preds = %45
%52 = load i32, i32* %10
%53 = shl i32 %52, 8
%54 = load i32, i32* %9
%55 = add nsw i32 %54, %53
store i32 %55, i32* %9
%56 = load i32*, i32** %4
%57 = call i32 @fgetc(i32* %56)
store i32 %57, i32* %10
%58 = load i32, i32* @EOF
%59 = icmp eq i32 %57, %58
br i1 %59, label %60, label %61
60: ; preds = %51
br label %94
61: ; preds = %51
%62 = load i32, i32* %10
%63 = load i32, i32* %9
%64 = add nsw i32 %63, %62
store i32 %64, i32* %9
%65 = load i32, i32* %9
%66 = icmp slt i32 %65, 2
br i1 %66, label %67, label %71
67: ; preds = %61
%68 = load i32, i32* @stderr
%69 = load i8*, i8** %3
%70 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %69)
store i32 1, i32* %7
br label %94
71: ; preds = %61
%72 = load i32, i32* %9
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %86
74: ; preds = %71
%75 = load i32*, i32** %4
%76 = load i32, i32* %9
%77 = sub nsw i32 %76, 2
%78 = load i32, i32* @SEEK_CUR
%79 = call i64 @fseek(i32* %75, i32 %77, i32 %78)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %86
81: ; preds = %74
%82 = load i32, i32* @stderr
%83 = load i8*, i8** %3
%84 = call i32 @fprintf(i32 %82, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %83)
%85 = load i32, i32* @errno
store i32 %85, i32* %7
br label %94
86: ; preds = %74, %71
br label %87
87: ; preds = %86, %29
store i32 0, i32* %6
br label %93
88: ; preds = %26
%89 = load i32, i32* %5
%90 = icmp eq i32 %89, 255
br i1 %90, label %91, label %92
91: ; preds = %88
store i32 1, i32* %6
br label %92
92: ; preds = %91, %88
br label %93
93: ; preds = %92, %87
br label %21
94: ; preds = %81, %67, %60, %50, %39, %21
%95 = load i32*, i32** %4
%96 = call i32 @fclose(i32* %95)
%97 = load i32, i32* %7
store i32 %97, i32* %2
br label %98
98: ; preds = %94, %15
%99 = load i32, i32* %2
ret i32 %99
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"error: cannot open file %s\0A\00"
@errno = global i32 0
@EOF = global i32 0
@.str.2 = [7 x i8] c"%d %s\0A\00"
@.str.3 = [37 x i8] c"error: invalid segment length in %s\0A\00"
@SEEK_CUR = global i32 0
@.str.4 = [26 x i8] c"error: i/o error with %s\0A\00"
define i32 @print_jpeg_metadata_size(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = tail call i32 @fgetc(i32* %2)
%6 = load i32, i32* @EOF
%7 = icmp eq i32 %5, %6
br i1 %7, label %58, label %12
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%11 = load i32, i32* @errno
br label %61
12: ; preds = %4, %53
%13 = phi i32 [ %55, %53 ], [ %5, %4 ]
%14 = phi i32 [ %54, %53 ], [ 0, %4 ]
%15 = icmp eq i32 %14, 0
br i1 %15, label %50, label %16
16: ; preds = %12
%17 = tail call i32 @ftell(i32* %2)
%18 = icmp eq i32 %13, 216
br i1 %18, label %53, label %19
19: ; preds = %16
%20 = and i32 %13, 240
%21 = icmp eq i32 %20, 224
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = tail call i32 @ftell(i32* %2)
%24 = add nsw i32 %23, -2
%25 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %24, i8* %0)
br label %58
26: ; preds = %19
%27 = tail call i32 @fgetc(i32* %2)
%28 = load i32, i32* @EOF
%29 = icmp eq i32 %27, %28
br i1 %29, label %58, label %30
30: ; preds = %26
%31 = tail call i32 @fgetc(i32* %2)
%32 = load i32, i32* @EOF
%33 = icmp eq i32 %31, %32
br i1 %33, label %58, label %34
34: ; preds = %30
%35 = shl i32 %27, 8
%36 = add nsw i32 %31, %35
%37 = icmp slt i32 %36, 2
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = tail call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %58
41: ; preds = %34
%42 = add nsw i32 %36, -2
%43 = load i32, i32* @SEEK_CUR
%44 = tail call i64 @fseek(i32* %2, i32 %42, i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = tail call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%49 = load i32, i32* @errno
br label %58
50: ; preds = %12
%51 = icmp eq i32 %13, 255
%52 = zext i1 %51 to i32
br label %53
53: ; preds = %41, %16, %50
%54 = phi i32 [ %52, %50 ], [ 0, %41 ], [ 0, %16 ]
%55 = tail call i32 @fgetc(i32* %2)
%56 = load i32, i32* @EOF
%57 = icmp eq i32 %55, %56
br i1 %57, label %58, label %12
58: ; preds = %53, %26, %30, %4, %46, %38, %22
%59 = phi i32 [ %49, %46 ], [ 1, %38 ], [ 0, %22 ], [ 0, %4 ], [ 0, %30 ], [ 0, %26 ], [ 0, %53 ]
%60 = tail call i32 @fclose(i32* %2)
br label %61
61: ; preds = %58, %8
%62 = phi i32 [ %59, %58 ], [ %11, %8 ]
ret i32 %62
}