#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 scalar_t__ off_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ ENOSPC;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftruncate(int, int /*<<< orphan*/);
int getpid();
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int write(int, char *, int);
main(int argc, char **argv) {
  int fd, i;
  char fn[128];
  char buf[4096];
  sprintf(fn, "./ttest%d", getpid());
  fd = open(fn, O_CREAT | O_RDWR | O_TRUNC, 0600);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  memset(buf, 0xe5, sizeof(buf));
  while (1) {
    if (lseek(fd, (12 + 256 + 256 * 256) * 1024, SEEK_SET) == (off_t)-1) {
      perror("lseek");
    }
    for (i = 0; i < 32; i++) {
      if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
        if (errno != ENOSPC)
          perror("write");
        break;
      }
    }
    ftruncate(fd, 0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10b0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0x10a8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x10a8(%rbp),%rdi
mov    %eax,%edx
lea    0xe32(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
mov    0x2dfd(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1222 <main+0x92>
lea    0xdf7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x10a0(%rbp),%rdi
mov    $0xe5,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
mov    -0x14(%rbp),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
mov    $0x4043000,%esi
call   1060 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    1264 <main+0xd4>
lea    0xdb0(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x20,-0x18(%rbp)
jge    12d5 <main+0x145>
mov    -0x14(%rbp),%edi
lea    -0x10a0(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
cltq
cmp    $0x1000,%rax
je     12c2 <main+0x132>
mov    0x2d2a(%rip),%rax
mov    (%rax),%rax
mov    0x2d10(%rip),%rcx
cmp    (%rcx),%rax
je     12bd <main+0x12d>
lea    0xd5d(%rip),%rdi
call   1090 <perror@plt>
jmp    12d5 <main+0x145>
jmp    12c7 <main+0x137>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    126b <main+0xdb>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
jmp    1238 <main+0xa8>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1088,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe51(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   10a0 <sprintf@plt>
mov    0x2e35(%rip),%rax
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e1b(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1298 <main+0x108>
mov    %eax,%ebx
lea    0x80(%rsp),%r14
mov    %r14,%rdi
mov    $0xe5,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
mov    0x2db1(%rip),%r13
mov    0x2db2(%rip),%rbp
mov    0x2d9b(%rip),%r12
jmp    1229 <main+0x99>
nop
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
mov    0x0(%r13),%edx
mov    %ebx,%edi
mov    $0x4043000,%esi
call   1060 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    124b <main+0xbb>
lea    0xdc9(%rip),%rdi
call   1090 <perror@plt>
mov    $0x20,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
cmp    $0x1000,%eax
jne    1280 <main+0xf0>
add    $0xffffffff,%r15d
jne    1260 <main+0xd0>
jmp    1220 <main+0x90>
xchg   %ax,%ax
mov    0x0(%rbp),%rax
cmp    (%r12),%rax
je     1220 <main+0x90>
lea    0xd84(%rip),%rdi
call   1090 <perror@plt>
jmp    1220 <main+0x90>
lea    0xd6b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"./ttest%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@errno = global i64 0
@ENOSPC = global i64 0
@.str.3 = [6 x i8] c"write\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 [128 x i8]
%9 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%11 = call i32 (...) @getpid()
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_RDWR
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %13, i32 %18, i32 384)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 229, i32 4096)
br label %28
28: ; preds = %25, %56
%29 = load i32, i32* %6
%30 = load i32, i32* @SEEK_SET
%31 = call i64 @lseek(i32 %29, i32 67383296, i32 %30)
%32 = icmp eq i64 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
store i32 0, i32* %7
br label %36
36: ; preds = %53, %35
%37 = load i32, i32* %7
%38 = icmp slt i32 %37, 32
br i1 %38, label %39, label %56
39: ; preds = %36
%40 = load i32, i32* %6
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%42 = call i32 @write(i32 %40, i8* %41, i32 4096)
%43 = sext i32 %42 to i64
%44 = icmp ne i64 %43, 4096
br i1 %44, label %45, label %52
45: ; preds = %39
%46 = load i64, i64* @errno
%47 = load i64, i64* @ENOSPC
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %49, %45
br label %56
52: ; preds = %39
br label %53
53: ; preds = %52
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 1
store i32 %55, i32* %7
br label %36
56: ; preds = %51, %36
%57 = load i32, i32* %6
%58 = call i32 @ftruncate(i32 %57, i32 0)
br label %28
}

LLVM IR (-O2):
@.str = [10 x i8] c"./ttest%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@errno = global i64 0
@ENOSPC = global i64 0
@.str.3 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [128 x i8]
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = tail call i32 (...) @getpid()
%8 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %10, %9
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %5, i32 %13, i32 384)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32 @memset(i8* %6, i32 229, i32 4096)
br label %21
21: ; preds = %41, %19
%22 = load i32, i32* @SEEK_SET
%23 = call i64 @lseek(i32 %14, i32 67383296, i32 %22)
%24 = icmp eq i64 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
br label %31
28: ; preds = %31
%29 = add nuw nsw i32 %32, 1
%30 = icmp eq i32 %29, 32
br i1 %30, label %41, label %31
31: ; preds = %27, %28
%32 = phi i32 [ %29, %28 ], [ 0, %27 ]
%33 = call i32 @write(i32 %14, i8* %6, i32 4096)
%34 = icmp eq i32 %33, 4096
br i1 %34, label %28, label %35
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = load i64, i64* @ENOSPC
%38 = icmp eq i64 %36, %37
br i1 %38, label %41, label %39
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %28, %35, %39
%42 = call i32 @ftruncate(i32 %14, i32 0)
br label %21
}