#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 scalar_t__ ssize_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getpid();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stdout;
scalar_t__ write(int, char *, size_t);
void echo(int sock) {
  char buf[256];
  ssize_t bytes_r, bytes_w;
  pid_t pid;
  pid = getpid();
  fprintf(stdout, "%d: echo\n", pid);
  memset(buf, 0, 256);
  while ((bytes_r = read(sock, buf, 256))) {
    fprintf(stdout, "%d: echoing data\n", pid);
    if (bytes_r < 0) {
      perror("socket read error");
      exit(EXIT_FAILURE);
    }
    bytes_w = write(sock, buf, (size_t)bytes_r);
    if (bytes_w < bytes_r) {
      perror("socket write error");
      exit(EXIT_FAILURE);
    }
  }
  fprintf(stdout, "%d: exiting...\n", pid);
}


x86 assembly (clang, -O0):
<echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x124(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x124(%rbp),%edx
lea    0xe5f(%rip),%rsi
call   1070 <fprintf@plt>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
je     1271 <echo+0x101>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    -0x124(%rbp),%edx
lea    0xe16(%rip),%rsi
call   1070 <fprintf@plt>
cmpq   $0x0,-0x118(%rbp)
jge    1221 <echo+0xb1>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x118(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rax
cmp    -0x118(%rbp),%rax
jge    126c <echo+0xfc>
lea    0xdd5(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    11b9 <echo+0x49>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    -0x124(%rbp),%edx
lea    0xdba(%rip),%rsi
call   1070 <fprintf@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %edi,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r14d
mov    0x2e44(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe61(%rip),%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    0xe4b(%rip),%r15
nop
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    0x0(%r13),%edi
test   %rax,%rax
je     1206 <echo+0x96>
mov    %rax,%rbp
mov    %r15,%rsi
mov    %r14d,%edx
call   1070 <fprintf@plt>
test   %rbp,%rbp
js     1227 <echo+0xb7>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    %rbp,%rax
jge    11c0 <echo+0x50>
lea    0xe2a(%rip),%rdi
jmp    122e <echo+0xbe>
lea    0xe34(%rip),%rsi
mov    %r14d,%edx
call   1070 <fprintf@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdee(%rip),%rdi
call   1080 <perror@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [10 x i8] c"%d: echo\0A\00"
@.str.1 = [18 x i8] c"%d: echoing data\0A\00"
@.str.2 = [18 x i8] c"socket read error\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [19 x i8] c"socket write error\00"
@.str.4 = [16 x i8] c"%d: exiting...\0A\00"
define void @echo(i32 %0) {
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %2
%7 = call i32 (...) @getpid()
store i32 %7, i32* %6
%8 = load i32, i32* @stdout
%9 = load i32, i32* %6
%10 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 256)
br label %13
13: ; preds = %40, %1
%14 = load i32, i32* %2
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%16 = call i64 @read(i32 %14, i8* %15, i32 256)
store i64 %16, i64* %4
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %41
18: ; preds = %13
%19 = load i32, i32* @stdout
%20 = load i32, i32* %6
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i64, i64* %4
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %18
%29 = load i32, i32* %2
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%31 = load i64, i64* %4
%32 = call i64 @write(i32 %29, i8* %30, i64 %31)
store i64 %32, i64* %5
%33 = load i64, i64* %5
%34 = load i64, i64* %4
%35 = icmp slt i64 %33, %34
br i1 %35, label %36, label %40
36: ; preds = %28
%37 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %28
br label %13
41: ; preds = %13
%42 = load i32, i32* @stdout
%43 = load i32, i32* %6
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %43)
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [10 x i8] c"%d: echo\0A\00"
@.str.1 = [18 x i8] c"%d: echoing data\0A\00"
@.str.2 = [18 x i8] c"socket read error\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [19 x i8] c"socket write error\00"
@.str.4 = [16 x i8] c"%d: exiting...\0A\00"
define void @echo(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (...) @getpid()
%5 = load i32, i32* @stdout
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
%7 = call i32 @memset(i8* %3, i32 0, i32 256)
br label %8
8: ; preds = %19, %1
%9 = call i64 @read(i32 %0, i8* %3, i32 256)
%10 = icmp eq i64 %9, 0
%11 = load i32, i32* @stdout
br i1 %10, label %26, label %12
12: ; preds = %8
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%14 = icmp slt i64 %9, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
%20 = call i64 @write(i32 %0, i8* %3, i64 %9)
%21 = icmp slt i64 %20, %9
br i1 %21, label %22, label %8
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %8
%27 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %4)
ret void
}