#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 */
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getline(char **, size_t *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ puts(char *);
scalar_t__ read(int, char **, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int write(int, char *, int);
bool listen_loop(int fd, char *local_handle) {
  char *line = NULL;
  size_t linecap = 0;
  ssize_t linelen;
  char buffer[512 + 1];
  char *message = (char *)&buffer + 10;
  bool done = false;
  write(fd, local_handle, 10);
  while (read(fd, &buffer, 512) > 0) {
    buffer[512] = '\0';
    printf("%s> %s\n", (char *)&buffer, message);
    printf("%s> ", local_handle);
    if (strcmp(message, "\\quit") == 0) {
      done = false;
      puts("they quit");
      break;
    }
    linelen = getline(&line, &linecap, stdin);
    line[linelen - 1] = '\0';
    if (linelen < 0) {
      fprintf(stderr, "End of input\n");
      strcpy((char *)&buffer, "\\quit");
      write(fd, line, 512);
      done = true;
      break;
    }
    if (linelen > 512) {
      fprintf(stderr, "message must be less than 500 characters\n");
      continue;
    }
    bzero(buffer, 512);
    strncpy(buffer, local_handle, 10);
    strncpy(buffer + 10, line, 512 - (10 + 1));
    if (write(fd, buffer, 512) == -1) {
      perror("writing message");
      exit(EXIT_FAILURE);
    }
    if (strcmp(message, "\\quit") == 0) {
      puts("we quit");
      done = true;
      break;
    }
  }
  close(fd);
  return done;
}


x86 assembly (clang, -O0):
<listen_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x230(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x238(%rbp)
movl   $0x0,-0x23c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1060 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jle    13fd <listen_loop+0x22d>
movb   $0x0,-0x30(%rbp)
lea    -0x230(%rbp),%rsi
mov    -0x238(%rbp),%rdx
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x238(%rbp),%rdi
lea    0xd90(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    12a7 <listen_loop+0xd7>
movl   $0x0,-0x23c(%rbp)
lea    0xd76(%rip),%rdi
call   1050 <puts@plt>
jmp    13fd <listen_loop+0x22d>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10e0 <getline@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x24(%rbp)
jge    1323 <listen_loop+0x153>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
lea    0xd32(%rip),%rsi
call   10b0 <fprintf@plt>
lea    -0x230(%rbp),%rdi
lea    0xd0f(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x200,%edx
call   1060 <write@plt>
movl   $0x1,-0x23c(%rbp)
jmp    13fd <listen_loop+0x22d>
cmpl   $0x200,-0x24(%rbp)
jle    134a <listen_loop+0x17a>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
lea    0xceb(%rip),%rsi
call   10b0 <fprintf@plt>
jmp    121f <listen_loop+0x4f>
lea    -0x230(%rbp),%rdi
mov    $0x200,%esi
call   10d0 <bzero@plt>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
lea    -0x230(%rbp),%rdi
add    $0xa,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x1f5,%edx
call   1030 <strncpy@plt>
mov    -0x4(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1060 <write@plt>
cmp    $0xffffffff,%eax
jne    13c0 <listen_loop+0x1f0>
lea    0xca8(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c17(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x238(%rbp),%rdi
lea    0xc3f(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    13f8 <listen_loop+0x228>
lea    0xc81(%rip),%rdi
call   1050 <puts@plt>
movl   $0x1,-0x23c(%rbp)
jmp    13fd <listen_loop+0x22d>
jmp    121f <listen_loop+0x4f>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
mov    -0x23c(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    $0xa,%edx
call   1060 <write@plt>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x200,%edx
call   1090 <read@plt>
test   %rax,%rax
jle    1357 <listen_loop+0x187>
lea    0x1a(%rsp),%r12
lea    0x10(%rsp),%rbp
lea    0xddf(%rip),%r15
lea    0x8(%rsp),%r13
jmp    126e <listen_loop+0x9e>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
call   10b0 <fprintf@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1090 <read@plt>
test   %rax,%rax
jle    1357 <listen_loop+0x187>
movb   $0x0,0x210(%rsp)
lea    0xd83(%rip),%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd77(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     134b <listen_loop+0x17b>
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    %r13,%rsi
call   10e0 <getline@plt>
mov    (%rsp),%rcx
movslq %eax,%rdx
movb   $0x0,-0x1(%rdx,%rcx,1)
test   %edx,%edx
js     135b <listen_loop+0x18b>
cmp    $0x201,%eax
jae    1240 <listen_loop+0x70>
mov    %rbp,%rdi
mov    $0x200,%esi
call   10d0 <bzero@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
mov    (%rsp),%rsi
mov    %r12,%rdi
mov    $0x1f5,%edx
call   1030 <strncpy@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1060 <write@plt>
cmp    $0xffffffff,%eax
je     13b3 <listen_loop+0x1e3>
mov    %r12,%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
jne    1255 <listen_loop+0x85>
lea    0xd21(%rip),%rdi
call   1050 <puts@plt>
jmp    1392 <listen_loop+0x1c2>
lea    0xcc1(%rip),%rdi
call   1050 <puts@plt>
xor    %ebx,%ebx
jmp    1397 <listen_loop+0x1c7>
mov    0x2c76(%rip),%rax
mov    (%rax),%edi
lea    0xcb2(%rip),%rsi
call   10b0 <fprintf@plt>
lea    0xc96(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1040 <strcpy@plt>
mov    (%rsp),%rsi
mov    %r14d,%edi
mov    $0x200,%edx
call   1060 <write@plt>
mov    $0x1,%ebx
mov    %r14d,%edi
call   1080 <close@plt>
mov    %ebx,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc9b(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c0a(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"%s> %s\0A\00"
@.str.1 = [5 x i8] c"%s> \00"
@.str.2 = [6 x i8] c"\\quit\00"
@.str.3 = [10 x i8] c"they quit\00"
@stdin = global i32 0
@stderr = global i32 0
@.str.4 = [14 x i8] c"End of input\0A\00"
@.str.5 = [42 x i8] c"message must be less than 500 characters\0A\00"
@.str.6 = [16 x i8] c"writing message\00"
@EXIT_FAILURE = global i32 0
@.str.7 = [8 x i8] c"we quit\00"
define i32 @listen_loop(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca [513 x i8]
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i8* null, i8** %5
store i64 0, i64* %6
%11 = bitcast [513 x i8]* %8 to i8*
%12 = getelementptr inbounds i8, i8* %11, i64 10
store i8* %12, i8** %9
store i32 0, i32* %10
%13 = load i32, i32* %3
%14 = load i8*, i8** %4
%15 = call i32 @write(i32 %13, i8* %14, i32 10)
br label %16
16: ; preds = %81, %54, %2
%17 = load i32, i32* %3
%18 = bitcast [513 x i8]* %8 to i8**
%19 = call i64 @read(i32 %17, i8** %18, i32 512)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %82
21: ; preds = %16
%22 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 512
store i8 0, i8* %22
%23 = bitcast [513 x i8]* %8 to i8*
%24 = load i8*, i8** %9
%25 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %23, i8* %24)
%26 = load i8*, i8** %4
%27 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i8*, i8** %9
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %21
store i32 0, i32* %10
%32 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %82
33: ; preds = %21
%34 = load i32, i32* @stdin
%35 = call i32 @getline(i8** %5, i64* %6, i32 %34)
store i32 %35, i32* %7
%36 = load i8*, i8** %5
%37 = load i32, i32* %7
%38 = sub nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %51
43: ; preds = %33
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%46 = bitcast [513 x i8]* %8 to i8*
%47 = call i32 @strcpy(i8* %46, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* %3
%49 = load i8*, i8** %5
%50 = call i32 @write(i32 %48, i8* %49, i32 512)
store i32 1, i32* %10
br label %82
51: ; preds = %33
%52 = load i32, i32* %7
%53 = icmp sgt i32 %52, 512
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
br label %16
57: ; preds = %51
%58 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 0
%59 = call i32 @bzero(i8* %58, i32 512)
%60 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 0
%61 = load i8*, i8** %4
%62 = call i32 @strncpy(i8* %60, i8* %61, i32 10)
%63 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 0
%64 = getelementptr inbounds i8, i8* %63, i64 10
%65 = load i8*, i8** %5
%66 = call i32 @strncpy(i8* %64, i8* %65, i32 501)
%67 = load i32, i32* %3
%68 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 0
%69 = call i32 @write(i32 %67, i8* %68, i32 512)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %75
71: ; preds = %57
%72 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %57
%76 = load i8*, i8** %9
%77 = call i64 @strcmp(i8* %76, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%78 = icmp eq i64 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %75
%80 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %10
br label %82
81: ; preds = %75
br label %16
82: ; preds = %79, %43, %31, %16
%83 = load i32, i32* %3
%84 = call i32 @close(i32 %83)
%85 = load i32, i32* %10
ret i32 %85
}

LLVM IR (-O2):
@.str = [8 x i8] c"%s> %s\0A\00"
@.str.1 = [5 x i8] c"%s> \00"
@.str.2 = [6 x i8] c"\\quit\00"
@.str.3 = [10 x i8] c"they quit\00"
@stdin = global i32 0
@stderr = global i32 0
@.str.4 = [14 x i8] c"End of input\0A\00"
@.str.5 = [42 x i8] c"message must be less than 500 characters\0A\00"
@.str.6 = [16 x i8] c"writing message\00"
@EXIT_FAILURE = global i32 0
@.str.7 = [8 x i8] c"we quit\00"
define i32 @listen_loop(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [513 x i8]
%6 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%7 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%8 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 10
%10 = tail call i32 @write(i32 %0, i8* %1, i32 10)
%11 = bitcast [513 x i8]* %5 to i8**
%12 = call i64 @read(i32 %0, i8** %11, i32 512)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %61
14: ; preds = %2
%15 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 512
br label %16
16: ; preds = %14, %42
store i8 0, i8* %15
%17 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %9)
%18 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%19 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %61
23: ; preds = %16
%24 = load i32, i32* @stdin
%25 = call i32 @getline(i8** %3, i64* %4, i32 %24)
%26 = load i8*, i8** %3
%27 = add nsw i32 %25, -1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = icmp slt i32 %25, 0
br i1 %30, label %31, label %37
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%35 = load i8*, i8** %3
%36 = call i32 @write(i32 %0, i8* %35, i32 512)
br label %61
37: ; preds = %23
%38 = icmp ugt i32 %25, 512
br i1 %38, label %39, label %45
39: ; preds = %37
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %39, %56
%43 = call i64 @read(i32 %0, i8** %11, i32 512)
%44 = icmp sgt i64 %43, 0
br i1 %44, label %16, label %61
45: ; preds = %37
%46 = call i32 @bzero(i8* %8, i32 512)
%47 = call i32 @strncpy(i8* %8, i8* %1, i32 10)
%48 = load i8*, i8** %3
%49 = call i32 @strncpy(i8* %9, i8* %48, i32 501)
%50 = call i32 @write(i32 %0, i8* %8, i32 512)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %56
52: ; preds = %45
%53 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %45
%57 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %42
59: ; preds = %56
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %61
61: ; preds = %42, %2, %59, %31, %21
%62 = phi i32 [ 0, %21 ], [ 1, %31 ], [ 1, %59 ], [ 0, %2 ], [ 0, %42 ]
%63 = call i32 @close(i32 %0)
ret i32 %62
}