#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 fd;
  int events;
};

/* Variables and functions */
int /*<<< orphan*/ memset(struct pollfd *, int, int);
scalar_t__ realloc(struct pollfd *, int);
int add_poll(struct pollfd **pfd, int *n_fd, int fd, int events) {
  int out = -1;
  *pfd = (struct pollfd *)realloc(*pfd, (*n_fd + 1) * sizeof(struct pollfd));
  memset(&(*pfd)[*n_fd], 0x00, sizeof(struct pollfd));
  (*pfd)[*n_fd].fd = fd;
  out = *n_fd;
  (*pfd)[*n_fd].events = events;
  (*n_fd)++;
  return out;
}


x86 assembly (clang, -O0):
<add_poll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x3,%rax
add    %rax,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
movslq (%rcx),%rcx
mov    %edx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
movslq (%rcx),%rcx
mov    %edx,0x4(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_poll>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    (%rdi),%rdi
mov    (%rsi),%eax
lea    0x8(,%rax,8),%esi
call   1040 <realloc@plt>
mov    %rax,0x0(%rbp)
movslq (%rbx),%rcx
lea    (%rax,%rcx,8),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x0(%rbp),%rcx
movslq (%rbx),%rax
mov    %r15d,(%rcx,%rax,8)
movslq (%rbx),%rax
mov    %r14d,0x4(%rcx,%rax,8)
addl   $0x1,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
define i32 @add_poll(%struct.pollfd** %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca %struct.pollfd**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store %struct.pollfd** %0, %struct.pollfd*** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 -1, i32* %9
%10 = load %struct.pollfd**, %struct.pollfd*** %5
%11 = load %struct.pollfd*, %struct.pollfd** %10
%12 = load i32*, i32** %6
%13 = load i32, i32* %12
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 8
%17 = trunc i64 %16 to i32
%18 = call i64 @realloc(%struct.pollfd* %11, i32 %17)
%19 = inttoptr i64 %18 to %struct.pollfd*
%20 = load %struct.pollfd**, %struct.pollfd*** %5
store %struct.pollfd* %19, %struct.pollfd** %20
%21 = load %struct.pollfd**, %struct.pollfd*** %5
%22 = load %struct.pollfd*, %struct.pollfd** %21
%23 = load i32*, i32** %6
%24 = load i32, i32* %23
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %22, i64 %25
%27 = call i32 @memset(%struct.pollfd* %26, i32 0, i32 8)
%28 = load i32, i32* %7
%29 = load %struct.pollfd**, %struct.pollfd*** %5
%30 = load %struct.pollfd*, %struct.pollfd** %29
%31 = load i32*, i32** %6
%32 = load i32, i32* %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %30, i64 %33
%35 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %34, i32 0, i32 0
store i32 %28, i32* %35
%36 = load i32*, i32** %6
%37 = load i32, i32* %36
store i32 %37, i32* %9
%38 = load i32, i32* %8
%39 = load %struct.pollfd**, %struct.pollfd*** %5
%40 = load %struct.pollfd*, %struct.pollfd** %39
%41 = load i32*, i32** %6
%42 = load i32, i32* %41
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %40, i64 %43
%45 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %44, i32 0, i32 1
store i32 %38, i32* %45
%46 = load i32*, i32** %6
%47 = load i32, i32* %46
%48 = add nsw i32 %47, 1
store i32 %48, i32* %46
%49 = load i32, i32* %9
ret i32 %49
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
define i32 @add_poll(%struct.pollfd** nocapture %0, i32* nocapture %1, i32 %2, i32 %3) {
%5 = load %struct.pollfd*, %struct.pollfd** %0
%6 = load i32, i32* %1
%7 = shl i32 %6, 3
%8 = add i32 %7, 8
%9 = tail call i64 @realloc(%struct.pollfd* %5, i32 %8)
%10 = inttoptr i64 %9 to %struct.pollfd*
store %struct.pollfd* %10, %struct.pollfd** %0
%11 = load i32, i32* %1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i64 %12
%14 = tail call i32 @memset(%struct.pollfd* %13, i32 0, i32 8)
%15 = load %struct.pollfd*, %struct.pollfd** %0
%16 = load i32, i32* %1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i64 %17, i32 0
store i32 %2, i32* %18
%19 = load i32, i32* %1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i64 %20, i32 1
store i32 %3, i32* %21
%22 = load i32, i32* %1
%23 = add nsw i32 %22, 1
store i32 %23, i32* %1
ret i32 %19
}