#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 events;
  int revents;
  int /*<<< orphan*/ fd;
};
typedef int in_port_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int POLLIN;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int SOCK_DGRAM;
int SOCK_STREAM;
size_t __arraycount(struct pollfd *);
int /*<<< orphan*/ atoi(int /*<<< orphan*/);
int /*<<< orphan*/ cr(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ err(int, char *);
int getopt(int, char **, char *);
int /*<<< orphan*/ handle(int, int /*<<< orphan*/);
int /*<<< orphan*/ optarg;
int poll(struct pollfd *, size_t, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ usage(int);
int main(int argc, char *argv[]) {
  struct pollfd pfd[1];
  int type = SOCK_STREAM, c;
  in_port_t port = 6161;
  signal(SIGCHLD, SIG_IGN);
  while ((c = getopt(argc, argv, "up:")) != -1)
    switch (c) {
    case 'u':
      type = SOCK_DGRAM;
      break;
    case 'p':
      port = (in_port_t)atoi(optarg);
      break;
    default:
      usage(c);
    }
  pfd[0].fd = cr(AF_INET, type, port);
  pfd[0].events = POLLIN;
  for (;;) {
    if (poll(pfd, __arraycount(pfd), -1) == -1)
      err(1, "poll");
    for (size_t i = 0; i < __arraycount(pfd); i++) {
      if ((pfd[i].revents & POLLIN) == 0)
        continue;
      handle(type, pfd[i].fd);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0x1811,-0x2c(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe22(%rip),%rdx
call   10a0 <getopt@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
je     1248 <main+0xb8>
mov    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x70,%eax
je     1225 <main+0x95>
jmp    1203 <main+0x73>
mov    -0x3c(%rbp),%eax
sub    $0x75,%eax
jne    123b <main+0xab>
jmp    1214 <main+0x84>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1243 <main+0xb3>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   10b0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
jmp    1243 <main+0xb3>
mov    -0x28(%rbp),%edi
call   1080 <usage@plt>
jmp    11d0 <main+0x40>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1030 <cr@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x20(%rbp),%rdi
call   1070 <__arraycount@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
mov    $0xffffffff,%edx
call   1090 <poll@plt>
cmp    $0xffffffff,%eax
jne    12a7 <main+0x117>
mov    $0x1,%edi
lea    0xd62(%rip),%rsi
call   1060 <err@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x20(%rbp),%rdi
call   1070 <__arraycount@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
cmp    %rcx,%rax
jae    131e <main+0x18e>
lea    -0x20(%rbp),%rax
imul   $0xc,-0x38(%rbp),%rcx
add    %rcx,%rax
mov    0x4(%rax),%eax
mov    0x2ce2(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    12f6 <main+0x166>
jmp    130d <main+0x17d>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rax
imul   $0xc,-0x38(%rbp),%rcx
add    %rcx,%rax
mov    0x8(%rax),%esi
call   1040 <handle@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    12af <main+0x11f>
jmp    126b <main+0xdb>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e06(%rip),%rax
mov    (%rax),%r13d
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    $0x1811,%r15d
lea    0xe2f(%rip),%r12
mov    0x2df8(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   10a0 <getopt@plt>
cmp    $0xffffffff,%eax
je     121d <main+0x8d>
cmp    $0x70,%eax
je     1205 <main+0x75>
cmp    $0x75,%eax
je     1218 <main+0x88>
mov    %eax,%edi
call   1080 <usage@plt>
jmp    11e0 <main+0x50>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   10b0 <atoi@plt>
mov    %eax,%r15d
jmp    11e0 <main+0x50>
mov    (%rbx),%r13d
jmp    11e0 <main+0x50>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    %r13d,%esi
mov    %r15d,%edx
call   1030 <cr@plt>
mov    %eax,0x10(%rsp)
mov    0x2d8c(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rbp
lea    0xdb5(%rip),%r14
nop
mov    %rbp,%rdi
call   1070 <__arraycount@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
mov    $0xffffffff,%edx
call   1090 <poll@plt>
cmp    $0xffffffff,%eax
jne    127a <main+0xea>
mov    $0x1,%edi
mov    %r14,%rsi
call   1060 <err@plt>
mov    %rbp,%rdi
call   1070 <__arraycount@plt>
test   %rax,%rax
je     1250 <main+0xc0>
xor    %ebx,%ebx
jmp    12a1 <main+0x111>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    %rbp,%rdi
call   1070 <__arraycount@plt>
cmp    %rax,%rbx
jae    1250 <main+0xc0>
mov    (%r15),%eax
test   %eax,0xc(%rsp)
je     1290 <main+0x100>
mov    0x10(%rsp),%esi
mov    %r13d,%edi
call   1040 <handle@plt>
jmp    1290 <main+0x100>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@SOCK_STREAM = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str = [4 x i8] c"up:\00"
@SOCK_DGRAM = global i32 0
@optarg = global i32 0
@AF_INET = global i32 0
@POLLIN = global i32 0
@.str.1 = [5 x i8] c"poll\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1 x %struct.pollfd]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @SOCK_STREAM
store i32 %11, i32* %7
store i32 6161, i32* %9
%12 = load i32, i32* @SIGCHLD
%13 = load i32, i32* @SIG_IGN
%14 = call i32 @signal(i32 %12, i32 %13)
br label %15
15: ; preds = %30, %2
%16 = load i32, i32* %4
%17 = load i8**, i8*** %5
%18 = call i32 @getopt(i32 %16, i8** %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 %18, i32* %8
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %31
20: ; preds = %15
%21 = load i32, i32* %8
switch i32 %21, label %27 [
i32 117, label %22
i32 112, label %24
]
22: ; preds = %20
%23 = load i32, i32* @SOCK_DGRAM
store i32 %23, i32* %7
br label %30
24: ; preds = %20
%25 = load i32, i32* @optarg
%26 = call i32 @atoi(i32 %25)
store i32 %26, i32* %9
br label %30
27: ; preds = %20
%28 = load i32, i32* %8
%29 = call i32 @usage(i32 %28)
br label %30
30: ; preds = %27, %24, %22
br label %15
31: ; preds = %15
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* %7
%34 = load i32, i32* %9
%35 = call i32 @cr(i32 %32, i32 %33, i32 %34)
%36 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %36, i32 0, i32 2
store i32 %35, i32* %37
%38 = load i32, i32* @POLLIN
%39 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%40 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %39, i32 0, i32 0
store i32 %38, i32* %40
br label %41
41: ; preds = %74, %31
%42 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%43 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%44 = call i64 @__arraycount(%struct.pollfd* %43)
%45 = call i32 @poll(%struct.pollfd* %42, i64 %44, i32 -1)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %47, %41
store i64 0, i64* %10
br label %50
50: ; preds = %71, %49
%51 = load i64, i64* %10
%52 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%53 = call i64 @__arraycount(%struct.pollfd* %52)
%54 = icmp ult i64 %51, %53
br i1 %54, label %55, label %74
55: ; preds = %50
%56 = load i64, i64* %10
%57 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 %56
%58 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %57, i32 0, i32 1
%59 = load i32, i32* %58
%60 = load i32, i32* @POLLIN
%61 = and i32 %59, %60
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %55
br label %71
64: ; preds = %55
%65 = load i32, i32* %7
%66 = load i64, i64* %10
%67 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 %66
%68 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %67, i32 0, i32 2
%69 = load i32, i32* %68
%70 = call i32 @handle(i32 %65, i32 %69)
br label %71
71: ; preds = %64, %63
%72 = load i64, i64* %10
%73 = add i64 %72, 1
store i64 %73, i64* %10
br label %50
74: ; preds = %50
br label %41
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@SOCK_STREAM = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str = [4 x i8] c"up:\00"
@SOCK_DGRAM = global i32 0
@optarg = global i32 0
@AF_INET = global i32 0
@POLLIN = global i32 0
@.str.1 = [5 x i8] c"poll\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [1 x %struct.pollfd]
%4 = bitcast [1 x %struct.pollfd]* %3 to i8*
%5 = load i32, i32* @SOCK_STREAM
%6 = load i32, i32* @SIGCHLD
%7 = load i32, i32* @SIG_IGN
%8 = tail call i32 @signal(i32 %6, i32 %7)
br label %9
9: ; preds = %16, %2
%10 = phi i32 [ %17, %16 ], [ %5, %2 ]
%11 = phi i32 [ %13, %16 ], [ 6161, %2 ]
br label %12
12: ; preds = %9, %18
%13 = phi i32 [ %11, %9 ], [ %20, %18 ]
br label %14
14: ; preds = %12, %21
%15 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
switch i32 %15, label %21 [
i32 -1, label %23
i32 117, label %16
i32 112, label %18
]
16: ; preds = %14
%17 = load i32, i32* @SOCK_DGRAM
br label %9
18: ; preds = %14
%19 = load i32, i32* @optarg
%20 = tail call i32 @atoi(i32 %19)
br label %12
21: ; preds = %14
%22 = tail call i32 @usage(i32 %15)
br label %14
23: ; preds = %14
%24 = load i32, i32* @AF_INET
%25 = tail call i32 @cr(i32 %24, i32 %10, i32 %13)
%26 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0
%27 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
store i32 %25, i32* %27
%28 = load i32, i32* @POLLIN
%29 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
store i32 %28, i32* %29
%30 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
br label %31
31: ; preds = %40, %23
%32 = call i64 @__arraycount(%struct.pollfd* %26)
%33 = call i32 @poll(%struct.pollfd* %26, i64 %32, i32 -1)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
%38 = call i64 @__arraycount(%struct.pollfd* %26)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %50, %37
br label %31
41: ; preds = %37, %50
%42 = phi i64 [ %51, %50 ], [ 0, %37 ]
%43 = load i32, i32* %30
%44 = load i32, i32* @POLLIN
%45 = and i32 %44, %43
%46 = icmp eq i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %41
%48 = load i32, i32* %27
%49 = call i32 @handle(i32 %10, i32 %48)
br label %50
50: ; preds = %41, %47
%51 = add nuw i64 %42, 1
%52 = call i64 @__arraycount(%struct.pollfd* %26)
%53 = icmp ult i64 %51, %52
br i1 %53, label %41, label %40
}