#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*/ close(int);
int getpid();
scalar_t__ isdigit(char);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
void manage_child(int pipe[]) {
  char byte;
  int letters = 0;
  int digits = 0;
  printf("Child process (PID %d)\n", getpid());
  printf("Enter C-D to end.\n");
  close(pipe[1]);
  while (read(pipe[0], &byte, 1) > 0) {
    if (isdigit(byte)) {
      digits++;
    } else {
      letters++;
    }
  }
  printf("\n%d digits received\n", digits);
  printf("%d letters received\n", letters);
}


x86 assembly (clang, -O0):
<manage_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jle    11e8 <manage_child+0x98>
movsbl -0x9(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     11da <manage_child+0x8a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11e3 <manage_child+0x93>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    119b <manage_child+0x4b>
mov    -0x14(%rbp),%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<manage_child>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe86(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xeb8(%rip),%rdi
call   1040 <puts@plt>
mov    0x4(%r14),%edi
call   1070 <close@plt>
mov    (%r14),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    $0x0,%ebp
test   %rax,%rax
jle    11f0 <manage_child+0x90>
xor    %ebp,%ebp
lea    0x7(%rsp),%r15
xor    %ebx,%ebx
nopl   (%rax)
movsbl 0x7(%rsp),%edi
call   1030 <isdigit@plt>
xor    %ecx,%ecx
xor    %edx,%edx
test   %rax,%rax
setne  %cl
sete   %dl
add    %edx,%ebx
add    %ecx,%ebp
mov    (%r14),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jg     11c0 <manage_child+0x60>
lea    0xe21(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe26(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printf@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Child process (PID %d)\0A\00"
@.str.1 = [19 x i8] c"Enter C-D to end.\0A\00"
@.str.2 = [21 x i8] c"\0A%d digits received\0A\00"
@.str.3 = [21 x i8] c"%d letters received\0A\00"
define void @manage_child(i32* %0) {
%2 = alloca i32*
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %2
store i32 0, i32* %4
store i32 0, i32* %5
%6 = call i32 (...) @getpid()
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32*, i32** %2
%10 = getelementptr inbounds i32, i32* %9, i64 1
%11 = load i32, i32* %10
%12 = call i32 @close(i32 %11)
br label %13
13: ; preds = %29, %1
%14 = load i32*, i32** %2
%15 = getelementptr inbounds i32, i32* %14, i64 0
%16 = load i32, i32* %15
%17 = call i64 @read(i32 %16, i8* %3, i32 1)
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %13
%20 = load i8, i8* %3
%21 = call i64 @isdigit(i8 signext %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %29
26: ; preds = %19
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %29
29: ; preds = %26, %23
br label %13
30: ; preds = %13
%31 = load i32, i32* %5
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %4
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %33)
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"Child process (PID %d)\0A\00"
@.str.2 = [21 x i8] c"\0A%d digits received\0A\00"
@.str.3 = [21 x i8] c"%d letters received\0A\00"
@str = [18 x i8] c"Enter C-D to end.\00"
define void @manage_child(i32* nocapture %0) {
%2 = alloca i8
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%6 = getelementptr inbounds i32, i32* %0, i64 1
%7 = load i32, i32* %6
%8 = tail call i32 @close(i32 %7)
%9 = load i32, i32* %0
%10 = call i64 @read(i32 %9, i8* %2, i32 1)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %1, %12
%13 = phi i32 [ %22, %12 ], [ 0, %1 ]
%14 = phi i32 [ %19, %12 ], [ 0, %1 ]
%15 = load i8, i8* %2
%16 = tail call i64 @isdigit(i8 signext %15)
%17 = icmp eq i64 %16, 0
%18 = zext i1 %17 to i32
%19 = add nuw nsw i32 %14, %18
%20 = xor i1 %17, true
%21 = zext i1 %20 to i32
%22 = add nuw nsw i32 %13, %21
%23 = load i32, i32* %0
%24 = call i64 @read(i32 %23, i8* %2, i32 1)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %12, label %26
26: ; preds = %12, %1
%27 = phi i32 [ 0, %1 ], [ %19, %12 ]
%28 = phi i32 [ 0, %1 ], [ %22, %12 ]
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %27)
ret void
}