#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 stat {
  unsigned int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
unsigned int CRC_32(unsigned char *, unsigned int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned int fread(unsigned char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
unsigned int fwrite(unsigned char *, int, unsigned int, int /*<<< orphan*/ *);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stat(char *, struct stat *);
int main(int argc, char **argv) {
  if (argc != 3) {
    printf(
        "args error! the command is: mkparameter [src_path] [target_path]\n");
    return -1;
  }
  printf("src_path: %s, target_path: %s\n", argv[1], argv[2]);
  char *src_path = argv[1];
  char *target_path = argv[2];
  FILE *fp_src = fopen(src_path, "rb");
  if (!fp_src) {
    printf("can't open file %s!\n", src_path);
    return -1;
  }
  struct stat srcFileInfo;
  stat(src_path, &srcFileInfo);
  unsigned int uiParmLength = srcFileInfo.st_size;
  printf("the original parameter size: %u\n", uiParmLength);
  unsigned char *buffer = malloc(uiParmLength + 20);
  *(unsigned int *)buffer = 0x4D524150;
  *(unsigned int *)(buffer + 4) = uiParmLength;
  unsigned int readCount = fread(buffer + 8, 1, uiParmLength, fp_src);
  if (readCount != uiParmLength) {
    printf("read original parameter error!\n");
    fclose(fp_src);
    free(buffer);
    return -1;
  }
  *(unsigned int *)(buffer + 8 + uiParmLength) =
      CRC_32(buffer + 8, uiParmLength);
  fclose(fp_src);
  FILE *fp_target = fopen(target_path, "w+b");
  if (!fp_target) {
    printf("can't open file %s\n", target_path);
    free(buffer);
    return -1;
  }
  if (fwrite(buffer, 1, uiParmLength + 12, fp_target) != uiParmLength + 12) {
    printf("write file fail!");
    fclose(fp_target);
    free(buffer);
    return -1;
  }
  fclose(fp_target);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ca <main+0x3a>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13af <main+0x21f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123d <main+0xad>
mov    -0x18(%rbp),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13af <main+0x21f>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x34(%rbp),%edi
add    $0x14,%edi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movl   $0x4d524150,(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x40(%rbp),%rdi
add    $0x8,%rdi
mov    -0x34(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x34(%rbp),%eax
je     12d8 <main+0x148>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13af <main+0x21f>
mov    -0x40(%rbp),%rdi
add    $0x8,%rdi
mov    -0x34(%rbp),%esi
call   1090 <CRC_32@plt>
mov    %eax,%edx
mov    -0x40(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %edx,0x8(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
lea    0xdb1(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1344 <main+0x1b4>
mov    -0x20(%rbp),%rsi
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13af <main+0x21f>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%edx
add    $0xc,%edx
mov    -0x50(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x34(%rbp),%ecx
add    $0xc,%ecx
cmp    %ecx,%eax
je     1396 <main+0x206>
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x50(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13af <main+0x21f>
mov    -0x50(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jne    12a4 <main+0x104>
mov    %rsi,%rbx
mov    0x8(%rsi),%rsi
mov    0x10(%rbx),%rdx
lea    0xe38(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%rbx),%r15
mov    0x10(%rbx),%r14
lea    0xe41(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12b7 <main+0x117>
mov    %rax,%r12
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1080 <stat@plt>
mov    0x8(%rsp),%r13d
lea    0xe2c(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x14(%r13),%edi
call   1090 <malloc@plt>
mov    %rax,%r15
movl   $0x4d524150,(%rax)
mov    %r13d,0x4(%rax)
mov    %rax,%rbx
add    $0x8,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
cmp    %r13d,%eax
jne    12cf <main+0x12f>
mov    %rbx,%rdi
mov    %r13d,%esi
call   10a0 <CRC_32@plt>
mov    %eax,(%rbx,%r13,1)
mov    %r12,%rdi
call   1060 <fclose@plt>
lea    0xdec(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12dd <main+0x13d>
mov    %rax,%r12
add    $0xc,%r13d
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
cmp    %r13d,%eax
jne    12f0 <main+0x150>
mov    %r12,%rdi
call   1060 <fclose@plt>
xor    %ebx,%ebx
jmp    130b <main+0x16b>
lea    0xdf5(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
jmp    1313 <main+0x173>
lea    0xd64(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%ebx
jmp    1313 <main+0x173>
lea    0xdab(%rip),%rdi
call   1040 <puts@plt>
jmp    12fe <main+0x15e>
lea    0xd78(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1306 <main+0x166>
lea    0xd79(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    $0xffffffff,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [66 x i8] c"args error! the command is: mkparameter [src_path] [target_path]\0A\00"
@.str.1 = [31 x i8] c"src_path: %s, target_path: %s\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [21 x i8] c"can't open file %s!\0A\00"
@.str.4 = [33 x i8] c"the original parameter size: %u\0A\00"
@.str.5 = [32 x i8] c"read original parameter error!\0A\00"
@.str.6 = [4 x i8] c"w+b\00"
@.str.7 = [20 x i8] c"can't open file %s\0A\00"
@.str.8 = [17 x i8] c"write file fail!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %111
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* %24)
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %6
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
store i8* %31, i8** %7
%32 = load i8*, i8** %6
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %33, i32** %8
%34 = load i32*, i32** %8
%35 = icmp ne i32* %34, null
br i1 %35, label %39, label %36
36: ; preds = %18
%37 = load i8*, i8** %6
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %37)
store i32 -1, i32* %3
br label %111
39: ; preds = %18
%40 = load i8*, i8** %6
%41 = call i32 @stat(i8* %40, %struct.stat* %9)
%42 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%43 = load i32, i32* %42
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %10
%47 = add i32 %46, 20
%48 = call i8* @malloc(i32 %47)
store i8* %48, i8** %11
%49 = load i8*, i8** %11
%50 = bitcast i8* %49 to i32*
store i32 1297236304, i32* %50
%51 = load i32, i32* %10
%52 = load i8*, i8** %11
%53 = getelementptr inbounds i8, i8* %52, i64 4
%54 = bitcast i8* %53 to i32*
store i32 %51, i32* %54
%55 = load i8*, i8** %11
%56 = getelementptr inbounds i8, i8* %55, i64 8
%57 = load i32, i32* %10
%58 = load i32*, i32** %8
%59 = call i32 @fread(i8* %56, i32 1, i32 %57, i32* %58)
store i32 %59, i32* %12
%60 = load i32, i32* %12
%61 = load i32, i32* %10
%62 = icmp ne i32 %60, %61
br i1 %62, label %63, label %69
63: ; preds = %39
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
%65 = load i32*, i32** %8
%66 = call i32 @fclose(i32* %65)
%67 = load i8*, i8** %11
%68 = call i32 @free(i8* %67)
store i32 -1, i32* %3
br label %111
69: ; preds = %39
%70 = load i8*, i8** %11
%71 = getelementptr inbounds i8, i8* %70, i64 8
%72 = load i32, i32* %10
%73 = call i32 @CRC_32(i8* %71, i32 %72)
%74 = load i8*, i8** %11
%75 = getelementptr inbounds i8, i8* %74, i64 8
%76 = load i32, i32* %10
%77 = zext i32 %76 to i64
%78 = getelementptr inbounds i8, i8* %75, i64 %77
%79 = bitcast i8* %78 to i32*
store i32 %73, i32* %79
%80 = load i32*, i32** %8
%81 = call i32 @fclose(i32* %80)
%82 = load i8*, i8** %7
%83 = call i32* @fopen(i8* %82, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
store i32* %83, i32** %13
%84 = load i32*, i32** %13
%85 = icmp ne i32* %84, null
br i1 %85, label %91, label %86
86: ; preds = %69
%87 = load i8*, i8** %7
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i8* %87)
%89 = load i8*, i8** %11
%90 = call i32 @free(i8* %89)
store i32 -1, i32* %3
br label %111
91: ; preds = %69
%92 = load i8*, i8** %11
%93 = load i32, i32* %10
%94 = add i32 %93, 12
%95 = load i32*, i32** %13
%96 = call i32 @fwrite(i8* %92, i32 1, i32 %94, i32* %95)
%97 = load i32, i32* %10
%98 = add i32 %97, 12
%99 = icmp ne i32 %96, %98
br i1 %99, label %100, label %106
100: ; preds = %91
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%102 = load i32*, i32** %13
%103 = call i32 @fclose(i32* %102)
%104 = load i8*, i8** %11
%105 = call i32 @free(i8* %104)
store i32 -1, i32* %3
br label %111
106: ; preds = %91
%107 = load i32*, i32** %13
%108 = call i32 @fclose(i32* %107)
%109 = load i8*, i8** %11
%110 = call i32 @free(i8* %109)
store i32 0, i32* %3
br label %111
111: ; preds = %106, %100, %86, %63, %36, %16
%112 = load i32, i32* %3
ret i32 %112
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [31 x i8] c"src_path: %s, target_path: %s\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [21 x i8] c"can't open file %s!\0A\00"
@.str.4 = [33 x i8] c"the original parameter size: %u\0A\00"
@.str.6 = [4 x i8] c"w+b\00"
@.str.7 = [20 x i8] c"can't open file %s\0A\00"
@.str.8 = [17 x i8] c"write file fail!\00"
@str = [31 x i8] c"read original parameter error!\00"
@str.9 = [65 x i8] c"args error! the command is: mkparameter [src_path] [target_path]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp eq i32 %0, 3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @str.9, i64 0, i64 0))
br label %58
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* %11)
%13 = load i8*, i8** %8
%14 = load i8*, i8** %10
%15 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %7
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %13)
br label %58
19: ; preds = %7
%20 = bitcast %struct.stat* %3 to i8*
%21 = call i32 @stat(i8* %13, %struct.stat* %3)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = add i32 %23, 20
%26 = tail call i8* @malloc(i32 %25)
%27 = bitcast i8* %26 to i32*
store i32 1297236304, i32* %27
%28 = getelementptr inbounds i8, i8* %26, i64 4
%29 = bitcast i8* %28 to i32*
store i32 %23, i32* %29
%30 = getelementptr inbounds i8, i8* %26, i64 8
%31 = tail call i32 @fread(i8* %30, i32 1, i32 %23, i32* %15)
%32 = icmp eq i32 %31, %23
br i1 %32, label %36, label %33
33: ; preds = %19
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%35 = tail call i32 @fclose(i32* %15)
br label %55
36: ; preds = %19
%37 = tail call i32 @CRC_32(i8* %30, i32 %23)
%38 = zext i32 %23 to i64
%39 = getelementptr inbounds i8, i8* %30, i64 %38
%40 = bitcast i8* %39 to i32*
store i32 %37, i32* %40
%41 = tail call i32 @fclose(i32* %15)
%42 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %46
44: ; preds = %36
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i8* %14)
br label %55
46: ; preds = %36
%47 = add i32 %23, 12
%48 = tail call i32 @fwrite(i8* %26, i32 1, i32 %47, i32* %42)
%49 = icmp eq i32 %48, %47
br i1 %49, label %53, label %50
50: ; preds = %46
%51 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%52 = tail call i32 @fclose(i32* %42)
br label %55
53: ; preds = %46
%54 = tail call i32 @fclose(i32* %42)
br label %55
55: ; preds = %44, %50, %53, %33
%56 = phi i32 [ -1, %33 ], [ -1, %50 ], [ 0, %53 ], [ -1, %44 ]
%57 = tail call i32 @free(i8* %26)
br label %58
58: ; preds = %17, %55, %5
%59 = phi i32 [ -1, %5 ], [ %56, %55 ], [ -1, %17 ]
ret i32 %59
}