#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 {
  int st_blksize;
};

/* Variables and functions */
int FALLOC_FL_KEEP_SIZE;
int FALLOC_FL_PUNCH_HOLE;
int /*<<< orphan*/ O_WRONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fallocate(int, int, int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fstat(int, struct stat *);
int /*<<< orphan*/ fsync(int);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 5) {
    fprintf(stderr, "usage: %s <path> <start block> <end block> <stride>\n",
            argv[0]);
    fprintf(stderr, "\n");
    fprintf(stderr,
            "Punches holes in an existing file designated by <path>.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Holes are punched on a per-filesystem block basis, "
                    "beginning at <start block> and ending at <end block> "
                    "(exclusive). Blocks can be skipped with <stride>; a value "
                    "of 1 means every block in the range will be punched.\n");
    exit(1);
  }
  int start_block = atoi(argv[2]);
  int end_block = atoi(argv[3]);
  int stride = atoi(argv[4]);
  int fd = open(argv[1], O_WRONLY, 0644);
  if (fd < 0) {
    perror("open");
    return fd;
  }
  struct stat sbuf;
  int ret = fstat(fd, &sbuf);
  if (ret < 0) {
    perror("fstat");
    return ret;
  }
  int block_num;
  for (block_num = start_block; block_num < end_block; block_num += stride) {
    ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
                    block_num * sbuf.st_blksize, sbuf.st_blksize);
    if (ret < 0) {
      perror("fallocate");
      return ret;
    }
  }
  fsync(fd);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x5,-0x8(%rbp)
