#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 */

/* Variables and functions */
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int);
scalar_t__ sendToLog(char *, char *, int);
scalar_t__ write(int, char *, int);
void serverReadWrite(int sock, char *ip, int port) {
  char buffer[256];
  bzero(buffer, 256);
  if (read(sock, buffer, 255) < 0) {
    error("ERROR reading from socket");
  }
  printf("TCP message received: %s\n", buffer);
  if (sendToLog(buffer, ip, port) < 0)
    error("ERROR sending to log server");
  if (write(sock, "I got your message", 18) < 0) {
    error("ERROR writing to socket");
  }
}


x86 assembly (clang, -O0):
<serverReadWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1080 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    11b0 <serverReadWrite+0x50>
lea    0xe55(%rip),%rdi
call   1060 <error@plt>
lea    -0x120(%rbp),%rsi
lea    0xe5c(%rip),%rdi
call   1040 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <sendToLog@plt>
cmp    $0x0,%rax
jge    11ec <serverReadWrite+0x8c>
lea    0xe4d(%rip),%rdi
call   1060 <error@plt>
mov    -0x4(%rbp),%edi
lea    0xe5a(%rip),%rsi
mov    $0x12,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1216 <serverReadWrite+0xb6>
lea    0xe52(%rip),%rdi
call   1060 <error@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serverReadWrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
call   1080 <bzero@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xff,%edx
call   1050 <read@plt>
test   %rax,%rax
jns    11a5 <serverReadWrite+0x45>
lea    0xe60(%rip),%rdi
call   1060 <error@plt>
lea    0xe6e(%rip),%rdi
mov    %rsp,%rbp
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1070 <sendToLog@plt>
test   %rax,%rax
jns    11d6 <serverReadWrite+0x76>
lea    0xe63(%rip),%rdi
call   1060 <error@plt>
lea    0xe73(%rip),%rsi
mov    %ebx,%edi
mov    $0x12,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11fa <serverReadWrite+0x9a>
lea    0xe6e(%rip),%rdi
call   1060 <error@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"ERROR reading from socket\00"
@.str.1 = [26 x i8] c"TCP message received: %s\0A\00"
@.str.2 = [28 x i8] c"ERROR sending to log server\00"
@.str.3 = [19 x i8] c"I got your message\00"
@.str.4 = [24 x i8] c"ERROR writing to socket\00"
define void @serverReadWrite(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [256 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%9 = call i32 @bzero(i8* %8, i32 256)
%10 = load i32, i32* %4
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%12 = call i64 @read(i32 %10, i8* %11, i32 255)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%18 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = call i64 @sendToLog(i8* %19, i8* %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %16
%27 = load i32, i32* %4
%28 = call i64 @write(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 18)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"ERROR reading from socket\00"
@.str.1 = [26 x i8] c"TCP message received: %s\0A\00"
@.str.2 = [28 x i8] c"ERROR sending to log server\00"
@.str.3 = [19 x i8] c"I got your message\00"
@.str.4 = [24 x i8] c"ERROR writing to socket\00"
define void @serverReadWrite(i32 %0, i8* %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = call i32 @bzero(i8* %5, i32 256)
%7 = call i64 @read(i32 %0, i8* %5, i32 255)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%13 = call i64 @sendToLog(i8* %5, i8* %1, i32 %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = call i64 @write(i32 %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 18)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
ret void
}