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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ Usage();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
unsigned char *get_section(unsigned short *, int);
int open(char *, int /*<<< orphan*/);
unsigned int sectioncrc(unsigned char *, unsigned short);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int /*<<< orphan*/, unsigned char *, unsigned short);
int main(int argc, char *argv[]) {
  unsigned short a_section_size;
  unsigned char *a_section;
  int fd_sec;
  unsigned int crc;
  if (argc >= 2) {
    fd_sec = open(argv[1], O_RDONLY);
    if (fd_sec < 0) {
      fprintf(stderr, "Can't find file %s\n", argv[1]);
      return 2;
    }
  } else {
    Usage();
    return 2;
  }
  while (1) {
    a_section = get_section(&a_section_size, fd_sec);
    if (a_section_size <= 0 || a_section == 0) {
      return 0;
    }
    if (a_section[0] == 0x4E) {
      a_section[0] = 0x4F;
      a_section[13] = 0x4F;
    } else if (a_section[0] >= 0x50 && a_section[0] <= 0x5F) {
      a_section[0] = 0x60 | (a_section[0] & 0xF);
      a_section[13] = 0x60 | (a_section[13] & 0xF);
    }
    crc = sectioncrc(a_section, a_section_size - 4);
    a_section[a_section_size - 4] = (crc >> 24) & 0xFF;
    a_section[a_section_size - 3] = (crc >> 16) & 0xFF;
    a_section[a_section_size - 2] = (crc >> 8) & 0xFF;
    a_section[a_section_size - 1] = crc & 0xFF;
    write(STDOUT_FILENO, a_section, a_section_size);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11d1 <main+0x71>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11cc <main+0x6c>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe45(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1338 <main+0x1d8>
jmp    11e4 <main+0x84>
mov    $0x0,%al
call   1040 <Usage@plt>
movl   $0x2,-0x4(%rbp)
jmp    1338 <main+0x1d8>
jmp    11e9 <main+0x89>
mov    -0x24(%rbp),%esi
lea    -0x12(%rbp),%rdi
call   1060 <get_section@plt>
mov    %rax,-0x20(%rbp)
movzwl -0x12(%rbp),%eax
cmp    $0x0,%eax
jle    1211 <main+0xb1>
cmpq   $0x0,-0x20(%rbp)
jne    121d <main+0xbd>
movl   $0x0,-0x4(%rbp)
jmp    1338 <main+0x1d8>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x4e,%eax
jne    1241 <main+0xe1>
mov    -0x20(%rbp),%rax
movb   $0x4f,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x4f,0xd(%rax)
jmp    1292 <main+0x132>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x50,%eax
jl     128d <main+0x12d>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x5f,%eax
jg     128d <main+0x12d>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
and    $0xf,%eax
or     $0x60,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
movzbl 0xd(%rax),%eax
and    $0xf,%eax
or     $0x60,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,0xd(%rax)
jmp    1292 <main+0x132>
mov    -0x20(%rbp),%rdi
movzwl -0x12(%rbp),%eax
sub    $0x4,%eax
movzwl %ax,%esi
call   1080 <sectioncrc@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
shr    $0x18,%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
movzwl -0x12(%rbp),%ecx
sub    $0x4,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
movzwl -0x12(%rbp),%ecx
sub    $0x3,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
movzwl -0x12(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
movzwl -0x12(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
movzwl -0x12(%rbp),%edx
call   1030 <write@plt>
jmp    11e9 <main+0x89>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jl     126d <main+0x10d>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1276 <main+0x116>
mov    %eax,%ebp
lea    0xe(%rsp),%rdi
mov    %eax,%esi
call   1060 <get_section@plt>
xor    %r14d,%r14d
cmpw   $0x0,0xe(%rsp)
je     1295 <main+0x135>
mov    %rax,%rbx
test   %rax,%rax
je     1295 <main+0x135>
mov    0x2e2e(%rip),%r12
lea    0xe(%rsp),%r15
nopw   0x0(%rax,%rax,1)
movzbl (%rbx),%eax
cmp    $0x4e,%al
jne    11e0 <main+0x80>
movb   $0x4f,(%rbx)
movb   $0x4f,0xd(%rbx)
jmp    11fb <main+0x9b>
mov    %eax,%ecx
and    $0xf0,%cl
cmp    $0x50,%cl
jne    11fb <main+0x9b>
and    $0xf,%al
or     $0x60,%al
mov    %al,(%rbx)
movzbl 0xd(%rbx),%eax
and    $0xf,%al
or     $0x60,%al
mov    %al,0xd(%rbx)
movzwl 0xe(%rsp),%eax
add    $0xfffffffc,%eax
movzwl %ax,%esi
mov    %rbx,%rdi
call   1080 <sectioncrc@plt>
mov    %eax,%ecx
shr    $0x18,%ecx
movzwl 0xe(%rsp),%edx
mov    %cl,-0x4(%rdx,%rbx,1)
mov    %eax,%ecx
shr    $0x10,%ecx
movzwl 0xe(%rsp),%edx
mov    %cl,-0x3(%rdx,%rbx,1)
movzwl 0xe(%rsp),%ecx
mov    %ah,-0x2(%rcx,%rbx,1)
movzwl 0xe(%rsp),%ecx
mov    %al,-0x1(%rcx,%rbx,1)
mov    (%r12),%edi
movzwl 0xe(%rsp),%edx
mov    %rbx,%rsi
call   1030 <write@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <get_section@plt>
cmpw   $0x0,0xe(%rsp)
je     1295 <main+0x135>
mov    %rax,%rbx
test   %rax,%rax
jne    11d0 <main+0x70>
jmp    1295 <main+0x135>
xor    %eax,%eax
call   1040 <Usage@plt>
jmp    128f <main+0x12f>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xd76(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Can't find file %s\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i16
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp sge i32 %10, 2
br i1 %11, label %12, label %27
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %12
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %24)
store i32 2, i32* %3
br label %133
26: ; preds = %12
br label %29
27: ; preds = %2
%28 = call i32 (...) @Usage()
store i32 2, i32* %3
br label %133
29: ; preds = %26
br label %30
30: ; preds = %29, %83
%31 = load i32, i32* %8
%32 = call i8* @get_section(i16* %6, i32 %31)
store i8* %32, i8** %7
%33 = load i16, i16* %6
%34 = zext i16 %33 to i32
%35 = icmp sle i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %30
%37 = load i8*, i8** %7
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %40
39: ; preds = %36, %30
store i32 0, i32* %3
br label %133
40: ; preds = %36
%41 = load i8*, i8** %7
%42 = getelementptr inbounds i8, i8* %41, i64 0
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = icmp eq i32 %44, 78
br i1 %45, label %46, label %51
46: ; preds = %40
%47 = load i8*, i8** %7
%48 = getelementptr inbounds i8, i8* %47, i64 0
store i8 79, i8* %48
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i64 13
store i8 79, i8* %50
br label %83
51: ; preds = %40
%52 = load i8*, i8** %7
%53 = getelementptr inbounds i8, i8* %52, i64 0
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = icmp sge i32 %55, 80
br i1 %56, label %57, label %82
57: ; preds = %51
%58 = load i8*, i8** %7
%59 = getelementptr inbounds i8, i8* %58, i64 0
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = icmp sle i32 %61, 95
br i1 %62, label %63, label %82
63: ; preds = %57
%64 = load i8*, i8** %7
%65 = getelementptr inbounds i8, i8* %64, i64 0
%66 = load i8, i8* %65
%67 = zext i8 %66 to i32
%68 = and i32 %67, 15
%69 = or i32 96, %68
%70 = trunc i32 %69 to i8
%71 = load i8*, i8** %7
%72 = getelementptr inbounds i8, i8* %71, i64 0
store i8 %70, i8* %72
%73 = load i8*, i8** %7
%74 = getelementptr inbounds i8, i8* %73, i64 13
%75 = load i8, i8* %74
%76 = zext i8 %75 to i32
%77 = and i32 %76, 15
%78 = or i32 96, %77
%79 = trunc i32 %78 to i8
%80 = load i8*, i8** %7
%81 = getelementptr inbounds i8, i8* %80, i64 13
store i8 %79, i8* %81
br label %82
82: ; preds = %63, %57, %51
br label %83
83: ; preds = %82, %46
%84 = load i8*, i8** %7
%85 = load i16, i16* %6
%86 = zext i16 %85 to i32
%87 = sub nsw i32 %86, 4
%88 = trunc i32 %87 to i16
%89 = call i32 @sectioncrc(i8* %84, i16 zeroext %88)
store i32 %89, i32* %9
%90 = load i32, i32* %9
%91 = lshr i32 %90, 24
%92 = and i32 %91, 255
%93 = trunc i32 %92 to i8
%94 = load i8*, i8** %7
%95 = load i16, i16* %6
%96 = zext i16 %95 to i32
%97 = sub nsw i32 %96, 4
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds i8, i8* %94, i64 %98
store i8 %93, i8* %99
%100 = load i32, i32* %9
%101 = lshr i32 %100, 16
%102 = and i32 %101, 255
%103 = trunc i32 %102 to i8
%104 = load i8*, i8** %7
%105 = load i16, i16* %6
%106 = zext i16 %105 to i32
%107 = sub nsw i32 %106, 3
%108 = sext i32 %107 to i64
%109 = getelementptr inbounds i8, i8* %104, i64 %108
store i8 %103, i8* %109
%110 = load i32, i32* %9
%111 = lshr i32 %110, 8
%112 = and i32 %111, 255
%113 = trunc i32 %112 to i8
%114 = load i8*, i8** %7
%115 = load i16, i16* %6
%116 = zext i16 %115 to i32
%117 = sub nsw i32 %116, 2
%118 = sext i32 %117 to i64
%119 = getelementptr inbounds i8, i8* %114, i64 %118
store i8 %113, i8* %119
%120 = load i32, i32* %9
%121 = and i32 %120, 255
%122 = trunc i32 %121 to i8
%123 = load i8*, i8** %7
%124 = load i16, i16* %6
%125 = zext i16 %124 to i32
%126 = sub nsw i32 %125, 1
%127 = sext i32 %126 to i64
%128 = getelementptr inbounds i8, i8* %123, i64 %127
store i8 %122, i8* %128
%129 = load i32, i32* @STDOUT_FILENO
%130 = load i8*, i8** %7
%131 = load i16, i16* %6
%132 = call i32 @write(i32 %129, i8* %130, i16 zeroext %131)
br label %30
133: ; preds = %39, %27, %20
%134 = load i32, i32* %3
ret i32 %134
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Can't find file %s\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = bitcast i16* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %22
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %6
%13 = call i8* @get_section(i16* %3, i32 %10)
%14 = load i16, i16* %3
%15 = icmp eq i16 %14, 0
%16 = icmp eq i8* %13, null
%17 = select i1 %15, i1 true, i1 %16
br i1 %17, label %75, label %24
18: ; preds = %6
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %7
%21 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %75
22: ; preds = %2
%23 = tail call i32 (...) @Usage()
br label %75
24: ; preds = %12, %40
%25 = phi i8* [ %70, %40 ], [ %13, %12 ]
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 78
br i1 %27, label %28, label %30
28: ; preds = %24
store i8 79, i8* %25
%29 = getelementptr inbounds i8, i8* %25, i64 13
store i8 79, i8* %29
br label %40
30: ; preds = %24
%31 = and i8 %26, -16
%32 = icmp eq i8 %31, 80
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = and i8 %26, 15
%35 = or i8 %34, 96
store i8 %35, i8* %25
%36 = getelementptr inbounds i8, i8* %25, i64 13
%37 = load i8, i8* %36
%38 = and i8 %37, 15
%39 = or i8 %38, 96
store i8 %39, i8* %36
br label %40
40: ; preds = %30, %33, %28
%41 = load i16, i16* %3
%42 = add i16 %41, -4
%43 = call i32 @sectioncrc(i8* %25, i16 zeroext %42)
%44 = lshr i32 %43, 24
%45 = trunc i32 %44 to i8
%46 = load i16, i16* %3
%47 = zext i16 %46 to i64
%48 = add nsw i64 %47, -4
%49 = getelementptr inbounds i8, i8* %25, i64 %48
store i8 %45, i8* %49
%50 = lshr i32 %43, 16
%51 = trunc i32 %50 to i8
%52 = load i16, i16* %3
%53 = zext i16 %52 to i64
%54 = add nsw i64 %53, -3
%55 = getelementptr inbounds i8, i8* %25, i64 %54
store i8 %51, i8* %55
%56 = lshr i32 %43, 8
%57 = trunc i32 %56 to i8
%58 = load i16, i16* %3
%59 = zext i16 %58 to i64
%60 = add nsw i64 %59, -2
%61 = getelementptr inbounds i8, i8* %25, i64 %60
store i8 %57, i8* %61
%62 = trunc i32 %43 to i8
%63 = load i16, i16* %3
%64 = zext i16 %63 to i64
%65 = add nsw i64 %64, -1
%66 = getelementptr inbounds i8, i8* %25, i64 %65
store i8 %62, i8* %66
%67 = load i32, i32* @STDOUT_FILENO
%68 = load i16, i16* %3
%69 = call i32 @write(i32 %67, i8* %25, i16 zeroext %68)
%70 = call i8* @get_section(i16* %3, i32 %10)
%71 = load i16, i16* %3
%72 = icmp eq i16 %71, 0
%73 = icmp eq i8* %70, null
%74 = select i1 %72, i1 true, i1 %73
br i1 %74, label %75, label %24
75: ; preds = %40, %12, %22, %18
%76 = phi i32 [ 2, %18 ], [ 2, %22 ], [ 0, %12 ], [ 0, %40 ]
ret i32 %76
}