#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int POLLERR;
int POLLIN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int sockfd;
  struct pollfd read_poll[1];
  struct sockaddr_in servaddr;
  char buffer[1] = {'V'};
  long alive = 0;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(8888);
  inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
  connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  read_poll[0].fd = sockfd;
  read_poll[0].events = POLLIN;
  write(sockfd, buffer, 1);
  for (;;) {
    poll(read_poll, 1, -1);
    if (read_poll[0].revents & (POLLIN | POLLERR)) {
      read(sockfd, buffer, 1);
      write(sockfd, buffer, 1);
    }
    printf("alive %ld times\n", alive++);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe54(%rip),%al
mov    %al,-0x31(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x30(%rbp),%rdx
call   1080 <inet_pton@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1090 <poll@plt>
mov    -0x18(%rbp),%eax
mov    0x2d71(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d70(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     129f <main+0x10f>
mov    -0x14(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0x14(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x40(%rbp)
mov    %eax,%esi
lea    0xd4a(%rip),%rdi
call   1050 <printf@plt>
jmp    124a <main+0xba>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
movb   $0x56,0x7(%rsp)
mov    0x2e3b(%rip),%r12
mov    (%r12),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
lea    0x18(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x20(%rsp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,0x1c(%rsp)
mov    (%r12),%edi
mov    0x8(%r14),%rsi
mov    %r15,%rdx
call   1080 <inet_pton@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
mov    %ebp,0x8(%rsp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x7(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0x8(%rsp),%r15
mov    0x2da1(%rip),%r13
lea    0xdba(%rip),%r12
jmp    125d <main+0xcd>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
add    $0x1,%ebx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1090 <poll@plt>
mov    0x0(%r13),%eax
mov    0x2d5e(%rip),%rcx
or     (%rcx),%eax
test   %eax,0x10(%rsp)
je     1250 <main+0xc0>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1250 <main+0xc0>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.buffer = [1 x i8] c"V"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@POLLIN = global i32 0
@POLLERR = global i32 0
@.str = [17 x i8] c"alive %ld times\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1 x %struct.pollfd]
%8 = alloca %struct.sockaddr_in
%9 = alloca [1 x i8]
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [1 x i8]* %9 to i8*
store i64 0, i64* %10
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 8888)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%25 = call i32 @inet_pton(i32 %20, i8* %23, i32* %24)
%26 = load i32, i32* %6
%27 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%28 = call i32 @connect(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = load i32, i32* %6
%30 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%31 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load i32, i32* @POLLIN
%33 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %33, i32 0, i32 1
store i32 %32, i32* %34
%35 = load i32, i32* %6
%36 = getelementptr inbounds [1 x i8], [1 x i8]* %9, i64 0, i64 0
%37 = call i32 @write(i32 %35, i8* %36, i32 1)
br label %38
38: ; preds = %56, %2
%39 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%40 = call i32 @poll(%struct.pollfd* %39, i32 1, i32 -1)
%41 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%42 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %41, i32 0, i32 2
%43 = load i32, i32* %42
%44 = load i32, i32* @POLLIN
%45 = load i32, i32* @POLLERR
%46 = or i32 %44, %45
%47 = and i32 %43, %46
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %38
%50 = load i32, i32* %6
%51 = getelementptr inbounds [1 x i8], [1 x i8]* %9, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 1)
%53 = load i32, i32* %6
%54 = getelementptr inbounds [1 x i8], [1 x i8]* %9, i64 0, i64 0
%55 = call i32 @write(i32 %53, i8* %54, i32 1)
br label %56
56: ; preds = %49, %38
%57 = load i64, i64* %10
%58 = add nsw i64 %57, 1
store i64 %58, i64* %10
%59 = trunc i64 %57 to i32
%60 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %59)
br label %38
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@POLLIN = global i32 0
@POLLERR = global i32 0
@.str = [17 x i8] c"alive %ld times\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1 x %struct.pollfd]
%4 = alloca %struct.sockaddr_in
%5 = alloca [1 x i8]
%6 = bitcast [1 x %struct.pollfd]* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
store i8 86, i8* %8
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 8888)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%21 = call i32 @inet_pton(i32 %17, i8* %19, i32* %20)
%22 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%23 = call i32 @connect(i32 %11, %struct.sockaddr* %22, i32 12)
%24 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0
%25 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
store i32 %11, i32* %25
%26 = load i32, i32* @POLLIN
%27 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
store i32 %26, i32* %27
%28 = call i32 @write(i32 %11, i8* %8, i32 1)
%29 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
br label %30
30: ; preds = %42, %2
%31 = phi i64 [ 0, %2 ], [ %43, %42 ]
%32 = call i32 @poll(%struct.pollfd* %24, i32 1, i32 -1)
%33 = load i32, i32* %29
%34 = load i32, i32* @POLLIN
%35 = load i32, i32* @POLLERR
%36 = or i32 %35, %34
%37 = and i32 %36, %33
%38 = icmp eq i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %30
%40 = call i32 @read(i32 %11, i8* %8, i32 1)
%41 = call i32 @write(i32 %11, i8* %8, i32 1)
br label %42
42: ; preds = %39, %30
%43 = add nuw nsw i64 %31, 1
%44 = trunc i64 %31 to i32
%45 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %44)
br label %30
}