#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_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned long,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  if (argc < 5) {
    printf("Usage:\n");
    printf("  %s <hex offset> <string to write> <maxlen> <file>\n", argv[0]);
    return -1;
  }
  unsigned long offset = strtoul(argv[1], NULL, 16);
  char *replacement = argv[2];
  unsigned long maxlen = strtoul(argv[3], NULL, 10);
  FILE *f = fopen(argv[4], "r+");
  if (!f) {
    printf("ERROR: Could not open %s for writing!\n", argv[4]);
    return -1;
  }
  if (strlen(replacement) > maxlen) {
    printf("ERROR: Replacement string length (%lu) is greater than maxlen! "
           "(%lu)\n",
           strlen(replacement), maxlen);
    return -1;
  }
  fseek(f, offset, SEEK_SET);
  fwrite(replacement, strlen(replacement) + 1, 1, f);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    11bf <main+0x4f>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d0 <main+0x160>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
lea    0xe2f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1244 <main+0xd4>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rsi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d0 <main+0x160>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cltq
cmp    -0x28(%rbp),%rax
jbe    1282 <main+0x112>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%esi
mov    -0x28(%rbp),%rdx
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d0 <main+0x160>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
add    $0x1,%esi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x4,%edi
jg     11a9 <main+0x29>
lea    0xf0d(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%rsi
lea    0xe59(%rip),%rdi
jmp    1222 <main+0xa2>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <strtoul@plt>
mov    %rax,%r12
mov    0x10(%rbx),%r14
mov    0x18(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1090 <strtoul@plt>
mov    %rax,%r13
mov    0x20(%rbx),%rdi
lea    0xe57(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1217 <main+0x97>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <strlen@plt>
cltq
cmp    %rax,%r13
jae    1238 <main+0xb8>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0xe56(%rip),%rdi
mov    %eax,%esi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1229 <main+0xa9>
mov    0x20(%rbx),%rsi
lea    0xe16(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <fseek@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%esi
mov    %r14,%rdi
mov    $0x1,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    122e <main+0xae>

LLVM IR (-O0):
@.str = [8 x i8] c"Usage:\0A\00"
@.str.1 = [53 x i8] c" %s <hex offset> <string to write> <maxlen> <file>\0A\00"
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [39 x i8] c"ERROR: Could not open %s for writing!\0A\00"
@.str.4 = [70 x i8] c"ERROR: Replacement string length (%lu) is greater than maxlen! (%lu)\0A\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 5
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %65
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @strtoul(i8* %21, i32* null, i32 16)
store i64 %22, i64* %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
store i8* %25, i8** %7
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 3
%28 = load i8*, i8** %27
%29 = call i64 @strtoul(i8* %28, i32* null, i32 10)
store i64 %29, i64* %8
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 4
%32 = load i8*, i8** %31
%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** %9
%34 = load i32*, i32** %9
%35 = icmp ne i32* %34, null
br i1 %35, label %41, label %36
36: ; preds = %18
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 4
%39 = load i8*, i8** %38
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %39)
store i32 -1, i32* %3
br label %65
41: ; preds = %18
%42 = load i8*, i8** %7
%43 = call i32 @strlen(i8* %42)
%44 = sext i32 %43 to i64
%45 = load i64, i64* %8
%46 = icmp ugt i64 %44, %45
br i1 %46, label %47, label %52
47: ; preds = %41
%48 = load i8*, i8** %7
%49 = call i32 @strlen(i8* %48)
%50 = load i64, i64* %8
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.4, i64 0, i64 0), i32 %49, i64 %50)
store i32 -1, i32* %3
br label %65
52: ; preds = %41
%53 = load i32*, i32** %9
%54 = load i64, i64* %6
%55 = load i32, i32* @SEEK_SET
%56 = call i32 @fseek(i32* %53, i64 %54, i32 %55)
%57 = load i8*, i8** %7
%58 = load i8*, i8** %7
%59 = call i32 @strlen(i8* %58)
%60 = add nsw i32 %59, 1
%61 = load i32*, i32** %9
%62 = call i32 @fwrite(i8* %57, i32 %60, i32 1, i32* %61)
%63 = load i32*, i32** %9
%64 = call i32 @fclose(i32* %63)
store i32 0, i32* %3
br label %65
65: ; preds = %52, %47, %36, %12
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str.1 = [53 x i8] c" %s <hex offset> <string to write> <maxlen> <file>\0A\00"
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [39 x i8] c"ERROR: Could not open %s for writing!\0A\00"
@.str.4 = [70 x i8] c"ERROR: Replacement string length (%lu) is greater than maxlen! (%lu)\0A\00"
@SEEK_SET = global i32 0
@str = [7 x i8] c"Usage:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 5
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %38
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @strtoul(i8* nocapture %10, i32* null, i32 16)
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = tail call i64 @strtoul(i8* nocapture %15, i32* null, i32 10)
%17 = getelementptr inbounds i8*, i8** %1, i64 4
%18 = load i8*, i8** %17
%19 = tail call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %8
%22 = load i8*, i8** %17
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %22)
br label %38
24: ; preds = %8
%25 = tail call i32 @strlen(i8* %13)
%26 = sext i32 %25 to i64
%27 = icmp ult i64 %16, %26
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = tail call i32 @strlen(i8* %13)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([70 x i8], [70 x i8]* @.str.4, i64 0, i64 0), i32 %29, i64 %16)
br label %38
31: ; preds = %24
%32 = load i32, i32* @SEEK_SET
%33 = tail call i32 @fseek(i32* %19, i64 %11, i32 %32)
%34 = tail call i32 @strlen(i8* %13)
%35 = add nsw i32 %34, 1
%36 = tail call i32 @fwrite(i8* %13, i32 %35, i32 1, i32* %19)
%37 = tail call i32 @fclose(i32* %19)
br label %38
38: ; preds = %21, %28, %31, %4
%39 = phi i32 [ -1, %4 ], [ -1, %28 ], [ 0, %31 ], [ -1, %21 ]
ret i32 %39
}