#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 /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
scalar_t__ isdigit(char const);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char const *, char *);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  int retval = 0;
  if (argc < 2) {
    fputs("Error: Need more commands\n", stderr);
    retval = 1;
  } else {
    setuid(0);
    int fd = open("/sys/class/leds/smc::kbd_backlight/brightness", O_RDWR);
    if (fd != -1) {
      if (!strcmp(argv[1], "write")) {
        if (argc < 3) {
          fputs("Error: Need a value to set the backlight to\n", stderr);
          retval = 1;
        } else {
          int count = 0;
          while (isdigit(argv[2][count]))
            count++;
          if (count) {
            write(fd, argv[2], strlen(argv[2]));
          } else {
            fprintf(stderr, "Error: Invalid input (%s)\n", argv[2]);
            retval = 3;
          }
        }
      } else if (!strcmp(argv[1], "read")) {
        char buffer[4];
        buffer[3] = 0;
        read(fd, buffer, sizeof(buffer) - 1);
        for (int i = 0; i < sizeof(buffer) - 1; i++) {
          if (!isdigit(buffer[i]))
            buffer[i] = 0;
        }
        puts(buffer);
      } else {
        fprintf(stderr, "Error: Invalid operation (%s)\n", argv[1]);
      }
      close(fd);
    } else {
      perror("Error");
      retval = 2;
    }
  }
  return retval;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1208 <main+0x48>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xe09(%rip),%rdi
