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

/* Variables and functions */
int /*<<< orphan*/ NBD_CLEAR_QUE;
int /*<<< orphan*/ NBD_CLEAR_SOCK;
int /*<<< orphan*/ NBD_DISCONNECT;
int O_CLOEXEC;
int O_RDWR;
int /*<<< orphan*/ SIGUSR1;
scalar_t__ close(int);
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/);
scalar_t__ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int readpid(int /*<<< orphan*/ *, char *);
int nbd_disconnect(void) {
  pid_t client;
  int nbd;
  int ret;
  ret = readpid(&client, "/sys/block/nbd0/pid");
  if (ret < 0)
    return ret;
  if (kill(client, SIGUSR1) < 0)
    return -errno;
  nbd = open("/dev/nbd0", O_CLOEXEC | O_RDWR);
  if (nbd < 0)
    return -errno;
  if (ioctl(nbd, NBD_CLEAR_QUE) < 0) {
    ret = -errno;
    goto err;
  }
  if (ioctl(nbd, NBD_DISCONNECT) < 0) {
    ret = -errno;
    goto err;
  }
  if (ioctl(nbd, NBD_CLEAR_SOCK) < 0) {
    ret = -errno;
    goto err;
  }
  if (close(nbd) < 0)
    return -errno;
  return 0;
err:
  (void)close(nbd);
  return ret;
}


