#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(char *, char const *, char *);
int open(char const *, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char const *);
scalar_t__ write(int, char const *, scalar_t__);
int write_attribute(const char *path, const char *contents) {
  int fd;
  int retval = 1;
  ssize_t amt_written;
  fd = open(path, O_WRONLY);
  if (fd < 0) {
    error("cannot open %s -- %s\n", path, strerror(errno));
    goto out;
  }
  amt_written = write(fd, contents, strlen(contents) + 1);
  if (amt_written < 0) {
    error("cannot write attribute %s -- %s\n", path, strerror(errno));
    goto fd;
  }
  retval = 0;
fd:
  close(fd);
out:
  return retval;
}


x86 assembly (clang, -O0):
<write_attribute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c4 <write_attribute+0x64>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xe46(%rip),%rdi
call   1060 <error@plt>
jmp    123a <write_attribute+0xda>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    122b <write_attribute+0xcb>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xdf5(%rip),%rdi
call   1060 <error@plt>
jmp    1232 <write_attribute+0xd2>
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_attribute>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11c9 <write_attribute+0x69>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
xor    %ebx,%ebx
test   %rax,%rax
jns    11c0 <write_attribute+0x60>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe66(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1060 <error@plt>
mov    $0x1,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11ee <write_attribute+0x8e>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe22(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1060 <error@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@.str = [22 x i8] c"cannot open %s -- %s\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"cannot write attribute %s -- %s\0A\00"
define i32 @write_attribute(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 1, i32* %6
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %16)
br label %36
18: ; preds = %2
%19 = load i32, i32* %5
%20 = load i8*, i8** %4
%21 = load i8*, i8** %4
%22 = call i64 @strlen(i8* %21)
%23 = add nsw i64 %22, 1
%24 = call i64 @write(i32 %19, i8* %20, i64 %23)
store i64 %24, i64* %7
%25 = load i64, i64* %7
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load i8*, i8** %3
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %30)
br label %33
32: ; preds = %18
store i32 0, i32* %6
br label %33
33: ; preds = %32, %27
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %13
%37 = load i32, i32* %6
ret i32 %37
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@.str = [22 x i8] c"cannot open %s -- %s\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"cannot write attribute %s -- %s\0A\00"
define i32 @write_attribute(i8* %0, i8* nocapture %1) {
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %8)
br label %22
10: ; preds = %2
%11 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%12 = add nsw i64 %11, 1
%13 = tail call i64 @write(i32 %4, i8* %1, i64 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %17)
br label %19
19: ; preds = %10, %15
%20 = phi i32 [ 1, %15 ], [ 0, %10 ]
%21 = tail call i32 @close(i32 %4)
br label %22
22: ; preds = %19, %6
%23 = phi i32 [ 1, %6 ], [ %20, %19 ]
ret i32 %23
}