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

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
}