#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 */
char *malloc(size_t);
int memcmp(char const *, char *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ perror(char *);
int read(int, char *, size_t);
size_t strlen(char const *);
int recv_and_cmp(int fd, const char *msg) {
  size_t len = strlen(msg);
  size_t rest = len;
  char *buf = malloc(len + 1);
  memset(buf, 0, len + 1);
  while (rest > 0) {
    int readed = read(fd, buf + (len - rest), rest);
    if (readed == -1) {
      perror("test read");
    } else if (readed == 0) {
      return -1;
    } else {
      rest -= readed;
    }
  }
  return memcmp(msg, buf, len);
}


x86 assembly (clang, -O0):
<recv_and_cmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1040 <memset@plt>
cmpq   $0x0,-0x20(%rbp)
jbe    121b <recv_and_cmp+0xbb>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11ec <recv_and_cmp+0x8c>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
jmp    1216 <recv_and_cmp+0xb6>
cmpl   $0x0,-0x2c(%rbp)
jne    1202 <recv_and_cmp+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <recv_and_cmp+0xcf>
movslq -0x2c(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1216 <recv_and_cmp+0xb6>
jmp    11a8 <recv_and_cmp+0x48>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <memcmp@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_and_cmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbp
lea    0x1(%rax),%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memset@plt>
test   %rbp,%rbp
je     11e6 <recv_and_cmp+0x86>
lea    0xe5c(%rip),%r12
mov    %rbp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rsi
sub    %rbx,%rsi
add    %r13,%rsi
mov    %r15d,%edi
mov    %rbx,%rdx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11d9 <recv_and_cmp+0x79>
test   %eax,%eax
je     1202 <recv_and_cmp+0xa2>
cltq
sub    %rax,%rbx
test   %rbx,%rbx
jne    11b0 <recv_and_cmp+0x50>
jmp    11e6 <recv_and_cmp+0x86>
mov    %r12,%rdi
call   1080 <perror@plt>
test   %rbx,%rbx
jne    11b0 <recv_and_cmp+0x50>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <memcmp@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"test read\00"
define i32 @recv_and_cmp(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %6
%12 = load i64, i64* %6
store i64 %12, i64* %7
%13 = load i64, i64* %6
%14 = add i64 %13, 1
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = load i64, i64* %6
%18 = add i64 %17, 1
%19 = call i32 @memset(i8* %16, i32 0, i64 %18)
br label %20
20: ; preds = %46, %2
%21 = load i64, i64* %7
%22 = icmp ugt i64 %21, 0
br i1 %22, label %23, label %47
23: ; preds = %20
%24 = load i32, i32* %4
%25 = load i8*, i8** %8
%26 = load i64, i64* %6
%27 = load i64, i64* %7
%28 = sub i64 %26, %27
%29 = getelementptr inbounds i8, i8* %25, i64 %28
%30 = load i64, i64* %7
%31 = call i32 @read(i32 %24, i8* %29, i64 %30)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %23
%35 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %46
36: ; preds = %23
%37 = load i32, i32* %9
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36
store i32 -1, i32* %3
br label %52
40: ; preds = %36
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = load i64, i64* %7
%44 = sub i64 %43, %42
store i64 %44, i64* %7
br label %45
45: ; preds = %40
br label %46
46: ; preds = %45, %34
br label %20
47: ; preds = %20
%48 = load i8*, i8** %5
%49 = load i8*, i8** %8
%50 = load i64, i64* %6
%51 = call i32 @memcmp(i8* %48, i8* %49, i64 %50)
store i32 %51, i32* %3
br label %52
52: ; preds = %47, %39
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [10 x i8] c"test read\00"
define i32 @recv_and_cmp(i32 %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = add i64 %3, 1
%5 = tail call i8* @malloc(i64 %4)
%6 = tail call i32 @memset(i8* %5, i32 0, i64 %4)
%7 = icmp eq i64 %3, 0
br i1 %7, label %21, label %8
8: ; preds = %2, %18
%9 = phi i64 [ %19, %18 ], [ %3, %2 ]
%10 = sub i64 %3, %9
%11 = getelementptr inbounds i8, i8* %5, i64 %10
%12 = tail call i32 @read(i32 %0, i8* %11, i64 %9)
switch i32 %12, label %15 [
i32 -1, label %13
i32 0, label %23
]
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %18
15: ; preds = %8
%16 = sext i32 %12 to i64
%17 = sub i64 %9, %16
br label %18
18: ; preds = %13, %15
%19 = phi i64 [ %9, %13 ], [ %17, %15 ]
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %8
21: ; preds = %18, %2
%22 = tail call i32 @memcmp(i8* %1, i8* %5, i64 %3)
br label %23
23: ; preds = %8, %21
%24 = phi i32 [ %22, %21 ], [ -1, %8 ]
ret i32 %24
}