#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*/ O_RDWR;
unsigned long SIGKILL;
scalar_t__ _IO(char, int);
unsigned long atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
unsigned long getpid();
int ioctl(int, int, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int fd, rc;
  unsigned long pid, sig;
  char *nodename = "/dev/mycdrv";
  pid = getpid();
  sig = SIGKILL;
  if (argc > 1)
    pid = atoi(argv[1]);
  if (argc > 2)
    sig = atoi(argv[2]);
  if (argc > 3)
    nodename = argv[3];
  fd = open(nodename, O_RDWR);
  printf(" I opened the device node, file descriptor = %d\n", fd);
  rc = ioctl(fd, (int)_IO('k', 1), pid);
  printf("\n\n rc from ioctl setting pid is = %d \n\n", rc);
  rc = ioctl(fd, (int)_IO('k', 2), sig);
  printf("\n\n rc from ioctl setting signal is = %d \n\n", rc);
  rc = ioctl(fd, (int)_IO('k', 3), "anything");
  printf("\n\n rc from ioctl sending signal is = %d \n\n", rc);
  close(fd);
  printf("\n\n FINISHED, TERMINATING NORMALLY\n");
  exit(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)
lea    0xe63(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11d5 <main+0x55>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11f0 <main+0x70>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    1206 <main+0x86>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x6b,%edi
mov    $0x1,%esi
call   1050 <_IO@plt>
mov    -0x3c(%rbp),%edi
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    $0x6b,%edi
mov    $0x2,%esi
call   1050 <_IO@plt>
mov    -0x38(%rbp),%edi
mov    %eax,%esi
mov    -0x28(%rbp),%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    $0x6b,%edi
mov    $0x3,%esi
call   1050 <_IO@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%esi
lea    0xdd1(%rip),%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%r15
mov    0x2e2a(%rip),%rax
mov    (%rax),%r14
lea    0xe48(%rip),%r12
cmp    $0x2,%ebp
jl     11e3 <main+0x53>
mov    0x8(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %rax,%r15
cmp    $0x2,%ebp
je     11e3 <main+0x53>
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %rax,%r14
cmp    $0x4,%ebp
jb     11e3 <main+0x53>
mov    0x18(%rbx),%r12
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
lea    0xe0f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x6b,%edi
mov    $0x1,%esi
call   1060 <_IO@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <ioctl@plt>
lea    0xe13(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x6b,%edi
mov    $0x2,%esi
call   1060 <_IO@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <ioctl@plt>
lea    0xe0e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x6b,%edi
mov    $0x3,%esi
call   1060 <_IO@plt>
lea    0xe1a(%rip),%rdx
mov    %ebp,%edi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <ioctl@plt>
lea    0xe11(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0xe25(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/mycdrv\00"
@SIGKILL = global i64 0
@O_RDWR = global i32 0
@.str.1 = [49 x i8] c" I opened the device node, file descriptor = %d\0A\00"
@.str.2 = [40 x i8] c"\0A\0A rc from ioctl setting pid is = %d \0A\0A\00"
@.str.3 = [43 x i8] c"\0A\0A rc from ioctl setting signal is = %d \0A\0A\00"
@.str.4 = [9 x i8] c"anything\00"
@.str.5 = [43 x i8] c"\0A\0A rc from ioctl sending signal is = %d \0A\0A\00"
@.str.6 = [35 x i8] c"\0A\0A FINISHED, TERMINATING NORMALLY\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 i64
%9 = alloca i64
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %10
%11 = call i64 (...) @getpid()
store i64 %11, i64* %8
%12 = load i64, i64* @SIGKILL
store i64 %12, i64* %9
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i64 @atoi(i8* %18)
store i64 %19, i64* %8
br label %20
20: ; preds = %15, %2
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 2
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i64 @atoi(i8* %26)
store i64 %27, i64* %9
br label %28
28: ; preds = %23, %20
%29 = load i32, i32* %4
%30 = icmp sgt i32 %29, 3
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 3
%34 = load i8*, i8** %33
store i8* %34, i8** %10
br label %35
35: ; preds = %31, %28
%36 = load i8*, i8** %10
%37 = load i32, i32* @O_RDWR
%38 = call i32 @open(i8* %36, i32 %37)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %6
%42 = call i64 @_IO(i8 signext 107, i32 1)
%43 = trunc i64 %42 to i32
%44 = load i64, i64* %8
%45 = call i32 (i32, i32, ...) @ioctl(i32 %41, i32 %43, i64 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %46)
%48 = load i32, i32* %6
%49 = call i64 @_IO(i8 signext 107, i32 2)
%50 = trunc i64 %49 to i32
%51 = load i64, i64* %9
%52 = call i32 (i32, i32, ...) @ioctl(i32 %48, i32 %50, i64 %51)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %53)
%55 = load i32, i32* %6
%56 = call i64 @_IO(i8 signext 107, i32 3)
%57 = trunc i64 %56 to i32
%58 = call i32 (i32, i32, ...) @ioctl(i32 %55, i32 %57, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 %58, i32* %7
%59 = load i32, i32* %7
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%64 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/mycdrv\00"
@SIGKILL = global i64 0
@O_RDWR = global i32 0
@.str.1 = [49 x i8] c" I opened the device node, file descriptor = %d\0A\00"
@.str.2 = [40 x i8] c"\0A\0A rc from ioctl setting pid is = %d \0A\0A\00"
@.str.3 = [43 x i8] c"\0A\0A rc from ioctl setting signal is = %d \0A\0A\00"
@.str.4 = [9 x i8] c"anything\00"
@.str.5 = [43 x i8] c"\0A\0A rc from ioctl sending signal is = %d \0A\0A\00"
@str = [34 x i8] c"\0A\0A FINISHED, TERMINATING NORMALLY\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i64 (...) @getpid()
%4 = load i64, i64* @SIGKILL
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %19
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i64 @atoi(i8* %8)
%10 = icmp eq i32 %0, 2
br i1 %10, label %19, label %11
11: ; preds = %6
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i64 @atoi(i8* %13)
%15 = icmp ugt i32 %0, 3
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = getelementptr inbounds i8*, i8** %1, i64 3
%18 = load i8*, i8** %17
br label %19
19: ; preds = %2, %6, %16, %11
%20 = phi i64 [ %14, %16 ], [ %14, %11 ], [ %4, %6 ], [ %4, %2 ]
%21 = phi i64 [ %9, %16 ], [ %9, %11 ], [ %9, %6 ], [ %3, %2 ]
%22 = phi i8* [ %18, %16 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %11 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %6 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ]
%23 = load i32, i32* @O_RDWR
%24 = tail call i32 @open(i8* %22, i32 %23)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = tail call i64 @_IO(i8 signext 107, i32 1)
%27 = trunc i64 %26 to i32
%28 = tail call i32 (i32, i32, ...) @ioctl(i32 %24, i32 %27, i64 %21)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = tail call i64 @_IO(i8 signext 107, i32 2)
%31 = trunc i64 %30 to i32
%32 = tail call i32 (i32, i32, ...) @ioctl(i32 %24, i32 %31, i64 %20)
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = tail call i64 @_IO(i8 signext 107, i32 3)
%35 = trunc i64 %34 to i32
%36 = tail call i32 (i32, i32, ...) @ioctl(i32 %24, i32 %35, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %36)
%38 = tail call i32 @close(i32 %24)
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%40 = tail call i32 @exit(i32 0)
unreachable
}