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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t get_num(char *);
char *mmapfile(char *, size_t *);
int open(char *, int /*<<< orphan*/);
size_t pwrite(int, char *, size_t, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usage();
int main(int ac, char **av) {
  char *patch;
  size_t patchsize;
  int infd;
  size_t offset;
  if (ac != 5 && ac != 4)
    usage();
  offset = get_num(av[2]);
  patch = mmapfile(av[3], &patchsize);
  if (av[4]) {
    size_t newsize = get_num(av[4]);
    if (newsize > patchsize)
      fprintf(stderr, "kallsyms: warning, size larger than patch\n");
    if (newsize < patchsize)
      patchsize = newsize;
  }
  infd = open(av[1], O_RDWR);
  if (infd < 0) {
    fprintf(stderr, "Cannot open %s\n", av[1]);
    exit(1);
  }
  if (pwrite(infd, patch, patchsize, offset) != patchsize) {
    fprintf(stderr, "Cannot write patch to %s\n", av[1]);
    exit(1);
  }
  close(infd);
  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)
je     11b1 <main+0x31>
cmpl   $0x4,-0x8(%rbp)
je     11b1 <main+0x31>
mov    $0x0,%al
call   1080 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <get_num@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <mmapfile@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x20(%rax)
je     1237 <main+0xb7>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1040 <get_num@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    121c <main+0x9c>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1232 <main+0xb2>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1237 <main+0xb7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1283 <main+0x103>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1070 <pwrite@plt>
cmp    -0x20(%rbp),%rax
je     12ca <main+0x14a>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
and    $0xfffffffe,%edi
cmp    $0x4,%edi
je     119a <main+0x1a>
xor    %eax,%eax
call   1080 <usage@plt>
mov    0x10(%r12),%rdi
call   1040 <get_num@plt>
mov    %rax,%r14
mov    0x18(%r12),%rdi
mov    %rsp,%rsi
call   1030 <mmapfile@plt>
mov    %rax,%r15
mov    0x20(%r12),%rdi
test   %rdi,%rdi
je     11f6 <main+0x76>
call   1040 <get_num@plt>
mov    %rax,%rbx
mov    (%rsp),%rax
cmp    %rax,%rbx
jbe    11ed <main+0x6d>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rsp),%rax
cmp    %rax,%rbx
jae    11f6 <main+0x76>
mov    %rbx,(%rsp)
mov    0x8(%r12),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     123b <main+0xbb>
mov    %eax,%ebx
mov    (%rsp),%rdx
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rcx
call   1070 <pwrite@plt>
cmp    (%rsp),%rax
jne    1252 <main+0xd2>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r12),%rdx
lea    0xddb(%rip),%rsi
jmp    1267 <main+0xe7>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r12),%rdx
lea    0xdd4(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [43 x i8] c"kallsyms: warning, size larger than patch\0A\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"Cannot open %s\0A\00"
@.str.2 = [26 x i8] c"Cannot write patch to %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 5
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 4
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 (...) @usage()
br label %18
18: ; preds = %16, %13, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i64 @get_num(i8* %21)
store i64 %22, i64* %9
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 3
%25 = load i8*, i8** %24
%26 = call i8* @mmapfile(i8* %25, i64* %7)
store i8* %26, i8** %6
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 4
%29 = load i8*, i8** %28
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %49
31: ; preds = %18
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 4
%34 = load i8*, i8** %33
%35 = call i64 @get_num(i8* %34)
store i64 %35, i64* %10
%36 = load i64, i64* %10
%37 = load i64, i64* %7
%38 = icmp ugt i64 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %39, %31
%43 = load i64, i64* %10
%44 = load i64, i64* %7
%45 = icmp ult i64 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = load i64, i64* %10
store i64 %47, i64* %7
br label %48
48: ; preds = %46, %42
br label %49
49: ; preds = %48, %18
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = load i32, i32* @O_RDWR
%54 = call i32 @open(i8* %52, i32 %53)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %64
57: ; preds = %49
%58 = load i32, i32* @stderr
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 1
%61 = load i8*, i8** %60
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %61)
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %49
%65 = load i32, i32* %8
%66 = load i8*, i8** %6
%67 = load i64, i64* %7
%68 = load i64, i64* %9
%69 = call i64 @pwrite(i32 %65, i8* %66, i64 %67, i64 %68)
%70 = load i64, i64* %7
%71 = icmp ne i64 %69, %70
br i1 %71, label %72, label %79
72: ; preds = %64
%73 = load i32, i32* @stderr
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 1
%76 = load i8*, i8** %75
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %76)
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %64
%80 = load i32, i32* %8
%81 = call i32 @close(i32 %80)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [43 x i8] c"kallsyms: warning, size larger than patch\0A\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"Cannot open %s\0A\00"
@.str.2 = [26 x i8] c"Cannot write patch to %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = and i32 %0, -2
%6 = icmp eq i32 %5, 4
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 (...) @usage()
br label %9
9: ; preds = %7, %2
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i64 @get_num(i8* %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 3
%14 = load i8*, i8** %13
%15 = call i8* @mmapfile(i8* %14, i64* %3)
%16 = getelementptr inbounds i8*, i8** %1, i64 4
%17 = load i8*, i8** %16
%18 = icmp eq i8* %17, null
br i1 %18, label %31, label %19
19: ; preds = %9
%20 = call i64 @get_num(i8* %17)
%21 = load i64, i64* %3
%22 = icmp ugt i64 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%26 = load i64, i64* %3
br label %27
27: ; preds = %23, %19
%28 = phi i64 [ %26, %23 ], [ %21, %19 ]
%29 = icmp ult i64 %20, %28
br i1 %29, label %30, label %31
30: ; preds = %27
store i64 %20, i64* %3
br label %31
31: ; preds = %27, %30, %9
%32 = getelementptr inbounds i8*, i8** %1, i64 1
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_RDWR
%35 = call i32 @open(i8* %33, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %31
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %32
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %31
%43 = load i64, i64* %3
%44 = call i64 @pwrite(i32 %35, i8* %15, i64 %43, i64 %12)
%45 = load i64, i64* %3
%46 = icmp eq i64 %44, %45
br i1 %46, label %52, label %47
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %32
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %42
%53 = call i32 @close(i32 %35)
ret i32 0
}