#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_RDWR;
int O_TRUNC;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ write(int, char *, int);
void write_file(char *path, char *buf, int buf_len) {
  int fd;
  if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) < 0) {
    perror("can't open file");
    return;
  }
  write(fd, buf, buf_len);
  close(fd);
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    119d <write_file+0x5d>
lea    0xe6d(%rip),%rdi
call   1060 <perror@plt>
jmp    11b4 <write_file+0x74>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    0x2ea7(%rip),%rax
mov    0x2e98(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6d(%rip),%rax
or     (%rax),%esi
mov    0x2e6c(%rip),%rax
mov    (%rax),%edx
call   1050 <open@plt>
test   %eax,%eax
js     1191 <write_file+0x51>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <close@plt>
lea    0xe68(%rip),%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <perror@plt>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str = [16 x i8] c"can't open file\00"
define void @write_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = load i32, i32* @O_RDWR
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRWXU
%15 = call i32 @open(i8* %8, i32 %13, i32 %14)
store i32 %15, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %26
19: ; preds = %3
%20 = load i32, i32* %7
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = call i32 @write(i32 %20, i8* %21, i32 %22)
%24 = load i32, i32* %7
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %19, %17
ret void
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str = [16 x i8] c"can't open file\00"
define void @write_file(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %5, %4
%7 = load i32, i32* @O_RDWR
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRWXU
%10 = tail call i32 @open(i8* %0, i32 %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %17
14: ; preds = %3
%15 = tail call i32 @write(i32 %10, i8* %1, i32 %2)
%16 = tail call i32 @close(i32 %10)
br label %17
17: ; preds = %14, %12
ret void
}