#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_in6 {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ LOG_NOTICE;
int POLLHUP;
int POLLIN;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ poll(struct pollfd *, int, int);
int /*<<< orphan*/ *popen(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int update_from(int listener, char *updater) {
  int ac;
  char buf[2048];
  struct sockaddr_in6 sa;
  socklen_t sz = sizeof(sa);
  if ((ac = accept(listener, (struct sockaddr *)&sa, &sz)) < 1) {
    syslog(LOG_NOTICE, "failed to accept: %s", strerror(errno));
  } else {
    struct pollfd pa;
    FILE *prg;
    prg = popen(updater, "w");
    if (prg == NULL) {
      close(ac);
    } else {
      pa.fd = ac;
      pa.events = POLLIN | POLLHUP;
      for (;;) {
        int r = 0;
        poll(&pa, 1, 10);
        if ((pa.revents & POLLIN) == POLLIN) {
          r = recv(ac, buf, sizeof(buf), 0);
          if (r > 0)
            fwrite(buf, r, 1, prg);
        }
        if ((pa.revents & POLLHUP) == POLLHUP || r == 0) {
          close(ac);
          fclose(prg);
          break;
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<update_from>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x82c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x828(%rbp),%rsi
lea    -0x82c(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x1,%eax
jge    1204 <update_from+0x74>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x848(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x848(%rbp),%edi
mov    %eax,%edx
lea    0xe06(%rip),%rsi
call   1060 <syslog@plt>
jmp    1334 <update_from+0x1a4>
mov    -0x10(%rbp),%rdi
lea    0xe06(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x840(%rbp)
cmpq   $0x0,-0x840(%rbp)
jne    1236 <update_from+0xa6>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    132f <update_from+0x19f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x838(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    0x2d99(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x834(%rbp)
movl   $0x0,-0x844(%rbp)
lea    -0x838(%rbp),%rdi
mov    $0x1,%esi
mov    $0xa,%edx
call   1070 <poll@plt>
mov    -0x830(%rbp),%eax
mov    0x2d5c(%rip),%rcx
and    (%rcx),%eax
mov    0x2d53(%rip),%rcx
cmp    (%rcx),%eax
jne    12e1 <update_from+0x151>
mov    -0x14(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x844(%rbp)
cmpl   $0x0,-0x844(%rbp)
jle    12dc <update_from+0x14c>
lea    -0x820(%rbp),%rdi
mov    -0x844(%rbp),%esi
mov    -0x840(%rbp),%rcx
mov    $0x1,%edx
call   10a0 <fwrite@plt>
jmp    12e1 <update_from+0x151>
mov    -0x830(%rbp),%eax
mov    0x2cfa(%rip),%rcx
and    (%rcx),%eax
mov    0x2cf1(%rip),%rcx
cmp    (%rcx),%eax
je     130c <update_from+0x17c>
cmpl   $0x0,-0x844(%rbp)
jne    1325 <update_from+0x195>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x840(%rbp),%rdi
call   1040 <fclose@plt>
jmp    132a <update_from+0x19a>
jmp    1257 <update_from+0xc7>
jmp    132f <update_from+0x19f>
jmp    1334 <update_from+0x1a4>
xor    %eax,%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_from>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %rsi,%r14
movl   $0x4,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1090 <accept@plt>
test   %eax,%eax
jle    127a <update_from+0xea>
mov    %eax,%ebx
lea    0xe49(%rip),%rsi
mov    %r14,%rdi
call   1080 <popen@plt>
test   %rax,%rax
je     12a3 <update_from+0x113>
mov    %rax,%r14
mov    0x2df9(%rip),%r13
mov    0x2dfa(%rip),%rbp
mov    0x0(%rbp),%eax
or     0x0(%r13),%eax
mov    %ebx,0x8(%rsp)
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%r15
lea    0x20(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0xa,%edx
call   1070 <poll@plt>
mov    0x10(%rsp),%eax
not    %eax
test   %eax,0x0(%r13)
jne    1269 <update_from+0xd9>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    125a <update_from+0xca>
mov    %r12,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   10a0 <fwrite@plt>
mov    $0x1,%eax
mov    0x10(%rsp),%ecx
not    %ecx
test   %ecx,0x0(%rbp)
je     1269 <update_from+0xd9>
test   %eax,%eax
jne    1210 <update_from+0x80>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    12aa <update_from+0x11a>
mov    0x2d47(%rip),%rax
mov    (%rax),%ebx
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd68(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1060 <syslog@plt>
jmp    12aa <update_from+0x11a>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@LOG_NOTICE = global i32 0
@.str = [21 x i8] c"failed to accept: %s\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"w\00"
@POLLIN = global i32 0
@POLLHUP = global i32 0
define i32 @update_from(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [2048 x i8]
%7 = alloca %struct.sockaddr_in6
%8 = alloca i32
%9 = alloca %struct.pollfd
%10 = alloca i32*
%11 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 4, i32* %8
%12 = load i32, i32* %3
%13 = bitcast %struct.sockaddr_in6* %7 to %struct.sockaddr*
%14 = call i32 @accept(i32 %12, %struct.sockaddr* %13, i32* %8)
store i32 %14, i32* %5
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i32, i32* @LOG_NOTICE
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @syslog(i32 %17, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %74
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = call i32* @popen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %10
%24 = load i32*, i32** %10
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
br label %73
29: ; preds = %21
%30 = load i32, i32* %5
%31 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* @POLLIN
%33 = load i32, i32* @POLLHUP
%34 = or i32 %32, %33
%35 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 1
store i32 %34, i32* %35
br label %36
36: ; preds = %71, %29
store i32 0, i32* %11
%37 = call i32 @poll(%struct.pollfd* %9, i32 1, i32 10)
%38 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
%39 = load i32, i32* %38
%40 = load i32, i32* @POLLIN
%41 = and i32 %39, %40
%42 = load i32, i32* @POLLIN
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %56
44: ; preds = %36
%45 = load i32, i32* %5
%46 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%47 = call i32 @recv(i32 %45, i8* %46, i32 2048, i32 0)
store i32 %47, i32* %11
%48 = load i32, i32* %11
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %44
%51 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%52 = load i32, i32* %11
%53 = load i32*, i32** %10
%54 = call i32 @fwrite(i8* %51, i32 %52, i32 1, i32* %53)
br label %55
55: ; preds = %50, %44
br label %56
56: ; preds = %55, %36
%57 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
%58 = load i32, i32* %57
%59 = load i32, i32* @POLLHUP
%60 = and i32 %58, %59
%61 = load i32, i32* @POLLHUP
%62 = icmp eq i32 %60, %61
br i1 %62, label %66, label %63
63: ; preds = %56
%64 = load i32, i32* %11
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %71
66: ; preds = %63, %56
%67 = load i32, i32* %5
%68 = call i32 @close(i32 %67)
%69 = load i32*, i32** %10
%70 = call i32 @fclose(i32* %69)
br label %72
71: ; preds = %63
br label %36
72: ; preds = %66
br label %73
73: ; preds = %72, %26
br label %74
74: ; preds = %73, %16
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@LOG_NOTICE = global i32 0
@.str = [21 x i8] c"failed to accept: %s\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"w\00"
@POLLIN = global i32 0
@POLLHUP = global i32 0
define i32 @update_from(i32 %0, i8* nocapture %1) {
%3 = alloca [2048 x i8]
%4 = alloca %struct.sockaddr_in6
%5 = alloca i32
%6 = alloca %struct.pollfd
%7 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in6* %4 to i8*
%9 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%10 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%11 = call i32 @accept(i32 %0, %struct.sockaddr* %10, i32* %5)
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @LOG_NOTICE
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @syslog(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %54
18: ; preds = %2
%19 = bitcast %struct.pollfd* %6 to i8*
%20 = call i32* @popen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @close(i32 %11)
br label %53
24: ; preds = %18
%25 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 0
store i32 %11, i32* %25
%26 = load i32, i32* @POLLIN
%27 = load i32, i32* @POLLHUP
%28 = or i32 %27, %26
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 2
br label %31
31: ; preds = %42, %24
%32 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 10)
%33 = load i32, i32* %30
%34 = load i32, i32* @POLLIN
%35 = and i32 %34, %33
%36 = icmp eq i32 %35, %34
br i1 %36, label %37, label %50
37: ; preds = %31
%38 = call i32 @recv(i32 %11, i8* %7, i32 2048, i32 0)
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @fwrite(i8* %7, i32 %38, i32 1, i32* %20)
br label %42
42: ; preds = %37, %40
%43 = phi i32 [ 1, %40 ], [ %38, %37 ]
%44 = load i32, i32* %30
%45 = load i32, i32* @POLLHUP
%46 = and i32 %45, %44
%47 = icmp eq i32 %46, %45
%48 = icmp eq i32 %43, 0
%49 = select i1 %47, i1 true, i1 %48
br i1 %49, label %50, label %31
50: ; preds = %42, %31
%51 = call i32 @close(i32 %11)
%52 = call i32 @fclose(i32* %20)
br label %53
53: ; preds = %50, %22
br label %54
54: ; preds = %53, %13
ret i32 0
}