x86 assembly (clang, -O0):
<nbd_disconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rdi
lea    0xe9d(%rip),%rsi
call   1060 <readpid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1180 <nbd_disconnect+0x30>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <nbd_disconnect+0x165>
mov    -0x8(%rbp),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
cmp    $0x0,%rax
jge    11ae <nbd_disconnect+0x5e>
xor    %eax,%eax
mov    0x2e14(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <nbd_disconnect+0x165>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
lea    0xe4d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ec <nbd_disconnect+0x9c>
xor    %eax,%eax
mov    0x2dd6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <nbd_disconnect+0x165>
mov    -0xc(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    121a <nbd_disconnect+0xca>
xor    %eax,%eax
mov    0x2da8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x10(%rbp)
jmp    12a7 <nbd_disconnect+0x157>
mov    -0xc(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1248 <nbd_disconnect+0xf8>
xor    %eax,%eax
mov    0x2d7a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x10(%rbp)
jmp    12a7 <nbd_disconnect+0x157>
mov    -0xc(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1276 <nbd_disconnect+0x126>
xor    %eax,%eax
mov    0x2d4c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x10(%rbp)
jmp    12a7 <nbd_disconnect+0x157>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    129b <nbd_disconnect+0x14b>
xor    %eax,%eax
mov    0x2d27(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <nbd_disconnect+0x165>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <nbd_disconnect+0x165>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nbd_disconnect>:
push   %rbp
push   %rbx
push   %rax
lea    0xea6(%rip),%rsi
lea    0x4(%rsp),%rdi
call   1060 <readpid@plt>
test   %eax,%eax
js     1201 <nbd_disconnect+0xb1>
mov    0x4(%rsp),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
test   %rax,%rax
js     11f4 <nbd_disconnect+0xa4>
mov    0x2e1e(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11f4 <nbd_disconnect+0xa4>
mov    %eax,%ebx
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     1205 <nbd_disconnect+0xb5>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     1205 <nbd_disconnect+0xb5>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     1205 <nbd_disconnect+0xb5>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
test   %rax,%rax
jns    1217 <nbd_disconnect+0xc7>
mov    0x2dbd(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    1217 <nbd_disconnect+0xc7>
mov    %eax,%ebp
jmp    1217 <nbd_disconnect+0xc7>
mov    0x2dac(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"/sys/block/nbd0/pid\00"
@SIGUSR1 = global i32 0
@errno = global i32 0
@.str.1 = [10 x i8] c"/dev/nbd0\00"
@O_CLOEXEC = global i32 0
@O_RDWR = global i32 0
@NBD_CLEAR_QUE = global i32 0
@NBD_DISCONNECT = global i32 0
@NBD_CLEAR_SOCK = global i32 0
define i32 @nbd_disconnect() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 @readpid(i32* %2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = load i32, i32* %4
store i32 %9, i32* %1
br label %64
10: ; preds = %0
%11 = load i32, i32* %2
%12 = load i32, i32* @SIGUSR1
%13 = call i64 @kill(i32 %11, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
store i32 %17, i32* %1
br label %64
18: ; preds = %10
%19 = load i32, i32* @O_CLOEXEC
%20 = load i32, i32* @O_RDWR
%21 = or i32 %19, %20
%22 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %21)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
store i32 %27, i32* %1
br label %64
28: ; preds = %18
%29 = load i32, i32* %3
%30 = load i32, i32* @NBD_CLEAR_QUE
%31 = call i64 @ioctl(i32 %29, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
store i32 %35, i32* %4
br label %60
36: ; preds = %28
%37 = load i32, i32* %3
%38 = load i32, i32* @NBD_DISCONNECT
%39 = call i64 @ioctl(i32 %37, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* @errno
%43 = sub nsw i32 0, %42
store i32 %43, i32* %4
br label %60
44: ; preds = %36
%45 = load i32, i32* %3
%46 = load i32, i32* @NBD_CLEAR_SOCK
%47 = call i64 @ioctl(i32 %45, i32 %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i32, i32* @errno
%51 = sub nsw i32 0, %50
store i32 %51, i32* %4
br label %60
52: ; preds = %44
%53 = load i32, i32* %3
%54 = call i64 @close(i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = load i32, i32* @errno
%58 = sub nsw i32 0, %57
store i32 %58, i32* %1
br label %64
59: ; preds = %52
store i32 0, i32* %1
br label %64
60: ; preds = %49, %41, %33
%61 = load i32, i32* %3
%62 = call i64 @close(i32 %61)
%63 = load i32, i32* %4
store i32 %63, i32* %1
br label %64
64: ; preds = %60, %59, %56, %25, %15, %8
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
@.str = [20 x i8] c"/sys/block/nbd0/pid\00"
@SIGUSR1 = global i32 0
@errno = global i32 0
@.str.1 = [10 x i8] c"/dev/nbd0\00"
@O_CLOEXEC = global i32 0
@O_RDWR = global i32 0
@NBD_CLEAR_QUE = global i32 0
@NBD_DISCONNECT = global i32 0
@NBD_CLEAR_SOCK = global i32 0
define i32 @nbd_disconnect() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @readpid(i32* %1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%4 = icmp slt i32 %3, 0
br i1 %4, label %44, label %5
5: ; preds = %0
%6 = load i32, i32* %1
%7 = load i32, i32* @SIGUSR1
%8 = call i64 @kill(i32 %6, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %44
13: ; preds = %5
%14 = load i32, i32* @O_CLOEXEC
%15 = load i32, i32* @O_RDWR
%16 = or i32 %15, %14
%17 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
br label %44
22: ; preds = %13
%23 = load i32, i32* @NBD_CLEAR_QUE
%24 = call i64 @ioctl(i32 %17, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %40, label %26
26: ; preds = %22
%27 = load i32, i32* @NBD_DISCONNECT
%28 = call i64 @ioctl(i32 %17, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %40, label %30
30: ; preds = %26
%31 = load i32, i32* @NBD_CLEAR_SOCK
%32 = call i64 @ioctl(i32 %17, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %30
%35 = call i64 @close(i32 %17)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %34
%38 = load i32, i32* @errno
%39 = sub nsw i32 0, %38
br label %44
40: ; preds = %30, %26, %22
%41 = load i32, i32* @errno
%42 = sub nsw i32 0, %41
%43 = call i64 @close(i32 %17)
br label %44
44: ; preds = %34, %0, %40, %37, %19, %10
%45 = phi i32 [ %12, %10 ], [ %21, %19 ], [ %42, %40 ], [ %39, %37 ], [ %3, %0 ], [ 0, %34 ]
ret i32 %45
}