#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ ioctl(int, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned long);
int /*<<< orphan*/ read(int, unsigned long *, int);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int on;
  int led_no;
  int fd;
  if (argc != 3 || sscanf(argv[1], "%d", &led_no) != 1 ||
      sscanf(argv[2], "%d", &on) != 1 || on < 0 || on > 1 || led_no < 0 ||
      led_no > 4) {
    fprintf(stderr, "Usage: leds led_no 0|1\n");
    exit(1);
  }
  fd = open("/dev/ledsdev", 0);
  if (fd < 0) {
    perror("open leds device "
           "/dev/ledsdev");
    exit(1);
  }
  ioctl(fd, on, led_no);
  unsigned long leds_data = 0;
  read(fd, &leds_data, 4);
  printf("read from device: %x\n", leds_data);
  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   $0x3,-0x8(%rbp)
jne    121a <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe41(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    121a <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe20(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    121a <main+0x8a>
cmpl   $0x0,-0x14(%rbp)
jl     121a <main+0x8a>
cmpl   $0x1,-0x14(%rbp)
jg     121a <main+0x8a>
cmpl   $0x0,-0x18(%rbp)
jl     121a <main+0x8a>
cmpl   $0x4,-0x18(%rbp)
jle    1239 <main+0xa9>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdec(%rip),%rdi
xor    %esi,%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    126a <main+0xda>
lea    0xdc0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <ioctl@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    -0x28(%rbp),%rsi
lea    0xd9d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jne    124c <main+0xbc>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe54(%rip),%rsi
mov    %rsp,%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    124c <main+0xbc>
mov    0x10(%rbx),%rdi
lea    0xe38(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    124c <main+0xbc>
mov    0x4(%rsp),%eax
test   %eax,%eax
js     124c <main+0xbc>
cmp    $0x1,%eax
jg     124c <main+0xbc>
mov    (%rsp),%eax
test   %eax,%eax
js     124c <main+0xbc>
cmp    $0x5,%eax
jge    124c <main+0xbc>
lea    0xe35(%rip),%rdi
xor    %esi,%esi
call   1090 <open@plt>
test   %eax,%eax
js     126b <main+0xdb>
mov    %eax,%ebx
mov    0x4(%rsp),%esi
mov    (%rsp),%edx
mov    %eax,%edi
call   1040 <ioctl@plt>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <read@plt>
mov    0x8(%rsp),%rsi
lea    0xe01(%rip),%rdi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xda7(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xda9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Usage: leds led_no 0|1\0A\00"
@.str.2 = [13 x i8] c"/dev/ledsdev\00"
@.str.3 = [30 x i8] c"open leds device /dev/ledsdev\00"
@.str.4 = [22 x i8] c"read from device: %x\0A\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 i64
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, 3
br i1 %11, label %36, label %12
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %7)
%17 = icmp ne i32 %16, 1
br i1 %17, label %36, label %18
18: ; preds = %12
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%23 = icmp ne i32 %22, 1
br i1 %23, label %36, label %24
24: ; preds = %18
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %24
%28 = load i32, i32* %6
%29 = icmp sgt i32 %28, 1
br i1 %29, label %36, label %30
30: ; preds = %27
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = load i32, i32* %7
%35 = icmp sgt i32 %34, 4
br i1 %35, label %36, label %40
36: ; preds = %33, %30, %27, %24, %18, %12, %2
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %33
%41 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 0)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %40
%48 = load i32, i32* %8
%49 = load i32, i32* %6
%50 = load i32, i32* %7
%51 = call i32 @ioctl(i32 %48, i32 %49, i32 %50)
store i64 0, i64* %9
%52 = load i32, i32* %8
%53 = call i32 @read(i32 %52, i64* %9, i32 4)
%54 = load i64, i64* %9
%55 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %54)
%56 = load i32, i32* %8
%57 = call i32 @close(i32 %56)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Usage: leds led_no 0|1\0A\00"
@.str.2 = [13 x i8] c"/dev/ledsdev\00"
@.str.3 = [30 x i8] c"open leds device /dev/ledsdev\00"
@.str.4 = [22 x i8] c"read from device: %x\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = icmp eq i32 %0, 3
br i1 %8, label %9, label %29
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %29
14: ; preds = %9
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%18 = icmp ne i32 %17, 1
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
%21 = select i1 %18, i1 true, i1 %20
%22 = icmp sgt i32 %19, 1
%23 = select i1 %21, i1 true, i1 %22
%24 = load i32, i32* %4
%25 = icmp slt i32 %24, 0
%26 = select i1 %23, i1 true, i1 %25
%27 = icmp sgt i32 %24, 4
%28 = select i1 %26, i1 true, i1 %27
br i1 %28, label %29, label %33
29: ; preds = %14, %9, %2
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %14
%34 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 0)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %33
%40 = load i32, i32* %3
%41 = load i32, i32* %4
%42 = call i32 @ioctl(i32 %34, i32 %40, i32 %41)
%43 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%44 = call i32 @read(i32 %34, i64* %5, i32 4)
%45 = load i64, i64* %5
%46 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %45)
%47 = call i32 @close(i32 %34)
ret i32 0
}