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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
char atoi(char *);
int /*<<< orphan*/ atol(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  char val;
  int fd;
  if (argc != 3 && argc != 4) {
    fprintf(stderr, "usage: vpatch filename loc [val]\n");
    exit(1);
  }
  if (argc == 3) {
    fd = open(argv[1], O_RDWR);
    if (fd < 0) {
      perror("open");
      exit(1);
    }
    if (lseek(fd, atol(argv[2]), SEEK_SET) < 0) {
      perror("lseek");
      exit(1);
    }
    if (read(fd, &val, sizeof(val)) != sizeof(val)) {
      perror("read");
      exit(1);
    }
    printf("0x%x\n", val & 255);
    exit(0);
  }
  val = atoi(argv[3]);
  fd = open(argv[1], O_RDWR);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  if (lseek(fd, atol(argv[2]), SEEK_SET) < 0) {
    perror("lseek");
    exit(1);
  }
  if (write(fd, &val, sizeof(val)) != sizeof(val)) {
    perror("write");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11e9 <main+0x49>
cmpl   $0x4,-0x8(%rbp)
je     11e9 <main+0x49>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
jne    12c4 <main+0x124>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    122c <main+0x8c>
lea    0xe05(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atol@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%esi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    1272 <main+0xd2>
lea    0xdc4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cltq
cmp    $0x1,%rax
je     12a5 <main+0x105>
lea    0xd97(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movsbl -0x11(%rbp),%eax
and    $0xff,%eax
lea    0xd7d(%rip),%rdi
movsbl %al,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    130d <main+0x16d>
lea    0xd24(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atol@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%esi
mov    0x2ca4(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    1353 <main+0x1b3>
lea    0xce3(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cltq
cmp    $0x1,%rax
je     1386 <main+0x1e6>
lea    0xcc1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    -0x5(%rdi),%eax
cmp    $0xfffffffd,%eax
ja     11ca <main+0x2a>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1240 <main+0xa0>
mov    0x8(%rbx),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     1263 <main+0xc3>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1080 <atol@plt>
mov    0x2dd6(%rip),%rcx
mov    (%rcx),%edx
mov    %ebp,%edi
mov    %eax,%esi
call   1050 <lseek@plt>
test   %rax,%rax
js     128e <main+0xee>
lea    0x7(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    12b4 <main+0x114>
movsbl 0x7(%rsp),%esi
lea    0xdfe(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    0x18(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %al,0x7(%rsp)
mov    0x8(%rbx),%rdi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
jns    126c <main+0xcc>
lea    0xdb8(%rip),%rdi
jmp    12c4 <main+0x124>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1080 <atol@plt>
mov    0x2d52(%rip),%rcx
mov    (%rcx),%edx
mov    %ebp,%edi
mov    %eax,%esi
call   1050 <lseek@plt>
test   %rax,%rax
jns    1297 <main+0xf7>
lea    0xd92(%rip),%rdi
jmp    12c4 <main+0x124>
lea    0x7(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    12bd <main+0x11d>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xd72(%rip),%rdi
jmp    12c4 <main+0x124>
lea    0xd74(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [34 x i8] c"usage: vpatch filename loc [val]\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [6 x i8] c"0x%x\0A\00"
@.str.5 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 3
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 4
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %10, %2
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 3
br i1 %19, label %20, label %58
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %20
%32 = load i32, i32* %7
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32 @atol(i8* %35)
%37 = load i32, i32* @SEEK_SET
%38 = call i64 @lseek(i32 %32, i32 %36, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %31
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %31
%44 = load i32, i32* %7
%45 = call i32 @read(i32 %44, i8* %6, i32 1)
%46 = sext i32 %45 to i64
%47 = icmp ne i64 %46, 1
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %43
%52 = load i8, i8* %6
%53 = sext i8 %52 to i32
%54 = and i32 %53, 255
%55 = trunc i32 %54 to i8
%56 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8 signext %55)
%57 = call i32 @exit(i32 0)
unreachable
58: ; preds = %17
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 3
%61 = load i8*, i8** %60
%62 = call signext i8 @atoi(i8* %61)
store i8 %62, i8* %6
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 1
%65 = load i8*, i8** %64
%66 = load i32, i32* @O_RDWR
%67 = call i32 @open(i8* %65, i32 %66)
store i32 %67, i32* %7
%68 = load i32, i32* %7
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %58
%71 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %58
%74 = load i32, i32* %7
%75 = load i8**, i8*** %5
%76 = getelementptr inbounds i8*, i8** %75, i64 2
%77 = load i8*, i8** %76
%78 = call i32 @atol(i8* %77)
%79 = load i32, i32* @SEEK_SET
%80 = call i64 @lseek(i32 %74, i32 %78, i32 %79)
%81 = icmp slt i64 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %73
%83 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %73
%86 = load i32, i32* %7
%87 = call i32 @write(i32 %86, i8* %6, i32 1)
%88 = sext i32 %87 to i64
%89 = icmp ne i64 %88, 1
br i1 %89, label %90, label %93
90: ; preds = %85
%91 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%92 = call i32 @exit(i32 1)
unreachable
93: ; preds = %85
%94 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"usage: vpatch filename loc [val]\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [6 x i8] c"0x%x\0A\00"
@.str.5 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = add i32 %0, -5
%5 = icmp ult i32 %4, -2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = icmp eq i32 %0, 3
br i1 %11, label %12, label %41
12: ; preds = %10
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = tail call i32 @atol(i8* %23)
%25 = load i32, i32* @SEEK_SET
%26 = tail call i64 @lseek(i32 %16, i32 %24, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %21
%32 = call i32 @read(i32 %16, i8* %3, i32 1)
%33 = icmp eq i32 %32, 1
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %31
%38 = load i8, i8* %3
%39 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8 signext %38)
%40 = tail call i32 @exit(i32 0)
unreachable
41: ; preds = %10
%42 = getelementptr inbounds i8*, i8** %1, i64 3
%43 = load i8*, i8** %42
%44 = tail call signext i8 @atoi(i8* %43)
store i8 %44, i8* %3
%45 = getelementptr inbounds i8*, i8** %1, i64 1
%46 = load i8*, i8** %45
%47 = load i32, i32* @O_RDWR
%48 = tail call i32 @open(i8* %46, i32 %47)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %41
%51 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%52 = tail call i32 @exit(i32 1)
unreachable
53: ; preds = %41
%54 = getelementptr inbounds i8*, i8** %1, i64 2
%55 = load i8*, i8** %54
%56 = tail call i32 @atol(i8* %55)
%57 = load i32, i32* @SEEK_SET
%58 = tail call i64 @lseek(i32 %48, i32 %56, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %53
%61 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%62 = tail call i32 @exit(i32 1)
unreachable
63: ; preds = %53
%64 = call i32 @write(i32 %48, i8* %3, i32 1)
%65 = icmp eq i32 %64, 1
br i1 %65, label %69, label %66
66: ; preds = %63
%67 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%68 = tail call i32 @exit(i32 1)
unreachable
69: ; preds = %63
%70 = tail call i32 @exit(i32 0)
unreachable
}