#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ errx(int, char *, ...);
int lseek(int, int, int /*<<< orphan*/);
int open(char const *, int);
int /*<<< orphan*/ printf(char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  const char *filename;
  int size;
  int fd;
  int r;
  char byte;
  if (argc != 3) {
    errx(1, "Usage: sparsefile <filename> <size>");
  }
  filename = argv[1];
  size = atoi(argv[2]);
  byte = '\n';
  if (size == 0) {
    err(1, "Sparse files of length zero are not meaningful");
  }
  printf("Creating a sparse file of size %d\n", size);
  fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
  if (fd < 0) {
    err(1, "%s: create", filename);
  }
  if (lseek(fd, size - 1, SEEK_SET) == -1) {
    err(1, "%s: lseek", filename);
  }
  r = write(fd, &byte, 1);
  if (r < 0) {
    err(1, "%s: write", filename);
  } else if (r != 1) {
    errx(1, "%s: write: Unexpected result count %d", filename, r);
  }
  close(fd);
  return 0;
}


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   $0x3,-0x8(%rbp)
je     11b3 <main+0x33>
mov    $0x1,%edi
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
movb   $0xa,-0x25(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11f0 <main+0x70>
mov    $0x1,%edi
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x1c(%rbp),%esi
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    0x2de5(%rip),%rax
or     (%rax),%esi
mov    0x2dd4(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1247 <main+0xc7>
mov    -0x18(%rbp),%rdx
mov    $0x1,%edi
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    127e <main+0xfe>
mov    -0x18(%rbp),%rdx
mov    $0x1,%edi
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x20(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12b8 <main+0x138>
mov    -0x18(%rbp),%rdx
mov    $0x1,%edi
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
jmp    12e1 <main+0x161>
cmpl   $0x1,-0x24(%rbp)
je     12dc <main+0x15c>
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    $0x1,%edi
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
jmp    12e1 <main+0x161>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
je     11a3 <main+0x23>
lea    0xe69(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <errx@plt>
mov    0x8(%rbx),%r14
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
movb   $0xa,0xf(%rsp)
test   %eax,%eax
jne    11ce <main+0x4e>
lea    0xe62(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <err@plt>
lea    0xe7e(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    0x2ddd(%rip),%rax
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    121b <main+0x9b>
lea    0xe6a(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <err@plt>
add    $0xffffffff,%ebx
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %ebx,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    124b <main+0xcb>
lea    0xe45(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <err@plt>
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
js     127f <main+0xff>
cmp    $0x1,%eax
je     1295 <main+0x115>
lea    0xe29(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <errx@plt>
jmp    1295 <main+0x115>
lea    0xe05(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <err@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Usage: sparsefile <filename> <size>\00"
@.str.1 = [47 x i8] c"Sparse files of length zero are not meaningful\00"
@.str.2 = [35 x i8] c"Creating a sparse file of size %d\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.3 = [11 x i8] c"%s: create\00"
@SEEK_SET = global i32 0
@.str.4 = [10 x i8] c"%s: lseek\00"
@.str.5 = [10 x i8] c"%s: write\00"
@.str.6 = [38 x i8] c"%s: write: Unexpected result count %d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
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, 3
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %6
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %7
store i8 10, i8* %10
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %15
%28 = load i32, i32* %7
%29 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i8*, i8** %6
%31 = load i32, i32* @O_WRONLY
%32 = load i32, i32* @O_CREAT
%33 = or i32 %31, %32
%34 = load i32, i32* @O_TRUNC
%35 = or i32 %33, %34
%36 = call i32 @open(i8* %30, i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %27
%40 = load i8*, i8** %6
%41 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %39, %27
%43 = load i32, i32* %8
%44 = load i32, i32* %7
%45 = sub nsw i32 %44, 1
%46 = load i32, i32* @SEEK_SET
%47 = call i32 @lseek(i32 %43, i32 %45, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = load i8*, i8** %6
%51 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %49, %42
%53 = load i32, i32* %8
%54 = call i32 @write(i32 %53, i8* %10, i32 1)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i8*, i8** %6
%59 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %58)
br label %68
60: ; preds = %52
%61 = load i32, i32* %9
%62 = icmp ne i32 %61, 1
br i1 %62, label %63, label %67
63: ; preds = %60
%64 = load i8*, i8** %6
%65 = load i32, i32* %9
%66 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i8* %64, i32 %65)
br label %67
67: ; preds = %63, %60
br label %68
68: ; preds = %67, %57
%69 = load i32, i32* %8
%70 = call i32 @close(i32 %69)
ret i32 0
}

LLVM IR (-O2):
@.str = [36 x i8] c"Usage: sparsefile <filename> <size>\00"
@.str.1 = [47 x i8] c"Sparse files of length zero are not meaningful\00"
@.str.2 = [35 x i8] c"Creating a sparse file of size %d\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.3 = [11 x i8] c"%s: create\00"
@SEEK_SET = global i32 0
@.str.4 = [10 x i8] c"%s: lseek\00"
@.str.5 = [10 x i8] c"%s: write\00"
@.str.6 = [38 x i8] c"%s: write: Unexpected result count %d\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = icmp eq i32 %0, 3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
store i8 10, i8* %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %7
%17 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = tail call i32 @open(i8* %9, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %9)
br label %27
27: ; preds = %25, %16
%28 = add nsw i32 %12, -1
%29 = load i32, i32* @SEEK_SET
%30 = tail call i32 @lseek(i32 %23, i32 %28, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %9)
br label %34
34: ; preds = %32, %27
%35 = call i32 @write(i32 %23, i8* %3, i32 1)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %9)
br label %43
39: ; preds = %34
%40 = icmp eq i32 %35, 1
br i1 %40, label %43, label %41
41: ; preds = %39
%42 = tail call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i8* %9, i32 %35)
br label %43
43: ; preds = %39, %41, %37
%44 = tail call i32 @close(i32 %23)
ret i32 0
}