#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 timeval {
  int dummy;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ fsin;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ daytime(char *);
int /*<<< orphan*/ errexit(char *, ...);
int /*<<< orphan*/ errno;
int passiveTCP(char *, int);
int passiveUDP(char *);
scalar_t__ recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
                    unsigned int *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sockaddr *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char *service = "daytime";
  char buf[128 + 1];
  struct sockaddr_in fsin;
  unsigned int alen;
  int tsock;
  int usock;
  int nfds;
  fd_set rfds;
  switch (argc) {
  case 1:
    break;
  case 2:
    service = argv[1];
    break;
  default:
    errexit("usage: daytimed [port]\n");
  }
  tsock = passiveTCP(service, 32);
  usock = passiveUDP(service);
  nfds = ((tsock) > (usock) ? (tsock) : (usock)) + 1;
  FD_ZERO(&rfds);
  while (1) {
    FD_SET(tsock, &rfds);
    FD_SET(usock, &rfds);
    if (select(nfds, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0) < 0)
      errexit("select error: %s\n", strerror(errno));
    if (FD_ISSET(tsock, &rfds)) {
      int ssock;
      alen = sizeof(fsin);
      ssock = accept(tsock, (struct sockaddr *)&fsin, &alen);
      if (ssock < 0)
        errexit("accept failed: %s\n", strerror(errno));
      daytime(buf);
      (void)write(ssock, buf, strlen(buf));
      (void)close(ssock);
    }
    if (FD_ISSET(usock, &rfds)) {
      alen = sizeof(fsin);
      if (recvfrom(usock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin,
                   &alen) < 0)
        errexit("recvfrom: %s\n", strerror(errno));
      daytime(buf);
      (void)sendto(usock, buf, strlen(buf), 0, (struct sockaddr *)&fsin,
                   sizeof(fsin));
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xdf0(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc4(%rbp)
sub    $0x1,%eax
je     123f <main+0x4f>
jmp    122b <main+0x3b>
mov    -0xc4(%rbp),%eax
sub    $0x2,%eax
je     1244 <main+0x54>
jmp    1255 <main+0x65>
jmp    1263 <main+0x73>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1263 <main+0x73>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1080 <errexit@plt>
mov    -0x18(%rbp),%rdi
mov    $0x20,%esi
call   10f0 <passiveTCP@plt>
mov    %eax,-0xb0(%rbp)
mov    -0x18(%rbp),%rdi
call   10d0 <passiveUDP@plt>
mov    %eax,-0xb4(%rbp)
mov    -0xb0(%rbp),%eax
cmp    -0xb4(%rbp),%eax
jle    12a9 <main+0xb9>
mov    -0xb0(%rbp),%eax
mov    %eax,-0xc8(%rbp)
jmp    12b5 <main+0xc5>
mov    -0xb4(%rbp),%eax
mov    %eax,-0xc8(%rbp)
mov    -0xc8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb8(%rbp)
lea    -0xbc(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0xb0(%rbp),%edi
lea    -0xbc(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0xb4(%rbp),%edi
lea    -0xbc(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0xb8(%rbp),%edi
lea    -0xbc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10b0 <select@plt>
cmp    $0x0,%rax
jge    1339 <main+0x149>
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    %eax,%esi
lea    0xcee(%rip),%rdi
mov    $0x0,%al
call   1080 <errexit@plt>
mov    -0xb0(%rbp),%edi
lea    -0xbc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13fa <main+0x20a>
movl   $0x4,-0xac(%rbp)
mov    -0xb0(%rbp),%edi
lea    -0xa8(%rbp),%rsi
lea    -0xac(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0xc0(%rbp)
cmpl   $0x0,-0xc0(%rbp)
jge    13a9 <main+0x1b9>
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    %eax,%esi
lea    0xc90(%rip),%rdi
mov    $0x0,%al
call   1080 <errexit@plt>
lea    -0xa0(%rbp),%rdi
call   1090 <daytime@plt>
mov    -0xc0(%rbp),%eax
mov    %eax,-0xd4(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xd0(%rbp)
lea    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xd4(%rbp),%edi
mov    -0xd0(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0xc0(%rbp),%edi
call   1070 <close@plt>
mov    -0xb4(%rbp),%edi
lea    -0xbc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     14c4 <main+0x2d4>
movl   $0x4,-0xac(%rbp)
mov    -0xb4(%rbp),%edi
lea    -0xa0(%rbp),%rsi
lea    -0xa8(%rbp),%r8
mov    $0x81,%edx
xor    %ecx,%ecx
lea    -0xac(%rbp),%r9
call   10c0 <recvfrom@plt>
cmp    $0x0,%rax
jge    146f <main+0x27f>
mov    0x2b88(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    %eax,%esi
lea    0xbdd(%rip),%rdi
mov    $0x0,%al
call   1080 <errexit@plt>
lea    -0xa0(%rbp),%rdi
call   1090 <daytime@plt>
mov    -0xb4(%rbp),%eax
mov    %eax,-0xe4(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xe0(%rbp)
lea    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xe4(%rbp),%edi
mov    -0xe0(%rbp),%rsi
mov    %eax,%edx
lea    -0xa8(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1060 <sendto@plt>
jmp    12d0 <main+0xe0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
lea    0xdf8(%rip),%rbp
cmp    $0x1,%edi
je     1226 <main+0x36>
cmp    $0x2,%edi
jne    1218 <main+0x28>
mov    0x8(%rsi),%rbp
jmp    1226 <main+0x36>
lea    0xde9(%rip),%rdi
xor    %eax,%eax
call   1080 <errexit@plt>
mov    %rbp,%rdi
mov    $0x20,%esi
call   10f0 <passiveTCP@plt>
mov    %eax,%r13d
mov    %rbp,%rdi
call   10d0 <passiveUDP@plt>
mov    %eax,%r15d
cmp    %eax,%r13d
cmovg  %r13d,%eax
add    $0x1,%eax
mov    %eax,0x4(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <FD_ZERO@plt>
lea    0x10(%rsp),%rbp
lea    0x8(%rsp),%r14
jmp    1298 <main+0xa8>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1090 <daytime@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0x4,%r9d
call   1060 <sendto@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1110 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1110 <FD_SET@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
test   %rax,%rax
jns    12e4 <main+0xf4>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xd45(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <errexit@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     135f <main+0x16f>
movl   $0x4,(%rsp)
mov    %r13d,%r12d
mov    %r13d,%edi
mov    %r14,%r13
mov    %r14,%rsi
mov    %rsp,%rdx
call   10e0 <accept@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1334 <main+0x144>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xd07(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <errexit@plt>
mov    %rbp,%rdi
call   1090 <daytime@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %r13,%r14
mov    %r12d,%r13d
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1298 <main+0xa8>
movl   $0x4,(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x81,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %rsp,%r9
call   10c0 <recvfrom@plt>
test   %rax,%rax
jns    1270 <main+0x80>
mov    0x2c3e(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xc95(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <errexit@plt>
jmp    1270 <main+0x80>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"daytime\00"
@.str.1 = [24 x i8] c"usage: daytimed [port]\0A\00"
@.str.2 = [18 x i8] c"select error: %s\0A\00"
@errno = global i32 0
@.str.3 = [19 x i8] c"accept failed: %s\0A\00"
@.str.4 = [14 x i8] c"recvfrom: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [129 x i8]
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %6
%15 = load i32, i32* %4
switch i32 %15, label %21 [
i32 1, label %16
i32 2, label %17
]
16: ; preds = %2
br label %23
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %6
br label %23
21: ; preds = %2
%22 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %17, %16
%24 = load i8*, i8** %6
%25 = call i32 @passiveTCP(i8* %24, i32 32)
store i32 %25, i32* %10
%26 = load i8*, i8** %6
%27 = call i32 @passiveUDP(i8* %26)
store i32 %27, i32* %11
%28 = load i32, i32* %10
%29 = load i32, i32* %11
%30 = icmp sgt i32 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = load i32, i32* %10
br label %35
33: ; preds = %23
%34 = load i32, i32* %11
br label %35
35: ; preds = %33, %31
%36 = phi i32 [ %32, %31 ], [ %34, %33 ]
%37 = add nsw i32 %36, 1
store i32 %37, i32* %12
%38 = call i32 @FD_ZERO(i32* %13)
br label %39
39: ; preds = %35, %98
%40 = load i32, i32* %10
%41 = call i32 @FD_SET(i32 %40, i32* %13)
%42 = load i32, i32* %11
%43 = call i32 @FD_SET(i32 %42, i32* %13)
%44 = load i32, i32* %12
%45 = call i64 @select(i32 %44, i32* %13, i32* null, i32* null, %struct.timeval* null)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %39
%48 = load i32, i32* @errno
%49 = call i32 @strerror(i32 %48)
%50 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %47, %39
%52 = load i32, i32* %10
%53 = call i64 @FD_ISSET(i32 %52, i32* %13)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %75
55: ; preds = %51
store i32 4, i32* %9
%56 = load i32, i32* %10
%57 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%58 = call i32 @accept(i32 %56, %struct.sockaddr* %57, i32* %9)
store i32 %58, i32* %14
%59 = load i32, i32* %14
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %55
%62 = load i32, i32* @errno
%63 = call i32 @strerror(i32 %62)
%64 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %63)
br label %65
65: ; preds = %61, %55
%66 = getelementptr inbounds [129 x i8], [129 x i8]* %7, i64 0, i64 0
%67 = call i32 @daytime(i8* %66)
%68 = load i32, i32* %14
%69 = getelementptr inbounds [129 x i8], [129 x i8]* %7, i64 0, i64 0
%70 = getelementptr inbounds [129 x i8], [129 x i8]* %7, i64 0, i64 0
%71 = call i32 @strlen(i8* %70)
%72 = call i32 @write(i32 %68, i8* %69, i32 %71)
%73 = load i32, i32* %14
%74 = call i32 @close(i32 %73)
br label %75
75: ; preds = %65, %51
%76 = load i32, i32* %11
%77 = call i64 @FD_ISSET(i32 %76, i32* %13)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %98
79: ; preds = %75
store i32 4, i32* %9
%80 = load i32, i32* %11
%81 = getelementptr inbounds [129 x i8], [129 x i8]* %7, i64 0, i64 0
%82 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%83 = call i64 @recvfrom(i32 %80, i8* %81, i32 129, i32 0, %struct.sockaddr* %82, i32* %9)
%84 = icmp slt i64 %83, 0
br i1 %84, label %85, label %89
85: ; preds = %79
%86 = load i32, i32* @errno
%87 = call i32 @strerror(i32 %86)
%88 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %87)
br label %89
89: ; preds = %85, %79
%90 = getelementptr inbounds [129 x i8], [129 x i8]* %7, i64 0, i64 0
%91 = call i32 @daytime(i8* %90)
%92 = load i32, i32* %11
%93 = getelementptr inbounds [129 x i8], [129 x i8]* %7, i64 0, i64 0
%94 = getelementptr inbounds [129 x i8], [129 x i8]* %7, i64 0, i64 0
%95 = call i32 @strlen(i8* %94)
%96 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%97 = call i32 @sendto(i32 %92, i8* %93, i32 %95, i32 0, %struct.sockaddr* %96, i32 4)
br label %98
98: ; preds = %89, %75
br label %39
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32 }
@.str = [8 x i8] c"daytime\00"
@.str.1 = [24 x i8] c"usage: daytimed [port]\0A\00"
@.str.2 = [18 x i8] c"select error: %s\0A\00"
@errno = global i32 0
@.str.3 = [19 x i8] c"accept failed: %s\0A\00"
@.str.4 = [14 x i8] c"recvfrom: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [129 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = getelementptr inbounds [129 x i8], [129 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
switch i32 %0, label %14 [
i32 1, label %16
i32 2, label %11
]
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
br label %16
14: ; preds = %2
%15 = tail call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %11, %2
%17 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %14 ], [ %13, %11 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %2 ]
%18 = tail call i32 @passiveTCP(i8* %17, i32 32)
%19 = tail call i32 @passiveUDP(i8* %17)
%20 = icmp sgt i32 %18, %19
%21 = select i1 %20, i32 %18, i32 %19
%22 = add nsw i32 %21, 1
%23 = call i32 @FD_ZERO(i32* %6)
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %25
25: ; preds = %63, %16
%26 = call i32 @FD_SET(i32 %18, i32* %6)
%27 = call i32 @FD_SET(i32 %19, i32* %6)
%28 = call i64 @select(i32 %22, i32* %6, i32* null, i32* null, %struct.timeval* null)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %30, %25
%35 = call i64 @FD_ISSET(i32 %18, i32* %6)
%36 = icmp eq i64 %35, 0
br i1 %36, label %49, label %37
37: ; preds = %34
store i32 4, i32* %5
%38 = call i32 @accept(i32 %18, %struct.sockaddr* %24, i32* %5)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %40, %37
%45 = call i32 @daytime(i8* %7)
%46 = call i32 @strlen(i8* %7)
%47 = call i32 @write(i32 %38, i8* %7, i32 %46)
%48 = call i32 @close(i32 %38)
br label %49
49: ; preds = %44, %34
%50 = call i64 @FD_ISSET(i32 %19, i32* %6)
%51 = icmp eq i64 %50, 0
br i1 %51, label %63, label %52
52: ; preds = %49
store i32 4, i32* %5
%53 = call i64 @recvfrom(i32 %19, i8* %7, i32 129, i32 0, %struct.sockaddr* %24, i32* %5)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = load i32, i32* @errno
%57 = call i32 @strerror(i32 %56)
%58 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %55, %52
%60 = call i32 @daytime(i8* %7)
%61 = call i32 @strlen(i8* %7)
%62 = call i32 @sendto(i32 %19, i8* %7, i32 %61, i32 0, %struct.sockaddr* %24, i32 4)
br label %63
63: ; preds = %59, %49
br label %25
}