#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ stderr;
int strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  int offset, length;
  char ch = '\0';
  char *buf;
  char *filename;
  FILE *f;
  if (argc < 4 || argc > 5) {
    fprintf(stderr,
            "corrupt: usage: corrupt filename offset length [character]");
    return EXIT_FAILURE;
  }
  filename = argv[1];
  offset = strtol(argv[2], NULL, 10);
  length = strtol(argv[3], NULL, 10);
  if (argc > 4)
    ch = argv[4][0];
  f = fopen(filename, "r+b");
  if (f == NULL) {
    fprintf(stderr, "corrupt: unable to open file: %s\n", filename);
    return EXIT_FAILURE;
  }
  if (fseek(f, offset, SEEK_SET) != 0) {
    fprintf(stderr, "corrupt: fseek failure.\n");
    return EXIT_FAILURE;
  }
  buf = malloc(length);
  if (buf == NULL) {
    fprintf(stderr, "corrupt: unable to allocate memory.");
    return EXIT_FAILURE;
  }
  memset(buf, ch, length);
  if (fwrite(buf, 1, length, f) != length) {
    fprintf(stderr, "corrupt: did not write %d characters.", length);
    return EXIT_FAILURE;
  }
  fclose(f);
  return EXIT_SUCCESS;
}


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)
movb   $0x0,-0x19(%rbp)
cmpl   $0x4,-0x8(%rbp)
jl     11ae <main+0x2e>
cmpl   $0x5,-0x8(%rbp)
jle    11d6 <main+0x56>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136c <main+0x1ec>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x4,-0x8(%rbp)
jle    122b <main+0xab>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rax
mov    (%rax),%al
mov    %al,-0x19(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1276 <main+0xf6>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136c <main+0x1ec>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2d54(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     12bd <main+0x13d>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136c <main+0x1ec>
mov    -0x18(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12fc <main+0x17c>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136c <main+0x1ec>
mov    -0x28(%rbp),%rdi
mov    -0x19(%rbp),%al
mov    -0x18(%rbp),%edx
movsbl %al,%esi
call   1040 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
cmp    -0x18(%rbp),%eax
je     1357 <main+0x1d7>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xd5f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2c83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136c <main+0x1ec>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2c61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebp
lea    -0x6(%rbp),%eax
cmp    $0xfffffffd,%eax
ja     11a7 <main+0x27>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
jmp    1223 <main+0xa3>
mov    %rsi,%rbx
mov    0x8(%rsi),%r12
mov    0x10(%rsi),%rdi
xor    %r13d,%r13d
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,%r15d
mov    0x18(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,%r14d
cmp    $0x5,%ebp
jb     11e3 <main+0x63>
mov    0x20(%rbx),%rax
mov    (%rax),%r13b
lea    0xe51(%rip),%rsi
mov    %r12,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1242 <main+0xc2>
mov    %rax,%rbx
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1080 <fseek@plt>
test   %rax,%rax
je     125e <main+0xde>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    122a <main+0xaa>
mov    %r14d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     12a6 <main+0x126>
mov    %rax,%rbp
movsbl %r13b,%esi
mov    %rax,%rdi
mov    %r14d,%edx
call   1040 <memset@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
cmp    %r14d,%eax
jne    12bb <main+0x13b>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2d24(%rip),%rax
jmp    1231 <main+0xb1>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
jmp    1223 <main+0xa3>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    122a <main+0xaa>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [59 x i8] c"corrupt: usage: corrupt filename offset length [character]\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [4 x i8] c"r+b\00"
@.str.2 = [34 x i8] c"corrupt: unable to open file: %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [25 x i8] c"corrupt: fseek failure.\0A\00"
@.str.4 = [36 x i8] c"corrupt: unable to allocate memory.\00"
@.str.5 = [38 x i8] c"corrupt: did not write %d characters.\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 0, i8* %8
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 4
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 5
br i1 %16, label %17, label %21
17: ; preds = %14, %2
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
store i32 %20, i32* %3
br label %90
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %10
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @strtol(i8* %27, i32* null, i32 10)
store i32 %28, i32* %6
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 3
%31 = load i8*, i8** %30
%32 = call i32 @strtol(i8* %31, i32* null, i32 10)
store i32 %32, i32* %7
%33 = load i32, i32* %4
%34 = icmp sgt i32 %33, 4
br i1 %34, label %35, label %41
35: ; preds = %21
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 4
%38 = load i8*, i8** %37
%39 = getelementptr inbounds i8, i8* %38, i64 0
%40 = load i8, i8* %39
store i8 %40, i8* %8
br label %41
41: ; preds = %35, %21
%42 = load i8*, i8** %10
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %43, i32** %11
%44 = load i32*, i32** %11
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %51
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %10
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = load i32, i32* @EXIT_FAILURE
store i32 %50, i32* %3
br label %90
51: ; preds = %41
%52 = load i32*, i32** %11
%53 = load i32, i32* %6
%54 = load i32, i32* @SEEK_SET
%55 = call i64 @fseek(i32* %52, i32 %53, i32 %54)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %51
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
store i32 %60, i32* %3
br label %90
61: ; preds = %51
%62 = load i32, i32* %7
%63 = call i8* @malloc(i32 %62)
store i8* %63, i8** %9
%64 = load i8*, i8** %9
%65 = icmp eq i8* %64, null
br i1 %65, label %66, label %70
66: ; preds = %61
%67 = load i32, i32* @stderr
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%69 = load i32, i32* @EXIT_FAILURE
store i32 %69, i32* %3
br label %90
70: ; preds = %61
%71 = load i8*, i8** %9
%72 = load i8, i8* %8
%73 = load i32, i32* %7
%74 = call i32 @memset(i8* %71, i8 signext %72, i32 %73)
%75 = load i8*, i8** %9
%76 = load i32, i32* %7
%77 = load i32*, i32** %11
%78 = call i32 @fwrite(i8* %75, i32 1, i32 %76, i32* %77)
%79 = load i32, i32* %7
%80 = icmp ne i32 %78, %79
br i1 %80, label %81, label %86
81: ; preds = %70
%82 = load i32, i32* @stderr
%83 = load i32, i32* %7
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %83)
%85 = load i32, i32* @EXIT_FAILURE
store i32 %85, i32* %3
br label %90
86: ; preds = %70
%87 = load i32*, i32** %11
%88 = call i32 @fclose(i32* %87)
%89 = load i32, i32* @EXIT_SUCCESS
store i32 %89, i32* %3
br label %90
90: ; preds = %86, %81, %66, %57, %46, %17
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [59 x i8] c"corrupt: usage: corrupt filename offset length [character]\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [4 x i8] c"r+b\00"
@.str.2 = [34 x i8] c"corrupt: unable to open file: %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [25 x i8] c"corrupt: fseek failure.\0A\00"
@.str.4 = [36 x i8] c"corrupt: unable to allocate memory.\00"
@.str.5 = [38 x i8] c"corrupt: did not write %d characters.\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -6
%4 = icmp ult i32 %3, -2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
br label %51
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @strtol(i8* nocapture %12, i32* null, i32 10)
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = tail call i32 @strtol(i8* nocapture %15, i32* null, i32 10)
%17 = icmp ugt i32 %0, 4
br i1 %17, label %18, label %22
18: ; preds = %8
%19 = getelementptr inbounds i8*, i8** %1, i64 4
%20 = load i8*, i8** %19
%21 = load i8, i8* %20
br label %22
22: ; preds = %18, %8
%23 = phi i8 [ %21, %18 ], [ 0, %8 ]
%24 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %51
29: ; preds = %22
%30 = load i32, i32* @SEEK_SET
%31 = tail call i64 @fseek(i32* %24, i32 %13, i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %51
36: ; preds = %29
%37 = tail call i8* @malloc(i32 %16)
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %51
42: ; preds = %36
%43 = tail call i32 @memset(i8* %37, i8 signext %23, i32 %16)
%44 = tail call i32 @fwrite(i8* %37, i32 1, i32 %16, i32* %24)
%45 = icmp eq i32 %44, %16
br i1 %45, label %49, label %46
46: ; preds = %42
%47 = load i32, i32* @stderr
%48 = tail call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %16)
br label %51
49: ; preds = %42
%50 = tail call i32 @fclose(i32* %24)
br label %51
51: ; preds = %49, %46, %39, %33, %26, %5
%52 = phi i32* [ @EXIT_FAILURE, %5 ], [ @EXIT_FAILURE, %26 ], [ @EXIT_FAILURE, %33 ], [ @EXIT_FAILURE, %39 ], [ @EXIT_FAILURE, %46 ], [ @EXIT_SUCCESS, %49 ]
%53 = load i32, i32* %52
ret i32 %53
}