#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*/ abort();
char *alloca(size_t);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int sprintf(char *, char *, ...);
size_t strlen(char const *);
scalar_t__ write(int, char *, size_t);
int setbrightness(const char *device, long int value) {
  size_t p = 0, n = sizeof("//brightness") - 1;
  char *path, buf[128];
  ssize_t r = 0;
  int fd;
  n += strlen("/sys/class/backlight");
  n += strlen(device);
  path = alloca(n + 1);
  if (sprintf(path, "%s/%s/brightness", "/sys/class/backlight", device) !=
      (int)n)
    abort();
  if (sprintf(buf, "%li\n", value) < 0)
    abort();
  fd = open(path, O_WRONLY);
  if (fd < 0)
    return -1;
  for (n = strlen(buf); p < n; p += (size_t)r)
    if ((r = write(fd, &buf[p], n - p)) < 0)
      break;
  close(fd);
  return r < 0 ? -1 : 0;
}


x86 assembly (clang, -O0):
<setbrightness>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0xc,-0x28(%rbp)
movq   $0x0,-0xb8(%rbp)
mov    -0x28(%rbp),%rax
add    $0x14,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1070 <alloca@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xe25(%rip),%rsi
lea    0xe2f(%rip),%rdx
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x28(%rbp),%rcx
cmp    %ecx,%eax
je     11fc <setbrightness+0x8c>
mov    $0x0,%al
call   1030 <abort@plt>
lea    -0xb0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
cmp    $0x0,%eax
jge    1225 <setbrightness+0xb5>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x30(%rbp),%rdi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xbc(%rbp)
cmpl   $0x0,-0xbc(%rbp)
jge    1256 <setbrightness+0xe6>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e4 <setbrightness+0x174>
lea    -0xb0(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    12c1 <setbrightness+0x151>
mov    -0xbc(%rbp),%edi
lea    -0xb0(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0xb8(%rbp)
cmp    $0x0,%rax
jge    12a8 <setbrightness+0x138>
jmp    12c1 <setbrightness+0x151>
jmp    12ad <setbrightness+0x13d>
mov    -0xb8(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1266 <setbrightness+0xf6>
mov    -0xbc(%rbp),%edi
call   1060 <close@plt>
mov    -0xb8(%rbp),%rdx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%rdx
cmovl  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setbrightness>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
call   1050 <strlen@plt>
mov    %rax,%rbx
lea    0x21(%rax),%rdi
call   1070 <alloca@plt>
mov    %rax,%r15
lea    0xe62(%rip),%rsi
lea    0xe6c(%rip),%rdx
mov    %rax,%rdi
mov    %rbp,%rcx
xor    %eax,%eax
call   1090 <sprintf@plt>
add    $0x20,%ebx
cmp    %ebx,%eax
jne    1249 <setbrightness+0xd9>
lea    0xe62(%rip),%rsi
mov    %rsp,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <sprintf@plt>
test   %eax,%eax
js     1249 <setbrightness+0xd9>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1228 <setbrightness+0xb8>
mov    %eax,%r14d
mov    %rsp,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
je     122f <setbrightness+0xbf>
mov    %rax,%r15
xor    %ebp,%ebp
nop
lea    (%rsp,%rbp,1),%rsi
mov    %r15,%rdx
sub    %rbp,%rdx
mov    %r14d,%edi
call   1040 <write@plt>
mov    %rax,%rbx
test   %rax,%rax
js     1222 <setbrightness+0xb2>
add    %rbx,%rbp
cmp    %rbp,%r15
ja     1200 <setbrightness+0x90>
sar    $0x3f,%rbx
jmp    1231 <setbrightness+0xc1>
mov    $0xffffffff,%ebx
jmp    1239 <setbrightness+0xc9>
xor    %ebx,%ebx
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"%s/%s/brightness\00"
@.str.1 = [21 x i8] c"/sys/class/backlight\00"
@.str.2 = [5 x i8] c"%li\0A\00"
@O_WRONLY = global i32 0
define i32 @setbrightness(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca [128 x i8]
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 0, i64* %6
store i64 12, i64* %7
store i64 0, i64* %10
%12 = load i64, i64* %7
%13 = add i64 %12, 20
store i64 %13, i64* %7
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = load i64, i64* %7
%17 = add i64 %16, %15
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = add i64 %18, 1
%20 = call i8* @alloca(i64 %19)
store i8* %20, i8** %8
%21 = load i8*, i8** %8
%22 = load i8*, i8** %4
%23 = call i32 (i8*, i8*, ...) @sprintf(i8* %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i64, i64* %7
%25 = trunc i64 %24 to i32
%26 = icmp ne i32 %23, %25
br i1 %26, label %27, label %29
27: ; preds = %2
%28 = call i32 (...) @abort()
unreachable
29: ; preds = %2
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%31 = load i64, i64* %5
%32 = call i32 (i8*, i8*, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 (...) @abort()
unreachable
36: ; preds = %29
%37 = load i8*, i8** %8
%38 = load i32, i32* @O_WRONLY
%39 = call i32 @open(i8* %37, i32 %38)
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 -1, i32* %3
br label %72
43: ; preds = %36
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%45 = call i64 @strlen(i8* %44)
store i64 %45, i64* %7
br label %46
46: ; preds = %61, %43
%47 = load i64, i64* %6
%48 = load i64, i64* %7
%49 = icmp ult i64 %47, %48
br i1 %49, label %50, label %65
50: ; preds = %46
%51 = load i32, i32* %11
%52 = load i64, i64* %6
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 %52
%54 = load i64, i64* %7
%55 = load i64, i64* %6
%56 = sub i64 %54, %55
%57 = call i64 @write(i32 %51, i8* %53, i64 %56)
store i64 %57, i64* %10
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %50
br label %65
60: ; preds = %50
br label %61
61: ; preds = %60
%62 = load i64, i64* %10
%63 = load i64, i64* %6
%64 = add i64 %63, %62
store i64 %64, i64* %6
br label %46
65: ; preds = %59, %46
%66 = load i32, i32* %11
%67 = call i32 @close(i32 %66)
%68 = load i64, i64* %10
%69 = icmp slt i64 %68, 0
%70 = zext i1 %69 to i64
%71 = select i1 %69, i32 -1, i32 0
store i32 %71, i32* %3
br label %72
72: ; preds = %65, %42
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [17 x i8] c"%s/%s/brightness\00"
@.str.1 = [21 x i8] c"/sys/class/backlight\00"
@.str.2 = [5 x i8] c"%li\0A\00"
@O_WRONLY = global i32 0
define i32 @setbrightness(i8* %0, i64 %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add i64 %5, 33
%7 = tail call i8* @alloca(i64 %6)
%8 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %7, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%9 = trunc i64 %5 to i32
%10 = add i32 %9, 32
%11 = icmp eq i32 %8, %10
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = tail call i32 (...) @abort()
unreachable
14: ; preds = %2
%15 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %1)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 (...) @abort()
unreachable
19: ; preds = %14
%20 = load i32, i32* @O_WRONLY
%21 = tail call i32 @open(i8* %7, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %41, label %23
23: ; preds = %19
%24 = call i64 @strlen(i8* %4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %38, label %26
26: ; preds = %23, %26
%27 = phi i64 [ %32, %26 ], [ 0, %23 ]
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %27
%29 = sub i64 %24, %27
%30 = call i64 @write(i32 %21, i8* %28, i64 %29)
%31 = icmp sgt i64 %30, -1
%32 = add i64 %30, %27
%33 = icmp ugt i64 %24, %32
%34 = select i1 %31, i1 %33, i1 false
br i1 %34, label %26, label %35
35: ; preds = %26
%36 = ashr i64 %30, 63
%37 = trunc i64 %36 to i32
br label %38
38: ; preds = %35, %23
%39 = phi i32 [ 0, %23 ], [ %37, %35 ]
%40 = tail call i32 @close(i32 %21)
br label %41
41: ; preds = %19, %38
%42 = phi i32 [ %39, %38 ], [ -1, %19 ]
ret i32 %42
}