#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 int uint8_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, void *, int /*<<< orphan*/);
int write_trigger_values(uint8_t led_no, char *value) {
  int fd, ret = 0;
  char buf[100];
  snprintf(buf, sizeof(buf),
           "/sys/class/leds"
           "/beaglebone:green:usr%d/trigger",
           led_no);
  fd = open(buf, O_WRONLY);
  if (fd <= 0) {
    perror(" write trigger error\n");
    return -1;
  }
  ret = write(fd, (void *)value, strlen(value));
  if (ret <= 0) {
    printf("trigger value write error\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_trigger_values>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x64,%esi
lea    0xe74(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11c8 <write_trigger_values+0x68>
lea    0xe78(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <write_trigger_values+0xc5>
mov    -0x14(%rbp),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x8c(%rbp),%edi
mov    -0x88(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     121e <write_trigger_values+0xbe>
lea    0xe38(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <write_trigger_values+0xc5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_trigger_values>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%rbx
mov    %edi,%ecx
lea    0xe8e(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x64,%esi
call   1060 <snprintf@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
jle    11cd <write_trigger_values+0x6d>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jg     11c6 <write_trigger_values+0x66>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5b(%rip),%rdi
call   1080 <perror@plt>
jmp    11c1 <write_trigger_values+0x61>

LLVM IR (-O0):
@.str = [47 x i8] c"/sys/class/leds/beaglebone:green:usr%d/trigger\00"
@O_WRONLY = global i32 0
@.str.1 = [22 x i8] c" write trigger error\0A\00"
@.str.2 = [27 x i8] c"trigger value write error\0A\00"
define i32 @write_trigger_values(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %7
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 100, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp sle i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %30
19: ; preds = %2
%20 = load i32, i32* %6
%21 = load i8*, i8** %5
%22 = load i8*, i8** %5
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @write(i32 %20, i8* %21, i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp sle i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %30
29: ; preds = %19
store i32 0, i32* %3
br label %30
30: ; preds = %29, %27, %17
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@.str = [47 x i8] c"/sys/class/leds/beaglebone:green:usr%d/trigger\00"
@O_WRONLY = global i32 0
@.str.1 = [22 x i8] c" write trigger error\0A\00"
@str = [26 x i8] c"trigger value write error\00"
define i32 @write_trigger_values(i32 %0, i8* %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 100, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %17
11: ; preds = %2
%12 = call i32 @strlen(i8* %1)
%13 = call i32 @write(i32 %7, i8* %1, i32 %12)
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %17
17: ; preds = %11, %15, %9
%18 = phi i32 [ -1, %9 ], [ -1, %15 ], [ 0, %11 ]
ret i32 %18
}