#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 const);
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 read(int const, char **, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int write(int const, char *, int);
int read_loop(const int fd, char *local_handle, char *remote_handle) {
  char *line = NULL;
  size_t linecap = 0;
  ssize_t linelen;
  char buffer[512 + 1];
  char *message = (char *)&buffer + 10;
  printf("%s> ", local_handle);
  while ((linelen = getline(&line, &linecap, stdin)) > 0) {
    line[linelen - 1] = '\0';
    if (strcmp(line, "\\quit") == 0) {
      break;
    }
    bzero(buffer, 512);
    strncpy(buffer, local_handle, 10);
    strncpy(message, line, 512 - (10 + 1));
    if (linelen > 512) {
      fprintf(stderr, "message must be less than 500 characters\n");
      continue;
    }
    if (write(fd, buffer, 512) == -1) {
      perror("writing message");
      exit(EXIT_FAILURE);
    }
    if (read(fd, &buffer, 512) == -1) {
      perror("reading message");
      exit(EXIT_FAILURE);
    }
    printf("%s> %s\n", remote_handle, message);
    if (strcmp(message, "\\quit") == 0) {
      break;
    }
    printf("%s> ", local_handle);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<read_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x230(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x238(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10c0 <getline@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    136e <read_loop+0x1be>
mov    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
lea    0xdcd(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    124c <read_loop+0x9c>
jmp    136e <read_loop+0x1be>
lea    -0x230(%rbp),%rdi
mov    $0x200,%esi
call   10b0 <bzero@plt>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
mov    -0x238(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x1f5,%edx
call   1030 <strncpy@plt>
cmpl   $0x200,-0x2c(%rbp)
jle    12ae <read_loop+0xfe>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
call   1090 <fprintf@plt>
jmp    11fa <read_loop+0x4a>
mov    -0x4(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    12e5 <read_loop+0x135>
lea    0xd63(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
jne    131c <read_loop+0x16c>
lea    0xd3c(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%rsi
mov    -0x238(%rbp),%rdx
lea    0xd27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x238(%rbp),%rdi
lea    0xcc2(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1357 <read_loop+0x1a7>
jmp    136e <read_loop+0x1be>
mov    -0x10(%rbp),%rsi
lea    0xc9e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11fa <read_loop+0x4a>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
mov    %rdx,0x18(%rsp)
mov    %rsi,%rbx
mov    %edi,%r13d
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe1b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10c0 <getline@plt>
test   %eax,%eax
jle    132d <read_loop+0x17d>
mov    %eax,%r14d
lea    0x2a(%rsp),%r15
lea    0xdea(%rip),%r12
lea    0x20(%rsp),%rbp
jmp    1268 <read_loop+0xb8>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10c0 <getline@plt>
mov    %eax,%r14d
test   %eax,%eax
jle    132d <read_loop+0x17d>
mov    0x8(%rsp),%rax
lea    -0x1(%r14),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     132d <read_loop+0x17d>
mov    %rbp,%rdi
mov    $0x200,%esi
call   10b0 <bzero@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
mov    0x8(%rsp),%rsi
mov    %r15,%rdi
mov    $0x1f5,%edx
call   1030 <strncpy@plt>
cmp    $0x201,%r14d
jae    1230 <read_loop+0x80>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1349 <read_loop+0x199>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1352 <read_loop+0x1a2>
lea    0xd5d(%rip),%rdi
mov    0x18(%rsp),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     132d <read_loop+0x17d>
lea    0xce2(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1245 <read_loop+0x95>
mov    %r13d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xce5(%rip),%rdi
jmp    1359 <read_loop+0x1a9>
lea    0xcec(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c6b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

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

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