#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_WRONLY;
int __get_pin_directory(char *, int);
int close(int);
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char const *);
int /*<<< orphan*/ strlen(char const *);
int write(int, char const *, int /*<<< orphan*/);
int __set_attribute(int pin, const char *attribute, const char *value) {
  char buf[128];
  int fd;
  int end = __get_pin_directory(buf, pin);
  if (end >= 128 || snprintf(buf + end, 128, attribute) >= 128)
    return -1;
  if ((fd = open(buf, O_WRONLY)) == -1)
    return -1;
  if (write(fd, value, strlen(value)) == -1) {
    close(fd);
    return -1;
  }
  return close(fd);
}


x86 assembly (clang, -O0):
<__set_attribute>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <__get_pin_directory@plt>
mov    %eax,-0xa8(%rbp)
cmpl   $0x80,-0xa8(%rbp)
jge    11c5 <__set_attribute+0x65>
lea    -0xa0(%rbp),%rdi
movslq -0xa8(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
mov    $0x80,%esi
call   1060 <snprintf@plt>
cmp    $0x80,%eax
jl     11d1 <__set_attribute+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <__set_attribute+0x103>
lea    -0xa0(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xa4(%rbp)
cmp    $0xffffffff,%eax
jne    1201 <__set_attribute+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <__set_attribute+0x103>
mov    -0xa4(%rbp),%eax
mov    %eax,-0xb4(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xb4(%rbp),%edi
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1255 <__set_attribute+0xf5>
mov    -0xa4(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <__set_attribute+0x103>
mov    -0xa4(%rbp),%edi
call   1070 <close@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__set_attribute>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%esi
mov    %rsp,%rdi
call   1030 <__get_pin_directory@plt>
mov    $0xffffffff,%ebp
cmp    $0x7f,%eax
jg     11df <__set_attribute+0x7f>
cltq
lea    (%rsp,%rax,1),%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1060 <snprintf@plt>
cmp    $0x7f,%eax
jg     11df <__set_attribute+0x7f>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11df <__set_attribute+0x7f>
mov    %eax,%r14d
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%ebx
mov    %r14d,%edi
call   1070 <close@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%ebx
cmove  %ebx,%ebp
mov    %ebp,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
define i32 @__set_attribute(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [128 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%12 = load i32, i32* %5
%13 = call i32 @__get_pin_directory(i8* %11, i32 %12)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = icmp sge i32 %14, 128
br i1 %15, label %24, label %16
16: ; preds = %3
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%18 = load i32, i32* %10
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8*, i8** %6
%22 = call i32 @snprintf(i8* %20, i32 128, i8* %21)
%23 = icmp sge i32 %22, 128
br i1 %23, label %24, label %25
24: ; preds = %16, %3
store i32 -1, i32* %4
br label %44
25: ; preds = %16
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%27 = load i32, i32* @O_WRONLY
%28 = call i32 @open(i8* %26, i32 %27)
store i32 %28, i32* %9
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 -1, i32* %4
br label %44
31: ; preds = %25
%32 = load i32, i32* %9
%33 = load i8*, i8** %7
%34 = load i8*, i8** %7
%35 = call i32 @strlen(i8* %34)
%36 = call i32 @write(i32 %32, i8* %33, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = load i32, i32* %9
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %4
br label %44
41: ; preds = %31
%42 = load i32, i32* %9
%43 = call i32 @close(i32 %42)
store i32 %43, i32* %4
br label %44
44: ; preds = %41, %38, %30, %24
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
define i32 @__set_attribute(i32 %0, i8* %1, i8* %2) {
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%6 = call i32 @__get_pin_directory(i8* %5, i32 %0)
%7 = icmp sgt i32 %6, 127
br i1 %7, label %23, label %8
8: ; preds = %3
%9 = sext i32 %6 to i64
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %9
%11 = call i32 @snprintf(i8* %10, i32 128, i8* %1)
%12 = icmp sgt i32 %11, 127
br i1 %12, label %23, label %13
13: ; preds = %8
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* %5, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %23, label %17
17: ; preds = %13
%18 = call i32 @strlen(i8* %2)
%19 = call i32 @write(i32 %15, i8* %2, i32 %18)
%20 = icmp eq i32 %19, -1
%21 = call i32 @close(i32 %15)
%22 = select i1 %20, i32 -1, i32 %21
br label %23
23: ; preds = %17, %13, %3, %8
%24 = phi i32 [ -1, %8 ], [ -1, %3 ], [ -1, %13 ], [ %22, %17 ]
ret i32 %24
}