#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 open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ truncate(char *, int);
int write(int, char *, int);
int main(void) {
  int fd;
  fd = open("bar", O_RDWR);
  if (fd < 0)
    perror("open");
  if (write(fd, "This is a test file for truncate!", 33) != 33)
    perror("write");
  close(fd);
  if (truncate("bar", 19) < 0)
    perror("truncate");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118d <main+0x3d>
lea    0xe7c(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
lea    0xe72(%rip),%rsi
mov    $0x21,%edx
call   1030 <write@plt>
cmp    $0x21,%eax
je     11b6 <main+0x66>
lea    0xe7a(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
lea    0xe3b(%rip),%rdi
mov    $0x13,%esi
call   1050 <truncate@plt>
cmp    $0x0,%rax
jge    11e5 <main+0x95>
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11a5 <main+0x55>
lea    0xe96(%rip),%rsi
mov    %ebx,%edi
mov    $0x21,%edx
call   1030 <write@plt>
cmp    $0x21,%eax
jne    11b3 <main+0x63>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xe6e(%rip),%rdi
mov    $0x13,%esi
call   1050 <truncate@plt>
test   %rax,%rax
js     11c1 <main+0x71>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe58(%rip),%rdi
call   1070 <perror@plt>
jmp    116c <main+0x1c>
lea    0xe71(%rip),%rdi
call   1070 <perror@plt>
jmp    1184 <main+0x34>
lea    0xe69(%rip),%rdi
call   1070 <perror@plt>
jmp    11a1 <main+0x51>

LLVM IR (-O0):
@.str = [4 x i8] c"bar\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [34 x i8] c"This is a test file for truncate!\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [9 x i8] c"truncate\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_RDWR
%4 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = load i32, i32* %2
%11 = call i32 @write(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 33)
%12 = icmp ne i32 %11, 33
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = load i32, i32* %2
%17 = call i32 @close(i32 %16)
%18 = call i64 @truncate(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 19)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"bar\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [34 x i8] c"This is a test file for truncate!\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [9 x i8] c"truncate\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 33)
%8 = icmp eq i32 %7, 33
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = tail call i32 @close(i32 %2)
%13 = tail call i64 @truncate(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 19)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
ret i32 0
}