#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 */
struct flash_partition_entry {
  int dummy;
};
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ add_flash_partition(struct flash_partition_entry *, size_t,
                                       char *, unsigned long, unsigned long);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
char *memchr(char *, int, int);
scalar_t__ memcmp(char *, char const *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
int read_partition_table(FILE *file, long offset,
                         struct flash_partition_entry *entries,
                         size_t max_entries, int type) {
  char buf[2048];
  char *ptr, *end;
  const char *parthdr = NULL;
  const char *fwuphdr = "fwup-ptn";
  const char *flashhdr = "partition";
  switch (type) {
  case 0:
    parthdr = fwuphdr;
    break;
  case 1:
    parthdr = flashhdr;
    break;
  default:
    do {
      fprintf(stderr,
              "Invalid partition table"
              ": %s\n",
              strerror(0));
      if (1)
        exit(1);
    } while (0);
  }
  if (fseek(file, offset, SEEK_SET) < 0)
    do {
      fprintf(stderr,
              "Can not seek in the firmware"
              ": %s\n",
              strerror(errno));
      if (1)
        exit(1);
    } while (0);
  if (fread(buf, 1, 2048, file) < 0)
    do {
      fprintf(stderr,
              "Can not read fwup-ptn from the firmware"
              ": %s\n",
              strerror(errno));
      if (1)
        exit(1);
    } while (0);
  buf[2047] = '\0';
  if (memcmp(buf, parthdr, strlen(parthdr)) != 0) {
    fprintf(stderr, "DEBUG: can not find fwuphdr\n");
    return 1;
  }
  ptr = buf;
  end = buf + sizeof(buf);
  while ((ptr + strlen(parthdr)) < end &&
         memcmp(ptr, parthdr, strlen(parthdr)) == 0) {
    char *end_part;
    char *end_element;
    char name[32] = {0};
    int name_len = 0;
    unsigned long base = 0;
    unsigned long size = 0;
    end_part = memchr(ptr, '\n', (end - ptr));
    if (end_part == NULL) {
      break;
    }
    for (int i = 0; i <= 4; i++) {
      if (end_part <= ptr)
        break;
      end_element = memchr(ptr, 0x20, (end_part - ptr));
      if (end_element == NULL) {
        do {
          fprintf(stderr,
                  "Ignoring the rest of the partition entries."
                  ": %s\n",
                  strerror(errno));
          if (1)
            exit(1);
        } while (0);
        break;
      }
      switch (i) {
      case 0:
        ptr = end_element + 1;
        continue;
      case 1:
        name_len = (end_element - ptr) > 31 ? 31 : (end_element - ptr);
        strncpy(name, ptr, name_len);
        name[name_len] = '\0';
        ptr = end_element + 1;
        continue;
      case 2:
        ptr = end_element + 1;
        continue;
      case 3:
        base = strtoul(ptr, NULL, 16);
        ptr = end_element + 1;
        continue;
      case 4:
        ptr = end_element + 1;
        size = strtoul(ptr, NULL, 16);
        ptr = end_part + 1;
        add_flash_partition(entries, max_entries, name, base, size);
        continue;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_partition_table>:
push   %rbp
mov    %rsp,%rbp
sub    $0x910,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
movq   $0x0,-0x848(%rbp)
lea    0xe23(%rip),%rax
mov    %rax,-0x850(%rbp)
lea    0xe1e(%rip),%rax
mov    %rax,-0x858(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x8b0(%rbp)
test   %eax,%eax
je     1230 <read_partition_table+0x70>
jmp    121c <read_partition_table+0x5c>
mov    -0x8b0(%rbp),%eax
sub    $0x1,%eax
je     1243 <read_partition_table+0x83>
jmp    1256 <read_partition_table+0x96>
mov    -0x850(%rbp),%rax
mov    %rax,-0x848(%rbp)
jmp    1292 <read_partition_table+0xd2>
mov    -0x858(%rbp),%rax
mov    %rax,-0x848(%rbp)
jmp    1292 <read_partition_table+0xd2>
jmp    125b <read_partition_table+0x9b>
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8b4(%rbp)
xor    %edi,%edi
call   10e0 <strerror@plt>
mov    -0x8b4(%rbp),%edi
mov    %rax,%rdx
lea    0xda6(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
call   10b0 <fseek@plt>
cmp    $0x0,%rax
jge    12f5 <read_partition_table+0x135>
jmp    12b7 <read_partition_table+0xf7>
mov    0x2d22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8b8(%rbp)
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x8b8(%rbp),%edi
mov    %rax,%rdx
lea    0xd60(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x830(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x800,%edx
call   1040 <fread@plt>
cmp    $0x0,%rax
jge    135c <read_partition_table+0x19c>
jmp    131e <read_partition_table+0x15e>
mov    0x2cbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8bc(%rbp)
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x8bc(%rbp),%edi
mov    %rax,%rdx
lea    0xd1b(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movb   $0x0,-0x31(%rbp)
lea    -0x830(%rbp),%rax
mov    %rax,-0x8d0(%rbp)
mov    -0x848(%rbp),%rax
mov    %rax,-0x8c8(%rbp)
mov    -0x848(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x8d0(%rbp),%rdi
mov    -0x8c8(%rbp),%rsi
mov    %eax,%edx
call   1090 <memcmp@plt>
cmp    $0x0,%rax
je     13ca <read_partition_table+0x20a>
mov    0x2c32(%rip),%rax
mov    (%rax),%edi
lea    0xcdc(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    174c <read_partition_table+0x58c>
lea    -0x830(%rbp),%rax
mov    %rax,-0x838(%rbp)
lea    -0x830(%rbp),%rax
add    $0x800,%rax
mov    %rax,-0x840(%rbp)
mov    -0x838(%rbp),%rax
mov    %rax,-0x8e0(%rbp)
mov    -0x848(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x8e0(%rbp),%rcx
cltq
add    %rax,%rcx
xor    %eax,%eax
cmp    -0x840(%rbp),%rcx
mov    %al,-0x8d1(%rbp)
jae    1471 <read_partition_table+0x2b1>
mov    -0x838(%rbp),%rax
mov    %rax,-0x8f0(%rbp)
mov    -0x848(%rbp),%rax
mov    %rax,-0x8e8(%rbp)
mov    -0x848(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x8f0(%rbp),%rdi
mov    -0x8e8(%rbp),%rsi
mov    %eax,%edx
call   1090 <memcmp@plt>
cmp    $0x0,%rax
sete   %al
mov    %al,-0x8d1(%rbp)
mov    -0x8d1(%rbp),%al
test   $0x1,%al
jne    1484 <read_partition_table+0x2c4>
jmp    1745 <read_partition_table+0x585>
lea    -0x890(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
movl   $0x0,-0x894(%rbp)
movq   $0x0,-0x8a0(%rbp)
movq   $0x0,-0x8a8(%rbp)
mov    -0x838(%rbp),%rdi
mov    -0x840(%rbp),%rax
mov    -0x838(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
mov    $0xa,%esi
call   1080 <memchr@plt>
mov    %rax,-0x860(%rbp)
cmpq   $0x0,-0x860(%rbp)
jne    14f5 <read_partition_table+0x335>
jmp    1745 <read_partition_table+0x585>
movl   $0x0,-0x8ac(%rbp)
cmpl   $0x4,-0x8ac(%rbp)
jg     1740 <read_partition_table+0x580>
mov    -0x860(%rbp),%rax
cmp    -0x838(%rbp),%rax
ja     1525 <read_partition_table+0x365>
jmp    1740 <read_partition_table+0x580>
mov    -0x838(%rbp),%rdi
mov    -0x860(%rbp),%rax
mov    -0x838(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
mov    $0x20,%esi
call   1080 <memchr@plt>
mov    %rax,-0x868(%rbp)
cmpq   $0x0,-0x868(%rbp)
jne    15a1 <read_partition_table+0x3e1>
jmp    1563 <read_partition_table+0x3a3>
mov    0x2a76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8f4(%rbp)
mov    0x2a5f(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x8f4(%rbp),%edi
mov    %rax,%rdx
lea    0xb20(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x8ac(%rbp),%eax
mov    %rax,-0x900(%rbp)
sub    $0x4,%rax
ja     1727 <read_partition_table+0x567>
mov    -0x900(%rbp),%rax
lea    0xa3a(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x868(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x838(%rbp)
jmp    172c <read_partition_table+0x56c>
mov    -0x868(%rbp),%rax
mov    -0x838(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x1f,%rax
jle    1612 <read_partition_table+0x452>
mov    $0x1f,%eax
mov    %rax,-0x908(%rbp)
jmp    162a <read_partition_table+0x46a>
mov    -0x868(%rbp),%rax
mov    -0x838(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x908(%rbp)
mov    -0x908(%rbp),%rax
mov    %eax,-0x894(%rbp)
lea    -0x890(%rbp),%rdi
mov    -0x838(%rbp),%rsi
mov    -0x894(%rbp),%edx
call   1030 <strncpy@plt>
movslq -0x894(%rbp),%rax
movb   $0x0,-0x890(%rbp,%rax,1)
mov    -0x868(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x838(%rbp)
jmp    172c <read_partition_table+0x56c>
mov    -0x868(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x838(%rbp)
jmp    172c <read_partition_table+0x56c>
mov    -0x838(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10c0 <strtoul@plt>
mov    %rax,-0x8a0(%rbp)
mov    -0x868(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x838(%rbp)
jmp    172c <read_partition_table+0x56c>
mov    -0x868(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x838(%rbp)
mov    -0x838(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10c0 <strtoul@plt>
mov    %rax,-0x8a8(%rbp)
mov    -0x860(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x838(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    -0x890(%rbp),%rdx
mov    -0x8a0(%rbp),%rcx
mov    -0x8a8(%rbp),%r8
call   1050 <add_flash_partition@plt>
jmp    172c <read_partition_table+0x56c>
jmp    172c <read_partition_table+0x56c>
mov    -0x8ac(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8ac(%rbp)
jmp    14ff <read_partition_table+0x33f>
jmp    13ec <read_partition_table+0x22c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x910,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_partition_table>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x858,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rdi,%rbp
test   %r8d,%r8d
je     11e2 <read_partition_table+0x32>
cmp    $0x1,%r8d
jne    1480 <read_partition_table+0x2d0>
lea    0xe3d(%rip),%rbx
jmp    11e9 <read_partition_table+0x39>
lea    0xe2b(%rip),%rbx
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
call   10a0 <fseek@plt>
test   %rax,%rax
js     1440 <read_partition_table+0x290>
lea    0x50(%rsp),%rdi
mov    $0x1,%esi
mov    $0x800,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
test   %rax,%rax
js     1460 <read_partition_table+0x2b0>
movb   $0x0,0x84f(%rsp)
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x50(%rsp),%r12
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
je     126b <read_partition_table+0xbb>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%ecx
jmp    13f8 <read_partition_table+0x248>
mov    %rbx,%rdi
call   1060 <strlen@plt>
xor    %ecx,%ecx
cmp    $0x7ff,%eax
jg     13f8 <read_partition_table+0x248>
mov    %r15,0x10(%rsp)
mov    %r14,0x18(%rsp)
lea    0x850(%rsp),%rbp
lea    0xd67(%rip),%r13
mov    %rbx,0x28(%rsp)
jmp    12c9 <read_partition_table+0x119>
mov    %r12,%rbp
mov    0x28(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
add    %rbp,%rax
mov    %rbp,%r12
lea    0x850(%rsp),%rbp
cmp    %rbp,%rax
jae    13f6 <read_partition_table+0x246>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    13f6 <read_partition_table+0x246>
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
mov    %ebp,%edx
sub    %r12d,%edx
mov    %r12,%rdi
mov    $0xa,%esi
call   1070 <memchr@plt>
test   %rax,%rax
je     13f6 <read_partition_table+0x246>
mov    %rax,%r14
cmp    %r12,%rax
jbe    12a0 <read_partition_table+0xf0>
lea    0x1(%r14),%rax
mov    %rax,0x20(%rsp)
xor    %r15d,%r15d
xor    %eax,%eax
mov    %rax,0x8(%rsp)
jmp    1375 <read_partition_table+0x1c5>
nopl   0x0(%rax)
mov    %r13,%rbx
mov    %rbp,%r13
sub    %r12,%r13
cmp    $0x1f,%r13
mov    $0x1f,%eax
cmovge %rax,%r13
lea    0x30(%rsp),%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1030 <strncpy@plt>
movslq %r13d,%rax
mov    %rbx,%r13
movb   $0x0,0x30(%rsp,%rax,1)
add    $0x1,%rbp
add    $0x1,%r15d
mov    %rbp,%r12
cmp    %rbp,%r14
jbe    12a3 <read_partition_table+0xf3>
mov    %r14d,%edx
sub    %r12d,%edx
mov    %r12,%rdi
mov    $0x20,%esi
call   1070 <memchr@plt>
test   %rax,%rax
je     140c <read_partition_table+0x25c>
cmp    $0x4,%r15d
ja     12a0 <read_partition_table+0xf0>
mov    %rax,%rbp
mov    %r15d,%eax
movslq 0x0(%r13,%rax,4),%rax
add    %r13,%rax
jmp    *%rax
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10b0 <strtoul@plt>
mov    %rax,0x8(%rsp)
jmp    1361 <read_partition_table+0x1b1>
add    $0x1,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10b0 <strtoul@plt>
mov    0x10(%rsp),%rdi
mov    0x18(%rsp),%rsi
lea    0x30(%rsp),%rdx
mov    0x8(%rsp),%rcx
mov    %rax,%r8
call   1050 <add_flash_partition@plt>
mov    0x20(%rsp),%rbp
jmp    12a3 <read_partition_table+0xf3>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x858,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2bcd(%rip),%rax
mov    (%rax),%ebx
mov    0x2bbc(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xc86(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2b99(%rip),%rax
mov    (%rax),%ebx
mov    0x2b88(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xbe6(%rip),%rsi
jmp    142a <read_partition_table+0x27a>
mov    0x2b79(%rip),%rax
mov    (%rax),%ebx
mov    0x2b68(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xbe8(%rip),%rsi
jmp    142a <read_partition_table+0x27a>
mov    0x2b59(%rip),%rax
mov    (%rax),%ebx
xor    %edi,%edi
call   10d0 <strerror@plt>
lea    0xb90(%rip),%rsi
jmp    142a <read_partition_table+0x27a>

LLVM IR (-O0):
%struct.flash_partition_entry = type { i32 }
@.str = [9 x i8] c"fwup-ptn\00"
@.str.1 = [10 x i8] c"partition\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Invalid partition table: %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [34 x i8] c"Can not seek in the firmware: %s\0A\00"
@errno = global i32 0
@.str.4 = [45 x i8] c"Can not read fwup-ptn from the firmware: %s\0A\00"
@.str.5 = [29 x i8] c"DEBUG: can not find fwuphdr\0A\00"
@.str.6 = [49 x i8] c"Ignoring the rest of the partition entries.: %s\0A\00"
define i32 @read_partition_table(i32* %0, i64 %1, %struct.flash_partition_entry* %2, i64 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
%9 = alloca %struct.flash_partition_entry*
%10 = alloca i64
%11 = alloca i32
%12 = alloca [2048 x i8]
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i8*
%18 = alloca i8*
%19 = alloca i8*
%20 = alloca [32 x i8]
%21 = alloca i32
%22 = alloca i64
%23 = alloca i64
%24 = alloca i32
store i32* %0, i32** %7
store i64 %1, i64* %8
store %struct.flash_partition_entry* %2, %struct.flash_partition_entry** %9
store i64 %3, i64* %10
store i32 %4, i32* %11
store i8* null, i8** %15
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %16
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %17
%25 = load i32, i32* %11
switch i32 %25, label %30 [
i32 0, label %26
i32 1, label %28
]
26: ; preds = %5
%27 = load i8*, i8** %16
store i8* %27, i8** %15
br label %37
28: ; preds = %5
%29 = load i8*, i8** %17
store i8* %29, i8** %15
br label %37
30: ; preds = %5
br label %31
31: ; preds = %30
%32 = load i32, i32* @stderr
%33 = call i8* @strerror(i32 0)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; No predecessors!
br label %37
37: ; preds = %36, %28, %26
%38 = load i32*, i32** %7
%39 = load i64, i64* %8
%40 = load i32, i32* @SEEK_SET
%41 = call i64 @fseek(i32* %38, i64 %39, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %51
43: ; preds = %37
br label %44
44: ; preds = %43
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; No predecessors!
br label %51
51: ; preds = %50, %37
%52 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%53 = load i32*, i32** %7
%54 = call i64 @fread(i8* %52, i32 1, i32 2048, i32* %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %51
br label %57
57: ; preds = %56
%58 = load i32, i32* @stderr
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i8* %60)
%62 = call i32 @exit(i32 1)
unreachable
63: ; No predecessors!
br label %64
64: ; preds = %63, %51
%65 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 2047
store i8 0, i8* %65
%66 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%67 = load i8*, i8** %15
%68 = load i8*, i8** %15
%69 = call i32 @strlen(i8* %68)
%70 = call i64 @memcmp(i8* %66, i8* %67, i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %64
%73 = load i32, i32* @stderr
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %6
br label %195
75: ; preds = %64
%76 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
store i8* %76, i8** %13
%77 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%78 = getelementptr inbounds i8, i8* %77, i64 2048
store i8* %78, i8** %14
br label %79
79: ; preds = %193, %75
%80 = load i8*, i8** %13
%81 = load i8*, i8** %15
%82 = call i32 @strlen(i8* %81)
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %80, i64 %83
%85 = load i8*, i8** %14
%86 = icmp ult i8* %84, %85
br i1 %86, label %87, label %94
87: ; preds = %79
%88 = load i8*, i8** %13
%89 = load i8*, i8** %15
%90 = load i8*, i8** %15
%91 = call i32 @strlen(i8* %90)
%92 = call i64 @memcmp(i8* %88, i8* %89, i32 %91)
%93 = icmp eq i64 %92, 0
br label %94
94: ; preds = %87, %79
%95 = phi i1 [ false, %79 ], [ %93, %87 ]
br i1 %95, label %96, label %194
96: ; preds = %94
%97 = bitcast [32 x i8]* %20 to i8*
store i32 0, i32* %21
store i64 0, i64* %22
store i64 0, i64* %23
%98 = load i8*, i8** %13
%99 = load i8*, i8** %14
%100 = load i8*, i8** %13
%101 = ptrtoint i8* %99 to i64
%102 = ptrtoint i8* %100 to i64
%103 = sub i64 %101, %102
%104 = trunc i64 %103 to i32
%105 = call i8* @memchr(i8* %98, i32 10, i32 %104)
store i8* %105, i8** %18
%106 = load i8*, i8** %18
%107 = icmp eq i8* %106, null
br i1 %107, label %108, label %109
108: ; preds = %96
br label %194
109: ; preds = %96
store i32 0, i32* %24
br label %110
110: ; preds = %190, %109
%111 = load i32, i32* %24
%112 = icmp sle i32 %111, 4
br i1 %112, label %113, label %193
113: ; preds = %110
%114 = load i8*, i8** %18
%115 = load i8*, i8** %13
%116 = icmp ule i8* %114, %115
br i1 %116, label %117, label %118
117: ; preds = %113
br label %193
118: ; preds = %113
%119 = load i8*, i8** %13
%120 = load i8*, i8** %18
%121 = load i8*, i8** %13
%122 = ptrtoint i8* %120 to i64
%123 = ptrtoint i8* %121 to i64
%124 = sub i64 %122, %123
%125 = trunc i64 %124 to i32
%126 = call i8* @memchr(i8* %119, i32 32, i32 %125)
store i8* %126, i8** %19
%127 = load i8*, i8** %19
%128 = icmp eq i8* %127, null
br i1 %128, label %129, label %137
129: ; preds = %118
br label %130
130: ; preds = %129
%131 = load i32, i32* @stderr
%132 = load i32, i32* @errno
%133 = call i8* @strerror(i32 %132)
%134 = call i32 (i32, i8*, ...) @fprintf(i32 %131, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0), i8* %133)
%135 = call i32 @exit(i32 1)
unreachable
136: ; No predecessors!
br label %193
137: ; preds = %118
%138 = load i32, i32* %24
switch i32 %138, label %189 [
i32 0, label %139
i32 1, label %142
i32 2, label %168
i32 3, label %171
i32 4, label %176
]
139: ; preds = %137
%140 = load i8*, i8** %19
%141 = getelementptr inbounds i8, i8* %140, i64 1
store i8* %141, i8** %13
br label %190
142: ; preds = %137
%143 = load i8*, i8** %19
%144 = load i8*, i8** %13
%145 = ptrtoint i8* %143 to i64
%146 = ptrtoint i8* %144 to i64
%147 = sub i64 %145, %146
%148 = icmp sgt i64 %147, 31
br i1 %148, label %149, label %150
149: ; preds = %142
br label %156
150: ; preds = %142
%151 = load i8*, i8** %19
%152 = load i8*, i8** %13
%153 = ptrtoint i8* %151 to i64
%154 = ptrtoint i8* %152 to i64
%155 = sub i64 %153, %154
br label %156
156: ; preds = %150, %149
%157 = phi i64 [ 31, %149 ], [ %155, %150 ]
%158 = trunc i64 %157 to i32
store i32 %158, i32* %21
%159 = getelementptr inbounds [32 x i8], [32 x i8]* %20, i64 0, i64 0
%160 = load i8*, i8** %13
%161 = load i32, i32* %21
%162 = call i32 @strncpy(i8* %159, i8* %160, i32 %161)
%163 = load i32, i32* %21
%164 = sext i32 %163 to i64
%165 = getelementptr inbounds [32 x i8], [32 x i8]* %20, i64 0, i64 %164
store i8 0, i8* %165
%166 = load i8*, i8** %19
%167 = getelementptr inbounds i8, i8* %166, i64 1
store i8* %167, i8** %13
br label %190
168: ; preds = %137
%169 = load i8*, i8** %19
%170 = getelementptr inbounds i8, i8* %169, i64 1
store i8* %170, i8** %13
br label %190
171: ; preds = %137
%172 = load i8*, i8** %13
%173 = call i64 @strtoul(i8* %172, i32* null, i32 16)
store i64 %173, i64* %22
%174 = load i8*, i8** %19
%175 = getelementptr inbounds i8, i8* %174, i64 1
store i8* %175, i8** %13
br label %190
176: ; preds = %137
%177 = load i8*, i8** %19
%178 = getelementptr inbounds i8, i8* %177, i64 1
store i8* %178, i8** %13
%179 = load i8*, i8** %13
%180 = call i64 @strtoul(i8* %179, i32* null, i32 16)
store i64 %180, i64* %23
%181 = load i8*, i8** %18
%182 = getelementptr inbounds i8, i8* %181, i64 1
store i8* %182, i8** %13
%183 = load %struct.flash_partition_entry*, %struct.flash_partition_entry** %9
%184 = load i64, i64* %10
%185 = getelementptr inbounds [32 x i8], [32 x i8]* %20, i64 0, i64 0
%186 = load i64, i64* %22
%187 = load i64, i64* %23
%188 = call i32 @add_flash_partition(%struct.flash_partition_entry* %183, i64 %184, i8* %185, i64 %186, i64 %187)
br label %190
189: ; preds = %137
br label %190
190: ; preds = %189, %176, %171, %168, %156, %139
%191 = load i32, i32* %24
%192 = add nsw i32 %191, 1
store i32 %192, i32* %24
br label %110
193: ; preds = %136, %117, %110
br label %79
194: ; preds = %108, %94
store i32 0, i32* %6
br label %195
195: ; preds = %194, %72
%196 = load i32, i32* %6
ret i32 %196
}

LLVM IR (-O2):
%struct.flash_partition_entry = type { i32 }
@.str = [9 x i8] c"fwup-ptn\00"
@.str.1 = [10 x i8] c"partition\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Invalid partition table: %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [34 x i8] c"Can not seek in the firmware: %s\0A\00"
@errno = global i32 0
@.str.4 = [45 x i8] c"Can not read fwup-ptn from the firmware: %s\0A\00"
@.str.5 = [29 x i8] c"DEBUG: can not find fwuphdr\0A\00"
@.str.6 = [49 x i8] c"Ignoring the rest of the partition entries.: %s\0A\00"
define i32 @read_partition_table(i32* nocapture %0, i64 %1, %struct.flash_partition_entry* %2, i64 %3, i32 %4) {
%6 = alloca [2048 x i8]
%7 = alloca [32 x i8]
%8 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
switch i32 %4, label %10 [
i32 0, label %15
i32 1, label %9
]
9: ; preds = %5
br label %15
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = tail call i8* @strerror(i32 0)
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %5, %9
%16 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %5 ]
%17 = load i32, i32* @SEEK_SET
%18 = tail call i64 @fseek(i32* %0, i64 %1, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %15
%27 = call i64 @fread(i8* %8, i32 1, i32 2048, i32* %0)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = tail call i8* @strerror(i32 %31)
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %26
%36 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 2047
store i8 0, i8* %36
%37 = tail call i32 @strlen(i8* %16)
%38 = call i64 @memcmp(i8* %8, i8* %16, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %110
43: ; preds = %35
%44 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 2048
%45 = call i32 @strlen(i8* %16)
%46 = icmp slt i32 %45, 2048
br i1 %46, label %47, label %110
47: ; preds = %43
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%49 = ptrtoint i8* %44 to i64
br label %50
50: ; preds = %47, %104
%51 = phi i8* [ %8, %47 ], [ %105, %104 ]
%52 = call i32 @strlen(i8* %16)
%53 = call i64 @memcmp(i8* %51, i8* %16, i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %110
55: ; preds = %50
%56 = ptrtoint i8* %51 to i64
%57 = sub i64 %49, %56
%58 = trunc i64 %57 to i32
%59 = call i8* @memchr(i8* %51, i32 10, i32 %58)
%60 = icmp eq i8* %59, null
br i1 %60, label %66, label %61
61: ; preds = %55
%62 = icmp ugt i8* %59, %51
br i1 %62, label %63, label %104
63: ; preds = %61
%64 = ptrtoint i8* %59 to i64
%65 = getelementptr inbounds i8, i8* %59, i64 1
br label %67
66: ; preds = %55
br label %110
67: ; preds = %63, %99
%68 = phi i32 [ 0, %63 ], [ %102, %99 ]
%69 = phi i64 [ 0, %63 ], [ %100, %99 ]
%70 = phi i8* [ %51, %63 ], [ %101, %99 ]
%71 = ptrtoint i8* %70 to i64
%72 = sub i64 %64, %71
%73 = trunc i64 %72 to i32
%74 = call i8* @memchr(i8* %70, i32 32, i32 %73)
%75 = icmp eq i8* %74, null
br i1 %75, label %76, label %82
76: ; preds = %67
%77 = load i32, i32* @stderr
%78 = load i32, i32* @errno
%79 = call i8* @strerror(i32 %78)
%80 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0), i8* %79)
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %67
switch i32 %68, label %104 [
i32 0, label %99
i32 1, label %83
i32 2, label %99
i32 3, label %93
i32 4, label %95
]
83: ; preds = %82
%84 = ptrtoint i8* %74 to i64
%85 = sub i64 %84, %71
%86 = icmp slt i64 %85, 31
%87 = select i1 %86, i64 %85, i64 31
%88 = trunc i64 %87 to i32
%89 = call i32 @strncpy(i8* %48, i8* %70, i32 %88)
%90 = shl i64 %87, 32
%91 = ashr exact i64 %90, 32
%92 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 %91
store i8 0, i8* %92
br label %99
93: ; preds = %82
%94 = call i64 @strtoul(i8* nocapture %70, i32* null, i32 16)
br label %99
95: ; preds = %82
%96 = getelementptr inbounds i8, i8* %74, i64 1
%97 = call i64 @strtoul(i8* nocapture %96, i32* null, i32 16)
%98 = call i32 @add_flash_partition(%struct.flash_partition_entry* %2, i64 %3, i8* %48, i64 %69, i64 %97)
br label %104
99: ; preds = %82, %82, %93, %83
%100 = phi i64 [ %94, %93 ], [ %69, %83 ], [ %69, %82 ], [ %69, %82 ]
%101 = getelementptr inbounds i8, i8* %74, i64 1
%102 = add nuw nsw i32 %68, 1
%103 = icmp ugt i8* %59, %101
br i1 %103, label %67, label %104
104: ; preds = %82, %99, %95, %61
%105 = phi i8* [ %51, %61 ], [ %65, %95 ], [ %70, %82 ], [ %101, %99 ]
%106 = call i32 @strlen(i8* %16)
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds i8, i8* %105, i64 %107
%109 = icmp ult i8* %108, %44
br i1 %109, label %50, label %110
110: ; preds = %104, %50, %43, %66, %40
%111 = phi i32 [ 1, %40 ], [ 0, %66 ], [ 0, %43 ], [ 0, %50 ], [ 0, %104 ]
ret i32 %111
}