#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int EEXIST;
int ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int errno;
int ioctl(int, int, int const);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, int const);
int stat(char *, struct stat *);
int main() {
  int err;
  int fd = open("/dev/abctl", O_RDONLY);
  assert(fd >= 0);
  int r = ioctl(fd, 0x4186, 0);
  err = errno;
  perror("add existing");
  assert(r < 0);
  assert(err == EEXIST);
  const int NEWDEV = 10;
  struct stat s;
  char devname[32];
  sprintf(devname, "/dev/abuse%d", NEWDEV);
  r = stat(devname, &s);
  err = errno;
  perror("missing device check");
  assert(r == -1);
  assert(err == ENOENT);
  r = ioctl(fd, 0x4186, NEWDEV);
  err = errno;
  perror("add missing");
  assert(r == NEWDEV);
  r = stat(devname, &s);
  err = errno;
  perror("added device check");
  assert(r == 0);
  r = ioctl(fd, 0x4187, NEWDEV);
  err = errno;
  perror("remove device");
  assert(r == NEWDEV);
  r = stat(devname, &s);
  err = errno;
  perror("removed device check");
  assert(r == -1);
  assert(err == ENOENT);
  r = ioctl(fd, 0x4187, NEWDEV);
  err = errno;
  perror("remove missing device");
  assert(r == -1);
  assert(err == ENOENT);
  close(fd);
  printf("All test PASS!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0xc(%rbp),%edi
mov    $0x4186,%esi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
lea    0xe2e(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0x0,-0x10(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%eax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movl   $0xa,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xdfa(%rip),%rsi
mov    $0xa,%edx
call   10a0 <sprintf@plt>
lea    -0x40(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
lea    0xdda(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0xffffffff,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%eax
mov    0x2d55(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0xc(%rbp),%edi
mov    $0x4186,%esi
mov    $0xa,%edx
call   1040 <ioctl@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
lea    0xd98(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0xa,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
lea    -0x40(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
lea    0xd6b(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0x0,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0xc(%rbp),%edi
mov    $0x4187,%esi
mov    $0xa,%edx
call   1040 <ioctl@plt>
mov    %eax,-0x10(%rbp)
mov    0x2cc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
lea    0xd40(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0xa,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
lea    -0x40(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x10(%rbp)
mov    0x2c8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
lea    0xd15(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0xffffffff,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%eax
mov    0x2c4e(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0xc(%rbp),%edi
mov    $0x4187,%esi
mov    $0xa,%edx
call   1040 <ioctl@plt>
mov    %eax,-0x10(%rbp)
mov    0x2c33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
lea    0xcd3(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0xffffffff,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%eax
mov    0x2bf7(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
lea    0xcab(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%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
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r12d
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
mov    %r12d,%edi
mov    $0x4186,%esi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    %eax,%ebp
mov    0x2e06(%rip),%rax
mov    (%rax),%ebx
mov    %rax,%r13
lea    0xe35(%rip),%rdi
call   1090 <perror@plt>
shr    $0x1f,%ebp
mov    %ebp,%edi
call   1060 <assert@plt>
mov    0x2dec(%rip),%rax
xor    %edi,%edi
cmp    (%rax),%ebx
sete   %dil
call   1060 <assert@plt>
lea    0xe18(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xa,%edx
call   10a0 <sprintf@plt>
lea    0x8(%rsp),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <stat@plt>
mov    %eax,%ebp
mov    0x0(%r13),%ebx
lea    0xdf6(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
cmp    $0xffffffff,%ebp
sete   %dil
call   1060 <assert@plt>
mov    0x2d77(%rip),%r13
xor    %edi,%edi
cmp    0x0(%r13),%ebx
sete   %dil
call   1060 <assert@plt>
mov    %r12d,%edi
mov    $0x4186,%esi
mov    $0xa,%edx
call   1040 <ioctl@plt>
mov    %eax,%ebp
lea    0xdc7(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
cmp    $0xa,%ebp
sete   %dil
call   1060 <assert@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <stat@plt>
mov    %eax,%ebp
lea    0xdac(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
test   %ebp,%ebp
sete   %dil
call   1060 <assert@plt>
mov    %r12d,%edi
mov    $0x4187,%esi
mov    $0xa,%edx
call   1040 <ioctl@plt>
mov    %eax,%ebp
lea    0xd92(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
cmp    $0xa,%ebp
sete   %dil
call   1060 <assert@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <stat@plt>
mov    %eax,%ebp
mov    0x2ce2(%rip),%r14
mov    (%r14),%ebx
lea    0xd6f(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
cmp    $0xffffffff,%ebp
sete   %dil
call   1060 <assert@plt>
xor    %edi,%edi
cmp    0x0(%r13),%ebx
sete   %dil
call   1060 <assert@plt>
mov    %r12d,%edi
mov    $0x4187,%esi
mov    $0xa,%edx
call   1040 <ioctl@plt>
mov    %eax,%ebp
mov    (%r14),%ebx
lea    0xd44(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
cmp    $0xffffffff,%ebp
sete   %dil
call   1060 <assert@plt>
xor    %edi,%edi
cmp    0x0(%r13),%ebx
sete   %dil
call   1060 <assert@plt>
mov    %r12d,%edi
call   1050 <close@plt>
lea    0xd29(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [11 x i8] c"/dev/abctl\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [13 x i8] c"add existing\00"
@EEXIST = global i32 0
@.str.2 = [13 x i8] c"/dev/abuse%d\00"
@.str.3 = [21 x i8] c"missing device check\00"
@ENOENT = global i32 0
@.str.4 = [12 x i8] c"add missing\00"
@.str.5 = [19 x i8] c"added device check\00"
@.str.6 = [14 x i8] c"remove device\00"
@.str.7 = [21 x i8] c"removed device check\00"
@.str.8 = [22 x i8] c"remove missing device\00"
@.str.9 = [16 x i8] c"All test PASS!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca [32 x i8]
store i32 0, i32* %1
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp sge i32 %10, 0
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32, i32* %3
%15 = call i32 @ioctl(i32 %14, i32 16774, i32 0)
store i32 %15, i32* %4
%16 = load i32, i32* @errno
store i32 %16, i32* %2
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* %2
%23 = load i32, i32* @EEXIST
%24 = icmp eq i32 %22, %23
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
store i32 10, i32* %5
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%28 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 10)
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%30 = call i32 @stat(i8* %29, %struct.stat* %6)
store i32 %30, i32* %4
%31 = load i32, i32* @errno
store i32 %31, i32* %2
%32 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, -1
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = load i32, i32* %2
%38 = load i32, i32* @ENOENT
%39 = icmp eq i32 %37, %38
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
%42 = load i32, i32* %3
%43 = call i32 @ioctl(i32 %42, i32 16774, i32 10)
store i32 %43, i32* %4
%44 = load i32, i32* @errno
store i32 %44, i32* %2
%45 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%46 = load i32, i32* %4
%47 = icmp eq i32 %46, 10
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
%50 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%51 = call i32 @stat(i8* %50, %struct.stat* %6)
store i32 %51, i32* %4
%52 = load i32, i32* @errno
store i32 %52, i32* %2
%53 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%54 = load i32, i32* %4
%55 = icmp eq i32 %54, 0
%56 = zext i1 %55 to i32
%57 = call i32 @assert(i32 %56)
%58 = load i32, i32* %3
%59 = call i32 @ioctl(i32 %58, i32 16775, i32 10)
store i32 %59, i32* %4
%60 = load i32, i32* @errno
store i32 %60, i32* %2
%61 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%62 = load i32, i32* %4
%63 = icmp eq i32 %62, 10
%64 = zext i1 %63 to i32
%65 = call i32 @assert(i32 %64)
%66 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%67 = call i32 @stat(i8* %66, %struct.stat* %6)
store i32 %67, i32* %4
%68 = load i32, i32* @errno
store i32 %68, i32* %2
%69 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%70 = load i32, i32* %4
%71 = icmp eq i32 %70, -1
%72 = zext i1 %71 to i32
%73 = call i32 @assert(i32 %72)
%74 = load i32, i32* %2
%75 = load i32, i32* @ENOENT
%76 = icmp eq i32 %74, %75
%77 = zext i1 %76 to i32
%78 = call i32 @assert(i32 %77)
%79 = load i32, i32* %3
%80 = call i32 @ioctl(i32 %79, i32 16775, i32 10)
store i32 %80, i32* %4
%81 = load i32, i32* @errno
store i32 %81, i32* %2
%82 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0))
%83 = load i32, i32* %4
%84 = icmp eq i32 %83, -1
%85 = zext i1 %84 to i32
%86 = call i32 @assert(i32 %85)
%87 = load i32, i32* %2
%88 = load i32, i32* @ENOENT
%89 = icmp eq i32 %87, %88
%90 = zext i1 %89 to i32
%91 = call i32 @assert(i32 %90)
%92 = load i32, i32* %3
%93 = call i32 @close(i32 %92)
%94 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [11 x i8] c"/dev/abctl\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [13 x i8] c"add existing\00"
@EEXIST = global i32 0
@.str.2 = [13 x i8] c"/dev/abuse%d\00"
@.str.3 = [21 x i8] c"missing device check\00"
@ENOENT = global i32 0
@.str.4 = [12 x i8] c"add missing\00"
@.str.5 = [19 x i8] c"added device check\00"
@.str.6 = [14 x i8] c"remove device\00"
@.str.7 = [21 x i8] c"removed device check\00"
@.str.8 = [22 x i8] c"remove missing device\00"
@str = [15 x i8] c"All test PASS!\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = alloca [32 x i8]
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = xor i32 %4, -1
%6 = lshr i32 %5, 31
%7 = tail call i32 @assert(i32 %6)
%8 = tail call i32 @ioctl(i32 %4, i32 16774, i32 0)
%9 = load i32, i32* @errno
%10 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%11 = lshr i32 %8, 31
%12 = tail call i32 @assert(i32 %11)
%13 = load i32, i32* @EEXIST
%14 = icmp eq i32 %9, %13
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = bitcast %struct.stat* %1 to i8*
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%19 = call i32 @sprintf(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 10)
%20 = call i32 @stat(i8* %18, %struct.stat* %1)
%21 = load i32, i32* @errno
%22 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i32 %20, -1
%24 = zext i1 %23 to i32
%25 = tail call i32 @assert(i32 %24)
%26 = load i32, i32* @ENOENT
%27 = icmp eq i32 %21, %26
%28 = zext i1 %27 to i32
%29 = tail call i32 @assert(i32 %28)
%30 = tail call i32 @ioctl(i32 %4, i32 16774, i32 10)
%31 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp eq i32 %30, 10
%33 = zext i1 %32 to i32
%34 = tail call i32 @assert(i32 %33)
%35 = call i32 @stat(i8* %18, %struct.stat* %1)
%36 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%37 = icmp eq i32 %35, 0
%38 = zext i1 %37 to i32
%39 = tail call i32 @assert(i32 %38)
%40 = tail call i32 @ioctl(i32 %4, i32 16775, i32 10)
%41 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%42 = icmp eq i32 %40, 10
%43 = zext i1 %42 to i32
%44 = tail call i32 @assert(i32 %43)
%45 = call i32 @stat(i8* %18, %struct.stat* %1)
%46 = load i32, i32* @errno
%47 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%48 = icmp eq i32 %45, -1
%49 = zext i1 %48 to i32
%50 = tail call i32 @assert(i32 %49)
%51 = load i32, i32* @ENOENT
%52 = icmp eq i32 %46, %51
%53 = zext i1 %52 to i32
%54 = tail call i32 @assert(i32 %53)
%55 = tail call i32 @ioctl(i32 %4, i32 16775, i32 10)
%56 = load i32, i32* @errno
%57 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0))
%58 = icmp eq i32 %55, -1
%59 = zext i1 %58 to i32
%60 = tail call i32 @assert(i32 %59)
%61 = load i32, i32* @ENOENT
%62 = icmp eq i32 %56, %61
%63 = zext i1 %62 to i32
%64 = tail call i32 @assert(i32 %63)
%65 = tail call i32 @close(i32 %4)
%66 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
ret i32 0
}