#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 ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ O_WRONLY;
scalar_t__ close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
void set_led(char *path, bool value) {
  int fd = open(path, O_WRONLY);
  if (fd == -1) {
    perror("ERROR setting LED - open()");
    return;
  }
  char buf = '0' + value;
  ssize_t ret;
  do {
    ret = write(fd, &buf, 1);
  } while (ret == 0 || (ret == -1 && errno == EINTR));
  if (ret == -1)
    perror("ERROR setting LED - write()");
  if (close(fd))
    perror("ERROR setting LED - close()");
}


x86 assembly (clang, -O0):
<set_led>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    117f <set_led+0x3f>
lea    0xe8b(%rip),%rdi
call   1060 <perror@plt>
jmp    1219 <set_led+0xd9>
mov    -0xc(%rbp),%eax
add    $0x30,%eax
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    $0x1,%al
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x19(%rbp)
je     11da <set_led+0x9a>
xor    %eax,%eax
cmpl   $0xffffffff,-0x18(%rbp)
mov    %al,-0x1a(%rbp)
jne    11d4 <set_led+0x94>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1188 <set_led+0x48>
cmpl   $0xffffffff,-0x18(%rbp)
jne    11fb <set_led+0xbb>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1219 <set_led+0xd9>
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_led>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11bd <set_led+0x7d>
mov    %eax,%ebx
add    $0x30,%bpl
mov    %bpl,0x7(%rsp)
lea    0x7(%rsp),%r14
mov    0x2e6d(%rip),%r15
mov    0x2e5e(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
je     1180 <set_led+0x40>
cmp    $0xffffffff,%eax
jne    11a1 <set_led+0x61>
mov    0x0(%rbp),%rcx
cmp    %rcx,(%r15)
je     1180 <set_led+0x40>
cmp    $0xffffffff,%eax
je     11c6 <set_led+0x86>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    11d4 <set_led+0x94>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3c(%rip),%rdi
jmp    11db <set_led+0x9b>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
jmp    11a6 <set_led+0x66>
lea    0xe5c(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <perror@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@.str = [27 x i8] c"ERROR setting LED - open()\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [28 x i8] c"ERROR setting LED - write()\00"
@.str.2 = [28 x i8] c"ERROR setting LED - close()\00"
define void @set_led(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%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 eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %47
15: ; preds = %2
%16 = load i32, i32* %4
%17 = add nsw i32 48, %16
%18 = trunc i32 %17 to i8
store i8 %18, i8* %6
br label %19
19: ; preds = %34, %15
%20 = load i32, i32* %5
%21 = call i32 @write(i32 %20, i8* %6, i32 1)
store i32 %21, i32* %7
br label %22
22: ; preds = %19
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, 0
br i1 %24, label %34, label %25
25: ; preds = %22
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br label %32
32: ; preds = %28, %25
%33 = phi i1 [ false, %25 ], [ %31, %28 ]
br label %34
34: ; preds = %32, %22
%35 = phi i1 [ true, %22 ], [ %33, %32 ]
br i1 %35, label %19, label %36
36: ; preds = %34
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = load i32, i32* %5
%43 = call i64 @close(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %13, %45, %41
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@.str = [27 x i8] c"ERROR setting LED - open()\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [28 x i8] c"ERROR setting LED - write()\00"
@.str.2 = [28 x i8] c"ERROR setting LED - close()\00"
define void @set_led(i8* nocapture %0, i32 %1) {
%3 = alloca i8
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %31
9: ; preds = %2
%10 = trunc i32 %1 to i8
%11 = add i8 %10, 48
store i8 %11, i8* %3
br label %12
12: ; preds = %21, %9
%13 = call i32 @write(i32 %5, i8* %3, i32 1)
%14 = icmp eq i32 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %12
%16 = icmp eq i32 %13, -1
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
%20 = select i1 %16, i1 %19, i1 false
br i1 %20, label %21, label %22
21: ; preds = %15, %12
br label %12
22: ; preds = %15
br i1 %16, label %23, label %25
23: ; preds = %22
%24 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %22
%26 = tail call i64 @close(i32 %5)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
br label %31
31: ; preds = %30, %7
ret void
}