#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;
};
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ PF_INET;
int POLLHUP;
int POLLIN;
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ creds(int);
int /*<<< orphan*/ do_connect(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fatal(int, char *);
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ poll(struct pollfd *, int, int);
scalar_t__ read_write(int, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sock;
  struct pollfd fds[2];
  int nfds = 2;
  int one = 1;
  openlog("netconf-subsys", 0, LOG_DAEMON);
  if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    fatal(1, "Failed to create socket");
  do_connect(sock);
  (void)setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
  creds(sock);
  fds[0].fd = sock;
  fds[0].events = POLLIN;
  fds[1].fd = 0;
  fds[1].events = POLLIN;
  while (1) {
    fds[0].revents = 0;
    fds[1].revents = 0;
    if (poll(&fds[0], nfds, -1) < 0) {
      perror("Poll failed:");
      continue;
    }
    if (fds[1].revents & (POLLIN | POLLHUP)) {
      if (read_write(0, sock) == 0) {
        nfds = 1;
        shutdown(sock, SHUT_WR);
      }
    } else if (fds[1].revents) {
      fatal(7, "Poll error on stdin");
    } else if (fds[0].revents & (POLLIN | POLLHUP)) {
      if (read_write(sock, 1) == 0) {
        exit(0);
      }
    } else if (fds[0].revents) {
      fatal(8, "Poll error from server");
    }
  }
}


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)
movl   $0x2,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
lea    0xe1c(%rip),%rdi
xor    %esi,%esi
call   10a0 <openlog@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1221 <main+0x71>
mov    $0x1,%edi
lea    0xdf3(%rip),%rsi
call   10c0 <fatal@plt>
mov    -0x14(%rbp),%edi
call   1030 <do_connect@plt>
mov    -0x14(%rbp),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x14(%rbp),%edi
call   1050 <creds@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    $0xffffffff,%edx
call   1070 <poll@plt>
cmp    $0x0,%rax
jge    12b4 <main+0x104>
lea    0xd7d(%rip),%rdi
call   1080 <perror@plt>
jmp    127a <main+0xca>
mov    -0x1c(%rbp),%eax
mov    0x2d02(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d19(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1305 <main+0x155>
mov    -0x14(%rbp),%esi
xor    %edi,%edi
call   1090 <read_write@plt>
cmp    $0x0,%rax
jne    1300 <main+0x150>
movl   $0x1,-0x34(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
call   1060 <shutdown@plt>
jmp    1392 <main+0x1e2>
cmpl   $0x0,-0x1c(%rbp)
je     1325 <main+0x175>
mov    $0x7,%edi
lea    0xd19(%rip),%rsi
call   10c0 <fatal@plt>
jmp    138d <main+0x1dd>
mov    -0x28(%rbp),%eax
mov    0x2c91(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2ca8(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1368 <main+0x1b8>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1090 <read_write@plt>
cmp    $0x0,%rax
jne    1363 <main+0x1b3>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1388 <main+0x1d8>
cmpl   $0x0,-0x28(%rbp)
je     1383 <main+0x1d3>
mov    $0x8,%edi
lea    0xcca(%rip),%rsi
call   10c0 <fatal@plt>
jmp    1388 <main+0x1d8>
jmp    138d <main+0x1dd>
jmp    1392 <main+0x1e2>
jmp    127a <main+0xca>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movl   $0x1,0xc(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
lea    0xe2a(%rip),%rdi
xor    %esi,%esi
call   10a0 <openlog@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    120e <main+0x5e>
lea    0xe0b(%rip),%rsi
mov    $0x1,%edi
call   10c0 <fatal@plt>
mov    %r14d,%edi
call   1030 <do_connect@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %r14d,%edi
call   1050 <creds@plt>
mov    %r14d,0x10(%rsp)
mov    0x2d71(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x14(%rsp)
movl   $0x0,0x1c(%rsp)
mov    %eax,0x20(%rsp)
mov    $0x2,%ebp
lea    0x10(%rsp),%rbx
lea    0xdb4(%rip),%r12
mov    0x2d66(%rip),%r13
jmp    1288 <main+0xd8>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1080 <perror@plt>
movl   $0x0,0x18(%rsp)
movl   $0x0,0x24(%rsp)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0xffffffff,%edx
call   1070 <poll@plt>
test   %rax,%rax
js     1280 <main+0xd0>
mov    0x24(%rsp),%ecx
mov    0x0(%r13),%eax
or     (%r15),%eax
test   %ecx,%eax
je     12e2 <main+0x132>
xor    %edi,%edi
mov    %r14d,%esi
call   1090 <read_write@plt>
test   %rax,%rax
jne    1288 <main+0xd8>
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1060 <shutdown@plt>
mov    $0x1,%ebp
jmp    1288 <main+0xd8>
test   %ecx,%ecx
je     12f9 <main+0x149>
mov    $0x7,%edi
lea    0xd42(%rip),%rsi
call   10c0 <fatal@plt>
jmp    1288 <main+0xd8>
mov    0x18(%rsp),%ecx
test   %eax,%ecx
je     1319 <main+0x169>
mov    %r14d,%edi
mov    $0x1,%esi
call   1090 <read_write@plt>
test   %rax,%rax
jne    1288 <main+0xd8>
jmp    1337 <main+0x187>
test   %ecx,%ecx
je     1288 <main+0xd8>
mov    $0x8,%edi
lea    0xd1b(%rip),%rsi
call   10c0 <fatal@plt>
jmp    1288 <main+0xd8>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [15 x i8] c"netconf-subsys\00"
@LOG_DAEMON = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [24 x i8] c"Failed to create socket\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@POLLIN = global i32 0
@.str.2 = [13 x i8] c"Poll failed:\00"
@POLLHUP = global i32 0
@SHUT_WR = global i32 0
@.str.3 = [20 x i8] c"Poll error on stdin\00"
@.str.4 = [23 x i8] c"Poll error from server\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [2 x %struct.pollfd]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 2, i32* %8
store i32 1, i32* %9
%10 = load i32, i32* @LOG_DAEMON
%11 = call i32 @openlog(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 0, i32 %10)
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @fatal(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32, i32* %6
%20 = call i32 @do_connect(i32 %19)
%21 = load i32, i32* %6
%22 = load i32, i32* @IPPROTO_TCP
%23 = load i32, i32* @TCP_NODELAY
%24 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i32* %9, i32 4)
%25 = load i32, i32* %6
%26 = call i32 @creds(i32 %25)
%27 = load i32, i32* %6
%28 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load i32, i32* @POLLIN
%31 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%32 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %31, i32 0, i32 1
store i32 %30, i32* %32
%33 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %33, i32 0, i32 0
store i32 0, i32* %34
%35 = load i32, i32* @POLLIN
%36 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %36, i32 0, i32 1
store i32 %35, i32* %37
br label %38
38: ; preds = %18, %47, %100
%39 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%40 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %39, i32 0, i32 2
store i32 0, i32* %40
%41 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%42 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %41, i32 0, i32 2
store i32 0, i32* %42
%43 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%44 = load i32, i32* %8
%45 = call i64 @poll(%struct.pollfd* %43, i32 %44, i32 -1)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %38
%48 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %38
49: ; preds = %38
%50 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%51 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %50, i32 0, i32 2
%52 = load i32, i32* %51
%53 = load i32, i32* @POLLIN
%54 = load i32, i32* @POLLHUP
%55 = or i32 %53, %54
%56 = and i32 %52, %55
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %67
58: ; preds = %49
%59 = load i32, i32* %6
%60 = call i64 @read_write(i32 0, i32 %59)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %58
store i32 1, i32* %8
%63 = load i32, i32* %6
%64 = load i32, i32* @SHUT_WR
%65 = call i32 @shutdown(i32 %63, i32 %64)
br label %66
66: ; preds = %62, %58
br label %100
67: ; preds = %49
%68 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 1
%69 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %68, i32 0, i32 2
%70 = load i32, i32* %69
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %67
%73 = call i32 @fatal(i32 7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %99
74: ; preds = %67
%75 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%76 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %75, i32 0, i32 2
%77 = load i32, i32* %76
%78 = load i32, i32* @POLLIN
%79 = load i32, i32* @POLLHUP
%80 = or i32 %78, %79
%81 = and i32 %77, %80
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %90
83: ; preds = %74
%84 = load i32, i32* %6
%85 = call i64 @read_write(i32 %84, i32 1)
%86 = icmp eq i64 %85, 0
br i1 %86, label %87, label %89
87: ; preds = %83
%88 = call i32 @exit(i32 0)
unreachable
89: ; preds = %83
br label %98
90: ; preds = %74
%91 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %7, i64 0, i64 0
%92 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %91, i32 0, i32 2
%93 = load i32, i32* %92
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %97
95: ; preds = %90
%96 = call i32 @fatal(i32 8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %97
97: ; preds = %95, %90
br label %98
98: ; preds = %97, %89
br label %99
99: ; preds = %98, %72
br label %100
100: ; preds = %99, %66
br label %38
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str = [15 x i8] c"netconf-subsys\00"
@LOG_DAEMON = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [24 x i8] c"Failed to create socket\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@POLLIN = global i32 0
@.str.2 = [13 x i8] c"Poll failed:\00"
@POLLHUP = global i32 0
@SHUT_WR = global i32 0
@.str.3 = [20 x i8] c"Poll error on stdin\00"
@.str.4 = [23 x i8] c"Poll error from server\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x %struct.pollfd]
%4 = alloca i32
%5 = bitcast [2 x %struct.pollfd]* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = load i32, i32* @LOG_DAEMON
%8 = tail call i32 @openlog(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 0, i32 %7)
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @fatal(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = tail call i32 @do_connect(i32 %11)
%17 = load i32, i32* @IPPROTO_TCP
%18 = load i32, i32* @TCP_NODELAY
%19 = call i32 @setsockopt(i32 %11, i32 %17, i32 %18, i32* %4, i32 4)
%20 = call i32 @creds(i32 %11)
%21 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%22 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
store i32 %11, i32* %22
%23 = load i32, i32* @POLLIN
%24 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 0
store i32 0, i32* %25
%26 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 1
store i32 %23, i32* %26
%27 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
%28 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 2
br label %29
29: ; preds = %48, %15
%30 = phi i32 [ 1, %48 ], [ 2, %15 ]
br label %31
31: ; preds = %55, %29
store i32 0, i32* %27
store i32 0, i32* %28
%32 = call i64 @poll(%struct.pollfd* %21, i32 %30, i32 -1)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31, %34
%35 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %27
store i32 0, i32* %28
%36 = call i64 @poll(%struct.pollfd* %21, i32 %30, i32 -1)
%37 = icmp slt i64 %36, 0
br i1 %37, label %34, label %38
38: ; preds = %34, %31
%39 = load i32, i32* %28
%40 = load i32, i32* @POLLIN
%41 = load i32, i32* @POLLHUP
%42 = or i32 %41, %40
%43 = and i32 %42, %39
%44 = icmp eq i32 %43, 0
br i1 %44, label %51, label %45
45: ; preds = %38
%46 = call i64 @read_write(i32 0, i32 %11)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %45
%49 = load i32, i32* @SHUT_WR
%50 = call i32 @shutdown(i32 %11, i32 %49)
br label %29
51: ; preds = %38
%52 = icmp eq i32 %39, 0
br i1 %52, label %56, label %53
53: ; preds = %51
%54 = call i32 @fatal(i32 7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %65, %67, %60, %45
br label %31
56: ; preds = %51
%57 = load i32, i32* %27
%58 = and i32 %57, %42
%59 = icmp eq i32 %58, 0
br i1 %59, label %65, label %60
60: ; preds = %56
%61 = call i64 @read_write(i32 %11, i32 1)
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %55
63: ; preds = %60
%64 = call i32 @exit(i32 0)
unreachable
65: ; preds = %56
%66 = icmp eq i32 %57, 0
br i1 %66, label %55, label %67
67: ; preds = %65
%68 = call i32 @fatal(i32 8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %55
}