#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 O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ftruncate(int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int fd = open("test.txt", O_RDWR | O_CREAT | O_EXCL, 0777);
  if (fd == -1) {
    perror("open test.txt");
    return 1;
  };
  write(fd, "test1 test2 test3 test4 test5",
        strlen("test1 test2 test3 test4 test5"));
  ftruncate(fd, 0);
  write(fd, "test6 test7", strlen("test6 test7"));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
lea    0xe74(%rip),%rdi
mov    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c0 <main+0x60>
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    121f <main+0xbf>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    0xe41(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%edx
lea    0xe30(%rip),%rsi
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    0xe32(%rip),%rdi
call   1040 <strlen@plt>
mov    -0xc(%rbp),%edi
mov    %eax,%edx
lea    0xe21(%rip),%rsi
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e75(%rip),%rax
mov    0x2e86(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e53(%rip),%rax
or     (%rax),%esi
lea    0xe7f(%rip),%rdi
mov    $0x1ff,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11e8 <main+0x88>
mov    %eax,%ebx
lea    0xe70(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %r14d,%r14d
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
lea    0xe67(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe11(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%r14d
jmp    11e0 <main+0x80>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [14 x i8] c"open test.txt\00"
@.str.2 = [30 x i8] c"test1 test2 test3 test4 test5\00"
@.str.3 = [12 x i8] c"test6 test7\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %3, %4
%6 = load i32, i32* @O_EXCL
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7, i32 511)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %24
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 @strlen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @write(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %2
%18 = call i32 @ftruncate(i32 %17, i32 0)
%19 = load i32, i32* %2
%20 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @write(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %2
%23 = call i32 @close(i32 %22)
store i32 0, i32* %1
br label %24
24: ; preds = %13, %11
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [14 x i8] c"open test.txt\00"
@.str.2 = [30 x i8] c"test1 test2 test3 test4 test5\00"
@.str.3 = [12 x i8] c"test6 test7\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_EXCL
%5 = or i32 %3, %4
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5, i32 511)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %17
10: ; preds = %0
%11 = tail call i32 @strlen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = tail call i32 @ftruncate(i32 %6, i32 0)
%14 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = tail call i32 @close(i32 %6)
br label %17
17: ; preds = %10, %8
%18 = phi i32 [ 1, %8 ], [ 0, %10 ]
ret i32 %18
}