#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*/ EXIT_FAILURE;
int O_RDONLY;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void truncateFile() {
  int fd;
  if ((fd = open("./newfile", O_RDONLY | O_TRUNC)) == -1) {
    fprintf(stderr, "Unable to open './newfile': %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  printf("'./newfile' opened. File descriptor is: %d\n", fd);
  printf("'./newfile' truncated -- see 'ls -l openex'\n");
  (void)close(fd);
}


x86 assembly (clang, -O0):
<truncateFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11cc <truncateFile+0x6c>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x8(%rbp),%edi
mov    %rax,%rdx
lea    0xe51(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<truncateFile>:
push   %rbx
mov    0x2e68(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe76(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11ba <truncateFile+0x4a>
mov    %eax,%ebx
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1050 <close@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%ebx
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe32(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"./newfile\00"
@O_RDONLY = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"Unable to open './newfile': %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [44 x i8] c"'./newfile' opened. File descriptor is: %d\0A\00"
@.str.3 = [45 x i8] c"'./newfile' truncated -- see 'ls -l openex'\0A\00"
define void @truncateFile() {
%1 = alloca i32
%2 = load i32, i32* @O_RDONLY
%3 = load i32, i32* @O_TRUNC
%4 = or i32 %2, %3
%5 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %1
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %14
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = load i32, i32* %1
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%18 = load i32, i32* %1
%19 = call i32 @close(i32 %18)
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"./newfile\00"
@O_RDONLY = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"Unable to open './newfile': %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [44 x i8] c"'./newfile' opened. File descriptor is: %d\0A\00"
@str = [44 x i8] c"'./newfile' truncated -- see 'ls -l openex'\00"
define void @truncateFile() {
%1 = load i32, i32* @O_RDONLY
%2 = load i32, i32* @O_TRUNC
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %13
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %4)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 @close(i32 %4)
ret void
}