#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;
int /*<<< orphan*/ _IO(char, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  printf("Trying to reset the usecount of the /dev/spid0 driver\n");
  int fd = open("/dev/spid0", O_RDWR);
  if (fd < 0) {
    printf("Error while oppening /dev/spid0: %s\n", strerror(errno));
    return -1;
  }
  if (ioctl(fd, _IO('E', 0)) == 0) {
    printf("Usecount is reset successful\nNow it should be possible to remove "
           "the device from the kernel\n");
  }
  close(fd);
  return 0;
}


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)
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xea3(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d1 <main+0x71>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <main+0xb4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x45,%edi
xor    %esi,%esi
call   1040 <_IO@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%esi
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jne    1205 <main+0xa5>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xeb6(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11cb <main+0x5b>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    $0x45,%edi
xor    %esi,%esi
call   1050 <_IO@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1060 <ioctl@plt>
test   %rax,%rax
jne    11c2 <main+0x52>
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    11ef <main+0x7f>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe2b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [55 x i8] c"Trying to reset the usecount of the /dev/spid0 driver\0A\00"
@.str.1 = [11 x i8] c"/dev/spid0\00"
@O_RDWR = global i32 0
@.str.2 = [37 x i8] c"Error while oppening /dev/spid0: %s\0A\00"
@errno = global i32 0
@.str.3 = [93 x i8] c"Usecount is reset successful\0ANow it should be possible to remove the device from the kernel\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %14)
store i32 -1, i32* %3
br label %26
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i32 @_IO(i8 signext 69, i32 0)
%19 = call i64 @ioctl(i32 %17, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i32, i32* %6
%25 = call i32 @close(i32 %24)
store i32 0, i32* %3
br label %26
26: ; preds = %23, %12
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"/dev/spid0\00"
@O_RDWR = global i32 0
@.str.2 = [37 x i8] c"Error while oppening /dev/spid0: %s\0A\00"
@errno = global i32 0
@str = [54 x i8] c"Trying to reset the usecount of the /dev/spid0 driver\00"
@str.4 = [92 x i8] c"Usecount is reset successful\0ANow it should be possible to remove the device from the kernel\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %19
11: ; preds = %2
%12 = tail call i32 @_IO(i8 signext 69, i32 0)
%13 = tail call i64 @ioctl(i32 %5, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([92 x i8], [92 x i8]* @str.4, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = tail call i32 @close(i32 %5)
br label %19
19: ; preds = %17, %7
%20 = phi i32 [ -1, %7 ], [ 0, %17 ]
ret i32 %20
}