#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 /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int getpid();
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int, ...);
int strlen(char *);
int write(int, char *, int);
doService(int fd) {
  int i = 0;
  char buff[80];
  char buff2[80];
  int ret;
  int socket_fd = (int)fd;
  ret = read(socket_fd, buff, sizeof(buff));
  while (ret > 0) {
    buff[ret] = '\0';
    sprintf(buff2, "Server [%d] received: %s\n", getpid(), buff);
    write(1, buff2, strlen(buff2));
    ret = write(fd, "caracola ", 8);
    if (ret < 0) {
      perror("Error writing to socket");
      exit(1);
    }
    ret = read(socket_fd, buff, sizeof(buff));
  }
  if (ret < 0) {
    perror("Error reading from socket");
  }
  sprintf(buff2, "Server [%d] ends service\n", getpid());
  write(1, buff2, strlen(buff2));
}


x86 assembly (clang, -O0):
<doService>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xb8(%rbp)
mov    -0xb8(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x50,%edx
call   1060 <read@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
jle    127a <doService+0x10a>
movslq -0xb4(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
lea    -0xb0(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0xc8(%rbp),%rdi
mov    %eax,%edx
lea    -0x60(%rbp),%rcx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0xb0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
lea    -0xb0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xc0(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    0xdf2(%rip),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
jge    125b <doService+0xeb>
lea    0xdd8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xb8(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x50,%edx
call   1060 <read@plt>
mov    %eax,-0xb4(%rbp)
jmp    11a8 <doService+0x38>
cmpl   $0x0,-0xb4(%rbp)
jge    1293 <doService+0x123>
lea    0xdae(%rip),%rdi
call   1070 <perror@plt>
lea    -0xb0(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0xd8(%rbp),%rdi
mov    %eax,%edx
lea    0xd9e(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0xb0(%rbp),%rax
mov    %rax,-0xd0(%rbp)
lea    -0xb0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xd0(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doService>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa0,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x50,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    120c <doService+0x9c>
lea    0xe66(%rip),%r14
lea    0x50(%rsp),%r15
mov    %rsp,%r12
lea    0xe71(%rip),%r13
nopl   0x0(%rax)
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
mov    %r12,%rcx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    $0x1,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1265 <doService+0xf5>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x50,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     11b0 <doService+0x40>
test   %eax,%eax
js     1257 <doService+0xe7>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe38(%rip),%rsi
lea    0x50(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
add    $0xa0,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdde(%rip),%rdi
call   1070 <perror@plt>
jmp    1210 <doService+0xa0>
lea    0xdb8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Server [%d] received: %s\0A\00"
@.str.1 = [10 x i8] c"caracola \00"
@.str.2 = [24 x i8] c"Error writing to socket\00"
@.str.3 = [26 x i8] c"Error reading from socket\00"
@.str.4 = [26 x i8] c"Server [%d] ends service\0A\00"
define i32 @doService(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [80 x i8]
%6 = alloca [80 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
%9 = load i32, i32* %3
store i32 %9, i32* %8
%10 = load i32, i32* %8
%11 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 80)
store i32 %12, i32* %7
br label %13
13: ; preds = %35, %1
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %39
16: ; preds = %13
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 %18
store i8 0, i8* %19
%20 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%21 = call i32 (...) @getpid()
%22 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%23 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %21, i8* %22)
%24 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%26 = call i32 @strlen(i8* %25)
%27 = call i32 @write(i32 1, i8* %24, i32 %26)
%28 = load i32, i32* %3
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 8)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %16
%33 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %16
%36 = load i32, i32* %8
%37 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%38 = call i32 @read(i32 %36, i8* %37, i32 80)
store i32 %38, i32* %7
br label %13
39: ; preds = %13
%40 = load i32, i32* %7
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
%45 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%46 = call i32 (...) @getpid()
%47 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %45, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%48 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%49 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @write(i32 1, i8* %48, i32 %50)
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
@.str = [26 x i8] c"Server [%d] received: %s\0A\00"
@.str.1 = [10 x i8] c"caracola \00"
@.str.2 = [24 x i8] c"Error writing to socket\00"
@.str.3 = [26 x i8] c"Error reading from socket\00"
@.str.4 = [26 x i8] c"Server [%d] ends service\0A\00"
define i32 @doService(i32 %0) {
%2 = alloca [80 x i8]
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%6 = call i32 @read(i32 %0, i8* %4, i32 80)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %24
8: ; preds = %1, %21
%9 = phi i32 [ %22, %21 ], [ %6, %1 ]
%10 = zext i32 %9 to i64
%11 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %10
store i8 0, i8* %11
%12 = call i32 (...) @getpid()
%13 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %4)
%14 = call i32 @strlen(i8* %5)
%15 = call i32 @write(i32 1, i8* %5, i32 %14)
%16 = call i32 @write(i32 %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 8)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %8
%19 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %8
%22 = call i32 @read(i32 %0, i8* %4, i32 80)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %8, label %24
24: ; preds = %21, %1
%25 = phi i32 [ %6, %1 ], [ %22, %21 ]
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = call i32 (...) @getpid()
%31 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = call i32 @strlen(i8* %5)
%33 = call i32 @write(i32 1, i8* %5, i32 %32)
ret i32 undef
}