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

/* Variables and functions */
int POLLIN;
int /*<<< orphan*/ SHUT_WR;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ poll(struct pollfd *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
void handle_connection(int sockfd) {
  char sendline[1024], recvline[1024];
  int maxfdp, stdineof;
  struct pollfd pfds[2];
  int n;
  pfds[0].fd = sockfd;
  pfds[0].events = POLLIN;
  pfds[1].fd = STDIN_FILENO;
  pfds[1].events = POLLIN;
  for (;;) {
    poll(pfds, 2, -1);
    if (pfds[0].revents & POLLIN) {
      n = read(sockfd, recvline, 1024);
      if (n == 0) {
        fprintf(stderr, "client: server is closed.\n");
        close(sockfd);
      }
      write(STDOUT_FILENO, recvline, n);
    }
    if (pfds[1].revents & POLLIN) {
      n = read(STDIN_FILENO, sendline, 1024);
      if (n == 0) {
        shutdown(sockfd, SHUT_WR);
        continue;
      }
      write(sockfd, sendline, n);
    }
  }
}


x86 assembly (clang, -O0):
<handle_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x830(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x82c(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x824(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x820(%rbp)
lea    -0x830(%rbp),%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
mov    -0x828(%rbp),%eax
mov    0x2e11(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1231 <handle_connection+0xd1>
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x834(%rbp)
cmpl   $0x0,-0x834(%rbp)
jne    1216 <handle_connection+0xb6>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    -0x810(%rbp),%rsi
mov    -0x834(%rbp),%edx
call   1030 <write@plt>
mov    -0x81c(%rbp),%eax
mov    0x2d9a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12a1 <handle_connection+0x141>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x834(%rbp)
cmpl   $0x0,-0x834(%rbp)
jne    128c <handle_connection+0x12c>
mov    -0x4(%rbp),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
jmp    11a4 <handle_connection+0x44>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x834(%rbp),%edx
call   1030 <write@plt>
jmp    11a4 <handle_connection+0x44>

x86 assembly (clang, -O2):
<handle_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %edi,%ebx
mov    %edi,(%rsp)
mov    0x2e5b(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    0x2e34(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0xc(%rsp)
mov    %eax,0x10(%rsp)
mov    %rsp,%r14
lea    0x420(%rsp),%r15
lea    0x20(%rsp),%r12
jmp    11bc <handle_connection+0x5c>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14,%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
mov    0x0(%r13),%eax
test   %eax,0x8(%rsp)
je     1220 <handle_connection+0xc0>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1209 <handle_connection+0xa9>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    0x0(%r13),%eax
test   %eax,0x14(%rsp)
je     11bc <handle_connection+0x5c>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    11b0 <handle_connection+0x50>
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <shutdown@plt>
jmp    11bc <handle_connection+0x5c>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"client: server is closed.\0A\00"
@STDOUT_FILENO = global i32 0
@SHUT_WR = global i32 0
define void @handle_connection(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca [2 x %struct.pollfd]
%8 = alloca i32
store i32 %0, i32* %2
%9 = load i32, i32* %2
%10 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
store i32 %9, i32* %11
%12 = load i32, i32* @POLLIN
%13 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %13, i32 0, i32 1
store i32 %12, i32* %14
%15 = load i32, i32* @STDIN_FILENO
%16 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = load i32, i32* @POLLIN
%19 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %19, i32 0, i32 1
store i32 %18, i32* %20
br label %21
21: ; preds = %68, %59, %1
%22 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%23 = call i32 @poll(%struct.pollfd* %22, i32 2, i32 -1)
%24 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%25 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %24, i32 0, i32 2
%26 = load i32, i32* %25
%27 = load i32, i32* @POLLIN
%28 = and i32 %26, %27
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %46
30: ; preds = %21
%31 = load i32, i32* %2
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 1024)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
br label %41
41: ; preds = %36, %30
%42 = load i32, i32* @STDOUT_FILENO
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%44 = load i32, i32* %8
%45 = call i32 @write(i32 %42, i8* %43, i32 %44)
br label %46
46: ; preds = %41, %21
%47 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%48 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %47, i32 0, i32 2
%49 = load i32, i32* %48
%50 = load i32, i32* @POLLIN
%51 = and i32 %49, %50
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %68
53: ; preds = %46
%54 = load i32, i32* @STDIN_FILENO
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%56 = call i32 @read(i32 %54, i8* %55, i32 1024)
store i32 %56, i32* %8
%57 = load i32, i32* %8
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %53
%60 = load i32, i32* %2
%61 = load i32, i32* @SHUT_WR
%62 = call i32 @shutdown(i32 %60, i32 %61)
br label %21
63: ; preds = %53
%64 = load i32, i32* %2
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%66 = load i32, i32* %8
%67 = call i32 @write(i32 %64, i8* %65, i32 %66)
br label %68
68: ; preds = %63, %46
br label %21
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"client: server is closed.\0A\00"
@STDOUT_FILENO = global i32 0
@SHUT_WR = global i32 0
define void @handle_connection(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = alloca [2 x %struct.pollfd]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast [2 x %struct.pollfd]* %4 to i8*
%8 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%9 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 0
store i32 %0, i32* %9
%10 = load i32, i32* @POLLIN
%11 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @STDIN_FILENO
%13 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 1
store i32 %10, i32* %14
%15 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 2
%16 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 2
br label %17
17: ; preds = %39, %1
%18 = call i32 @poll(%struct.pollfd* %8, i32 2, i32 -1)
%19 = load i32, i32* %15
%20 = load i32, i32* @POLLIN
%21 = and i32 %20, %19
%22 = icmp eq i32 %21, 0
br i1 %22, label %34, label %23
23: ; preds = %17
%24 = call i32 @read(i32 %0, i8* %6, i32 1024)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @close(i32 %0)
br label %30
30: ; preds = %26, %23
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @write(i32 %31, i8* %6, i32 %24)
%33 = load i32, i32* @POLLIN
br label %34
34: ; preds = %30, %17
%35 = phi i32 [ %33, %30 ], [ %20, %17 ]
%36 = load i32, i32* %16
%37 = and i32 %35, %36
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34, %47, %44
br label %17
40: ; preds = %34
%41 = load i32, i32* @STDIN_FILENO
%42 = call i32 @read(i32 %41, i8* %5, i32 1024)
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @SHUT_WR
%46 = call i32 @shutdown(i32 %0, i32 %45)
br label %39
47: ; preds = %40
%48 = call i32 @write(i32 %0, i8* %5, i32 %42)
br label %39
}