#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 */
struct stat {
  int /*<<< orphan*/ st_dev;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ die(char *, ...);
int /*<<< orphan*/ errno;
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
scalar_t__ major(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ *strchr(char *, char);
int strlen(char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  struct stat st;
  char buf[1024];
  int len, fd;
  if (argc != 2)
    die("Usage: xf86-video-intel-backlight-helper <iface>\n");
  if (strchr(argv[1], '/') != NULL)
    die("Invalid interface '%s': contains '/'\n", argv[1]);
  if (snprintf(buf, sizeof(buf), "/sys/class/backlight/%s/brightness",
               argv[1]) >= sizeof(buf))
    die("Invalid interface '%s': name too long\n", argv[1]);
  fd = open(buf, O_RDWR);
  if (fd < 0 || fstat(fd, &st) || major(st.st_dev))
    die("Invalid interface '%s': unknown backlight file\n", argv[1]);
  while (fgets(buf, sizeof(buf), stdin)) {
    len = strlen(buf);
    if (write(fd, buf, len) != len)
      die("Failed to update backlight interface '%s': errno=%d\n", argv[1],
          errno);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c1 <main+0x31>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     11f3 <main+0x63>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0x400,%esi
lea    0xe4a(%rip),%rdx
call   1060 <snprintf@plt>
cltq
cmp    $0x400,%rax
jb     1237 <main+0xa7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
lea    -0x420(%rbp),%rdi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x0,-0x428(%rbp)
jl     128a <main+0xfa>
mov    -0x428(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
jne    128a <main+0xfa>
mov    -0x18(%rbp),%edi
call   1070 <major@plt>
cmp    $0x0,%rax
je     12a0 <main+0x110>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
jmp    12a5 <main+0x115>
lea    -0x420(%rbp),%rdi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1323 <main+0x193>
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x424(%rbp)
mov    -0x428(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1030 <write@plt>
cmp    -0x424(%rbp),%eax
je     131e <main+0x18e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2cc2(%rip),%rax
mov    (%rax),%edx
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
jmp    12a5 <main+0x115>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
je     11b7 <main+0x27>
lea    0xe50(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x8(%r14),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11dc <main+0x4c>
mov    0x8(%r14),%rsi
lea    0xe5d(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x8(%r14),%rcx
lea    0xe71(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
call   1060 <snprintf@plt>
cmp    $0x400,%eax
jb     120f <main+0x7f>
mov    0x8(%r14),%rsi
lea    0xe73(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1090 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
js     1249 <main+0xb9>
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
call   10b0 <fstat@plt>
test   %rax,%rax
jne    1249 <main+0xb9>
mov    0x8(%rsp),%edi
call   1070 <major@plt>
test   %rax,%rax
je     125b <main+0xcb>
mov    0x8(%r14),%rsi
lea    0xe4e(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x2d7e(%rip),%r13
mov    0x0(%r13),%edx
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     12da <main+0x14a>
lea    0x10(%rsp),%rbx
lea    0xe4c(%rip),%r15
jmp    12a6 <main+0x116>
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     12da <main+0x14a>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1290 <main+0x100>
mov    0x8(%r14),%rsi
mov    0x2d04(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %eax,%eax
call   10a0 <die@plt>
jmp    1290 <main+0x100>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [50 x i8] c"Usage: xf86-video-intel-backlight-helper <iface>\0A\00"
@.str.1 = [38 x i8] c"Invalid interface '%s': contains '/'\0A\00"
@.str.2 = [35 x i8] c"/sys/class/backlight/%s/brightness\00"
@.str.3 = [39 x i8] c"Invalid interface '%s': name too long\0A\00"
@O_RDWR = global i32 0
@.str.4 = [48 x i8] c"Invalid interface '%s': unknown backlight file\0A\00"
@stdin = global i32 0
@.str.5 = [53 x i8] c"Failed to update backlight interface '%s': errno=%d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32* @strchr(i8* %17, i8 signext 47)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %20, %14
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @snprintf(i8* %26, i32 1024, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = sext i32 %30 to i64
%32 = icmp uge i64 %31, 1024
br i1 %32, label %33, label %38
33: ; preds = %25
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %33, %25
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%40 = load i32, i32* @O_RDWR
%41 = call i32 @open(i8* %39, i32 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp slt i32 %42, 0
br i1 %43, label %53, label %44
44: ; preds = %38
%45 = load i32, i32* %9
%46 = call i64 @fstat(i32 %45, %struct.stat* %6)
%47 = icmp ne i64 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %44
%49 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i64 @major(i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %48, %44, %38
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0), i8* %56)
br label %58
58: ; preds = %53, %48
br label %59
59: ; preds = %79, %58
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%61 = load i32, i32* @stdin
%62 = call i64 @fgets(i8* %60, i32 1024, i32 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %80
64: ; preds = %59
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%66 = call i32 @strlen(i8* %65)
store i32 %66, i32* %8
%67 = load i32, i32* %9
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%69 = load i32, i32* %8
%70 = call i32 @write(i32 %67, i8* %68, i32 %69)
%71 = load i32, i32* %8
%72 = icmp ne i32 %70, %71
br i1 %72, label %73, label %79
73: ; preds = %64
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 1
%76 = load i8*, i8** %75
%77 = load i32, i32* @errno
%78 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i8* %76, i32 %77)
br label %79
79: ; preds = %73, %64
br label %59
80: ; preds = %59
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [50 x i8] c"Usage: xf86-video-intel-backlight-helper <iface>\0A\00"
@.str.1 = [38 x i8] c"Invalid interface '%s': contains '/'\0A\00"
@.str.2 = [35 x i8] c"/sys/class/backlight/%s/brightness\00"
@.str.3 = [39 x i8] c"Invalid interface '%s': name too long\0A\00"
@O_RDWR = global i32 0
@.str.4 = [48 x i8] c"Invalid interface '%s': unknown backlight file\0A\00"
@stdin = global i32 0
@.str.5 = [53 x i8] c"Failed to update backlight interface '%s': errno=%d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [1024 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 2
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @strchr(i8* %12, i8 signext 47)
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = load i8*, i8** %11
%17 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %15, %10
%19 = load i8*, i8** %11
%20 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = icmp ugt i32 %20, 1023
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i8*, i8** %11
%24 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %18
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* %6, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %37, label %29
29: ; preds = %25
%30 = call i64 @fstat(i32 %27, %struct.stat* %3)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i64 @major(i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %32, %29, %25
%38 = load i8*, i8** %11
%39 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %37, %32
%41 = load i32, i32* @stdin
%42 = call i64 @fgets(i8* %6, i32 1024, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %56, label %44
44: ; preds = %40, %52
%45 = call i32 @strlen(i8* %6)
%46 = call i32 @write(i32 %27, i8* %6, i32 %45)
%47 = icmp eq i32 %46, %45
br i1 %47, label %52, label %48
48: ; preds = %44
%49 = load i8*, i8** %11
%50 = load i32, i32* @errno
%51 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i8* %49, i32 %50)
br label %52
52: ; preds = %48, %44
%53 = load i32, i32* @stdin
%54 = call i64 @fgets(i8* %6, i32 1024, i32 %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %44
56: ; preds = %52, %40
ret i32 0
}