#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*/ printf(char *, int, int, char *);
int read(int, char *, int);
scalar_t__ strlen(char *);
int read_msg(int clientfd) {
  int bytes_read;
  char message[256];
  int offset = 0;
  bytes_read = read(clientfd, message, sizeof message);
  if (bytes_read > 0) {
    message[bytes_read] = 0;
    while (offset < bytes_read) {
      printf("Client %d Message (%d): [%s]\n", clientfd, bytes_read,
             message + offset);
      offset += strlen(message + offset) + 1;
    }
  }
  return bytes_read;
}


x86 assembly (clang, -O0):
<read_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x114(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11de <read_msg+0xae>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
mov    -0x114(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11d9 <read_msg+0xa9>
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    -0x110(%rbp),%rcx
movslq -0x114(%rbp),%rax
add    %rax,%rcx
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
lea    -0x110(%rbp),%rdi
movslq -0x114(%rbp),%rax
add    %rax,%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
add    $0x1,%rcx
movslq -0x114(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x114(%rbp)
jmp    1175 <read_msg+0x45>
jmp    11de <read_msg+0xae>
mov    -0x8(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_msg>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %edi,%r15d
mov    %rsp,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    119a <read_msg+0x6a>
mov    %r12d,%eax
movb   $0x0,(%rsp,%rax,1)
xor    %eax,%eax
lea    0xe9a(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
movslq %eax,%rbp
lea    (%rsp,%rbp,1),%rbx
mov    %r14,%rdi
mov    %r15d,%esi
mov    %r12d,%edx
mov    %rbx,%rcx
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %ebp,%eax
add    $0x1,%eax
cmp    %r12d,%eax
jl     1170 <read_msg+0x40>
mov    %r12d,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Client %d Message (%d): [%s]\0A\00"
define i32 @read_msg(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %5
%6 = load i32, i32* %2
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = call i32 @read(i32 %6, i8* %7, i32 256)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %38
11: ; preds = %1
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %19, %11
%16 = load i32, i32* %5
%17 = load i32, i32* %3
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %37
19: ; preds = %15
%20 = load i32, i32* %2
%21 = load i32, i32* %3
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %21, i8* %25)
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = call i64 @strlen(i8* %30)
%32 = add nsw i64 %31, 1
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = add nsw i64 %34, %32
%36 = trunc i64 %35 to i32
store i32 %36, i32* %5
br label %15
37: ; preds = %15
br label %38
38: ; preds = %37, %1
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [30 x i8] c"Client %d Message (%d): [%s]\0A\00"
define i32 @read_msg(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 256)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %19
6: ; preds = %1
%7 = zext i32 %4 to i64
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %7
store i8 0, i8* %8
br label %9
9: ; preds = %6, %9
%10 = phi i32 [ 0, %6 ], [ %17, %9 ]
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %11
%13 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %4, i8* %12)
%14 = call i64 @strlen(i8* %12)
%15 = trunc i64 %14 to i32
%16 = add nsw i32 %10, 1
%17 = add i32 %16, %15
%18 = icmp slt i32 %17, %4
br i1 %18, label %9, label %19
19: ; preds = %9, %1
ret i32 %4
}