#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*/ uleds_dev;
struct uleds_user_dev {
  int max_brightness;
  int /*<<< orphan*/ name;
};
struct timespec {
  int /*<<< orphan*/ tv_nsec;
  int /*<<< orphan*/ tv_sec;
};
typedef int /*<<< orphan*/ brightness;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ LED_MAX_NAME_SIZE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/, int /*<<< orphan*/, int);
int read(int, int *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int write(int, struct uleds_user_dev *, int);
int main(int argc, char const *argv[]) {
  struct uleds_user_dev uleds_dev;
  int fd, ret;
  int brightness;
  struct timespec ts;
  if (argc != 2) {
    fprintf(stderr, "Requires <device-name> argument\n");
    return 1;
  }
  strncpy(uleds_dev.name, argv[1], LED_MAX_NAME_SIZE);
  uleds_dev.max_brightness = 100;
  fd = open("/dev/uleds", O_RDWR);
  if (fd == -1) {
    perror("Failed to open /dev/uleds");
    return 1;
  }
  ret = write(fd, &uleds_dev, sizeof(uleds_dev));
  if (ret == -1) {
    perror("Failed to write to /dev/uleds");
    close(fd);
    return 1;
  }
  while (1) {
    ret = read(fd, &brightness, sizeof(brightness));
    if (ret == -1) {
      perror("Failed to read from /dev/uleds");
      close(fd);
      return 1;
    }
    clock_gettime(CLOCK_MONOTONIC, &ts);
    printf("[%ld.%09ld] %u\n", ts.tv_sec, ts.tv_nsec, brightness);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d1 <main+0x41>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <main+0x148>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
movl   $0x64,-0x18(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    122b <main+0x9b>
lea    0xe07(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <main+0x148>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1050 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1269 <main+0xd9>
lea    0xdeb(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <main+0x148>
jmp    126e <main+0xde>
mov    -0x1c(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12ac <main+0x11c>
lea    0xdc6(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <main+0x148>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xdaa(%rip),%rdi
call   1060 <printf@plt>
jmp    126e <main+0xde>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
je     11ca <main+0x3a>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x8(%rsi),%rsi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
movl   $0x64,0x18(%rsp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    120c <main+0x7c>
lea    0xe1c(%rip),%rdi
call   10b0 <perror@plt>
jmp    11b7 <main+0x27>
mov    %eax,%ebx
lea    0x18(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
jne    122d <main+0x9d>
lea    0xe10(%rip),%rdi
jmp    129b <main+0x10b>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     1294 <main+0x104>
mov    0x2d96(%rip),%r13
lea    0x10(%rsp),%r14
lea    0xe22(%rip),%r15
lea    0xc(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
mov    %r14,%rsi
call   1040 <clock_gettime@plt>
mov    0x10(%rsp),%edx
mov    0x14(%rsp),%esi
mov    0xc(%rsp),%ecx
mov    %r15,%rdi
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
jne    1260 <main+0xd0>
lea    0xdbe(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    11b7 <main+0x27>

LLVM IR (-O0):
%struct.uleds_user_dev = type { i32, i32 }
%struct.timespec = type { i32, i32 }
@stderr = global i32 0
@.str = [33 x i8] c"Requires <device-name> argument\0A\00"
@LED_MAX_NAME_SIZE = global i32 0
@.str.1 = [11 x i8] c"/dev/uleds\00"
@O_RDWR = global i32 0
@.str.2 = [26 x i8] c"Failed to open /dev/uleds\00"
@.str.3 = [30 x i8] c"Failed to write to /dev/uleds\00"
@.str.4 = [31 x i8] c"Failed to read from /dev/uleds\00"
@CLOCK_MONOTONIC = global i32 0
@.str.5 = [16 x i8] c"[%ld.%09ld] %u\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.uleds_user_dev
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.timespec
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %59
16: ; preds = %2
%17 = getelementptr inbounds %struct.uleds_user_dev, %struct.uleds_user_dev* %6, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @LED_MAX_NAME_SIZE
%23 = call i32 @strncpy(i32 %18, i8* %21, i32 %22)
%24 = getelementptr inbounds %struct.uleds_user_dev, %struct.uleds_user_dev* %6, i32 0, i32 0
store i32 100, i32* %24
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %59
31: ; preds = %16
%32 = load i32, i32* %7
%33 = call i32 @write(i32 %32, %struct.uleds_user_dev* %6, i32 8)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* %7
%39 = call i32 @close(i32 %38)
store i32 1, i32* %3
br label %59
40: ; preds = %31
br label %41
41: ; preds = %40, %50
%42 = load i32, i32* %7
%43 = call i32 @read(i32 %42, i32* %9, i32 4)
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
store i32 1, i32* %3
br label %59
50: ; preds = %41
%51 = load i32, i32* @CLOCK_MONOTONIC
%52 = call i32 @clock_gettime(i32 %51, %struct.timespec* %10)
%53 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 1
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 0
%56 = load i32, i32* %55
%57 = load i32, i32* %9
%58 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %54, i32 %56, i32 %57)
br label %41
59: ; preds = %46, %36, %29, %13
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.uleds_user_dev = type { i32, i32 }
%struct.timespec = type { i32, i32 }
@stderr = global i32 0
@.str = [33 x i8] c"Requires <device-name> argument\0A\00"
@LED_MAX_NAME_SIZE = global i32 0
@.str.1 = [11 x i8] c"/dev/uleds\00"
@O_RDWR = global i32 0
@.str.2 = [26 x i8] c"Failed to open /dev/uleds\00"
@.str.3 = [30 x i8] c"Failed to write to /dev/uleds\00"
@.str.4 = [31 x i8] c"Failed to read from /dev/uleds\00"
@CLOCK_MONOTONIC = global i32 0
@.str.5 = [16 x i8] c"[%ld.%09ld] %u\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.uleds_user_dev
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = bitcast %struct.uleds_user_dev* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.timespec* %5 to i8*
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %48
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @LED_MAX_NAME_SIZE
%17 = tail call i32 @strncpy(i32 undef, i8* %15, i32 %16)
%18 = getelementptr inbounds %struct.uleds_user_dev, %struct.uleds_user_dev* %3, i64 0, i32 0
store i32 100, i32* %18
%19 = load i32, i32* @O_RDWR
%20 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %48
24: ; preds = %13
%25 = call i32 @write(i32 %20, %struct.uleds_user_dev* %3, i32 8)
%26 = icmp eq i32 %25, -1
br i1 %26, label %33, label %27
27: ; preds = %24
%28 = call i32 @read(i32 %20, i32* %4, i32 4)
%29 = icmp eq i32 %28, -1
br i1 %29, label %36, label %30
30: ; preds = %27
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
br label %39
33: ; preds = %24
%34 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%35 = tail call i32 @close(i32 %20)
br label %48
36: ; preds = %39, %27
%37 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @close(i32 %20)
br label %48
39: ; preds = %30, %39
%40 = load i32, i32* @CLOCK_MONOTONIC
%41 = call i32 @clock_gettime(i32 %40, %struct.timespec* %5)
%42 = load i32, i32* %31
%43 = load i32, i32* %32
%44 = load i32, i32* %4
%45 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %42, i32 %43, i32 %44)
%46 = call i32 @read(i32 %20, i32* %4, i32 4)
%47 = icmp eq i32 %46, -1
br i1 %47, label %36, label %39
48: ; preds = %36, %33, %22, %10
ret i32 1
}