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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ printf(char *, char *, int, ...);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *);
int main(int argc, char **argv) {
  char *orig = argv[1];
  unsigned int patchoffset;
  char *patch = argv[3];
  char *dest = argv[4];
  unsigned char *buffer;
  int origsize;
  int readed;
  sscanf(argv[2], "%x", &patchoffset);
  printf("patching %s at %0x(%s) with %s to %s\n", orig, patchoffset, argv[2],
         patch, dest);
  FILE *f = fopen(orig, "r");
  fseek(f, 0, SEEK_END);
  origsize = ftell(f);
  fseek(f, 0, SEEK_SET);
  buffer = malloc(origsize);
  readed = fread(buffer, 1, origsize, f);
  printf("orig %s (%d bytes)\n", orig, readed);
  fclose(f);
  f = fopen(patch, "r");
  readed = fread(buffer + patchoffset, 1, origsize - patchoffset, f);
  printf("patch %s (%d bytes)\n", patch, readed);
  fclose(f);
  f = fopen(dest, "w");
  readed = fwrite(buffer, 1, origsize, f);
  printf("desc %s (%d bytes)\n", dest, readed);
  fclose(f);
  free(buffer);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe1e(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1090 <sscanf@plt>
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x48(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x48(%rbp),%rdi
mov    0x2d89(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x3c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x48(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x40(%rbp),%edx
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
lea    0xd84(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    %rax,%rdi
mov    -0x3c(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    -0x48(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x40(%rbp),%edx
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
lea    0xd5c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x48(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    %eax,-0x40(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%edx
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x8(%rsi),%r12
mov    0x10(%rsi),%rdi
mov    0x18(%rsi),%r14
mov    0x20(%rsi),%rbp
lea    0xe3b(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1090 <sscanf@plt>
mov    0x4(%rsp),%edx
mov    0x10(%rbx),%rcx
lea    0xe25(%rip),%rdi
mov    %r12,%rsi
mov    %r14,%r8
mov    %r14,%rbx
mov    %rbp,%r9
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe31(%rip),%rsi
mov    %r12,%rdi
call   10b0 <fopen@plt>
mov    %rax,%r13
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r13,%rdi
call   1070 <ftell@plt>
mov    %eax,%r15d
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r15d,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
lea    0xdd2(%rip),%rdi
mov    %r12,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
lea    0xdb2(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r12
mov    0x4(%rsp),%edi
mov    %r15d,%edx
sub    %edi,%edx
add    %r14,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   1040 <fread@plt>
lea    0xda0(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
lea    0xd9a(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
lea    0xd77(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%x\00"
@.str.1 = [38 x i8] c"patching %s at %0x(%s) with %s to %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [20 x i8] c"orig %s (%d bytes)\0A\00"
@.str.4 = [21 x i8] c"patch %s (%d bytes)\0A\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [20 x i8] c"desc %s (%d bytes)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%13 = load i8**, i8*** %4
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %5
%16 = load i8**, i8*** %4
%17 = getelementptr inbounds i8*, i8** %16, i64 3
%18 = load i8*, i8** %17
store i8* %18, i8** %7
%19 = load i8**, i8*** %4
%20 = getelementptr inbounds i8*, i8** %19, i64 4
%21 = load i8*, i8** %20
store i8* %21, i8** %8
%22 = load i8**, i8*** %4
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%26 = load i8*, i8** %5
%27 = load i32, i32* %6
%28 = load i8**, i8*** %4
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = load i8*, i8** %7
%32 = load i8*, i8** %8
%33 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 %27, i8* %30, i8* %31, i8* %32)
%34 = load i8*, i8** %5
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %35, i32** %12
%36 = load i32*, i32** %12
%37 = load i32, i32* @SEEK_END
%38 = call i32 @fseek(i32* %36, i32 0, i32 %37)
%39 = load i32*, i32** %12
%40 = call i32 @ftell(i32* %39)
store i32 %40, i32* %10
%41 = load i32*, i32** %12
%42 = load i32, i32* @SEEK_SET
%43 = call i32 @fseek(i32* %41, i32 0, i32 %42)
%44 = load i32, i32* %10
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %9
%46 = load i8*, i8** %9
%47 = load i32, i32* %10
%48 = load i32*, i32** %12
%49 = call i32 @fread(i8* %46, i32 1, i32 %47, i32* %48)
store i32 %49, i32* %11
%50 = load i8*, i8** %5
%51 = load i32, i32* %11
%52 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %50, i32 %51)
%53 = load i32*, i32** %12
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** %7
%56 = call i32* @fopen(i8* %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %56, i32** %12
%57 = load i8*, i8** %9
%58 = load i32, i32* %6
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i32, i32* %10
%62 = load i32, i32* %6
%63 = sub i32 %61, %62
%64 = load i32*, i32** %12
%65 = call i32 @fread(i8* %60, i32 1, i32 %63, i32* %64)
store i32 %65, i32* %11
%66 = load i8*, i8** %7
%67 = load i32, i32* %11
%68 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %66, i32 %67)
%69 = load i32*, i32** %12
%70 = call i32 @fclose(i32* %69)
%71 = load i8*, i8** %8
%72 = call i32* @fopen(i8* %71, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %72, i32** %12
%73 = load i8*, i8** %9
%74 = load i32, i32* %10
%75 = load i32*, i32** %12
%76 = call i32 @fwrite(i8* %73, i32 1, i32 %74, i32* %75)
store i32 %76, i32* %11
%77 = load i8*, i8** %8
%78 = load i32, i32* %11
%79 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %77, i32 %78)
%80 = load i32*, i32** %12
%81 = call i32 @fclose(i32* %80)
%82 = load i8*, i8** %9
%83 = call i32 @free(i8* %82)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%x\00"
@.str.1 = [38 x i8] c"patching %s at %0x(%s) with %s to %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [20 x i8] c"orig %s (%d bytes)\0A\00"
@.str.4 = [21 x i8] c"patch %s (%d bytes)\0A\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [20 x i8] c"desc %s (%d bytes)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds i8*, i8** %1, i64 3
%8 = load i8*, i8** %7
%9 = getelementptr inbounds i8*, i8** %1, i64 4
%10 = load i8*, i8** %9
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%14 = load i32, i32* %3
%15 = load i8*, i8** %11
%16 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %5, i32 %14, i8* %15, i8* %8, i8* %10)
%17 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = call i32 @ftell(i32* %17)
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %17, i32 0, i32 %21)
%23 = call i8* @malloc(i32 %20)
%24 = call i32 @fread(i8* %23, i32 1, i32 %20, i32* %17)
%25 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %5, i32 %24)
%26 = call i32 @fclose(i32* %17)
%27 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %3
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %23, i64 %29
%31 = sub i32 %20, %28
%32 = call i32 @fread(i8* %30, i32 1, i32 %31, i32* %27)
%33 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %8, i32 %32)
%34 = call i32 @fclose(i32* %27)
%35 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%36 = call i32 @fwrite(i8* %23, i32 1, i32 %20, i32* %35)
%37 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %10, i32 %36)
%38 = call i32 @fclose(i32* %35)
%39 = call i32 @free(i8* %23)
ret i32 0
}