#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ up;
struct shl_pty {
  int dummy;
};
struct TYPE_2__ {
  struct shl_pty *ptr;
};
struct epoll_event {
  int events;
  TYPE_1__ data;
};

/* Variables and functions */
int EAGAIN;
int EINVAL;
int EPOLLERR;
int EPOLLET;
int EPOLLHUP;
int EPOLLIN;
int EPOLLOUT;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int /*<<< orphan*/ epoll_ctl(int, int /*<<< orphan*/, int /*<<< orphan*/,
                             struct epoll_event *);
int /*<<< orphan*/ memset(struct epoll_event *, int /*<<< orphan*/, int);
int shl_pty_dispatch(struct shl_pty *);
int /*<<< orphan*/ shl_pty_get_fd(struct shl_pty *);
int shl_pty_bridge_dispatch_pty(int bridge, struct shl_pty *pty) {
  struct epoll_event up;
  int r;
  if (bridge < 0 || !pty)
    return -EINVAL;
  r = shl_pty_dispatch(pty);
  if (r == -EAGAIN) {
    memset(&up, 0, sizeof(up));
    up.events = EPOLLHUP | EPOLLERR | EPOLLIN | EPOLLOUT | EPOLLET;
    up.data.ptr = pty;
    epoll_ctl(bridge, EPOLL_CTL_ADD, shl_pty_get_fd(pty), &up);
  }
  return 0;
}


