#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ftruncate(int, int);
int get_file_size(char const *);
int /*<<< orphan*/ make_file(char const *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  const char *pfile = "temp-file.x";
  make_file(pfile);
  printf("file size : [%lu]\n", get_file_size(pfile));
  int fd = open(pfile, O_RDWR);
  if (fd < 0) {
    printf("open file faild\n");
    return 1;
  } else {
    printf("fd is : [%d]\n", fd);
  }
  ftruncate(fd, 1000);
  close(fd);
  printf("file size : [%lu]\n", get_file_size(pfile));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <make_file@plt>
mov    -0x10(%rbp),%rdi
call   1060 <get_file_size@plt>
mov    %eax,%esi
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d5 <main+0x75>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    121b <main+0xbb>
mov    -0x14(%rbp),%esi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x3e8,%esi
call   1040 <ftruncate@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1060 <get_file_size@plt>
mov    %eax,%esi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe86(%rip),%rbp
mov    %rbp,%rdi
call   1090 <make_file@plt>
mov    %rbp,%rdi
call   1070 <get_file_size@plt>
lea    0xe7b(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11f4 <main+0x84>
mov    %eax,%ebp
lea    0xe65(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    $0x3e8,%esi
call   1050 <ftruncate@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe23(%rip),%rdi
call   1070 <get_file_size@plt>
lea    0xe23(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1205 <main+0x95>
lea    0xe32(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"temp-file.x\00"
@.str.1 = [19 x i8] c"file size : [%lu]\0A\00"
@O_RDWR = global i32 0
@.str.2 = [17 x i8] c"open file faild\0A\00"
@.str.3 = [14 x i8] c"fd is : [%d]\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @make_file(i8* %4)
%6 = load i8*, i8** %2
%7 = call i32 @get_file_size(i8* %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = load i8*, i8** %2
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %27
16: ; preds = %0
%17 = load i32, i32* %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16
%20 = load i32, i32* %3
%21 = call i32 @ftruncate(i32 %20, i32 1000)
%22 = load i32, i32* %3
%23 = call i32 @close(i32 %22)
%24 = load i8*, i8** %2
%25 = call i32 @get_file_size(i8* %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %25)
store i32 0, i32* %1
br label %27
27: ; preds = %19, %14
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [12 x i8] c"temp-file.x\00"
@.str.1 = [19 x i8] c"file size : [%lu]\0A\00"
@O_RDWR = global i32 0
@.str.3 = [14 x i8] c"fd is : [%d]\0A\00"
@str = [16 x i8] c"open file faild\00"
define i32 @main() {
%1 = tail call i32 @make_file(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @get_file_size(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %15
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %5)
%11 = tail call i32 @ftruncate(i32 %5, i32 1000)
%12 = tail call i32 @close(i32 %5)
%13 = tail call i32 @get_file_size(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %9, %7
%16 = phi i32 [ 1, %7 ], [ 0, %9 ]
ret i32 %16
}