call   1070 <fputs@plt>
movl   $0x1,-0x14(%rbp)
jmp    13e7 <main+0x227>
xor    %edi,%edi
call   10e0 <setuid@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0xdfc(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     13cf <main+0x20f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe09(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    1317 <main+0x157>
cmpl   $0x3,-0x8(%rbp)
jge    1279 <main+0xb9>
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
lea    0xde7(%rip),%rdi
call   1070 <fputs@plt>
movl   $0x1,-0x14(%rbp)
jmp    1312 <main+0x152>
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     12ad <main+0xed>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1280 <main+0xc0>
cmpl   $0x0,-0x1c(%rbp)
je     12e9 <main+0x129>
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
jmp    130d <main+0x14d>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xd7b(%rip),%rsi
call   10b0 <fprintf@plt>
movl   $0x3,-0x14(%rbp)
jmp    1312 <main+0x152>
jmp    13c2 <main+0x202>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd71(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    13a0 <main+0x1e0>
movb   $0x0,-0x1d(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x3,%edx
call   1090 <read@plt>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0x3,%rax
jae    1392 <main+0x1d2>
movslq -0x24(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
jne    137f <main+0x1bf>
movslq -0x24(%rbp),%rax
movb   $0x0,-0x20(%rbp,%rax,1)
jmp    1384 <main+0x1c4>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1350 <main+0x190>
lea    -0x20(%rbp),%rdi
call   1040 <puts@plt>
jmp    13bd <main+0x1fd>
mov    0x2c31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xce4(%rip),%rsi
call   10b0 <fprintf@plt>
jmp    13c2 <main+0x202>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
jmp    13e2 <main+0x222>
lea    0xce5(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x2,-0x14(%rbp)
jmp    13e7 <main+0x227>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11eb <main+0x2b>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    0xe24(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x1,%ebx
jmp    134e <main+0x18e>
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %edi,%edi
call   10e0 <setuid@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xe14(%rip),%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     135b <main+0x19b>
mov    %eax,%r14d
mov    0x8(%rbx),%r15
lea    0xe26(%rip),%rsi
mov    %r15,%rdi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     125f <main+0x9f>
lea    0xe61(%rip),%rsi
mov    %r15,%rdi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1283 <main+0xc3>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
mov    %r15,%rdx
call   10b0 <fprintf@plt>
jmp    1344 <main+0x184>
cmp    $0x3,%ebp
jae    12e2 <main+0x122>
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
lea    0xddb(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x1,%ebx
jmp    1346 <main+0x186>
movb   $0x0,0x7(%rsp)
lea    0x4(%rsp),%rsi
mov    %r14d,%edi
mov    $0x3,%edx
call   1090 <read@plt>
movsbl 0x4(%rsp),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
jne    12ae <main+0xee>
movb   $0x0,0x4(%rsp)
movsbl 0x5(%rsp),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
jne    12c2 <main+0x102>
movb   $0x0,0x5(%rsp)
movsbl 0x6(%rsp),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
jne    12d6 <main+0x116>
movb   $0x0,0x6(%rsp)
lea    0x4(%rsp),%rdi
call   1040 <puts@plt>
jmp    1344 <main+0x184>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x10(%rbx),%rax
movsbl (%rax,%rbp,1),%edi
call   1030 <isdigit@plt>
add    $0x1,%rbp
test   %rax,%rax
jne    12f0 <main+0x130>
cmp    $0x1,%ebp
jne    132b <main+0x16b>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xd5d(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x3,%ebx
jmp    1346 <main+0x186>
mov    0x10(%rbx),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
xor    %ebx,%ebx
mov    %r14d,%edi
call   1080 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd59(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x2,%ebx
jmp    134e <main+0x18e>

LLVM IR (-O0):
@.str = [27 x i8] c"Error: Need more commands\0A\00"
@stderr = global i32 0
@.str.1 = [46 x i8] c"/sys/class/leds/smc::kbd_backlight/brightness\00"
@O_RDWR = global i32 0
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [45 x i8] c"Error: Need a value to set the backlight to\0A\00"
@.str.4 = [27 x i8] c"Error: Invalid input (%s)\0A\00"
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [31 x i8] c"Error: Invalid operation (%s)\0A\00"
@.str.7 = [6 x i8] c"Error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fputs(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 1, i32* %6
br label %115
16: ; preds = %2
%17 = call i32 @setuid(i32 0)
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %112
22: ; preds = %16
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @strcmp(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp ne i32 %26, 0
br i1 %27, label %69, label %28
28: ; preds = %22
%29 = load i32, i32* %4
%30 = icmp slt i32 %29, 3
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = call i32 @fputs(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %32)
store i32 1, i32* %6
br label %68
34: ; preds = %28
store i32 0, i32* %8
br label %35
35: ; preds = %45, %34
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = call i64 @isdigit(i8 signext %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %35
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
store i32 %47, i32* %8
br label %35
48: ; preds = %35
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %61
51: ; preds = %48
%52 = load i32, i32* %7
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 2
%55 = load i8*, i8** %54
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 2
%58 = load i8*, i8** %57
%59 = call i32 @strlen(i8* %58)
%60 = call i32 @write(i32 %52, i8* %55, i32 %59)
br label %67
61: ; preds = %48
%62 = load i32, i32* @stderr
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 2
%65 = load i8*, i8** %64
%66 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %65)
store i32 3, i32* %6
br label %67
67: ; preds = %61, %51
br label %68
68: ; preds = %67, %31
br label %109
69: ; preds = %22
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 1
%72 = load i8*, i8** %71
%73 = call i32 @strcmp(i8* %72, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%74 = icmp ne i32 %73, 0
br i1 %74, label %102, label %75
75: ; preds = %69
%76 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 0, i8* %76
%77 = load i32, i32* %7
%78 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%79 = call i32 @read(i32 %77, i8* %78, i32 3)
store i32 0, i32* %10
br label %80
80: ; preds = %96, %75
%81 = load i32, i32* %10
%82 = sext i32 %81 to i64
%83 = icmp ult i64 %82, 3
br i1 %83, label %84, label %99
84: ; preds = %80
%85 = load i32, i32* %10
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 %86
%88 = load i8, i8* %87
%89 = call i64 @isdigit(i8 signext %88)
%90 = icmp ne i64 %89, 0
br i1 %90, label %95, label %91
91: ; preds = %84
%92 = load i32, i32* %10
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 %93
store i8 0, i8* %94
br label %95
95: ; preds = %91, %84
br label %96
96: ; preds = %95
%97 = load i32, i32* %10
%98 = add nsw i32 %97, 1
store i32 %98, i32* %10
br label %80
99: ; preds = %80
%100 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%101 = call i32 @puts(i8* %100)
br label %108
102: ; preds = %69
%103 = load i32, i32* @stderr
%104 = load i8**, i8*** %5
%105 = getelementptr inbounds i8*, i8** %104, i64 1
%106 = load i8*, i8** %105
%107 = call i32 @fprintf(i32 %103, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %106)
br label %108
108: ; preds = %102, %99
br label %109
109: ; preds = %108, %68
%110 = load i32, i32* %7
%111 = call i32 @close(i32 %110)
br label %114
112: ; preds = %16
%113 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i32 2, i32* %6
br label %114
114: ; preds = %112, %109
br label %115
115: ; preds = %114, %13
%116 = load i32, i32* %6
ret i32 %116
}

LLVM IR (-O2):
@.str = [27 x i8] c"Error: Need more commands\0A\00"
@stderr = global i32 0
@.str.1 = [46 x i8] c"/sys/class/leds/smc::kbd_backlight/brightness\00"
@O_RDWR = global i32 0
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [45 x i8] c"Error: Need a value to set the backlight to\0A\00"
@.str.4 = [27 x i8] c"Error: Invalid input (%s)\0A\00"
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [31 x i8] c"Error: Invalid operation (%s)\0A\00"
@.str.7 = [6 x i8] c"Error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fputs(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %77
8: ; preds = %2
%9 = tail call i32 @setuid(i32 0)
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %75, label %13
13: ; preds = %8
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @strcmp(i8* dereferenceable(1) %15, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %44
18: ; preds = %13
%19 = icmp ult i32 %0, 3
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = getelementptr inbounds i8*, i8** %1, i64 2
br label %25
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = tail call i32 @fputs(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %23)
br label %72
25: ; preds = %25, %20
%26 = phi i64 [ %32, %25 ], [ 0, %20 ]
%27 = load i8*, i8** %21
%28 = getelementptr inbounds i8, i8* %27, i64 %26
%29 = load i8, i8* %28
%30 = tail call i64 @isdigit(i8 signext %29)
%31 = icmp eq i64 %30, 0
%32 = add nuw i64 %26, 1
br i1 %31, label %33, label %25
33: ; preds = %25
%34 = trunc i64 %26 to i32
%35 = icmp eq i32 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = load i8*, i8** %21
%38 = tail call i32 @strlen(i8* %37)
%39 = tail call i32 @write(i32 %11, i8* %37, i32 %38)
br label %72
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %21
%43 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %42)
br label %72
44: ; preds = %13
%45 = tail call i32 @strcmp(i8* dereferenceable(1) %15, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %69
47: ; preds = %44
%48 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%49 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
store i8 0, i8* %49
%50 = call i32 @read(i32 %11, i8* %48, i32 3)
%51 = load i8, i8* %48
%52 = tail call i64 @isdigit(i8 signext %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %47
store i8 0, i8* %48
br label %55
55: ; preds = %47, %54
%56 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%57 = load i8, i8* %56
%58 = tail call i64 @isdigit(i8 signext %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %55
store i8 0, i8* %56
br label %61
61: ; preds = %60, %55
%62 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%63 = load i8, i8* %62
%64 = tail call i64 @isdigit(i8 signext %63)
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %61
store i8 0, i8* %62
br label %67
67: ; preds = %66, %61
%68 = call i32 @puts(i8* %48)
br label %72
69: ; preds = %44
%70 = load i32, i32* @stderr
%71 = tail call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %15)
br label %72
72: ; preds = %36, %40, %67, %69, %22
%73 = phi i32 [ 0, %69 ], [ 0, %67 ], [ 1, %22 ], [ 0, %36 ], [ 3, %40 ]
%74 = tail call i32 @close(i32 %11)
br label %77
75: ; preds = %8
%76 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %77
77: ; preds = %72, %75, %5
%78 = phi i32 [ 1, %5 ], [ %73, %72 ], [ 2, %75 ]
ret i32 %78
}