#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ KDSETLED;
int /*<<< orphan*/ O_WRONLY;
int close(int);
int ioctl(int, int /*<<< orphan*/, unsigned char);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sscanf(char *, char *, unsigned char *);
int main(int argc, char *argv[]) {
  int fd;
  unsigned char state;
  if (argc < 2 || sscanf(argv[1], "%hhu", &state) != 1) {
    printf("Usage: ledcns STATE\n");
    return EXIT_FAILURE;
  }
  if ((fd = open("/dev/console", O_WRONLY)) == -1) {
    perror("open");
    return EXIT_FAILURE;
  }
  if (ioctl(fd, KDSETLED, state) == -1) {
    perror("ioctl");
    return EXIT_FAILURE;
  }
  if (close(fd) == -1) {
    perror("close");
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11a1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe71(%rip),%rsi
lea    -0x15(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     11be <main+0x5e>
lea    0xe5d(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1271 <main+0x111>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11fc <main+0x9c>
lea    0xe41(%rip),%rdi
call   1080 <perror@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1271 <main+0x111>
mov    -0x14(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
movzbl -0x15(%rbp),%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1237 <main+0xd7>
lea    0xe0b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1271 <main+0x111>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1265 <main+0x105>
lea    0xde3(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1271 <main+0x111>
mov    0x2d64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jl     11cf <main+0x6f>
mov    0x8(%rsi),%rdi
lea    0xe8b(%rip),%rsi
lea    0xf(%rsp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11cf <main+0x6f>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11ea <main+0x8a>
mov    %eax,%ebx
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
movzbl 0xf(%rsp),%edx
mov    %ebx,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11f3 <main+0x93>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     11fc <main+0x9c>
mov    0x2e03(%rip),%rax
jmp    11e2 <main+0x82>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe21(%rip),%rdi
jmp    1203 <main+0xa3>
lea    0xe1d(%rip),%rdi
jmp    1203 <main+0xa3>
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
jmp    11db <main+0x7b>

LLVM IR (-O0):
@.str = [5 x i8] c"%hhu\00"
@.str.1 = [21 x i8] c"Usage: ledcns STATE\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"/dev/console\00"
@O_WRONLY = global i32 0
@.str.3 = [5 x i8] c"open\00"
@KDSETLED = global i32 0
@.str.4 = [6 x i8] c"ioctl\00"
@.str.5 = [6 x i8] c"close\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @sscanf(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %7)
%15 = icmp ne i32 %14, 1
br i1 %15, label %16, label %19
16: ; preds = %10, %2
%17 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
store i32 %18, i32* %3
br label %44
19: ; preds = %10
%20 = load i32, i32* @O_WRONLY
%21 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %20)
store i32 %21, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
store i32 %25, i32* %3
br label %44
26: ; preds = %19
%27 = load i32, i32* %6
%28 = load i32, i32* @KDSETLED
%29 = load i8, i8* %7
%30 = call i32 @ioctl(i32 %27, i32 %28, i8 zeroext %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
store i32 %34, i32* %3
br label %44
35: ; preds = %26
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
store i32 %41, i32* %3
br label %44
42: ; preds = %35
%43 = load i32, i32* @EXIT_SUCCESS
store i32 %43, i32* %3
br label %44
44: ; preds = %42, %39, %32, %23, %16
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str = [5 x i8] c"%hhu\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"/dev/console\00"
@O_WRONLY = global i32 0
@.str.3 = [5 x i8] c"open\00"
@KDSETLED = global i32 0
@.str.4 = [6 x i8] c"ioctl\00"
@.str.5 = [6 x i8] c"close\00"
@EXIT_SUCCESS = global i32 0
@str = [20 x i8] c"Usage: ledcns STATE\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = icmp slt i32 %0, 2
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %3)
%9 = icmp eq i32 %8, 1
br i1 %9, label %12, label %10
10: ; preds = %5, %2
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %30
12: ; preds = %5
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %30
18: ; preds = %12
%19 = load i32, i32* @KDSETLED
%20 = load i8, i8* %3
%21 = call i32 @ioctl(i32 %14, i32 %19, i8 zeroext %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %30
25: ; preds = %18
%26 = call i32 @close(i32 %14)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %30
30: ; preds = %25, %28, %23, %16, %10
%31 = phi i32* [ @EXIT_FAILURE, %10 ], [ @EXIT_FAILURE, %16 ], [ @EXIT_FAILURE, %23 ], [ @EXIT_FAILURE, %28 ], [ @EXIT_SUCCESS, %25 ]
%32 = load i32, i32* %31
ret i32 %32
}