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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, char **);
int /*<<< orphan*/ showhelp();
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ write(int, int *, int);
int main(int argc, char *argv[]) {
  int canf_ktest_dev, cmd;
  char command[10];
  char device_path[20] = "/dev/";
  strcat(device_path, "canf_ktest");
  canf_ktest_dev = open(device_path, O_WRONLY);
  if (canf_ktest_dev == -1) {
    perror("Opening device");
    return 1;
  }
  showhelp();
  while (1) {
    printf("> ");
    scanf("%s", &command);
    if (strcmp(command, "start") == 0)
      cmd = '1';
    else if (strcmp(command, "stop") == 0)
      cmd = '2';
    else if (strcmp(command, "quit") == 0)
      break;
    else {
      printf("Bad command\n");
      continue;
    }
    write(canf_ktest_dev, &cmd, sizeof(cmd));
  }
  close(canf_ktest_dev);
  return 0;
}


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)
mov    0xe53(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe50(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe4e(%rip),%eax
mov    %eax,-0x30(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   10b0 <strcat@plt>
lea    -0x40(%rbp),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120c <main+0x7c>
lea    0xe24(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d5 <main+0x145>
mov    $0x0,%al
call   10a0 <showhelp@plt>
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
lea    -0x22(%rbp),%rsi
lea    0xe07(%rip),%rdi
call   1070 <scanf@plt>
lea    -0x22(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1255 <main+0xc5>
movl   $0x31,-0x18(%rbp)
jmp    12b0 <main+0x120>
lea    -0x22(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    127b <main+0xeb>
movl   $0x32,-0x18(%rbp)
jmp    12ab <main+0x11b>
lea    -0x22(%rbp),%rdi
lea    0xdb9(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    129a <main+0x10a>
jmp    12c6 <main+0x136>
lea    0xda3(%rip),%rdi
call   1040 <printf@plt>
jmp    1213 <main+0x83>
jmp    12b0 <main+0x120>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    1213 <main+0x83>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movaps 0xe4b(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
lea    0xe4b(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   10c0 <strcat@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12af <main+0x10f>
mov    %eax,%ebp
xor    %eax,%eax
call   10b0 <showhelp@plt>
lea    0xe2e(%rip),%r14
lea    0xe2a(%rip),%r15
lea    0x2e(%rsp),%rbx
lea    0xe21(%rip),%r12
lea    0xe20(%rip),%r13
jmp    123a <main+0x9a>
nopl   0x0(%rax)
mov    $0x31,%eax
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %r14,%rdi
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <scanf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1220 <main+0x80>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1290 <main+0xf0>
mov    %rbx,%rdi
lea    0xdc8(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1297 <main+0xf7>
lea    0xdbc(%rip),%rdi
call   1030 <puts@plt>
jmp    123a <main+0x9a>
nop
mov    $0x32,%eax
jmp    1225 <main+0x85>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd69(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
jmp    12a0 <main+0x100>

LLVM IR (-O0):
@__const.main.device_path = [20 x i8] c"/dev/\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [11 x i8] c"canf_ktest\00"
@O_WRONLY = global i32 0
@.str.1 = [15 x i8] c"Opening device\00"
@.str.2 = [3 x i8] c"> \00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [6 x i8] c"start\00"
@.str.5 = [5 x i8] c"stop\00"
@.str.6 = [5 x i8] c"quit\00"
@.str.7 = [13 x i8] c"Bad command\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 [10 x i8]
%9 = alloca [20 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [20 x i8]* %9 to i8*
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%12 = call i32 @strcat(i8* %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %49
20: ; preds = %2
%21 = call i32 (...) @showhelp()
br label %22
22: ; preds = %20, %40, %43
%23 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%24 = bitcast [10 x i8]* %8 to i8**
%25 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %24)
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%27 = call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 49, i32* %7
br label %43
30: ; preds = %22
%31 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%32 = call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 50, i32* %7
br label %42
35: ; preds = %30
%36 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
br label %46
40: ; preds = %35
%41 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %22
42: ; preds = %34
br label %43
43: ; preds = %42, %29
%44 = load i32, i32* %6
%45 = call i32 @write(i32 %44, i32* %7, i32 4)
br label %22
46: ; preds = %39
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
store i32 0, i32* %3
br label %49
49: ; preds = %46, %18
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@__const.main.device_path = [20 x i8] c"/dev/\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [11 x i8] c"canf_ktest\00"
@O_WRONLY = global i32 0
@.str.1 = [15 x i8] c"Opening device\00"
@.str.2 = [3 x i8] c"> \00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [6 x i8] c"start\00"
@.str.5 = [5 x i8] c"stop\00"
@.str.6 = [5 x i8] c"quit\00"
@str = [12 x i8] c"Bad command\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [10 x i8]
%5 = alloca [20 x i8]
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%9 = call i32 @strcat(i8* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @O_WRONLY
%11 = call i32 @open(i8* %8, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %37
15: ; preds = %2
%16 = call i32 (...) @showhelp()
%17 = bitcast [10 x i8]* %4 to i8**
br label %18
18: ; preds = %31, %15
%19 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %17)
%21 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %18
%24 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %23
%27 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %26
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %31
31: ; preds = %29, %32
br label %18
32: ; preds = %23, %18
%33 = phi i32 [ 49, %18 ], [ 50, %23 ]
store i32 %33, i32* %3
%34 = call i32 @write(i32 %11, i32* %3, i32 4)
br label %31
35: ; preds = %26
%36 = call i32 @close(i32 %11)
br label %37
37: ; preds = %35, %13
%38 = phi i32 [ 1, %13 ], [ 0, %35 ]
ret i32 %38
}