#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 soc_t {
  int dummy;
};
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ POLLPRI;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ errno;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int poll(struct pollfd *, int, int);
int read(int, unsigned int *, int);
int soc_wait_for_interrupt(struct soc_t *soc, int fd, int ms) {
  unsigned int c = 0;
  struct pollfd polls;
  int x = 0;
  polls.fd = fd;
  polls.events = POLLPRI;
  x = read(fd, &c, 1);
  if (x != 1) {
    return -1;
  }
  lseek(fd, 0, SEEK_SET);
  x = poll(&polls, 1, ms);
  if (x == -1 && errno == EINTR) {
    x = 0;
  }
  return x;
}


x86 assembly (clang, -O0):
<soc_wait_for_interrupt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
je     118c <soc_wait_for_interrupt+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <soc_wait_for_interrupt+0xb4>
mov    -0x14(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x18(%rbp),%edx
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1050 <poll@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11de <soc_wait_for_interrupt+0xae>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%rax
jne    11de <soc_wait_for_interrupt+0xae>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<soc_wait_for_interrupt>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %esi,%ebp
movl   $0x0,0xc(%rsp)
mov    %esi,0x10(%rsp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x1,%ecx
jne    11b3 <soc_wait_for_interrupt+0x83>
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1050 <poll@plt>
cmp    $0xffffffff,%eax
jne    11b3 <soc_wait_for_interrupt+0x83>
mov    0x2e39(%rip),%rax
mov    (%rax),%rcx
mov    0x2e1f(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.soc_t = type { i32 }
%struct.pollfd = type { i32, i32 }
@POLLPRI = global i32 0
@SEEK_SET = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @soc_wait_for_interrupt(%struct.soc_t* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.soc_t*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.pollfd
%10 = alloca i32
store %struct.soc_t* %0, %struct.soc_t** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %10
%11 = load i32, i32* %6
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @POLLPRI
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* %6
%16 = call i32 @read(i32 %15, i32* %8, i32 1)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp ne i32 %17, 1
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 -1, i32* %4
br label %35
20: ; preds = %3
%21 = load i32, i32* %6
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @lseek(i32 %21, i32 0, i32 %22)
%24 = load i32, i32* %7
%25 = call i32 @poll(%struct.pollfd* %9, i32 1, i32 %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %20
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 0, i32* %10
br label %33
33: ; preds = %32, %28, %20
%34 = load i32, i32* %10
store i32 %34, i32* %4
br label %35
35: ; preds = %33, %19
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
%struct.soc_t = type { i32 }
%struct.pollfd = type { i32, i32 }
@POLLPRI = global i32 0
@SEEK_SET = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @soc_wait_for_interrupt(%struct.soc_t* nocapture readnone %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.pollfd
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = bitcast %struct.pollfd* %5 to i8*
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %1, i32* %8
%9 = load i32, i32* @POLLPRI
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i32 %9, i32* %10
%11 = call i32 @read(i32 %1, i32* %4, i32 1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %23
13: ; preds = %3
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @lseek(i32 %1, i32 0, i32 %14)
%16 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 %2)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp ne i64 %19, %20
%22 = sext i1 %21 to i32
br label %23
23: ; preds = %18, %13, %3
%24 = phi i32 [ -1, %3 ], [ %16, %13 ], [ %22, %18 ]
ret i32 %24
}