#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 */

/* Variables and functions */
int /*<<< orphan*/ EPOLLIN;
int /*<<< orphan*/ EPOLLOUT;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ delete_event(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ modify_event(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
void do_write(int epollfd, int fd, int sockfd, char *buf) {
  int nwrite;
  nwrite = write(fd, buf, strlen(buf));
  if (nwrite == -1) {
    perror("write error:");
    close(fd);
  } else {
    if (fd == STDOUT_FILENO)
      delete_event(epollfd, fd, EPOLLOUT);
    else
      modify_event(epollfd, fd, EPOLLIN);
  }
  memset(buf, 0, 1024);
}


x86 assembly (clang, -O0):
<do_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d0 <do_write+0x60>
lea    0xe42(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
jmp    1214 <do_write+0xa4>
mov    -0x8(%rbp),%eax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%eax
jne    11fb <do_write+0x8b>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
call   1050 <delete_event@plt>
jmp    120f <do_write+0x9f>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
call   1080 <modify_event@plt>
jmp    1214 <do_write+0xa4>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write>:
push   %rbp
push   %r14
push   %rbx
mov    %rcx,%rbx
mov    %esi,%ebp
mov    %edi,%r14d
mov    %rcx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11db <do_write+0x6b>
mov    0x2e54(%rip),%rax
cmp    %ebp,(%rax)
jne    11b5 <do_write+0x45>
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %ebp,%esi
call   1050 <delete_event@plt>
jmp    11c8 <do_write+0x58>
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %ebp,%esi
call   1080 <modify_event@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <memset@plt>
lea    0xe1e(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    11c8 <do_write+0x58>

LLVM IR (-O0):
@.str = [13 x i8] c"write error:\00"
@STDOUT_FILENO = global i32 0
@EPOLLOUT = global i32 0
@EPOLLIN = global i32 0
define void @do_write(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%10 = load i32, i32* %6
%11 = load i8*, i8** %8
%12 = load i8*, i8** %8
%13 = call i32 @strlen(i8* %12)
%14 = call i32 @write(i32 %10, i8* %11, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* %6
%20 = call i32 @close(i32 %19)
br label %36
21: ; preds = %4
%22 = load i32, i32* %6
%23 = load i32, i32* @STDOUT_FILENO
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = load i32, i32* @EPOLLOUT
%29 = call i32 @delete_event(i32 %26, i32 %27, i32 %28)
br label %35
30: ; preds = %21
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = load i32, i32* @EPOLLIN
%34 = call i32 @modify_event(i32 %31, i32 %32, i32 %33)
br label %35
35: ; preds = %30, %25
br label %36
36: ; preds = %35, %17
%37 = load i8*, i8** %8
%38 = call i32 @memset(i8* %37, i32 0, i32 1024)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"write error:\00"
@STDOUT_FILENO = global i32 0
@EPOLLOUT = global i32 0
@EPOLLIN = global i32 0
define void @do_write(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = tail call i32 @strlen(i8* %3)
%6 = tail call i32 @write(i32 %1, i8* %3, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @close(i32 %1)
br label %20
11: ; preds = %4
%12 = load i32, i32* @STDOUT_FILENO
%13 = icmp eq i32 %12, %1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EPOLLOUT
%16 = tail call i32 @delete_event(i32 %0, i32 %1, i32 %15)
br label %20
17: ; preds = %11
%18 = load i32, i32* @EPOLLIN
%19 = tail call i32 @modify_event(i32 %0, i32 %1, i32 %18)
br label %20
20: ; preds = %14, %17, %8
%21 = tail call i32 @memset(i8* %3, i32 0, i32 1024)
ret void
}