#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 uint8_t;
struct pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int POLLERR;
int POLLPRI;
scalar_t__ is_target_mounted(char const *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ poll(struct pollfd *, int, int);
void wait_for_target(const char *path) {
  struct pollfd pfd;
  int mfd = open("/proc/mounts", O_RDONLY, 0);
  pfd.fd = mfd;
  pfd.events = POLLERR | POLLPRI;
  pfd.revents = 0;
  uint8_t tries = 0;
  while (poll(&pfd, 1, -1) >= 0) {
    if (pfd.revents & POLLERR) {
      tries++;
      if (is_target_mounted(path)) {
        break;
      }
    }
    pfd.revents = 0;
    if (tries > 5) {
      break;
    }
  }
}


x86 assembly (clang, -O0):
<wait_for_target>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
lea    0xeb4(%rip),%rdi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    0x2e84(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1040 <poll@plt>
cmp    $0x0,%rax
jl     11f2 <wait_for_target+0xc2>
mov    -0x10(%rbp),%eax
mov    0x2e3a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11d7 <wait_for_target+0xa7>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <is_target_mounted@plt>
cmp    $0x0,%rax
je     11d2 <wait_for_target+0xa2>
jmp    11f2 <wait_for_target+0xc2>
jmp    11d7 <wait_for_target+0xa7>
movl   $0x0,-0x10(%rbp)
cmpl   $0x5,-0x20(%rbp)
jle    11ed <wait_for_target+0xbd>
jmp    11f2 <wait_for_target+0xc2>
jmp    117f <wait_for_target+0x4f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_target>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    0xeb3(%rip),%rdi
xor    %ebp,%ebp
xor    %edx,%edx
call   1050 <open@plt>
mov    0x2e83(%rip),%r15
mov    0x2e8c(%rip),%rcx
mov    (%rcx),%ecx
or     (%r15),%ecx
mov    %eax,0x8(%rsp)
mov    %ecx,0xc(%rsp)
movl   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rbx
jmp    118d <wait_for_target+0x5d>
movl   $0x0,0x10(%rsp)
cmp    $0x6,%ebp
jge    11bf <wait_for_target+0x8f>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1040 <poll@plt>
test   %rax,%rax
js     11bf <wait_for_target+0x8f>
mov    (%r15),%eax
test   %eax,0x10(%rsp)
je     1180 <wait_for_target+0x50>
mov    %r14,%rdi
call   1030 <is_target_mounted@plt>
test   %rax,%rax
jne    11bf <wait_for_target+0x8f>
add    $0x1,%ebp
jmp    1180 <wait_for_target+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [13 x i8] c"/proc/mounts\00"
@O_RDONLY = global i32 0
@POLLERR = global i32 0
@POLLPRI = global i32 0
define void @wait_for_target(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.pollfd
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6, i32 0)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* @POLLERR
%11 = load i32, i32* @POLLPRI
%12 = or i32 %10, %11
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 2
store i32 0, i32* %14
store i32 0, i32* %5
br label %15
15: ; preds = %37, %1
%16 = call i64 @poll(%struct.pollfd* %3, i32 1, i32 -1)
%17 = icmp sge i64 %16, 0
br i1 %17, label %18, label %38
18: ; preds = %15
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 2
%20 = load i32, i32* %19
%21 = load i32, i32* @POLLERR
%22 = and i32 %20, %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %18
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
%27 = load i8*, i8** %2
%28 = call i64 @is_target_mounted(i8* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
br label %38
31: ; preds = %24
br label %32
32: ; preds = %31, %18
%33 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 2
store i32 0, i32* %33
%34 = load i32, i32* %5
%35 = icmp sgt i32 %34, 5
br i1 %35, label %36, label %37
36: ; preds = %32
br label %38
37: ; preds = %32
br label %15
38: ; preds = %36, %30, %15
ret void
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str = [13 x i8] c"/proc/mounts\00"
@O_RDONLY = global i32 0
@POLLERR = global i32 0
@POLLPRI = global i32 0
define void @wait_for_target(i8* %0) {
%2 = alloca %struct.pollfd
%3 = bitcast %struct.pollfd* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4, i32 0)
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 %5, i32* %6
%7 = load i32, i32* @POLLERR
%8 = load i32, i32* @POLLPRI
%9 = or i32 %8, %7
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 2
store i32 0, i32* %11
br label %12
12: ; preds = %25, %1
%13 = phi i32 [ 0, %1 ], [ %26, %25 ]
%14 = call i64 @poll(%struct.pollfd* %2, i32 1, i32 -1)
%15 = icmp sgt i64 %14, -1
br i1 %15, label %16, label %28
16: ; preds = %12
%17 = load i32, i32* %11
%18 = load i32, i32* @POLLERR
%19 = and i32 %18, %17
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = add nsw i32 %13, 1
%23 = call i64 @is_target_mounted(i8* %0)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21, %16
%26 = phi i32 [ %22, %21 ], [ %13, %16 ]
store i32 0, i32* %11
%27 = icmp sgt i32 %26, 5
br i1 %27, label %28, label %12
28: ; preds = %25, %21, %12
ret void
}