je     1234 <main+0xa4>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe8e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12a3 <main+0x113>
lea    0xeb3(%rip),%rdi
call   1080 <perror@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1350 <main+0x1c0>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10b0 <fstat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    12d3 <main+0x143>
lea    0xe88(%rip),%rdi
call   1080 <perror@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1350 <main+0x1c0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1340 <main+0x1b0>
mov    -0x20(%rbp),%edi
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
mov    0x2cf0(%rip),%rax
or     (%rax),%esi
mov    -0x30(%rbp),%edx
imul   -0x28(%rbp),%edx
mov    -0x28(%rbp),%ecx
call   1040 <fallocate@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    132d <main+0x19d>
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1350 <main+0x1c0>
jmp    1332 <main+0x1a2>
mov    -0x1c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    12d9 <main+0x149>
mov    -0x20(%rbp),%edi
call   1060 <fsync@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
cmp    $0x5,%edi
jne    12aa <main+0x11a>
mov    0x10(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
mov    0x18(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,0xc(%rsp)
mov    0x20(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,0x8(%rsp)
mov    0x8(%rbp),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1275 <main+0xe5>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
call   10b0 <fstat@plt>
test   %eax,%eax
js     1283 <main+0xf3>
cmp    0xc(%rsp),%ebx
jge    1250 <main+0xc0>
mov    0x10(%rsp),%r13d
mov    %ebx,%ebp
imul   %r13d,%ebp
mov    0x8(%rsp),%r15d
imul   %r13d,%r15d
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
or     (%rax),%esi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %r13d,%ecx
call   1040 <fallocate@plt>
test   %eax,%eax
js     1296 <main+0x106>
add    0x8(%rsp),%ebx
add    %r15d,%ebp
cmp    0xc(%rsp),%ebx
jl     1220 <main+0x90>
mov    %r14d,%edi
call   1060 <fsync@plt>
mov    %r14d,%edi
call   1030 <close@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xeca(%rip),%rdi
call   1080 <perror@plt>
jmp    1263 <main+0xd3>
mov    %eax,%ebp
lea    0xebf(%rip),%rdi
call   1080 <perror@plt>
mov    %ebp,%r14d
jmp    1263 <main+0xd3>
mov    %eax,%r12d
lea    0xeb1(%rip),%rdi
call   1080 <perror@plt>
mov    %r12d,%r14d
jmp    1263 <main+0xd3>
mov    0x2d27(%rip),%rbx
mov    (%rbx),%edi
mov    0x0(%rbp),%rdx
lea    0xd42(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd9e(%rip),%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd54(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd71(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [53 x i8] c"usage: %s <path> <start block> <end block> <stride>\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [57 x i8] c"Punches holes in an existing file designated by <path>.\0A\00"
@.str.3 = [216 x i8] c"Holes are punched on a per-filesystem block basis, beginning at <start block> and ending at <end block> (exclusive). Blocks can be skipped with <stride>; a value of 1 means every block in the range will be punched.\0A\00"
@O_WRONLY = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [6 x i8] c"fstat\00"
@FALLOC_FL_KEEP_SIZE = global i32 0
@FALLOC_FL_PUNCH_HOLE = global i32 0
@.str.6 = [10 x i8] c"fallocate\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 i32
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca i32
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, 5
br i1 %14, label %15, label %30
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([216 x i8], [216 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %2
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %6
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 3
%37 = load i8*, i8** %36
%38 = call i32 @atoi(i8* %37)
store i32 %38, i32* %7
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 4
%41 = load i8*, i8** %40
%42 = call i32 @atoi(i8* %41)
store i32 %42, i32* %8
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = load i32, i32* @O_WRONLY
%47 = call i32 @open(i8* %45, i32 %46, i32 420)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %30
%51 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%52 = load i32, i32* %9
store i32 %52, i32* %3
br label %94
53: ; preds = %30
%54 = load i32, i32* %9
%55 = call i32 @fstat(i32 %54, %struct.stat* %10)
store i32 %55, i32* %11
%56 = load i32, i32* %11
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%60 = load i32, i32* %11
store i32 %60, i32* %3
br label %94
61: ; preds = %53
%62 = load i32, i32* %6
store i32 %62, i32* %12
br label %63
63: ; preds = %85, %61
%64 = load i32, i32* %12
%65 = load i32, i32* %7
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %89
67: ; preds = %63
%68 = load i32, i32* %9
%69 = load i32, i32* @FALLOC_FL_KEEP_SIZE
%70 = load i32, i32* @FALLOC_FL_PUNCH_HOLE
%71 = or i32 %69, %70
%72 = load i32, i32* %12
%73 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%74 = load i32, i32* %73
%75 = mul nsw i32 %72, %74
%76 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%77 = load i32, i32* %76
%78 = call i32 @fallocate(i32 %68, i32 %71, i32 %75, i32 %77)
store i32 %78, i32* %11
%79 = load i32, i32* %11
%80 = icmp slt i32 %79, 0
br i1 %80, label %81, label %84
81: ; preds = %67
%82 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%83 = load i32, i32* %11
store i32 %83, i32* %3
br label %94
84: ; preds = %67
br label %85
85: ; preds = %84
%86 = load i32, i32* %8
%87 = load i32, i32* %12
%88 = add nsw i32 %87, %86
store i32 %88, i32* %12
br label %63
89: ; preds = %63
%90 = load i32, i32* %9
%91 = call i32 @fsync(i32 %90)
%92 = load i32, i32* %9
%93 = call i32 @close(i32 %92)
br label %94
94: ; preds = %89, %81, %58, %50
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [53 x i8] c"usage: %s <path> <start block> <end block> <stride>\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [57 x i8] c"Punches holes in an existing file designated by <path>.\0A\00"
@.str.3 = [216 x i8] c"Holes are punched on a per-filesystem block basis, beginning at <start block> and ending at <end block> (exclusive). Blocks can be skipped with <stride>; a value of 1 means every block in the range will be punched.\0A\00"
@O_WRONLY = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [6 x i8] c"fstat\00"
@FALLOC_FL_KEEP_SIZE = global i32 0
@FALLOC_FL_PUNCH_HOLE = global i32 0
@.str.6 = [10 x i8] c"fallocate\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp eq i32 %0, 5
br i1 %4, label %18, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([216 x i8], [216 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call i32 @atoi(i8* %20)
%22 = getelementptr inbounds i8*, i8** %1, i64 3
%23 = load i8*, i8** %22
%24 = tail call i32 @atoi(i8* %23)
%25 = getelementptr inbounds i8*, i8** %1, i64 4
%26 = load i8*, i8** %25
%27 = tail call i32 @atoi(i8* %26)
%28 = getelementptr inbounds i8*, i8** %1, i64 1
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_WRONLY
%31 = tail call i32 @open(i8* %29, i32 %30, i32 420)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %18
%34 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %64
35: ; preds = %18
%36 = bitcast %struct.stat* %3 to i8*
%37 = call i32 @fstat(i32 %31, %struct.stat* %3)
%38 = icmp slt i32 %37, 0
br i1 %38, label %44, label %39
39: ; preds = %35
%40 = icmp slt i32 %21, %24
br i1 %40, label %41, label %59
41: ; preds = %39
%42 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%43 = load i32, i32* %42
br label %48
44: ; preds = %35
%45 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %62
46: ; preds = %48
%47 = icmp slt i32 %56, %24
br i1 %47, label %48, label %59
48: ; preds = %41, %46
%49 = phi i32 [ %21, %41 ], [ %56, %46 ]
%50 = load i32, i32* @FALLOC_FL_KEEP_SIZE
%51 = load i32, i32* @FALLOC_FL_PUNCH_HOLE
%52 = or i32 %51, %50
%53 = mul nsw i32 %43, %49
%54 = tail call i32 @fallocate(i32 %31, i32 %52, i32 %53, i32 %43)
%55 = icmp slt i32 %54, 0
%56 = add nsw i32 %49, %27
br i1 %55, label %57, label %46
57: ; preds = %48
%58 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
br label %62
59: ; preds = %46, %39
%60 = tail call i32 @fsync(i32 %31)
%61 = tail call i32 @close(i32 %31)
br label %62
62: ; preds = %57, %59, %44
%63 = phi i32 [ %37, %44 ], [ %54, %57 ], [ 0, %59 ]
br label %64
64: ; preds = %62, %33
%65 = phi i32 [ %31, %33 ], [ %63, %62 ]
ret i32 %65
}