#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 pollfd {
  int revents;
  int events;
  int /*<<< orphan*/ fd;
};
typedef int /*<<< orphan*/ SSL;

/* Variables and functions */
int POLLIN;
int POLLOUT;
#define SSL_ERROR_WANT_READ 129
#define SSL_ERROR_WANT_WRITE 128
int SSL_get_error(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ SSL_get_fd(int /*<<< orphan*/ *);
int poll(struct pollfd *, int, int);
int waitforit(SSL *ssl) {
  struct pollfd pfd;
  int ret;
  pfd.fd = SSL_get_fd(ssl);
  pfd.revents = 0;
  switch (SSL_get_error(ssl, -1)) {
  case SSL_ERROR_WANT_READ:
    pfd.events = POLLIN;
    break;
  case SSL_ERROR_WANT_WRITE:
    pfd.events = POLLOUT;
    break;
  default:
    return 0;
  }
  ret = poll(&pfd, 1, -1);
  if (ret != 1 || !(pfd.revents & pfd.events))
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<waitforit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <SSL_get_fd@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xffffffff,%esi
call   1030 <SSL_get_error@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0x80,%eax
je     1196 <waitforit+0x66>
jmp    1172 <waitforit+0x42>
mov    -0x28(%rbp),%eax
sub    $0x81,%eax
jne    11a7 <waitforit+0x77>
jmp    1185 <waitforit+0x55>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11b3 <waitforit+0x83>
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11b3 <waitforit+0x83>
movl   $0x0,-0x4(%rbp)
jmp    11f5 <waitforit+0xc5>
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1050 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    11e2 <waitforit+0xb2>
mov    -0x20(%rbp),%eax
and    -0x1c(%rbp),%eax
cmp    $0x0,%eax
jne    11ee <waitforit+0xbe>
movl   $0x0,-0x4(%rbp)
jmp    11f5 <waitforit+0xc5>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitforit>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1040 <SSL_get_fd@plt>
mov    %eax,0x8(%rsp)
movl   $0x0,(%rsp)
mov    %rbx,%rdi
mov    $0xffffffff,%esi
call   1030 <SSL_get_error@plt>
xor    %ebx,%ebx
cmp    $0x81,%eax
je     116e <waitforit+0x3e>
cmp    $0x80,%eax
jne    119e <waitforit+0x6e>
mov    0x2e8c(%rip),%rax
jmp    1175 <waitforit+0x45>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1050 <poll@plt>
cmp    $0x1,%eax
jne    119e <waitforit+0x6e>
mov    0x4(%rsp),%eax
xor    %ebx,%ebx
test   %eax,(%rsp)
setne  %bl
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLOUT = global i32 0
define i32 @waitforit(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.pollfd
%5 = alloca i32
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = call i32 @SSL_get_fd(i32* %6)
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 2
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 0
store i32 0, i32* %9
%10 = load i32*, i32** %3
%11 = call i32 @SSL_get_error(i32* %10, i32 -1)
switch i32 %11, label %18 [
i32 129, label %12
i32 128, label %15
]
12: ; preds = %1
%13 = load i32, i32* @POLLIN
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
store i32 %13, i32* %14
br label %19
15: ; preds = %1
%16 = load i32, i32* @POLLOUT
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
store i32 %16, i32* %17
br label %19
18: ; preds = %1
store i32 0, i32* %2
br label %32
19: ; preds = %15, %12
%20 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 -1)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 1
br i1 %22, label %30, label %23
23: ; preds = %19
%24 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
%27 = load i32, i32* %26
%28 = and i32 %25, %27
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %23, %19
store i32 0, i32* %2
br label %32
31: ; preds = %23
store i32 1, i32* %2
br label %32
32: ; preds = %31, %30, %18
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLOUT = global i32 0
define i32 @waitforit(i32* %0) {
%2 = alloca %struct.pollfd
%3 = bitcast %struct.pollfd* %2 to i8*
%4 = tail call i32 @SSL_get_fd(i32* %0)
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 2
store i32 %4, i32* %5
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 0, i32* %6
%7 = tail call i32 @SSL_get_error(i32* %0, i32 -1)
switch i32 %7, label %22 [
i32 129, label %9
i32 128, label %8
]
8: ; preds = %1
br label %9
9: ; preds = %1, %8
%10 = phi i32* [ @POLLOUT, %8 ], [ @POLLIN, %1 ]
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @poll(%struct.pollfd* %2, i32 1, i32 -1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %22
15: ; preds = %9
%16 = load i32, i32* %6
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
%18 = load i32, i32* %17
%19 = and i32 %18, %16
%20 = icmp ne i32 %19, 0
%21 = zext i1 %20 to i32
br label %22
22: ; preds = %15, %9, %1
%23 = phi i32 [ 0, %1 ], [ 0, %9 ], [ %21, %15 ]
ret i32 %23
}