#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 */
typedef struct signature TYPE_3__;
typedef struct TYPE_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ sign;
typedef int /*<<< orphan*/ oldsign;
struct signature {
  char const *magic;
  unsigned int device_id;
  char *firmware_version;
  unsigned int crc32;
};
struct TYPE_5__ {
  char *member_0;
};
struct TYPE_4__ {
  char member_0;
  char member_1;
  char member_2;
  char member_3;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ R_OK;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ W_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
unsigned int atoi(int /*<<< orphan*/ *);
int chksum_crc32(int /*<<< orphan*/ *);
int /*<<< orphan*/ chksum_crc32gentab();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(struct signature *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(TYPE_3__ *, int, int, int /*<<< orphan*/ *);
int getopt(int, char **, char const *);
int /*<<< orphan*/ *optarg;
size_t optind;
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strncmp(char const *, char *, int);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/ *, int);
scalar_t__ strtol(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  struct signature {
    const char magic[4];
    unsigned int device_id;
    char firmware_version[48];
    unsigned int crc32;
  } sign = {{'Z', 'N', 'B', 'G'}, 1, {"V.1.0.0(1.0.0)"}, 0};
  FILE *f;
  struct signature oldsign;
  char *filename;
  static const char *optString;
  int opt;
  if (argc < 1)
    usage(argv[0]);
  optString = "v:d:h";
  opt = getopt(argc, argv, optString);
  while (opt != -1) {
    switch (opt) {
    case 'v':
      if (optarg == NULL)
        usage(argv[0]);
      strncpy(sign.firmware_version, optarg, sizeof(sign.firmware_version) - 1);
      sign.firmware_version[sizeof(sign.firmware_version) - 1] = '\0';
      break;
    case 'd':
      sign.device_id = atoi(optarg);
      if (sign.device_id == 0)
        sign.device_id = (int)strtol(optarg, NULL, 16);
      break;
    case '?':
    case 'h':
      usage(argv[0]);
      break;
    default:
      break;
    }
    opt = getopt(argc, argv, optString);
  }
  chksum_crc32gentab();
  filename = argv[optind];
  if (access(filename, W_OK) || access(filename, R_OK)) {
    printf("Not open input file %s\n", filename);
    exit(1);
  }
  f = fopen(argv[optind], "r+");
  if (f != NULL) {
    fseek(f, sizeof(sign) * -1, SEEK_END);
    fread(&oldsign, sizeof(oldsign), 1, f);
    if (strncmp(oldsign.magic, "ZNBG", sizeof(oldsign.magic)) == 0) {
      printf("Image is already signed as:\nDevice ID: 0x%08x\nFirmware "
             "version: %s\nImage CRC32: 0x%x\n",
             oldsign.device_id, oldsign.firmware_version, oldsign.crc32);
      exit(0);
    }
    fseek(f, 0, SEEK_SET);
    sign.crc32 = chksum_crc32(f);
    fwrite(&sign, sizeof(sign), 1, f);
    fclose(f);
    printf("Image signed as:\nDevice ID: 0x%08x\nFirmware version: %s\nImage "
           "CRC32: 0x%x\n",
           sign.device_id, sign.firmware_version, sign.crc32);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xdcc(%rip),%rsi
mov    $0x3c,%edx
call   10b0 <memcpy@plt>
cmpl   $0x1,-0x8(%rbp)
jge    1254 <main+0x44>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <usage@plt>
lea    0xde1(%rip),%rax
mov    %rax,0x2e4e(%rip)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e40(%rip),%rdx
call   1100 <getopt@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0xffffffff,-0xa4(%rbp)
je     1390 <main+0x180>
mov    -0xa4(%rbp),%eax
mov    %eax,-0xa8(%rbp)
sub    $0x3f,%eax
je     135c <main+0x14c>
jmp    12a2 <main+0x92>
mov    -0xa8(%rbp),%eax
sub    $0x64,%eax
je     1320 <main+0x110>
jmp    12b6 <main+0xa6>
mov    -0xa8(%rbp),%eax
sub    $0x68,%eax
je     135c <main+0x14c>
jmp    12ca <main+0xba>
mov    -0xa8(%rbp),%eax
sub    $0x76,%eax
jne    136d <main+0x15d>
jmp    12de <main+0xce>
mov    0x2d13(%rip),%rax
cmpq   $0x0,(%rax)
jne    12fb <main+0xeb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <usage@plt>
lea    -0x50(%rbp),%rdi
add    $0x8,%rdi
mov    0x2cee(%rip),%rax
mov    (%rax),%rsi
mov    $0x2f,%edx
call   1040 <strncpy@plt>
movb   $0x0,-0x19(%rbp)
jmp    1372 <main+0x162>
mov    0x2cd1(%rip),%rax
mov    (%rax),%rdi
call   1110 <atoi@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    1357 <main+0x147>
mov    0x2cb5(%rip),%rax
mov    (%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10a0 <strtol@plt>
mov    %eax,-0x4c(%rbp)
jmp    1372 <main+0x162>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <usage@plt>
jmp    1372 <main+0x162>
jmp    1372 <main+0x162>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d30(%rip),%rdx
call   1100 <getopt@plt>
mov    %eax,-0xa4(%rbp)
jmp    127b <main+0x6b>
mov    $0x0,%al
call   1090 <chksum_crc32gentab@plt>
mov    -0x10(%rbp),%rax
mov    0x2c2e(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rdi
mov    0x2c1a(%rip),%rax
mov    (%rax),%esi
call   10e0 <access@plt>
cmp    $0x0,%rax
jne    13ee <main+0x1de>
mov    -0xa0(%rbp),%rdi
mov    0x2bdb(%rip),%rax
mov    (%rax),%esi
call   10e0 <access@plt>
cmp    $0x0,%rax
je     140d <main+0x1fd>
mov    -0xa0(%rbp),%rsi
lea    0xc46(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x2bb8(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xc34(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1512 <main+0x302>
mov    -0x58(%rbp),%rdi
mov    0x2b9b(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffc4,%esi
call   10c0 <fseek@plt>
lea    -0x98(%rbp),%rdi
mov    -0x58(%rbp),%rcx
mov    $0x3c,%esi
mov    $0x1,%edx
call   1060 <fread@plt>
lea    -0x98(%rbp),%rdi
lea    0xbe4(%rip),%rsi
mov    $0x4,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    14b6 <main+0x2a6>
mov    -0x94(%rbp),%esi
lea    -0x98(%rbp),%rdx
add    $0x8,%rdx
mov    -0x60(%rbp),%ecx
lea    0xbba(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
xor    %edi,%edi
call   1120 <exit@plt>
mov    -0x58(%rbp),%rdi
mov    0x2aff(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x58(%rbp),%rdi
call   1030 <chksum_crc32@plt>
mov    %eax,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rcx
mov    $0x3c,%esi
mov    $0x1,%edx
call   1130 <fwrite@plt>
mov    -0x58(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x4c(%rbp),%esi
lea    -0x50(%rbp),%rdx
add    $0x8,%rdx
mov    -0x18(%rbp),%ecx
lea    0xbad(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%rbx
mov    %edi,%r15d
movups 0xe10(%rip),%xmm0
movups %xmm0,0x2c(%rsp)
movups 0xdf8(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movups 0xddc(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movups 0xdc0(%rip),%xmm0
movaps %xmm0,(%rsp)
test   %edi,%edi
jg     1250 <main+0x50>
mov    (%rbx),%rdi
call   10c0 <usage@plt>
lea    0xde5(%rip),%rdx
mov    %rdx,0x2e4a(%rip)
mov    %r15d,%edi
mov    %rbx,%rsi
call   10f0 <getopt@plt>
lea    0x8(%rsp),%r14
mov    0x2d83(%rip),%r12
jmp    129a <main+0x9a>
nopw   0x0(%rax,%rax,1)
mov    (%rbx),%rdi
call   10c0 <usage@plt>
mov    0x2e19(%rip),%rdx
mov    %r15d,%edi
mov    %rbx,%rsi
call   10f0 <getopt@plt>
cmp    $0x67,%eax
jg     12e0 <main+0xe0>
cmp    $0x3f,%eax
je     1280 <main+0x80>
cmp    $0x64,%eax
jne    1316 <main+0x116>
mov    (%r12),%rbp
mov    %rbp,%rdi
call   1100 <atoi@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
jne    1288 <main+0x88>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10a0 <strtol@plt>
mov    %eax,0x4(%rsp)
jmp    1288 <main+0x88>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x68,%eax
je     1280 <main+0x80>
cmp    $0x76,%eax
jne    1288 <main+0x88>
mov    (%r12),%rsi
test   %rsi,%rsi
jne    12ff <main+0xff>
mov    (%rbx),%rdi
call   10c0 <usage@plt>
mov    (%r12),%rsi
mov    %r14,%rdi
mov    $0x2f,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x37(%rsp)
jmp    1288 <main+0x88>
cmp    $0xffffffff,%eax
jne    1288 <main+0x88>
xor    %eax,%eax
call   1090 <chksum_crc32gentab@plt>
mov    0x2ca3(%rip),%r15
mov    (%r15),%rax
mov    (%rbx,%rax,8),%rbp
mov    0x2c9d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10d0 <access@plt>
test   %rax,%rax
jne    143b <main+0x23b>
mov    0x2c63(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10d0 <access@plt>
test   %rax,%rax
jne    143b <main+0x23b>
mov    (%r15),%rax
mov    (%rbx,%rax,8),%rdi
lea    0xce4(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1429 <main+0x229>
mov    %rax,%rbx
mov    0x2c52(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xffffffc4,%esi
call   10b0 <fseek@plt>
lea    0x40(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x3c,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fread@plt>
lea    0xc9f(%rip),%rsi
mov    %rbp,%rdi
mov    $0x4,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     1456 <main+0x256>
mov    0x2be5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   1030 <chksum_crc32@plt>
mov    %eax,0x38(%rsp)
mov    %rsp,%rdi
mov    $0x3c,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1120 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
mov    0x4(%rsp),%esi
mov    0x38(%rsp),%ecx
lea    0xc99(%rip),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc00(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    0x48(%rsp),%rdx
mov    0x44(%rsp),%esi
mov    0x78(%rsp),%ecx
lea    0xbf8(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %edi,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.signature = type { [4 x i8], i32, [48 x i8], i32 }
%struct.signature.0 = type { i8*, i32, i8*, i32 }
@__const.main.sign = %struct.signature { [4 x i8] c"ZNBG", i32 1, [48 x i8] c"V.1.0.0(1.0.0)\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", i32 0 }
@main.optString = internal global i8* null
@.str = [6 x i8] c"v:d:h\00"
@optarg = global i32* null
@optind = global i64 0
@W_OK = global i32 0
@R_OK = global i32 0
@.str.1 = [24 x i8] c"Not open input file %s\0A\00"
@.str.2 = [3 x i8] c"r+\00"
@SEEK_END = global i32 0
@.str.3 = [5 x i8] c"ZNBG\00"
@.str.4 = [86 x i8] c"Image is already signed as:\0ADevice ID: 0x%08x\0AFirmware version: %s\0AImage CRC32: 0x%x\0A\00"
@SEEK_SET = global i32 0
@.str.5 = [75 x i8] c"Image signed as:\0ADevice ID: 0x%08x\0AFirmware version: %s\0AImage CRC32: 0x%x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.signature
%7 = alloca i32*
%8 = alloca %struct.signature
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast %struct.signature* %6 to i8*
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @usage(i8* %17)
br label %19
19: ; preds = %14, %2
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** @main.optString
%20 = load i32, i32* %4
%21 = load i8**, i8*** %5
%22 = load i8*, i8** @main.optString
%23 = call i32 @getopt(i32 %20, i8** %21, i8* %22)
store i32 %23, i32* %10
br label %24
24: ; preds = %63, %19
%25 = load i32, i32* %10
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %68
27: ; preds = %24
%28 = load i32, i32* %10
switch i32 %28, label %62 [
i32 118, label %29
i32 100, label %44
i32 63, label %57
i32 104, label %57
]
29: ; preds = %27
%30 = load i32*, i32** @optarg
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = call i32 @usage(i8* %35)
br label %37
37: ; preds = %32, %29
%38 = getelementptr inbounds %struct.signature, %struct.signature* %6, i32 0, i32 2
%39 = getelementptr inbounds [48 x i8], [48 x i8]* %38, i64 0, i64 0
%40 = load i32*, i32** @optarg
%41 = call i32 @strncpy(i8* %39, i32* %40, i32 47)
%42 = getelementptr inbounds %struct.signature, %struct.signature* %6, i32 0, i32 2
%43 = getelementptr inbounds [48 x i8], [48 x i8]* %42, i64 0, i64 47
store i8 0, i8* %43
br label %63
44: ; preds = %27
%45 = load i32*, i32** @optarg
%46 = call i32 @atoi(i32* %45)
%47 = getelementptr inbounds %struct.signature, %struct.signature* %6, i32 0, i32 1
store i32 %46, i32* %47
%48 = getelementptr inbounds %struct.signature, %struct.signature* %6, i32 0, i32 1
%49 = load i32, i32* %48
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %44
%52 = load i32*, i32** @optarg
%53 = call i64 @strtol(i32* %52, i32* null, i32 16)
%54 = trunc i64 %53 to i32
%55 = getelementptr inbounds %struct.signature, %struct.signature* %6, i32 0, i32 1
store i32 %54, i32* %55
br label %56
56: ; preds = %51, %44
br label %63
57: ; preds = %27, %27
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 0
%60 = load i8*, i8** %59
%61 = call i32 @usage(i8* %60)
br label %63
62: ; preds = %27
br label %63
63: ; preds = %62, %57, %56, %37
%64 = load i32, i32* %4
%65 = load i8**, i8*** %5
%66 = load i8*, i8** @main.optString
%67 = call i32 @getopt(i32 %64, i8** %65, i8* %66)
store i32 %67, i32* %10
br label %24
68: ; preds = %24
%69 = call i32 (...) @chksum_crc32gentab()
%70 = load i8**, i8*** %5
%71 = load i64, i64* @optind
%72 = getelementptr inbounds i8*, i8** %70, i64 %71
%73 = load i8*, i8** %72
store i8* %73, i8** %9
%74 = load i8*, i8** %9
%75 = load i32, i32* @W_OK
%76 = call i64 @access(i8* %74, i32 %75)
%77 = icmp ne i64 %76, 0
br i1 %77, label %83, label %78
78: ; preds = %68
%79 = load i8*, i8** %9
%80 = load i32, i32* @R_OK
%81 = call i64 @access(i8* %79, i32 %80)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %87
83: ; preds = %78, %68
%84 = load i8*, i8** %9
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %84)
%86 = call i32 @exit(i32 1)
unreachable
87: ; preds = %78
%88 = load i8**, i8*** %5
%89 = load i64, i64* @optind
%90 = getelementptr inbounds i8*, i8** %88, i64 %89
%91 = load i8*, i8** %90
%92 = call i32* @fopen(i8* %91, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %92, i32** %7
%93 = load i32*, i32** %7
%94 = icmp ne i32* %93, null
br i1 %94, label %95, label %134
95: ; preds = %87
%96 = load i32*, i32** %7
%97 = load i32, i32* @SEEK_END
%98 = call i32 @fseek(i32* %96, i32 -60, i32 %97)
%99 = bitcast %struct.signature* %8 to %struct.signature.0*
%100 = load i32*, i32** %7
%101 = call i32 @fread(%struct.signature.0* %99, i32 60, i32 1, i32* %100)
%102 = getelementptr inbounds %struct.signature, %struct.signature* %8, i32 0, i32 0
%103 = getelementptr inbounds [4 x i8], [4 x i8]* %102, i64 0, i64 0
%104 = call i64 @strncmp(i8* %103, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%105 = icmp eq i64 %104, 0
br i1 %105, label %106, label %115
106: ; preds = %95
%107 = getelementptr inbounds %struct.signature, %struct.signature* %8, i32 0, i32 1
%108 = load i32, i32* %107
%109 = getelementptr inbounds %struct.signature, %struct.signature* %8, i32 0, i32 2
%110 = getelementptr inbounds [48 x i8], [48 x i8]* %109, i64 0, i64 0
%111 = getelementptr inbounds %struct.signature, %struct.signature* %8, i32 0, i32 3
%112 = load i32, i32* %111
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.4, i64 0, i64 0), i32 %108, i8* %110, i32 %112)
%114 = call i32 @exit(i32 0)
unreachable
115: ; preds = %95
%116 = load i32*, i32** %7
%117 = load i32, i32* @SEEK_SET
%118 = call i32 @fseek(i32* %116, i32 0, i32 %117)
%119 = load i32*, i32** %7
%120 = call i32 @chksum_crc32(i32* %119)
%121 = getelementptr inbounds %struct.signature, %struct.signature* %6, i32 0, i32 3
store i32 %120, i32* %121
%122 = bitcast %struct.signature* %6 to %struct.signature.0*
%123 = load i32*, i32** %7
%124 = call i32 @fwrite(%struct.signature.0* %122, i32 60, i32 1, i32* %123)
%125 = load i32*, i32** %7
%126 = call i32 @fclose(i32* %125)
%127 = getelementptr inbounds %struct.signature, %struct.signature* %6, i32 0, i32 1
%128 = load i32, i32* %127
%129 = getelementptr inbounds %struct.signature, %struct.signature* %6, i32 0, i32 2
%130 = getelementptr inbounds [48 x i8], [48 x i8]* %129, i64 0, i64 0
%131 = getelementptr inbounds %struct.signature, %struct.signature* %6, i32 0, i32 3
%132 = load i32, i32* %131
%133 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.5, i64 0, i64 0), i32 %128, i8* %130, i32 %132)
br label %134
134: ; preds = %115, %87
ret i32 0
}

LLVM IR (-O2):
%struct.signature = type { [4 x i8], i32, [48 x i8], i32 }
%struct.signature.0 = type { i8*, i32, i8*, i32 }
@__const.main.sign = %struct.signature { [4 x i8] c"ZNBG", i32 1, [48 x i8] c"V.1.0.0(1.0.0)\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", i32 0 }
@main.optString = internal unnamed_addr global i8* null
@.str = [6 x i8] c"v:d:h\00"
@optarg = global i32* null
@optind = global i64 0
@W_OK = global i32 0
@R_OK = global i32 0
@.str.1 = [24 x i8] c"Not open input file %s\0A\00"
@.str.2 = [3 x i8] c"r+\00"
@SEEK_END = global i32 0
@.str.3 = [5 x i8] c"ZNBG\00"
@.str.4 = [86 x i8] c"Image is already signed as:\0ADevice ID: 0x%08x\0AFirmware version: %s\0AImage CRC32: 0x%x\0A\00"
@SEEK_SET = global i32 0
@.str.5 = [75 x i8] c"Image signed as:\0ADevice ID: 0x%08x\0AFirmware version: %s\0AImage CRC32: 0x%x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.signature
%4 = alloca %struct.signature
%5 = getelementptr inbounds %struct.signature, %struct.signature* %3, i64 0, i32 0, i64 0
%6 = getelementptr inbounds %struct.signature, %struct.signature* %4, i64 0, i32 0, i64 0
%7 = icmp slt i32 %0, 1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @usage(i8* %9)
br label %11
11: ; preds = %8, %2
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** @main.optString
%12 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds %struct.signature, %struct.signature* %3, i64 0, i32 1
%14 = getelementptr inbounds %struct.signature, %struct.signature* %3, i64 0, i32 2, i64 0
%15 = getelementptr inbounds %struct.signature, %struct.signature* %3, i64 0, i32 2, i64 47
br label %16
16: ; preds = %38, %11
%17 = phi i32 [ %12, %11 ], [ %40, %38 ]
switch i32 %17, label %38 [
i32 -1, label %41
i32 118, label %18
i32 100, label %28
i32 63, label %35
i32 104, label %35
]
18: ; preds = %16
%19 = load i32*, i32** @optarg
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i8*, i8** %1
%23 = call i32 @usage(i8* %22)
%24 = load i32*, i32** @optarg
br label %25
25: ; preds = %21, %18
%26 = phi i32* [ %24, %21 ], [ %19, %18 ]
%27 = call i32 @strncpy(i8* %14, i32* %26, i32 47)
store i8 0, i8* %15
br label %38
28: ; preds = %16
%29 = load i32*, i32** @optarg
%30 = call i32 @atoi(i32* %29)
store i32 %30, i32* %13
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %28
%33 = call i64 @strtol(i32* nocapture %29, i32* null, i32 16)
%34 = trunc i64 %33 to i32
store i32 %34, i32* %13
br label %38
35: ; preds = %16, %16
%36 = load i8*, i8** %1
%37 = call i32 @usage(i8* %36)
br label %38
38: ; preds = %16, %28, %32, %35, %25
%39 = load i8*, i8** @main.optString
%40 = call i32 @getopt(i32 %0, i8** %1, i8* %39)
br label %16
41: ; preds = %16
%42 = call i32 (...) @chksum_crc32gentab()
%43 = load i64, i64* @optind
%44 = getelementptr inbounds i8*, i8** %1, i64 %43
%45 = load i8*, i8** %44
%46 = load i32, i32* @W_OK
%47 = call i64 @access(i8* %45, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %41
%50 = load i32, i32* @R_OK
%51 = call i64 @access(i8* %45, i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %49, %41
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %45)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %49
%57 = load i64, i64* @optind
%58 = getelementptr inbounds i8*, i8** %1, i64 %57
%59 = load i8*, i8** %58
%60 = call i32* @fopen(i8* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%61 = icmp eq i32* %60, null
br i1 %61, label %88, label %62
62: ; preds = %56
%63 = load i32, i32* @SEEK_END
%64 = call i32 @fseek(i32* %60, i32 -60, i32 %63)
%65 = bitcast %struct.signature* %4 to %struct.signature.0*
%66 = call i32 @fread(%struct.signature.0* %65, i32 60, i32 1, i32* %60)
%67 = call i64 @strncmp(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%68 = icmp eq i64 %67, 0
br i1 %68, label %69, label %77
69: ; preds = %62
%70 = getelementptr inbounds %struct.signature, %struct.signature* %4, i64 0, i32 1
%71 = load i32, i32* %70
%72 = getelementptr inbounds %struct.signature, %struct.signature* %4, i64 0, i32 2, i64 0
%73 = getelementptr inbounds %struct.signature, %struct.signature* %4, i64 0, i32 3
%74 = load i32, i32* %73
%75 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([86 x i8], [86 x i8]* @.str.4, i64 0, i64 0), i32 %71, i8* %72, i32 %74)
%76 = call i32 @exit(i32 0)
unreachable
77: ; preds = %62
%78 = load i32, i32* @SEEK_SET
%79 = call i32 @fseek(i32* %60, i32 0, i32 %78)
%80 = call i32 @chksum_crc32(i32* %60)
%81 = getelementptr inbounds %struct.signature, %struct.signature* %3, i64 0, i32 3
store i32 %80, i32* %81
%82 = bitcast %struct.signature* %3 to %struct.signature.0*
%83 = call i32 @fwrite(%struct.signature.0* %82, i32 60, i32 1, i32* %60)
%84 = call i32 @fclose(i32* %60)
%85 = load i32, i32* %13
%86 = load i32, i32* %81
%87 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @.str.5, i64 0, i64 0), i32 %85, i8* %14, i32 %86)
br label %88
88: ; preds = %77, %56
ret i32 0
}