#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ detach_and_sleep(int);
int found_new_device(char const *);
int poll_device_plug(char const *, int *);
int /*<<< orphan*/ poll_device_unplug(char const *, int);
int /*<<< orphan*/ remove_device(char const *);
int /*<<< orphan*/ unmount_device(char const *);
int poll_removable_device(const char *sysfs_path, const char *dev_path) {
  int rc, mounted, optical = -1;
  for (;;) {
    rc = poll_device_plug(dev_path, &optical);
    if (rc == EXIT_FAILURE)
      return rc;
    rc = found_new_device(dev_path);
    mounted = (rc == EXIT_SUCCESS);
    poll_device_unplug(dev_path, optical);
    remove_device(dev_path);
    while (mounted && !unmount_device(dev_path))
      ;
    detach_and_sleep(1);
  }
}


x86 assembly (clang, -O0):
<poll_removable_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <poll_device_plug@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%eax
jne    11a2 <poll_removable_device+0x42>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rdi
call   1060 <found_new_device@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1080 <poll_device_unplug@plt>
mov    -0x10(%rbp),%rdi
call   1050 <remove_device@plt>
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x1d(%rbp)
je     11fd <poll_removable_device+0x9d>
mov    -0x10(%rbp),%rdi
call   1040 <unmount_device@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    120d <poll_removable_device+0xad>
jmp    1212 <poll_removable_device+0xb2>
jmp    11da <poll_removable_device+0x7a>
mov    $0x1,%edi
call   1070 <detach_and_sleep@plt>
jmp    1177 <poll_removable_device+0x17>

x86 assembly (clang, -O2):
<poll_removable_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
movl   $0xffffffff,0x4(%rsp)
ff
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <poll_device_plug@plt>
mov    0x2e56(%rip),%r15
cmp    (%r15),%eax
jne    119e <poll_removable_device+0x3e>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e33(%rip),%r12
lea    0x4(%rsp),%r14
jmp    11ca <poll_removable_device+0x6a>
nopl   0x0(%rax)
mov    $0x1,%edi
call   1070 <detach_and_sleep@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <poll_device_plug@plt>
cmp    (%r15),%eax
je     118f <poll_removable_device+0x2f>
mov    %rbx,%rdi
call   1060 <found_new_device@plt>
mov    %eax,%r13d
mov    (%r12),%ebp
mov    0x4(%rsp),%esi
mov    %rbx,%rdi
call   1080 <poll_device_unplug@plt>
mov    %rbx,%rdi
call   1050 <remove_device@plt>
cmp    %ebp,%r13d
jne    11b0 <poll_removable_device+0x50>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1040 <unmount_device@plt>
test   %eax,%eax
je     1200 <poll_removable_device+0xa0>
jmp    11b0 <poll_removable_device+0x50>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @poll_removable_device(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 -1, i32* %7
br label %8
8: ; preds = %39, %2
%9 = load i8*, i8** %4
%10 = call i32 @poll_device_plug(i8* %9, i32* %7)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = load i32, i32* @EXIT_FAILURE
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = load i32, i32* %5
ret i32 %15
16: ; preds = %8
%17 = load i8*, i8** %4
%18 = call i32 @found_new_device(i8* %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = load i32, i32* @EXIT_SUCCESS
%21 = icmp eq i32 %19, %20
%22 = zext i1 %21 to i32
store i32 %22, i32* %6
%23 = load i8*, i8** %4
%24 = load i32, i32* %7
%25 = call i32 @poll_device_unplug(i8* %23, i32 %24)
%26 = load i8*, i8** %4
%27 = call i32 @remove_device(i8* %26)
br label %28
28: ; preds = %38, %16
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i8*, i8** %4
%33 = call i32 @unmount_device(i8* %32)
%34 = icmp ne i32 %33, 0
%35 = xor i1 %34, true
br label %36
36: ; preds = %31, %28
%37 = phi i1 [ false, %28 ], [ %35, %31 ]
br i1 %37, label %38, label %39
38: ; preds = %36
br label %28
39: ; preds = %36
%40 = call i32 @detach_and_sleep(i32 1)
br label %8
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @poll_removable_device(i8* nocapture readnone %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 -1, i32* %3
%5 = call i32 @poll_device_plug(i8* %1, i32* %3)
%6 = load i32, i32* @EXIT_FAILURE
%7 = icmp eq i32 %5, %6
br i1 %7, label %8, label %10
8: ; preds = %20, %2
%9 = phi i32 [ %5, %2 ], [ %22, %20 ]
ret i32 %9
10: ; preds = %2, %20
%11 = call i32 @found_new_device(i8* %1)
%12 = load i32, i32* @EXIT_SUCCESS
%13 = icmp eq i32 %11, %12
%14 = load i32, i32* %3
%15 = call i32 @poll_device_unplug(i8* %1, i32 %14)
%16 = call i32 @remove_device(i8* %1)
br i1 %13, label %17, label %20
17: ; preds = %10, %17
%18 = call i32 @unmount_device(i8* %1)
%19 = icmp eq i32 %18, 0
br i1 %19, label %17, label %20
20: ; preds = %17, %10
%21 = call i32 @detach_and_sleep(i32 1)
%22 = call i32 @poll_device_plug(i8* %1, i32* %3)
%23 = load i32, i32* @EXIT_FAILURE
%24 = icmp eq i32 %22, %23
br i1 %24, label %8, label %10
}