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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int ftruncate(int, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
int main() {
  int fd = open("ftrun.txt", O_RDWR | O_CREAT);
  if (fd == -1) {
    perror("myread");
  }
  char buf[7] = "123456";
  buf[6] = '\0';
  ssize_t ret = write(fd, buf, sizeof(buf));
  if (ret == -1) {
    perror("mywrite");
  }
  int f = ftruncate(fd, 1000000);
  if (f == -1) {
    perror("myftruncate");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e99(%rip),%rax
or     (%rax),%esi
lea    0xe98(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1186 <main+0x46>
lea    0xe89(%rip),%rdi
call   1060 <perror@plt>
mov    0xe85(%rip),%eax
mov    %eax,-0xf(%rbp)
mov    0xe7f(%rip),%ax
mov    %ax,-0xb(%rbp)
mov    0xe77(%rip),%al
mov    %al,-0x9(%rbp)
movb   $0x0,-0x9(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xf(%rbp),%rsi
mov    $0x7,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d1 <main+0x91>
lea    0xe4c(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edi
mov    $0xf4240,%esi
call   1040 <ftruncate@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11f7 <main+0xb7>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e94(%rip),%rax
mov    0x2ea5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea2(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11ad <main+0x6d>
movw   $0x3635,0xc(%rsp)
movl   $0x34333231,0x8(%rsp)
34
movb   $0x0,0xe(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x7,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11bb <main+0x7b>
mov    %ebx,%edi
mov    $0xf4240,%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11c9 <main+0x89>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe56(%rip),%rdi
call   1060 <perror@plt>
jmp    116a <main+0x2a>
lea    0xe56(%rip),%rdi
call   1060 <perror@plt>
jmp    1194 <main+0x54>
lea    0xe50(%rip),%rdi
call   1060 <perror@plt>
jmp    11a5 <main+0x65>

LLVM IR (-O0):
@.str = [10 x i8] c"ftrun.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [7 x i8] c"myread\00"
@__const.main.buf = [7 x i8] c"123456\00"
@.str.2 = [8 x i8] c"mywrite\00"
@.str.3 = [12 x i8] c"myftruncate\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [7 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = bitcast [7 x i8]* %3 to i8*
%16 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 6
store i8 0, i8* %16
%17 = load i32, i32* %2
%18 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%19 = call i32 @write(i32 %17, i8* %18, i32 7)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %14
%25 = load i32, i32* %2
%26 = call i32 @ftruncate(i32 %25, i32 1000000)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"ftrun.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [7 x i8] c"myread\00"
@__const.main.buf = [7 x i8] c"123456\00"
@.str.2 = [8 x i8] c"mywrite\00"
@.str.3 = [12 x i8] c"myftruncate\00"
define i32 @main() {
%1 = alloca [7 x i8]
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = getelementptr inbounds [7 x i8], [7 x i8]* %1, i64 0, i64 0
%11 = getelementptr inbounds [7 x i8], [7 x i8]* %1, i64 0, i64 6
store i8 0, i8* %11
%12 = call i32 @write(i32 %5, i8* %10, i32 7)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = tail call i32 @ftruncate(i32 %5, i32 1000000)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
ret i32 0
}