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

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ exit(int);
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int recv(int /*<<< orphan*/, char *, int, int /*<<< orphan*/);
int recvfrom(int /*<<< orphan*/, char *, int, int /*<<< orphan*/,
             struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int main(int argc, char **argv) {
  char buf[1000];
  struct sockaddr addr;
  socklen_t addrlen;
  int len;
  printf("[%d] %s, %d\n", getpid(), argv[0], argc);
  while (1) {
    if (strcmp("stream", argv[1])) {
      if ((len = recvfrom(STDERR_FILENO, buf, 999, 0, (struct sockaddr *)&addr,
                          &addrlen)) == -1)
        exit(-1);
    } else {
      if ((len = recv(STDERR_FILENO, buf, 999, 0)) == -1)
        exit(-1);
    }
    buf[len] = 0;
    if ((len = strlen(buf))) {
      while (buf[--len] == '\n' || buf[len] == '\r')
        ;
      buf[len + 1] = 0;
    }
    if (!strcmp("quit", buf))
      break;
    printf(">> %s\n", buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x8(%rbp),%ecx
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe54(%rip),%rdi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1210 <main+0xa0>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    -0x400(%rbp),%rsi
mov    $0x3e7,%edx
xor    %ecx,%ecx
lea    -0x408(%rbp),%r8
lea    -0x40c(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x410(%rbp)
cmp    $0xffffffff,%eax
jne    120b <main+0x9b>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    124a <main+0xda>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    -0x400(%rbp),%rsi
mov    $0x3e7,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x410(%rbp)
cmp    $0xffffffff,%eax
jne    1245 <main+0xd5>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    124a <main+0xda>
movslq -0x410(%rbp),%rax
movb   $0x0,-0x400(%rbp,%rax,1)
lea    -0x400(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x410(%rbp)
cmp    $0x0,%eax
je     12e9 <main+0x179>
jmp    1279 <main+0x109>
mov    -0x410(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x410(%rbp)
cltq
movsbl -0x400(%rbp,%rax,1),%ecx
ff
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0x411(%rbp)
je     12be <main+0x14e>
movslq -0x410(%rbp),%rax
movsbl -0x400(%rbp,%rax,1),%eax
ff
cmp    $0xd,%eax
sete   %al
mov    %al,-0x411(%rbp)
mov    -0x411(%rbp),%al
test   $0x1,%al
jne    12d1 <main+0x161>
jmp    12d6 <main+0x166>
jmp    1279 <main+0x109>
mov    -0x410(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0x0,-0x400(%rbp,%rax,1)
lea    -0x400(%rbp),%rsi
lea    0xd1d(%rip),%rdi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    130b <main+0x19b>
jmp    1325 <main+0x1b5>
lea    -0x400(%rbp),%rsi
lea    0xd00(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    11aa <main+0x3a>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f8,%rsp
mov    %rsi,%r15
mov    %edi,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    (%r15),%rdx
lea    0xe69(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe64(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xe5f(%rip),%r12
lea    0xe5d(%rip),%r13
movabs $0xffffffff00000000,%rbp
ff ff ff
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%r15),%rsi
mov    %r14,%rdi
call   1070 <strcmp@plt>
mov    0x2dfd(%rip),%rcx
mov    (%rcx),%edi
mov    %rbx,%rsi
mov    $0x3e7,%edx
xor    %ecx,%ecx
test   %rax,%rax
je     1210 <main+0xa0>
lea    0x8(%rsp),%r8
lea    0xc(%rsp),%r9
call   1080 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    121a <main+0xaa>
jmp    1293 <main+0x123>
nopl   (%rax)
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1293 <main+0x123>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     125d <main+0xed>
cltq
lea    0xf(%rsp),%rcx
add    %rax,%rcx
shl    $0x20,%rax
jmp    1247 <main+0xd7>
nopl   (%rax)
add    %rbp,%rax
add    $0xffffffffffffffff,%rcx
movzbl (%rcx),%edx
cmp    $0xd,%dl
je     1240 <main+0xd0>
cmp    $0xa,%dl
je     1240 <main+0xd0>
sar    $0x20,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     127f <main+0x10f>
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11d0 <main+0x60>
xor    %eax,%eax
add    $0x3f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"[%d] %s, %d\0A\00"
@.str.1 = [7 x i8] c"stream\00"
@STDERR_FILENO = global i32 0
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [7 x i8] c">> %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1000 x i8]
%7 = alloca %struct.sockaddr
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (...) @getpid()
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = load i32, i32* %4
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10, i8* %13, i32 %14)
br label %16
16: ; preds = %2, %74
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = load i32, i32* @STDERR_FILENO
%24 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%25 = call i32 @recvfrom(i32 %23, i8* %24, i32 999, i32 0, %struct.sockaddr* %7, i32* %8)
store i32 %25, i32* %9
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %22
br label %38
30: ; preds = %16
%31 = load i32, i32* @STDERR_FILENO
%32 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%33 = call i32 @recv(i32 %31, i8* %32, i32 999, i32 0)
store i32 %33, i32* %9
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %30
br label %38
38: ; preds = %37, %29
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 %40
store i8 0, i8* %41
%42 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
store i32 %43, i32* %9
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %69
45: ; preds = %38
br label %46
46: ; preds = %63, %45
%47 = load i32, i32* %9
%48 = add nsw i32 %47, -1
store i32 %48, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 10
br i1 %53, label %61, label %54
54: ; preds = %46
%55 = load i32, i32* %9
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 %56
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 13
br label %61
61: ; preds = %54, %46
%62 = phi i1 [ true, %46 ], [ %60, %54 ]
br i1 %62, label %63, label %64
63: ; preds = %61
br label %46
64: ; preds = %61
%65 = load i32, i32* %9
%66 = add nsw i32 %65, 1
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 %67
store i8 0, i8* %68
br label %69
69: ; preds = %64, %38
%70 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%71 = call i64 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %74, label %73
73: ; preds = %69
br label %77
74: ; preds = %69
%75 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %75)
br label %16
77: ; preds = %73
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"[%d] %s, %d\0A\00"
@.str.1 = [7 x i8] c"stream\00"
@STDERR_FILENO = global i32 0
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [7 x i8] c">> %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1000 x i8]
%4 = alloca %struct.sockaddr
%5 = alloca i32
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sockaddr* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = tail call i32 (...) @getpid()
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %10, i32 %0)
%12 = getelementptr inbounds i8*, i8** %1, i64 1
br label %13
13: ; preds = %49, %2
%14 = load i8*, i8** %12
%15 = call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = icmp eq i64 %15, 0
%17 = load i32, i32* @STDERR_FILENO
br i1 %16, label %23, label %18
18: ; preds = %13
%19 = call i32 @recvfrom(i32 %17, i8* %6, i32 999, i32 0, %struct.sockaddr* %4, i32* %5)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %13
%24 = call i32 @recv(i32 %17, i8* %6, i32 999, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %23, %18
%29 = phi i32 [ %19, %18 ], [ %24, %23 ]
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 %30
store i8 0, i8* %31
%32 = call i32 @strlen(i8* %6)
%33 = icmp eq i32 %32, 0
br i1 %33, label %46, label %34
34: ; preds = %28
%35 = sext i32 %32 to i64
br label %36
36: ; preds = %41, %34
%37 = phi i64 [ %35, %34 ], [ %38, %41 ]
%38 = add i64 %37, -1
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 %38
%40 = load i8, i8* %39
switch i8 %40, label %42 [
i8 10, label %41
i8 13, label %41
]
41: ; preds = %36, %36
br label %36
42: ; preds = %36
%43 = shl i64 %37, 32
%44 = ashr exact i64 %43, 32
%45 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 %44
store i8 0, i8* %45
br label %46
46: ; preds = %42, %28
%47 = call i64 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%48 = icmp eq i64 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %46
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %13
51: ; preds = %46
ret i32 0
}