#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*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int receive_frm_pipe(int *fd, char *b) {
  int count = 0;
  printf("reading from %d\n", fd[0]);
  if (read(fd[0], b, 1) == -1)
    perror("rcv1");
  while (*b) {
    count++;
    b++;
    if (read(fd[0], b, 1) == -1)
      perror("rcv2");
  }
  return count;
}


x86 assembly (clang, -O0):
<receive_frm_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
lea    0xeac(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    1182 <receive_frm_pipe+0x52>
lea    0xe94(%rip),%rdi
call   1050 <perror@plt>
jmp    1187 <receive_frm_pipe+0x57>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11d7 <receive_frm_pipe+0xa7>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    11d2 <receive_frm_pipe+0xa2>
lea    0xe49(%rip),%rdi
call   1050 <perror@plt>
jmp    1187 <receive_frm_pipe+0x57>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_frm_pipe>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rdi),%esi
lea    0xeb8(%rip),%rdi
call   1030 <printf@plt>
mov    (%r14),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     11ae <receive_frm_pipe+0x7e>
cmpb   $0x0,(%r15)
je     11c0 <receive_frm_pipe+0x90>
add    $0x1,%r15
xor    %ebx,%ebx
lea    0xea1(%rip),%r12
jmp    118b <receive_frm_pipe+0x5b>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmpb   $0x0,0x0(%r13)
je     11c2 <receive_frm_pipe+0x92>
lea    (%r15,%rbx,1),%r13
mov    (%r14),%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    1180 <receive_frm_pipe+0x50>
mov    %r12,%rdi
call   1050 <perror@plt>
jmp    1180 <receive_frm_pipe+0x50>
lea    0xe5c(%rip),%rdi
call   1050 <perror@plt>
cmpb   $0x0,(%r15)
jne    1168 <receive_frm_pipe+0x38>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [17 x i8] c"reading from %d\0A\00"
@.str.1 = [5 x i8] c"rcv1\00"
@.str.2 = [5 x i8] c"rcv2\00"
define i32 @receive_frm_pipe(i32* nocapture %0, i8* nocapture %1) {
%3 = load i32, i32* %0
%4 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* %0
%6 = tail call i32 @read(i32 %5, i8* %1, i32 1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = load i8, i8* %1
%12 = icmp eq i8 %11, 0
br i1 %12, label %26, label %13
13: ; preds = %10, %23
%14 = phi i32 [ %16, %23 ], [ 0, %10 ]
%15 = phi i8* [ %17, %23 ], [ %1, %10 ]
%16 = add nuw nsw i32 %14, 1
%17 = getelementptr inbounds i8, i8* %15, i64 1
%18 = load i32, i32* %0
%19 = tail call i32 @read(i32 %18, i8* %17, i32 1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %13
%24 = load i8, i8* %17
%25 = icmp eq i8 %24, 0
br i1 %25, label %26, label %13
26: ; preds = %23, %10
%27 = phi i32 [ 0, %10 ], [ %16, %23 ]
ret i32 %27
}