#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int /*<<< orphan*/ read_int(int, int *);
unsigned read_string(int socket, char string[]) {
  int length;
  read_int(socket, &length);
  int rvalue = read(socket, string, length);
  if (rvalue == 0) {
    if (true) {
      printf("DEBUG: ");
      puts("read_string - no process has the pipe open for writing");
    };
  } else if (rvalue == -1) {
    perror("read_string - Failed to read string");
    exit(EXIT_FAILURE);
  }
  return rvalue;
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1060 <read_int@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11b4 <read_string+0x54>
lea    0xe62(%rip),%rdi
call   1040 <printf@plt>
lea    0xe5e(%rip),%rdi
call   1030 <puts@plt>
jmp    11dd <read_string+0x7d>
cmpl   $0xffffffff,-0x18(%rbp)
jne    11d8 <read_string+0x78>
lea    0xe7a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11dd <read_string+0x7d>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x4(%rsp),%rsi
call   1060 <read_int@plt>
mov    0x4(%rsp),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11a5 <read_string+0x45>
cmp    $0xffffffff,%ebx
jne    11bd <read_string+0x5d>
lea    0xead(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"DEBUG: \00"
@.str.1 = [55 x i8] c"read_string - no process has the pipe open for writing\00"
@.str.2 = [36 x i8] c"read_string - Failed to read string\00"
@EXIT_FAILURE = global i32 0
define i32 @read_string(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i32, i32* %3
%8 = call i32 @read_int(i32 %7, i32* %5)
%9 = load i32, i32* %3
%10 = load i8*, i8** %4
%11 = load i32, i32* %5
%12 = call i32 @read(i32 %9, i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @puts(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
br label %26
18: ; preds = %2
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %18
br label %26
26: ; preds = %25, %15
%27 = load i32, i32* %6
ret i32 %27
}

LLVM IR (-O2):
@.str = [8 x i8] c"DEBUG: \00"
@.str.1 = [55 x i8] c"read_string - no process has the pipe open for writing\00"
@.str.2 = [36 x i8] c"read_string - Failed to read string\00"
@EXIT_FAILURE = global i32 0
define i32 @read_string(i32 %0, i8* nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @read_int(i32 %0, i32* %3)
%6 = load i32, i32* %3
%7 = call i32 @read(i32 %0, i8* %1, i32 %6)
switch i32 %7, label %15 [
i32 0, label %8
i32 -1, label %11
]
8: ; preds = %2
%9 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
br label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2, %8
ret i32 %7
}