#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*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ fix_header(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ is_header_to_fix(int /*<<< orphan*/ *);
int open(char const *, int /*<<< orphan*/);
int read_header_rest(int, int /*<<< orphan*/ *);
int read_next_packet_start(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ seek_to_next(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stderr;
int write_header_back(int, int /*<<< orphan*/ *);
void fix_file(const char *name) {
  int fd = open(name, O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "Cannot open file '%s'\n", name);
    return;
  }
  uint8_t header_buffer[255 + 9];
  int packet_count = 0;
  int packet_fix_count = 0;
  fprintf(stderr, "Start fixing file '%s'\n", name);
  int r;
  while ((r = read_next_packet_start(fd, header_buffer)) > 0) {
    int readed = 0;
    packet_count++;
    if (is_header_to_fix(header_buffer)) {
      if ((r = read_header_rest(fd, header_buffer)) <= 0)
        break;
      fix_header(header_buffer);
      readed = write_header_back(fd, header_buffer);
      if (readed < 0) {
        fprintf(stderr, "Cannot write to file '%s'\n", name);
        break;
      }
      packet_fix_count++;
    }
    if (!seek_to_next(fd, header_buffer, readed)) {
      fprintf(stderr, "Cannot seek in file '%s'\n", name);
      break;
    }
  }
  if (r < 0)
    fprintf(stderr, "Cannot read from file '%s'\n", name);
  fprintf(stderr, "Checked %d pes headers and %d fixed\n", packet_count,
          packet_fix_count);
}


x86 assembly (clang, -O0):
<fix_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ce <fix_file+0x4e>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1358 <fix_file+0x1d8>
movl   $0x0,-0x434(%rbp)
movl   $0x0,-0x438(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0xc(%rbp),%edi
lea    -0x430(%rbp),%rsi
call   10a0 <read_next_packet_start@plt>
mov    %eax,-0x43c(%rbp)
cmp    $0x0,%eax
jle    130d <fix_file+0x18d>
movl   $0x0,-0x440(%rbp)
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
lea    -0x430(%rbp),%rdi
call   1050 <is_header_to_fix@plt>
cmp    $0x0,%rax
je     12ca <fix_file+0x14a>
mov    -0xc(%rbp),%edi
lea    -0x430(%rbp),%rsi
call   1040 <read_header_rest@plt>
mov    %eax,-0x43c(%rbp)
cmp    $0x0,%eax
jg     126d <fix_file+0xed>
jmp    130d <fix_file+0x18d>
lea    -0x430(%rbp),%rdi
call   1030 <fix_header@plt>
mov    -0xc(%rbp),%edi
lea    -0x430(%rbp),%rsi
call   1090 <write_header_back@plt>
mov    %eax,-0x440(%rbp)
cmpl   $0x0,-0x440(%rbp)
jge    12bb <fix_file+0x13b>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd80(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    130d <fix_file+0x18d>
mov    -0x438(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x438(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x440(%rbp),%edx
call   1080 <seek_to_next@plt>
cmp    $0x0,%eax
jne    1308 <fix_file+0x188>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd4e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    130d <fix_file+0x18d>
jmp    11fd <fix_file+0x7d>
cmpl   $0x0,-0x43c(%rbp)
jge    1335 <fix_file+0x1b5>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd36(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
mov    -0x434(%rbp),%edx
mov    -0x438(%rbp),%ecx
lea    0xd2f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fix_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rdi,%r14
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     123e <fix_file+0xbe>
mov    %eax,%ebp
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
xor    %r15d,%r15d
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rsp,%r13
xor    %r12d,%r12d
jmp    11f6 <fix_file+0x76>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
mov    %ebp,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1080 <seek_to_next@plt>
lea    0x1(%rbx),%r15d
test   %eax,%eax
je     1269 <fix_file+0xe9>
mov    %r15d,%ebx
mov    %ebp,%edi
mov    %r13,%rsi
call   10a0 <read_next_packet_start@plt>
test   %eax,%eax
jle    1275 <fix_file+0xf5>
mov    %r13,%rdi
call   1050 <is_header_to_fix@plt>
test   %rax,%rax
je     11e0 <fix_file+0x60>
mov    %ebp,%edi
mov    %r13,%rsi
call   1040 <read_header_rest@plt>
test   %eax,%eax
jle    1272 <fix_file+0xf2>
mov    %r13,%rdi
call   1030 <fix_header@plt>
mov    %ebp,%edi
mov    %r13,%rsi
call   1090 <write_header_back@plt>
test   %eax,%eax
js     1289 <fix_file+0x109>
add    $0x1,%r12d
jmp    11e2 <fix_file+0x62>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>
lea    0xdda(%rip),%rsi
jmp    1290 <fix_file+0x110>
add    $0x1,%ebx
test   %eax,%eax
mov    0x2d5a(%rip),%rbp
jns    12a7 <fix_file+0x127>
lea    0xddd(%rip),%rsi
jmp    129a <fix_file+0x11a>
lea    0xd9f(%rip),%rsi
add    $0x1,%ebx
mov    0x2d3e(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xdcf(%rip),%rsi
mov    %ebx,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"Cannot open file '%s'\0A\00"
@.str.1 = [24 x i8] c"Start fixing file '%s'\0A\00"
@.str.2 = [27 x i8] c"Cannot write to file '%s'\0A\00"
@.str.3 = [26 x i8] c"Cannot seek in file '%s'\0A\00"
@.str.4 = [28 x i8] c"Cannot read from file '%s'\0A\00"
@.str.5 = [37 x i8] c"Checked %d pes headers and %d fixed\0A\00"
define void @fix_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [264 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %2
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %77
18: ; preds = %1
store i32 0, i32* %5
store i32 0, i32* %6
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %2
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %64, %18
%23 = load i32, i32* %3
%24 = getelementptr inbounds [264 x i32], [264 x i32]* %4, i64 0, i64 0
%25 = call i32 @read_next_packet_start(i32 %23, i32* %24)
store i32 %25, i32* %7
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %65
27: ; preds = %22
store i32 0, i32* %8
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
%30 = getelementptr inbounds [264 x i32], [264 x i32]* %4, i64 0, i64 0
%31 = call i64 @is_header_to_fix(i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %54
33: ; preds = %27
%34 = load i32, i32* %3
%35 = getelementptr inbounds [264 x i32], [264 x i32]* %4, i64 0, i64 0
%36 = call i32 @read_header_rest(i32 %34, i32* %35)
store i32 %36, i32* %7
%37 = icmp sle i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %65
39: ; preds = %33
%40 = getelementptr inbounds [264 x i32], [264 x i32]* %4, i64 0, i64 0
%41 = call i32 @fix_header(i32* %40)
%42 = load i32, i32* %3
%43 = getelementptr inbounds [264 x i32], [264 x i32]* %4, i64 0, i64 0
%44 = call i32 @write_header_back(i32 %42, i32* %43)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %39
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %2
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %49)
br label %65
51: ; preds = %39
%52 = load i32, i32* %6
%53 = add nsw i32 %52, 1
store i32 %53, i32* %6
br label %54
54: ; preds = %51, %27
%55 = load i32, i32* %3
%56 = getelementptr inbounds [264 x i32], [264 x i32]* %4, i64 0, i64 0
%57 = load i32, i32* %8
%58 = call i32 @seek_to_next(i32 %55, i32* %56, i32 %57)
%59 = icmp ne i32 %58, 0
br i1 %59, label %64, label %60
60: ; preds = %54
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %2
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %62)
br label %65
64: ; preds = %54
br label %22
65: ; preds = %60, %47, %38, %22
%66 = load i32, i32* %7
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %65
%69 = load i32, i32* @stderr
%70 = load i8*, i8** %2
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %70)
br label %72
72: ; preds = %68, %65
%73 = load i32, i32* @stderr
%74 = load i32, i32* %5
%75 = load i32, i32* %6
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %74, i32 %75)
br label %77
77: ; preds = %72, %14
ret void
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"Cannot open file '%s'\0A\00"
@.str.1 = [24 x i8] c"Start fixing file '%s'\0A\00"
@.str.2 = [27 x i8] c"Cannot write to file '%s'\0A\00"
@.str.3 = [26 x i8] c"Cannot seek in file '%s'\0A\00"
@.str.4 = [28 x i8] c"Cannot read from file '%s'\0A\00"
@.str.5 = [37 x i8] c"Checked %d pes headers and %d fixed\0A\00"
define void @fix_file(i8* %0) {
%2 = alloca [264 x i32]
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %52
9: ; preds = %1
%10 = bitcast [264 x i32]* %2 to i8*
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = getelementptr inbounds [264 x i32], [264 x i32]* %2, i64 0, i64 0
br label %14
14: ; preds = %32, %9
%15 = phi i32 [ 0, %9 ], [ %20, %32 ]
%16 = phi i32 [ 0, %9 ], [ %33, %32 ]
%17 = call i32 @read_next_packet_start(i32 %4, i32* %13)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %14
%20 = add nuw nsw i32 %15, 1
%21 = call i64 @is_header_to_fix(i32* %13)
%22 = icmp eq i64 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %19
%24 = call i32 @read_header_rest(i32 %4, i32* %13)
%25 = icmp slt i32 %24, 1
br i1 %25, label %37, label %26
26: ; preds = %23
%27 = call i32 @fix_header(i32* %13)
%28 = call i32 @write_header_back(i32 %4, i32* %13)
%29 = icmp slt i32 %28, 0
br i1 %29, label %41, label %30
30: ; preds = %26
%31 = add nsw i32 %16, 1
br label %32
32: ; preds = %30, %19
%33 = phi i32 [ %31, %30 ], [ %16, %19 ]
%34 = phi i32 [ %28, %30 ], [ 0, %19 ]
%35 = call i32 @seek_to_next(i32 %4, i32* %13, i32 %34)
%36 = icmp eq i32 %35, 0
br i1 %36, label %41, label %14
37: ; preds = %23, %14
%38 = phi i32 [ %15, %14 ], [ %20, %23 ]
%39 = phi i32 [ %17, %14 ], [ %24, %23 ]
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %32, %26, %37
%42 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), %37 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), %26 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), %32 ]
%43 = phi i32 [ %16, %37 ], [ %16, %26 ], [ %33, %32 ]
%44 = phi i32 [ %38, %37 ], [ %20, %26 ], [ %20, %32 ]
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* %42, i8* %0)
br label %47
47: ; preds = %41, %37
%48 = phi i32 [ %16, %37 ], [ %43, %41 ]
%49 = phi i32 [ %38, %37 ], [ %44, %41 ]
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %49, i32 %48)
br label %52
52: ; preds = %47, %6
ret void
}