#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 */
struct shl_pty {
  int dummy;
};
struct TYPE_2__ {
  struct shl_pty *ptr;
};
struct epoll_event {
  int events;
  TYPE_1__ data;
};
typedef int /*<<< orphan*/ ev;

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


x86 assembly (clang, -O0):
<shl_pty_bridge_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    116c <shl_pty_bridge_add+0x2c>
xor    %eax,%eax
mov    0x2e56(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <shl_pty_bridge_add+0xec>
mov    -0x10(%rbp),%rdi
call   1050 <shl_pty_is_open@plt>
cmp    $0x0,%eax
jne    1191 <shl_pty_bridge_add+0x51>
xor    %eax,%eax
mov    0x2e29(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <shl_pty_bridge_add+0xec>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    0x2e1f(%rip),%rcx
or     (%rcx),%eax
mov    0x2e2e(%rip),%rcx
or     (%rcx),%eax
mov    0x2e05(%rip),%rcx
or     (%rcx),%eax
mov    0x2ddc(%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    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <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>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1225 <shl_pty_bridge_add+0xe5>
xor    %eax,%eax
mov    0x2da5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <shl_pty_bridge_add+0xec>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shl_pty_bridge_add>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
js     11e2 <shl_pty_bridge_add+0xa2>
mov    %rsi,%rbp
mov    %edi,%r14d
mov    %rsi,%rdi
call   1050 <shl_pty_is_open@plt>
test   %eax,%eax
je     11eb <shl_pty_bridge_add+0xab>
mov    %rsp,%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2e1b(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e51(%rip),%rax
or     (%rax),%ecx
mov    0x2e28(%rip),%rax
or     (%rax),%ecx
mov    0x2dff(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    %rbp,0x8(%rsp)
mov    0x2e26(%rip),%rax
mov    (%rax),%r12d
mov    %rbp,%rdi
call   1060 <shl_pty_get_fd@plt>
mov    %r14d,%edi
mov    %r12d,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   1040 <epoll_ctl@plt>
test   %eax,%eax
jns    11f6 <shl_pty_bridge_add+0xb6>
mov    0x2de0(%rip),%rax
jmp    11f2 <shl_pty_bridge_add+0xb2>
mov    0x2dcf(%rip),%rax
jmp    11f2 <shl_pty_bridge_add+0xb2>
mov    0x2dbe(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%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
@ENODEV = 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
@errno = global i32 0
define i32 @shl_pty_bridge_add(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 %10, label %13
10: ; preds = %2
%11 = load i32, i32* @EINVAL
%12 = sub nsw i32 0, %11
store i32 %12, i32* %3
br label %46
13: ; preds = %2
%14 = load %struct.shl_pty*, %struct.shl_pty** %5
%15 = call i32 @shl_pty_is_open(%struct.shl_pty* %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = load i32, i32* @ENODEV
%19 = sub nsw i32 0, %18
store i32 %19, i32* %3
br label %46
20: ; preds = %13
%21 = call i32 @memset(%struct.epoll_event* %6, i32 0, i32 16)
%22 = load i32, i32* @EPOLLHUP
%23 = load i32, i32* @EPOLLERR
%24 = or i32 %22, %23
%25 = load i32, i32* @EPOLLIN
%26 = or i32 %24, %25
%27 = load i32, i32* @EPOLLOUT
%28 = or i32 %26, %27
%29 = load i32, i32* @EPOLLET
%30 = or i32 %28, %29
%31 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %6, i32 0, i32 0
store i32 %30, i32* %31
%32 = load %struct.shl_pty*, %struct.shl_pty** %5
%33 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %6, i32 0, i32 1
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
store %struct.shl_pty* %32, %struct.shl_pty** %34
%35 = load i32, i32* %4
%36 = load i32, i32* @EPOLL_CTL_ADD
%37 = load %struct.shl_pty*, %struct.shl_pty** %5
%38 = call i32 @shl_pty_get_fd(%struct.shl_pty* %37)
%39 = call i32 @epoll_ctl(i32 %35, i32 %36, i32 %38, %struct.epoll_event* %6)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %20
%43 = load i32, i32* @errno
%44 = sub nsw i32 0, %43
store i32 %44, i32* %3
br label %46
45: ; preds = %20
store i32 0, i32* %3
br label %46
46: ; preds = %45, %42, %17, %10
%47 = load i32, i32* %3
ret i32 %47
}

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
@ENODEV = 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
@errno = global i32 0
define i32 @shl_pty_bridge_add(i32 %0, %struct.shl_pty* %1) {
%3 = alloca %struct.epoll_event
%4 = bitcast %struct.epoll_event* %3 to i8*
%5 = icmp slt i32 %0, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @EINVAL
%8 = sub nsw i32 0, %7
br label %35
9: ; preds = %2
%10 = tail call i32 @shl_pty_is_open(%struct.shl_pty* %1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @ENODEV
%14 = sub nsw i32 0, %13
br label %35
15: ; preds = %9
%16 = call i32 @memset(%struct.epoll_event* %3, i32 0, i32 16)
%17 = load i32, i32* @EPOLLHUP
%18 = load i32, i32* @EPOLLERR
%19 = or i32 %18, %17
%20 = load i32, i32* @EPOLLIN
%21 = or i32 %19, %20
%22 = load i32, i32* @EPOLLOUT
%23 = or i32 %21, %22
%24 = load i32, i32* @EPOLLET
%25 = or i32 %23, %24
%26 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 1, i32 0
store %struct.shl_pty* %1, %struct.shl_pty** %27
%28 = load i32, i32* @EPOLL_CTL_ADD
%29 = call i32 @shl_pty_get_fd(%struct.shl_pty* %1)
%30 = call i32 @epoll_ctl(i32 %0, i32 %28, i32 %29, %struct.epoll_event* %3)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %15
%33 = load i32, i32* @errno
%34 = sub nsw i32 0, %33
br label %35
35: ; preds = %15, %32, %12, %6
%36 = phi i32 [ %8, %6 ], [ %34, %32 ], [ %14, %12 ], [ 0, %15 ]
ret i32 %36
}