#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 */
typedef int uint64_t;
typedef int ssize_t;

/* Variables and functions */
scalar_t__ ancil_send_fd(int, int);
int /*<<< orphan*/ assert(int);
int eventfd(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int write(int, int *, int);
int connection_handler(int connection_fd) {
  int i;
  int efd;
  efd = eventfd(0, 0);
  assert(efd != -1);
  if (ancil_send_fd(connection_fd, efd)) {
    perror("ancil_send_fd");
    exit(1);
  } else {
    printf("Sent evfd %d\n", efd);
  }
  uint64_t u = getpid();
  ssize_t s;
  for (i = 0; i < 10; i++) {
    s = write(efd, &u, sizeof(uint64_t));
    if (s != sizeof(uint64_t))
      perror("write");
    else
      printf("Sent %llu\n", u);
    u++;
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<connection_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
xor    %esi,%esi
mov    %esi,%edi
call   10a0 <eventfd@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1070 <ancil_send_fd@plt>
cmp    $0x0,%rax
je     11e3 <connection_handler+0x53>
lea    0xe2c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%esi
lea    0xe21(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    1270 <connection_handler+0xe0>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
je     1240 <connection_handler+0xb0>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
jmp    124f <connection_handler+0xbf>
mov    -0x10(%rbp),%esi
lea    0xdd8(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1203 <connection_handler+0x73>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connection_handler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %edi,%edi
xor    %esi,%esi
call   10a0 <eventfd@plt>
mov    %eax,%r13d
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1060 <assert@plt>
mov    %ebp,%edi
mov    %r13d,%esi
call   1070 <ancil_send_fd@plt>
test   %rax,%rax
jne    1252 <connection_handler+0xc2>
lea    0xe3d(%rip),%rdi
mov    %r13d,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
mov    %eax,0x4(%rsp)
mov    $0xa,%ebx
lea    0x4(%rsp),%r14
lea    0xe2b(%rip),%r15
lea    0xe1e(%rip),%r12
xchg   %ax,%ax
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    1237 <connection_handler+0xa7>
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
add    $0x1,%ebp
mov    %ebp,0x4(%rsp)
mov    $0x1,%edi
call   10b0 <sleep@plt>
add    $0xffffffff,%ebx
jne    1200 <connection_handler+0x70>
jmp    1241 <connection_handler+0xb1>
mov    %r12,%rdi
call   1080 <perror@plt>
jmp    121f <connection_handler+0x8f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"ancil_send_fd\00"
@.str.1 = [14 x i8] c"Sent evfd %d\0A\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [11 x i8] c"Sent %llu\0A\00"
define i32 @connection_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = call i32 @eventfd(i32 0, i32 0)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, -1
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i32, i32* %2
%13 = load i32, i32* %4
%14 = call i64 @ancil_send_fd(i32 %12, i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = load i32, i32* %4
%21 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %19
%23 = call i32 (...) @getpid()
store i32 %23, i32* %5
store i32 0, i32* %3
br label %24
24: ; preds = %42, %22
%25 = load i32, i32* %3
%26 = icmp slt i32 %25, 10
br i1 %26, label %27, label %45
27: ; preds = %24
%28 = load i32, i32* %4
%29 = call i32 @write(i32 %28, i32* %5, i32 4)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = icmp ne i64 %31, 4
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %38
35: ; preds = %27
%36 = load i32, i32* %5
%37 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %35, %33
%39 = load i32, i32* %5
%40 = add nsw i32 %39, 1
store i32 %40, i32* %5
%41 = call i32 @sleep(i32 1)
br label %42
42: ; preds = %38
%43 = load i32, i32* %3
%44 = add nsw i32 %43, 1
store i32 %44, i32* %3
br label %24
45: ; preds = %24
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"ancil_send_fd\00"
@.str.1 = [14 x i8] c"Sent evfd %d\0A\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [11 x i8] c"Sent %llu\0A\00"
define i32 @connection_handler(i32 %0) {
%2 = alloca i32
%3 = tail call i32 @eventfd(i32 0, i32 0)
%4 = icmp ne i32 %3, -1
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call i64 @ancil_send_fd(i32 %0, i32 %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%14 = bitcast i32* %2 to i8*
%15 = tail call i32 (...) @getpid()
store i32 %15, i32* %2
br label %16
16: ; preds = %12, %25
%17 = phi i32 [ %15, %12 ], [ %26, %25 ]
%18 = phi i32 [ 0, %12 ], [ %28, %25 ]
%19 = call i32 @write(i32 %3, i32* %2, i32 4)
%20 = icmp eq i32 %19, 4
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %25
23: ; preds = %16
%24 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %17)
br label %25
25: ; preds = %23, %21
%26 = add nsw i32 %17, 1
store i32 %26, i32* %2
%27 = tail call i32 @sleep(i32 1)
%28 = add nuw nsw i32 %18, 1
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %16
30: ; preds = %25
ret i32 0
}