#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 close(int);
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int, char const *);
int /*<<< orphan*/ strlen(char const *);
int write(int, char const *, int /*<<< orphan*/);
int __set_attribute(int id, const char *filename, const char *value) {
  int fd;
  char buf[128];
  if (id < 0 || id > 4)
    return -1;
  if (snprintf(buf, 128,
               "/sys/class/leds/beaglebone:green:usr"
               "%d/%s",
               id, filename) >= 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    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     117a <__set_attribute+0x2a>
cmpl   $0x4,-0x8(%rbp)
jle    1186 <__set_attribute+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <__set_attribute+0xf2>
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    $0x80,%esi
lea    0xe60(%rip),%rdx
call   1050 <snprintf@plt>
cmp    $0x80,%eax
jl     11bc <__set_attribute+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <__set_attribute+0xf2>
lea    -0xa0(%rbp),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11e9 <__set_attribute+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <__set_attribute+0xf2>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xac(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xac(%rbp),%edi
mov    -0xa8(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1237 <__set_attribute+0xe7>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <__set_attribute+0xf2>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__set_attribute>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    $0xffffffff,%ebx
cmp    $0x4,%edi
ja     11c6 <__set_attribute+0x76>
mov    %rdx,%r14
mov    %rsi,%r8
mov    %edi,%ecx
lea    0xe8c(%rip),%rdx
mov    %rsp,%rdi
mov    $0x80,%esi
call   1050 <snprintf@plt>
cmp    $0x7f,%eax
jg     11c6 <__set_attribute+0x76>
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11c6 <__set_attribute+0x76>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1060 <close@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%r14d
cmove  %r14d,%ebx
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"/sys/class/leds/beaglebone:green:usr%d/%s\00"
@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 i32
%9 = alloca [128 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i32, i32* %5
%14 = icmp sgt i32 %13, 4
br i1 %14, label %15, label %16
15: ; preds = %12, %3
store i32 -1, i32* %4
br label %42
16: ; preds = %12
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%18 = load i32, i32* %5
%19 = load i8*, i8** %6
%20 = call i32 @snprintf(i8* %17, i32 128, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %18, i8* %19)
%21 = icmp sge i32 %20, 128
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -1, i32* %4
br label %42
23: ; preds = %16
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%25 = load i32, i32* @O_WRONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -1, i32* %4
br label %42
29: ; preds = %23
%30 = load i32, i32* %8
%31 = load i8*, i8** %7
%32 = load i8*, i8** %7
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @write(i32 %30, i8* %31, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %4
br label %42
39: ; preds = %29
%40 = load i32, i32* %8
%41 = call i32 @close(i32 %40)
store i32 %41, i32* %4
br label %42
42: ; preds = %39, %36, %28, %22, %15
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@.str = [42 x i8] c"/sys/class/leds/beaglebone:green:usr%d/%s\00"
@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 = icmp ugt i32 %0, 4
br i1 %6, label %20, label %7
7: ; preds = %3
%8 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%9 = icmp sgt i32 %8, 127
br i1 %9, label %20, label %10
10: ; preds = %7
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %5, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %20, label %14
14: ; preds = %10
%15 = call i32 @strlen(i8* %2)
%16 = call i32 @write(i32 %12, i8* %2, i32 %15)
%17 = icmp eq i32 %16, -1
%18 = call i32 @close(i32 %12)
%19 = select i1 %17, i32 -1, i32 %18
br label %20
20: ; preds = %14, %10, %7, %3
%21 = phi i32 [ -1, %3 ], [ -1, %7 ], [ -1, %10 ], [ %19, %14 ]
ret i32 %21
}