#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 {
  long st_size;
};

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
char *malloc(long);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, long);
int stat(char *, struct stat *);
unsigned int strtol(char *, char **, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, long);
int main(int argc, char **argv) {
  int f1, f2, r;
  long size;
  unsigned int offset;
  char *buff;
  struct stat sbuff;
  if (argc != 4) {
    printf("Usage: %s file1 file2 offset\nOverwrites the byte in file1 with "
           "those of file2, starting at offset.\n",
           argv[0]);
    exit(0);
  }
  r = stat(argv[1], &sbuff);
  if (r != 0) {
    perror("Couldn't stat file1.\n");
    exit(1);
  }
  size = sbuff.st_size;
  buff = malloc(size);
  if (buff == NULL) {
    perror("Couldn't malloc bytes for file1.\n");
  }
  f1 = open(argv[1], O_RDONLY);
  if (f1 < 0) {
    perror("Couldn't open file1.\n");
    exit(1);
  }
  read(f1, buff, size);
  close(f1);
  offset = strtol(argv[3], (char **)NULL, 0);
  if (offset > size) {
    printf("Offset bigger than size of file!\n");
    exit(1);
  }
  printf("Splicing in at offset 0x%X.\n", offset);
  f2 = open(argv[2], O_RDONLY);
  if (f2 < 0) {
    perror("Couldn't open file2.\n");
    exit(1);
  }
  read(f2, buff + offset, size);
  close(f2);
  f1 = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY);
  if (f1 < 0) {
    perror("Couldn't open file1 for writing.\n");
    exit(1);
  }
  write(f1, buff, size);
  close(f1);
  printf("All done. Thank you, come again.\n");
  exit(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   $0x4,-0x8(%rbp)
je     11dc <main+0x3c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1210 <main+0x70>
lea    0xe63(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    123c <main+0x9c>
lea    0xe43(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1275 <main+0xd5>
lea    0xe36(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <read@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%rax
jle    12c8 <main+0x128>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x2c(%rbp),%esi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1312 <main+0x172>
lea    0xdee(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%eax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
call   1060 <read@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2cb9(%rip),%rax
mov    (%rax),%esi
mov    0x2ca8(%rip),%rax
or     (%rax),%esi
mov    0x2c6f(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    137b <main+0x1db>
lea    0xd9b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x4,%edi
jne    1214 <main+0x64>
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   1080 <stat@plt>
test   %eax,%eax
jne    122c <main+0x7c>
mov    (%rsp),%r14
mov    %r14,%rdi
call   10a0 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11f3 <main+0x43>
lea    0xe8c(%rip),%rdi
call   10c0 <perror@plt>
mov    0x8(%rbx),%rdi
mov    0x2dda(%rip),%r13
mov    0x0(%r13),%esi
call   10b0 <open@plt>
test   %eax,%eax
jns    1242 <main+0x92>
lea    0xe8a(%rip),%rdi
jmp    1233 <main+0x83>
mov    (%rbx),%rsi
lea    0xde2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xe31(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <read@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x18(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <strtol@plt>
mov    %eax,%r12d
cmp    %r12,%r14
jge    1283 <main+0xd3>
lea    0xeb4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xe28(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rbx),%rdi
mov    0x0(%r13),%esi
call   10b0 <open@plt>
test   %eax,%eax
jns    12ae <main+0xfe>
lea    0xe23(%rip),%rdi
jmp    1233 <main+0x83>
mov    %eax,%ebp
mov    %r15,%rsi
add    %r12,%rsi
mov    %eax,%edi
mov    %r14,%rdx
call   1070 <read@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2d2a(%rip),%rax
mov    0x2d1b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbx),%rdi
mov    0x2cdc(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
test   %eax,%eax
jns    12fb <main+0x14b>
lea    0xdef(%rip),%rdi
jmp    1233 <main+0x83>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xdef(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [100 x i8] c"Usage: %s file1 file2 offset\0AOverwrites the byte in file1 with those of file2, starting at offset.\0A\00"
@.str.1 = [22 x i8] c"Couldn't stat file1.\0A\00"
@.str.2 = [34 x i8] c"Couldn't malloc bytes for file1.\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [22 x i8] c"Couldn't open file1.\0A\00"
@.str.4 = [34 x i8] c"Offset bigger than size of file!\0A\00"
@.str.5 = [29 x i8] c"Splicing in at offset 0x%X.\0A\00"
@.str.6 = [22 x i8] c"Couldn't open file2.\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@.str.7 = [34 x i8] c"Couldn't open file1 for writing.\0A\00"
@.str.8 = [34 x i8] c"All done. Thank you, come again.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 4
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @stat(i8* %24, %struct.stat* %12)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %21
%32 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%33 = load i64, i64* %32
store i64 %33, i64* %9
%34 = load i64, i64* %9
%35 = call i8* @malloc(i64 %34)
store i8* %35, i8** %11
%36 = load i8*, i8** %11
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %31
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = load i32, i32* @O_RDONLY
%45 = call i32 @open(i8* %43, i32 %44)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %40
%52 = load i32, i32* %6
%53 = load i8*, i8** %11
%54 = load i64, i64* %9
%55 = call i32 @read(i32 %52, i8* %53, i64 %54)
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 3
%60 = load i8*, i8** %59
%61 = call i32 @strtol(i8* %60, i8** null, i32 0)
store i32 %61, i32* %10
%62 = load i32, i32* %10
%63 = zext i32 %62 to i64
%64 = load i64, i64* %9
%65 = icmp sgt i64 %63, %64
br i1 %65, label %66, label %69
66: ; preds = %51
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %51
%70 = load i32, i32* %10
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %70)
%72 = load i8**, i8*** %5
%73 = getelementptr inbounds i8*, i8** %72, i64 2
%74 = load i8*, i8** %73
%75 = load i32, i32* @O_RDONLY
%76 = call i32 @open(i8* %74, i32 %75)
store i32 %76, i32* %7
%77 = load i32, i32* %7
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %69
%80 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %69
%83 = load i32, i32* %7
%84 = load i8*, i8** %11
%85 = load i32, i32* %10
%86 = zext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %84, i64 %86
%88 = load i64, i64* %9
%89 = call i32 @read(i32 %83, i8* %87, i64 %88)
%90 = load i32, i32* %7
%91 = call i32 @close(i32 %90)
%92 = load i8**, i8*** %5
%93 = getelementptr inbounds i8*, i8** %92, i64 1
%94 = load i8*, i8** %93
%95 = load i32, i32* @O_CREAT
%96 = load i32, i32* @O_TRUNC
%97 = or i32 %95, %96
%98 = load i32, i32* @O_WRONLY
%99 = or i32 %97, %98
%100 = call i32 @open(i8* %94, i32 %99)
store i32 %100, i32* %6
%101 = load i32, i32* %6
%102 = icmp slt i32 %101, 0
br i1 %102, label %103, label %106
103: ; preds = %82
%104 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
%105 = call i32 @exit(i32 1)
unreachable
106: ; preds = %82
%107 = load i32, i32* %6
%108 = load i8*, i8** %11
%109 = load i64, i64* %9
%110 = call i32 @write(i32 %107, i8* %108, i64 %109)
%111 = load i32, i32* %6
%112 = call i32 @close(i32 %111)
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
%114 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [100 x i8] c"Usage: %s file1 file2 offset\0AOverwrites the byte in file1 with those of file2, starting at offset.\0A\00"
@.str.1 = [22 x i8] c"Couldn't stat file1.\0A\00"
@.str.2 = [34 x i8] c"Couldn't malloc bytes for file1.\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [22 x i8] c"Couldn't open file1.\0A\00"
@.str.5 = [29 x i8] c"Splicing in at offset 0x%X.\0A\00"
@.str.6 = [22 x i8] c"Couldn't open file2.\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@.str.7 = [34 x i8] c"Couldn't open file1 for writing.\0A\00"
@str = [33 x i8] c"All done. Thank you, come again.\00"
@str.9 = [33 x i8] c"Offset bigger than size of file!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 4
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([100 x i8], [100 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = call i32 @stat(i8* %12, %struct.stat* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i64, i64* %19
%21 = tail call i8* @malloc(i64 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
%26 = load i8*, i8** %11
%27 = load i32, i32* @O_RDONLY
%28 = tail call i32 @open(i8* %26, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %25
%34 = tail call i32 @read(i32 %28, i8* %21, i64 %20)
%35 = tail call i32 @close(i32 %28)
%36 = getelementptr inbounds i8*, i8** %1, i64 3
%37 = load i8*, i8** %36
%38 = tail call i32 @strtol(i8* nocapture %37, i8** null, i32 0)
%39 = zext i32 %38 to i64
%40 = icmp slt i64 %20, %39
br i1 %40, label %41, label %44
41: ; preds = %33
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.9, i64 0, i64 0))
%43 = tail call i32 @exit(i32 1)
unreachable
44: ; preds = %33
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%46 = getelementptr inbounds i8*, i8** %1, i64 2
%47 = load i8*, i8** %46
%48 = load i32, i32* @O_RDONLY
%49 = tail call i32 @open(i8* %47, i32 %48)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%53 = tail call i32 @exit(i32 1)
unreachable
54: ; preds = %44
%55 = getelementptr inbounds i8, i8* %21, i64 %39
%56 = tail call i32 @read(i32 %49, i8* %55, i64 %20)
%57 = tail call i32 @close(i32 %49)
%58 = load i8*, i8** %11
%59 = load i32, i32* @O_CREAT
%60 = load i32, i32* @O_TRUNC
%61 = or i32 %60, %59
%62 = load i32, i32* @O_WRONLY
%63 = or i32 %61, %62
%64 = tail call i32 @open(i8* %58, i32 %63)
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %54
%67 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
%68 = tail call i32 @exit(i32 1)
unreachable
69: ; preds = %54
%70 = tail call i32 @write(i32 %64, i8* %21, i64 %20)
%71 = tail call i32 @close(i32 %64)
%72 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%73 = tail call i32 @exit(i32 0)
unreachable
}