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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int htonl(unsigned int);
int htons(unsigned short);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
int read(int, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, unsigned int,
                            unsigned int);
int write(int, unsigned char *, int);
int main(int argc, char *argv[]) {
  int in_fd = -1;
  int out_fd = -1;
  int state = -1;
  unsigned int i = 0;
  unsigned int length = 0;
  unsigned int loading_addr = 0;
  unsigned int generic32 = 0;
  unsigned int section = 0;
  unsigned short generic16 = 0;
  unsigned char generic8 = 0;
  char name[33] = {0};
  if (argc != 2) {
    printf("Usage:\n\textract_sections <file_name>\n");
    exit(EXIT_FAILURE);
  }
  in_fd = open(argv[1], O_RDONLY);
  if (in_fd == -1) {
    printf("error opening %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  state = read(in_fd, &generic32, 4);
  if (state == -1) {
    printf("file corrupted: %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  printf("length of %s in bytes [%x]\n", argv[1], htonl(generic32));
  state = read(in_fd, &name[0], 32);
  if (state == -1) {
    printf("file corrupted: %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  printf("name and version string [%s]\n", name);
  state = read(in_fd, &generic32, 4);
  if (state == -1) {
    printf("file corrupted: %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  printf("entry address [%x]\n", htonl(generic32));
  for (;;) {
    state = read(in_fd, &generic32, 4);
    if (state == -1) {
      printf("file corrupted: %s\n", argv[1]);
      exit(EXIT_FAILURE);
    }
    loading_addr = htonl(generic32);
    state = read(in_fd, &generic32, 4);
    if (state == -1) {
      printf("file corrupted: %s\n", argv[1]);
      exit(EXIT_FAILURE);
    }
    length = htonl(generic32);
    if (length != 0) {
      section++;
      printf("\tdata/code section[%d]: loading address [%x]\n", section,
             loading_addr);
      printf("\tdata/code section[%d]: length[%x]\n", section, length);
      memset(&name[0], 0, sizeof(name));
      snprintf(&name[0], sizeof(name), "%s_section_%d_%x", argv[1], section,
               loading_addr);
      out_fd = open(name, O_WRONLY | O_CREAT, S_IRWXU);
      if (out_fd == -1) {
        printf("error opening %s\n", argv[2]);
        exit(EXIT_FAILURE);
      }
    } else {
      printf("we reached the end of the image\n");
      break;
    }
    for (i = 0; i < length; i++) {
      state = read(in_fd, &generic8, 1);
      if (state == -1) {
        printf("file corrupted: %s\n", argv[1]);
        exit(EXIT_FAILURE);
      }
      if (state == 0) {
        printf("end of %s reached\n", argv[1]);
        break;
      }
      state = write(out_fd, &generic8, 1);
      if (state == -1) {
        printf("error writing: %s\n", name);
        exit(EXIT_FAILURE);
      }
    }
    printf("\twrote file: %s\n", name);
    close(out_fd);
    state = read(in_fd, &generic16, 2);
    if (state == -1) {
      printf("file corrupted: %s\n", argv[1]);
      exit(EXIT_FAILURE);
    }
    printf("\tcrc16     section[%d]: checksum[%x]\n", section,
           htons(generic16));
  }
  close(in_fd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movw   $0x0,-0x32(%rbp)
movb   $0x0,-0x33(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x21,%edx
call   1080 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
je     122e <main+0x8e>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1277 <main+0xd7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   10a0 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12bb <main+0x11b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x68(%rbp)
mov    -0x2c(%rbp),%edi
call   1070 <htonl@plt>
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x20,%edx
mov    $0x0,%al
call   10a0 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1327 <main+0x187>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x60(%rbp),%rsi
lea    0xd36(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   10a0 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    137d <main+0x1dd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xcd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c52(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1070 <htonl@plt>
mov    %eax,%esi
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   10a0 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    13d9 <main+0x239>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xc74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2bf6(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1070 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   10a0 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1428 <main+0x288>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xc25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2ba7(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1070 <htonl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     14ff <main+0x35f>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xc47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xc61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x21,%edx
call   1080 <memset@plt>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x30(%rbp),%r8d
mov    -0x28(%rbp),%r9d
mov    $0x21,%esi
lea    0xc4e(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2b02(%rip),%rax
mov    (%rax),%esi
mov    0x2b41(%rip),%rax
or     (%rax),%esi
mov    0x2b20(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    14fa <main+0x35a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xb41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2ad5(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    1512 <main+0x372>
lea    0xbf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1664 <main+0x4c4>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jae    15df <main+0x43f>
mov    -0x14(%rbp),%edi
lea    -0x33(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   10a0 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1569 <main+0x3c9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xae4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2a66(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    158e <main+0x3ee>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xb9c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    15df <main+0x43f>
mov    -0x18(%rbp),%edi
lea    -0x33(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    15cc <main+0x42c>
lea    -0x60(%rbp),%rsi
lea    0xb7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2a03(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    15d1 <main+0x431>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1519 <main+0x379>
lea    -0x60(%rbp),%rsi
lea    0xb5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    -0x14(%rbp),%edi
lea    -0x32(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   10a0 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    163d <main+0x49d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xa10(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2992(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x6c(%rbp)
movzwl -0x32(%rbp),%edi
call   1040 <htons@plt>
mov    -0x6c(%rbp),%esi
mov    %eax,%edx
lea    0xafd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1395 <main+0x1f5>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    0x294d(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
movl   $0x0,0x4(%rsp)
movw   $0x0,0xa(%rsp)
movb   $0x0,0x9(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movb   $0x0,0x40(%rsp)
cmp    $0x2,%edi
jne    1215 <main+0x65>
mov    %rsi,%r13
mov    0x8(%rsi),%rdi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
jne    1226 <main+0x76>
mov    0x8(%r13),%rsi
lea    0xdf0(%rip),%rdi
jmp    12f2 <main+0x142>
lea    0xf38(%rip),%rdi
call   1030 <puts@plt>
jmp    12f9 <main+0x149>
mov    %eax,%r15d
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   10b0 <read@plt>
mov    0x8(%r13),%rbp
cmp    $0xffffffff,%eax
jne    1254 <main+0xa4>
lea    0xdc6(%rip),%rdi
mov    %rbp,%rsi
jmp    12f2 <main+0x142>
mov    0x4(%rsp),%edi
call   1080 <htonl@plt>
lea    0xdc2(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x20(%rsp),%rsi
mov    %r15d,%edi
mov    $0x20,%edx
xor    %eax,%eax
call   10b0 <read@plt>
cmp    $0xffffffff,%eax
je     12e7 <main+0x137>
lea    0xdb2(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x4(%rsp),%rsi
mov    %r15d,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   10b0 <read@plt>
cmp    $0xffffffff,%eax
je     12e7 <main+0x137>
mov    0x4(%rsp),%edi
call   1080 <htonl@plt>
lea    0xd9b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x4(%rsp),%rsi
mov    %r15d,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   10b0 <read@plt>
cmp    $0xffffffff,%eax
jne    1307 <main+0x157>
mov    0x8(%r13),%rsi
lea    0xd20(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0x4(%rsp),%rbp
lea    0x9(%rsp),%r12
movl   $0x0,0xc(%rsp)
mov    %r13,0x10(%rsp)
xchg   %ax,%ax
mov    0x4(%rsp),%edi
call   1080 <htonl@plt>
mov    %eax,%ebx
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   10b0 <read@plt>
cmp    $0xffffffff,%eax
je     12e7 <main+0x137>
mov    0x4(%rsp),%edi
call   1080 <htonl@plt>
test   %eax,%eax
je     14f2 <main+0x342>
mov    %eax,%r14d
mov    0xc(%rsp),%ebp
add    $0x1,%ebp
lea    0xd10(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,0x1c(%rsp)
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd28(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15d,%ebx
mov    %r13,%r15
lea    0x20(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x21,%edx
call   1090 <memset@plt>
mov    0x8(%r15),%rcx
mov    %ebx,%r15d
mov    %r13,%rdi
mov    $0x21,%esi
lea    0xd10(%rip),%rdx
mov    %ebp,0xc(%rsp)
mov    %ebp,%r8d
mov    0x1c(%rsp),%r9d
call   1070 <snprintf@plt>
mov    0x2c2a(%rip),%rax
mov    (%rax),%esi
mov    0x2bd9(%rip),%rax
or     (%rax),%esi
mov    0x2c00(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %eax,%eax
call   10c0 <open@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
je     1514 <main+0x364>
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   10b0 <read@plt>
test   %eax,%eax
je     1440 <main+0x290>
cmp    $0xffffffff,%eax
je     14d3 <main+0x323>
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     14e1 <main+0x331>
add    $0xffffffff,%r14d
jne    1400 <main+0x250>
jmp    1457 <main+0x2a7>
mov    0x10(%rsp),%rax
mov    0x8(%rax),%rsi
lea    0xc87(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xc9f(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13d,%edi
call   10a0 <close@plt>
mov    %r15d,%edi
lea    0xa(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   10b0 <read@plt>
cmp    $0xffffffff,%eax
je     14d3 <main+0x323>
movzwl 0xa(%rsp),%edi
call   1050 <htons@plt>
lea    0xc72(%rip),%rdi
mov    0xc(%rsp),%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15d,%edi
lea    0x4(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   10b0 <read@plt>
cmp    $0xffffffff,%eax
mov    0x10(%rsp),%r13
jne    1320 <main+0x170>
jmp    12e7 <main+0x137>
mov    0x10(%rsp),%rax
mov    0x8(%rax),%rsi
jmp    12eb <main+0x13b>
lea    0xc02(%rip),%rdi
lea    0x20(%rsp),%rsi
jmp    12f2 <main+0x142>
lea    0xc3b(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
call   10a0 <close@plt>
mov    0x2ab3(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x10(%rsp),%rax
mov    0x10(%rax),%rsi
jmp    1209 <main+0x59>

LLVM IR (-O0):
@.str = [38 x i8] c"Usage:\0A\09extract_sections <file_name>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [18 x i8] c"error opening %s\0A\00"
@.str.2 = [20 x i8] c"file corrupted: %s\0A\00"
@.str.3 = [28 x i8] c"length of %s in bytes [%x]\0A\00"
@.str.4 = [30 x i8] c"name and version string [%s]\0A\00"
@.str.5 = [20 x i8] c"entry address [%x]\0A\00"
@.str.6 = [46 x i8] c"\09data/code section[%d]: loading address [%x]\0A\00"
@.str.7 = [36 x i8] c"\09data/code section[%d]: length[%x]\0A\00"
@.str.8 = [17 x i8] c"%s_section_%d_%x\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@.str.9 = [33 x i8] c"we reached the end of the image\0A\00"
@.str.10 = [19 x i8] c"end of %s reached\0A\00"
@.str.11 = [19 x i8] c"error writing: %s\0A\00"
@.str.12 = [17 x i8] c"\09wrote file: %s\0A\00"
@.str.13 = [38 x i8] c"\09crc16 section[%d]: checksum[%x]\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i16
%15 = alloca i8
%16 = alloca [33 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 -1, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i16 0, i16* %14
store i8 0, i8* %15
%17 = bitcast [33 x i8]* %16 to i8*
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 2
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i32, i32* @O_RDONLY
%29 = call i32 (i8*, i32, ...) @open(i8* %27, i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %39
32: ; preds = %24
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %24
%40 = load i32, i32* %6
%41 = call i32 (i32, ...) @read(i32 %40, i32* %12, i32 4)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %51
44: ; preds = %39
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %47)
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %39
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 1
%54 = load i8*, i8** %53
%55 = load i32, i32* %12
%56 = call i32 @htonl(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %54, i32 %56)
%58 = load i32, i32* %6
%59 = getelementptr inbounds [33 x i8], [33 x i8]* %16, i64 0, i64 0
%60 = call i32 (i32, ...) @read(i32 %58, i8* %59, i32 32)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %70
63: ; preds = %51
%64 = load i8**, i8*** %5
%65 = getelementptr inbounds i8*, i8** %64, i64 1
%66 = load i8*, i8** %65
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %66)
%68 = load i32, i32* @EXIT_FAILURE
%69 = call i32 @exit(i32 %68)
unreachable
70: ; preds = %51
%71 = getelementptr inbounds [33 x i8], [33 x i8]* %16, i64 0, i64 0
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %71)
%73 = load i32, i32* %6
%74 = call i32 (i32, ...) @read(i32 %73, i32* %12, i32 4)
store i32 %74, i32* %8
%75 = load i32, i32* %8
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %84
77: ; preds = %70
%78 = load i8**, i8*** %5
%79 = getelementptr inbounds i8*, i8** %78, i64 1
%80 = load i8*, i8** %79
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %80)
%82 = load i32, i32* @EXIT_FAILURE
%83 = call i32 @exit(i32 %82)
unreachable
84: ; preds = %70
%85 = load i32, i32* %12
%86 = call i32 @htonl(i32 %85)
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %86)
br label %88
88: ; preds = %210, %84
%89 = load i32, i32* %6
%90 = call i32 (i32, ...) @read(i32 %89, i32* %12, i32 4)
store i32 %90, i32* %8
%91 = load i32, i32* %8
%92 = icmp eq i32 %91, -1
br i1 %92, label %93, label %100
93: ; preds = %88
%94 = load i8**, i8*** %5
%95 = getelementptr inbounds i8*, i8** %94, i64 1
%96 = load i8*, i8** %95
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %96)
%98 = load i32, i32* @EXIT_FAILURE
%99 = call i32 @exit(i32 %98)
unreachable
100: ; preds = %88
%101 = load i32, i32* %12
%102 = call i32 @htonl(i32 %101)
store i32 %102, i32* %11
%103 = load i32, i32* %6
%104 = call i32 (i32, ...) @read(i32 %103, i32* %12, i32 4)
store i32 %104, i32* %8
%105 = load i32, i32* %8
%106 = icmp eq i32 %105, -1
br i1 %106, label %107, label %114
107: ; preds = %100
%108 = load i8**, i8*** %5
%109 = getelementptr inbounds i8*, i8** %108, i64 1
%110 = load i8*, i8** %109
%111 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %110)
%112 = load i32, i32* @EXIT_FAILURE
%113 = call i32 @exit(i32 %112)
unreachable
114: ; preds = %100
%115 = load i32, i32* %12
%116 = call i32 @htonl(i32 %115)
store i32 %116, i32* %10
%117 = load i32, i32* %10
%118 = icmp ne i32 %117, 0
br i1 %118, label %119, label %153
119: ; preds = %114
%120 = load i32, i32* %13
%121 = add i32 %120, 1
store i32 %121, i32* %13
%122 = load i32, i32* %13
%123 = load i32, i32* %11
%124 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0), i32 %122, i32 %123)
%125 = load i32, i32* %13
%126 = load i32, i32* %10
%127 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i32 %125, i32 %126)
%128 = getelementptr inbounds [33 x i8], [33 x i8]* %16, i64 0, i64 0
%129 = call i32 @memset(i8* %128, i32 0, i32 33)
%130 = getelementptr inbounds [33 x i8], [33 x i8]* %16, i64 0, i64 0
%131 = load i8**, i8*** %5
%132 = getelementptr inbounds i8*, i8** %131, i64 1
%133 = load i8*, i8** %132
%134 = load i32, i32* %13
%135 = load i32, i32* %11
%136 = call i32 @snprintf(i8* %130, i32 33, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i8* %133, i32 %134, i32 %135)
%137 = getelementptr inbounds [33 x i8], [33 x i8]* %16, i64 0, i64 0
%138 = load i32, i32* @O_WRONLY
%139 = load i32, i32* @O_CREAT
%140 = or i32 %138, %139
%141 = load i32, i32* @S_IRWXU
%142 = call i32 (i8*, i32, ...) @open(i8* %137, i32 %140, i32 %141)
store i32 %142, i32* %7
%143 = load i32, i32* %7
%144 = icmp eq i32 %143, -1
br i1 %144, label %145, label %152
145: ; preds = %119
%146 = load i8**, i8*** %5
%147 = getelementptr inbounds i8*, i8** %146, i64 2
%148 = load i8*, i8** %147
%149 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %148)
%150 = load i32, i32* @EXIT_FAILURE
%151 = call i32 @exit(i32 %150)
unreachable
152: ; preds = %119
br label %155
153: ; preds = %114
%154 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0))
br label %215
155: ; preds = %152
store i32 0, i32* %9
br label %156
156: ; preds = %191, %155
%157 = load i32, i32* %9
%158 = load i32, i32* %10
%159 = icmp ult i32 %157, %158
br i1 %159, label %160, label %194
160: ; preds = %156
%161 = load i32, i32* %6
%162 = call i32 (i32, ...) @read(i32 %161, i8* %15, i32 1)
store i32 %162, i32* %8
%163 = load i32, i32* %8
%164 = icmp eq i32 %163, -1
br i1 %164, label %165, label %172
165: ; preds = %160
%166 = load i8**, i8*** %5
%167 = getelementptr inbounds i8*, i8** %166, i64 1
%168 = load i8*, i8** %167
%169 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %168)
%170 = load i32, i32* @EXIT_FAILURE
%171 = call i32 @exit(i32 %170)
unreachable
172: ; preds = %160
%173 = load i32, i32* %8
%174 = icmp eq i32 %173, 0
br i1 %174, label %175, label %180
175: ; preds = %172
%176 = load i8**, i8*** %5
%177 = getelementptr inbounds i8*, i8** %176, i64 1
%178 = load i8*, i8** %177
%179 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0), i8* %178)
br label %194
180: ; preds = %172
%181 = load i32, i32* %7
%182 = call i32 @write(i32 %181, i8* %15, i32 1)
store i32 %182, i32* %8
%183 = load i32, i32* %8
%184 = icmp eq i32 %183, -1
br i1 %184, label %185, label %190
185: ; preds = %180
%186 = getelementptr inbounds [33 x i8], [33 x i8]* %16, i64 0, i64 0
%187 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.11, i64 0, i64 0), i8* %186)
%188 = load i32, i32* @EXIT_FAILURE
%189 = call i32 @exit(i32 %188)
unreachable
190: ; preds = %180
br label %191
191: ; preds = %190
%192 = load i32, i32* %9
%193 = add i32 %192, 1
store i32 %193, i32* %9
br label %156
194: ; preds = %175, %156
%195 = getelementptr inbounds [33 x i8], [33 x i8]* %16, i64 0, i64 0
%196 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0), i8* %195)
%197 = load i32, i32* %7
%198 = call i32 @close(i32 %197)
%199 = load i32, i32* %6
%200 = call i32 (i32, ...) @read(i32 %199, i16* %14, i32 2)
store i32 %200, i32* %8
%201 = load i32, i32* %8
%202 = icmp eq i32 %201, -1
br i1 %202, label %203, label %210
203: ; preds = %194
%204 = load i8**, i8*** %5
%205 = getelementptr inbounds i8*, i8** %204, i64 1
%206 = load i8*, i8** %205
%207 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %206)
%208 = load i32, i32* @EXIT_FAILURE
%209 = call i32 @exit(i32 %208)
unreachable
210: ; preds = %194
%211 = load i32, i32* %13
%212 = load i16, i16* %14
%213 = call i32 @htons(i16 zeroext %212)
%214 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.13, i64 0, i64 0), i32 %211, i32 %213)
br label %88
215: ; preds = %153
%216 = load i32, i32* %6
%217 = call i32 @close(i32 %216)
%218 = load i32, i32* @EXIT_SUCCESS
%219 = call i32 @exit(i32 %218)
unreachable
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [18 x i8] c"error opening %s\0A\00"
@.str.2 = [20 x i8] c"file corrupted: %s\0A\00"
@.str.3 = [28 x i8] c"length of %s in bytes [%x]\0A\00"
@.str.4 = [30 x i8] c"name and version string [%s]\0A\00"
@.str.5 = [20 x i8] c"entry address [%x]\0A\00"
@.str.6 = [46 x i8] c"\09data/code section[%d]: loading address [%x]\0A\00"
@.str.7 = [36 x i8] c"\09data/code section[%d]: length[%x]\0A\00"
@.str.8 = [17 x i8] c"%s_section_%d_%x\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@.str.10 = [19 x i8] c"end of %s reached\0A\00"
@.str.11 = [19 x i8] c"error writing: %s\0A\00"
@.str.12 = [17 x i8] c"\09wrote file: %s\0A\00"
@.str.13 = [38 x i8] c"\09crc16 section[%d]: checksum[%x]\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [32 x i8] c"we reached the end of the image\00"
@str.14 = [37 x i8] c"Usage:\0A\09extract_sections <file_name>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i16
%5 = alloca i8
%6 = alloca [33 x i8]
%7 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%8 = bitcast i16* %4 to i8*
store i16 0, i16* %4
store i8 0, i8* %5
%9 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%10 = icmp eq i32 %0, 2
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.14, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 (i8*, i32, ...) @open(i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %15
%22 = load i8*, i8** %16
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %15
%27 = call i32 (i32, ...) @read(i32 %19, i32* %3, i32 4)
%28 = icmp eq i32 %27, -1
%29 = load i8*, i8** %16
br i1 %28, label %30, label %34
30: ; preds = %26
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %26
%35 = load i32, i32* %3
%36 = call i32 @htonl(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %29, i32 %36)
%38 = call i32 (i32, ...) @read(i32 %19, i8* %9, i32 32)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %34
%41 = load i8*, i8** %16
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %34
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %9)
%47 = call i32 (i32, ...) @read(i32 %19, i32* %3, i32 4)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %54
49: ; preds = %45
%50 = load i8*, i8** %16
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %45
%55 = load i32, i32* %3
%56 = call i32 @htonl(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %56)
%58 = call i32 (i32, ...) @read(i32 %19, i32* %3, i32 4)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %65
60: ; preds = %135, %54
%61 = load i8*, i8** %16
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %61)
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %54, %135
%66 = phi i32 [ %81, %135 ], [ 0, %54 ]
%67 = load i32, i32* %3
%68 = call i32 @htonl(i32 %67)
%69 = call i32 (i32, ...) @read(i32 %19, i32* %3, i32 4)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %76
71: ; preds = %65
%72 = load i8*, i8** %16
%73 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %72)
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %65
%77 = load i32, i32* %3
%78 = call i32 @htonl(i32 %77)
%79 = icmp eq i32 %78, 0
br i1 %79, label %99, label %80
80: ; preds = %76
%81 = add i32 %66, 1
%82 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0), i32 %81, i32 %68)
%83 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i32 %81, i32 %78)
%84 = call i32 @memset(i8* %9, i32 0, i32 33)
%85 = load i8*, i8** %16
%86 = call i32 @snprintf(i8* %9, i32 33, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i8* %85, i32 %81, i32 %68)
%87 = load i32, i32* @O_WRONLY
%88 = load i32, i32* @O_CREAT
%89 = or i32 %88, %87
%90 = load i32, i32* @S_IRWXU
%91 = call i32 (i8*, i32, ...) @open(i8* %9, i32 %89, i32 %90)
%92 = icmp eq i32 %91, -1
br i1 %92, label %93, label %106
93: ; preds = %80
%94 = getelementptr inbounds i8*, i8** %1, i64 2
%95 = load i8*, i8** %94
%96 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %95)
%97 = load i32, i32* @EXIT_FAILURE
%98 = call i32 @exit(i32 %97)
unreachable
99: ; preds = %76
%100 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%101 = call i32 @close(i32 %19)
%102 = load i32, i32* @EXIT_SUCCESS
%103 = call i32 @exit(i32 %102)
unreachable
104: ; preds = %117
%105 = icmp eq i32 %120, %78
br i1 %105, label %125, label %106
106: ; preds = %80, %104
%107 = phi i32 [ %120, %104 ], [ 0, %80 ]
%108 = call i32 (i32, ...) @read(i32 %19, i8* %5, i32 1)
switch i32 %108, label %117 [
i32 -1, label %109
i32 0, label %114
]
109: ; preds = %106
%110 = load i8*, i8** %16
%111 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %110)
%112 = load i32, i32* @EXIT_FAILURE
%113 = call i32 @exit(i32 %112)
unreachable
114: ; preds = %106
%115 = load i8*, i8** %16
%116 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0), i8* %115)
br label %125
117: ; preds = %106
%118 = call i32 @write(i32 %91, i8* %5, i32 1)
%119 = icmp eq i32 %118, -1
%120 = add nuw i32 %107, 1
br i1 %119, label %121, label %104
121: ; preds = %117
%122 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.11, i64 0, i64 0), i8* %9)
%123 = load i32, i32* @EXIT_FAILURE
%124 = call i32 @exit(i32 %123)
unreachable
125: ; preds = %104, %114
%126 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0), i8* %9)
%127 = call i32 @close(i32 %91)
%128 = call i32 (i32, ...) @read(i32 %19, i16* %4, i32 2)
%129 = icmp eq i32 %128, -1
br i1 %129, label %130, label %135
130: ; preds = %125
%131 = load i8*, i8** %16
%132 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %131)
%133 = load i32, i32* @EXIT_FAILURE
%134 = call i32 @exit(i32 %133)
unreachable
135: ; preds = %125
%136 = load i16, i16* %4
%137 = call i32 @htons(i16 zeroext %136)
%138 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.13, i64 0, i64 0), i32 %81, i32 %137)
%139 = call i32 (i32, ...) @read(i32 %19, i32* %3, i32 4)
%140 = icmp eq i32 %139, -1
br i1 %140, label %60, label %65
}