x86 assembly (clang, -O0):
<shl_pty_bridge_dispatch_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1164 <shl_pty_bridge_dispatch_pty+0x24>
cmpq   $0x0,-0x10(%rbp)
jne    1177 <shl_pty_bridge_dispatch_pty+0x37>
xor    %eax,%eax
mov    0x2e53(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <shl_pty_bridge_dispatch_pty+0xd4>
mov    -0x10(%rbp),%rdi
call   1060 <shl_pty_dispatch@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e11(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    120d <shl_pty_bridge_dispatch_pty+0xcd>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    0x2e17(%rip),%rcx
or     (%rcx),%eax
mov    0x2e26(%rip),%rcx
or     (%rcx),%eax
mov    0x2dfd(%rip),%rcx
or     (%rcx),%eax
mov    0x2de4(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <shl_pty_get_fd@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    %eax,%edx
lea    -0x20(%rbp),%rcx
call   1040 <epoll_ctl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shl_pty_bridge_dispatch_pty>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
js     11e5 <shl_pty_bridge_dispatch_pty+0xa5>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11e5 <shl_pty_bridge_dispatch_pty+0xa5>
mov    %edi,%r14d
mov    %rbx,%rdi
call   1060 <shl_pty_dispatch@plt>
mov    0x2e2e(%rip),%rcx
xor    %ebp,%ebp
add    (%rcx),%eax
jne    11f0 <shl_pty_bridge_dispatch_pty+0xb0>
mov    %rsp,%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e43(%rip),%rax
or     (%rax),%ecx
mov    0x2e1a(%rip),%rax
or     (%rax),%ecx
mov    0x2e01(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    %rbx,0x8(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%r12d
mov    %rbx,%rdi
call   1050 <shl_pty_get_fd@plt>
mov    %r14d,%edi
mov    %r12d,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   1040 <epoll_ctl@plt>
jmp    11f0 <shl_pty_bridge_dispatch_pty+0xb0>
mov    0x2dd4(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.shl_pty = type { i32 }
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { %struct.shl_pty* }
@EINVAL = global i32 0
@EAGAIN = global i32 0
@EPOLLHUP = global i32 0
@EPOLLERR = global i32 0
@EPOLLIN = global i32 0
@EPOLLOUT = global i32 0
@EPOLLET = global i32 0
@EPOLL_CTL_ADD = global i32 0
define i32 @shl_pty_bridge_dispatch_pty(i32 %0, %struct.shl_pty* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.shl_pty*
%6 = alloca %struct.epoll_event
%7 = alloca i32
store i32 %0, i32* %4
store %struct.shl_pty* %1, %struct.shl_pty** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load %struct.shl_pty*, %struct.shl_pty** %5
%12 = icmp ne %struct.shl_pty* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10, %2
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
store i32 %15, i32* %3
br label %44
16: ; preds = %10
%17 = load %struct.shl_pty*, %struct.shl_pty** %5
%18 = call i32 @shl_pty_dispatch(%struct.shl_pty* %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = load i32, i32* @EAGAIN
%21 = sub nsw i32 0, %20
%22 = icmp eq i32 %19, %21
br i1 %22, label %23, label %43
23: ; preds = %16
%24 = call i32 @memset(%struct.epoll_event* %6, i32 0, i32 16)
%25 = load i32, i32* @EPOLLHUP
%26 = load i32, i32* @EPOLLERR
%27 = or i32 %25, %26
%28 = load i32, i32* @EPOLLIN
%29 = or i32 %27, %28
%30 = load i32, i32* @EPOLLOUT
%31 = or i32 %29, %30
%32 = load i32, i32* @EPOLLET
%33 = or i32 %31, %32
%34 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %6, i32 0, i32 0
store i32 %33, i32* %34
%35 = load %struct.shl_pty*, %struct.shl_pty** %5
%36 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %6, i32 0, i32 1
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 0
store %struct.shl_pty* %35, %struct.shl_pty** %37
%38 = load i32, i32* %4
%39 = load i32, i32* @EPOLL_CTL_ADD
%40 = load %struct.shl_pty*, %struct.shl_pty** %5
%41 = call i32 @shl_pty_get_fd(%struct.shl_pty* %40)
%42 = call i32 @epoll_ctl(i32 %38, i32 %39, i32 %41, %struct.epoll_event* %6)
br label %43
43: ; preds = %23, %16
store i32 0, i32* %3
br label %44
44: ; preds = %43, %13
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.shl_pty = type { i32 }
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { %struct.shl_pty* }
@EINVAL = global i32 0
@EAGAIN = global i32 0
@EPOLLHUP = global i32 0
@EPOLLERR = global i32 0
@EPOLLIN = global i32 0
@EPOLLOUT = global i32 0
@EPOLLET = global i32 0
@EPOLL_CTL_ADD = global i32 0
define i32 @shl_pty_bridge_dispatch_pty(i32 %0, %struct.shl_pty* %1) {
%3 = alloca %struct.epoll_event
%4 = bitcast %struct.epoll_event* %3 to i8*
%5 = icmp sgt i32 %0, -1
%6 = icmp ne %struct.shl_pty* %1, null
%7 = and i1 %5, %6
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @EINVAL
%10 = sub nsw i32 0, %9
br label %32
11: ; preds = %2
%12 = tail call i32 @shl_pty_dispatch(%struct.shl_pty* %1)
%13 = load i32, i32* @EAGAIN
%14 = sub nsw i32 0, %13
%15 = icmp eq i32 %12, %14
br i1 %15, label %16, label %32
16: ; preds = %11
%17 = call i32 @memset(%struct.epoll_event* %3, i32 0, i32 16)
%18 = load i32, i32* @EPOLLHUP
%19 = load i32, i32* @EPOLLERR
%20 = or i32 %19, %18
%21 = load i32, i32* @EPOLLIN
%22 = or i32 %20, %21
%23 = load i32, i32* @EPOLLOUT
%24 = or i32 %22, %23
%25 = load i32, i32* @EPOLLET
%26 = or i32 %24, %25
%27 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 1, i32 0
store %struct.shl_pty* %1, %struct.shl_pty** %28
%29 = load i32, i32* @EPOLL_CTL_ADD
%30 = call i32 @shl_pty_get_fd(%struct.shl_pty* %1)
%31 = call i32 @epoll_ctl(i32 %0, i32 %29, i32 %30, %struct.epoll_event* %3)
br label %32
32: ; preds = %11, %16, %8
%33 = phi i32 [ %10, %8 ], [ 0, %16 ], [ 0, %11 ]
ret i32 %33
}