#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*/ FIONREAD;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int ioctl(int, int /*<<< orphan*/, int *);
char *malloc(int);
int read(int, char *, int);
scalar_t__ writeMessageInFifo(char *);
void handleProcess2(int fd[]) {
  char *buffer = NULL;
  char byte = 0;
  int count = 0;
  close(fd[1]);
  while (read(fd[0], &byte, 1) == 1) {
    if (ioctl(fd[0], FIONREAD, &count) != -1) {
      buffer = malloc(count + 1);
      buffer[0] = byte;
      if (read(fd[0], buffer + 1, count) == count) {
        if (writeMessageInFifo(buffer)) {
          free(buffer);
          execl("./Ej2", "Ej2", NULL);
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<handleProcess2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    1252 <handleProcess2+0xe2>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     124d <handleProcess2+0xdd>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x11(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%edx
call   1070 <read@plt>
cmp    -0x18(%rbp),%eax
jne    1248 <handleProcess2+0xd8>
mov    -0x10(%rbp),%rdi
call   1040 <writeMessageInFifo@plt>
cmp    $0x0,%rax
je     1243 <handleProcess2+0xd3>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xdcd(%rip),%rdi
lea    0xdc8(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execl@plt>
jmp    1248 <handleProcess2+0xd8>
jmp    124d <handleProcess2+0xdd>
jmp    119b <handleProcess2+0x2b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handleProcess2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
movb   $0x0,0x3(%rsp)
movl   $0x0,0x4(%rsp)
mov    0x4(%rdi),%edi
call   1060 <close@plt>
mov    (%rbx),%edi
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    1244 <handleProcess2+0xd4>
mov    0x2e34(%rip),%r13
lea    0x4(%rsp),%r14
lea    0x3(%rsp),%r15
jmp    11d4 <handleProcess2+0x64>
mov    (%rbx),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    1244 <handleProcess2+0xd4>
mov    (%rbx),%edi
mov    0x0(%r13),%esi
mov    %r14,%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11c0 <handleProcess2+0x50>
mov    0x4(%rsp),%r12d
lea    0x1(%r12),%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
movzbl 0x3(%rsp),%eax
mov    %al,0x0(%rbp)
mov    (%rbx),%edi
lea    0x1(%rbp),%rsi
mov    %r12d,%edx
call   1070 <read@plt>
cmp    0x4(%rsp),%eax
jne    11c0 <handleProcess2+0x50>
mov    %rbp,%rdi
call   1040 <writeMessageInFifo@plt>
test   %rax,%rax
je     11c0 <handleProcess2+0x50>
mov    %rbp,%rdi
call   1030 <free@plt>
lea    0xdcf(%rip),%rdi
lea    0xdca(%rip),%rsi
xor    %edx,%edx
call   1090 <execl@plt>
jmp    11c0 <handleProcess2+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FIONREAD = global i32 0
@.str = [6 x i8] c"./Ej2\00"
@.str.1 = [4 x i8] c"Ej2\00"
define void @handleProcess2(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32
store i32* %0, i32** %2
store i8* null, i8** %3
store i8 0, i8* %4
store i32 0, i32* %5
%6 = load i32*, i32** %2
%7 = getelementptr inbounds i32, i32* %6, i64 1
%8 = load i32, i32* %7
%9 = call i32 @close(i32 %8)
br label %10
10: ; preds = %49, %1
%11 = load i32*, i32** %2
%12 = getelementptr inbounds i32, i32* %11, i64 0
%13 = load i32, i32* %12
%14 = call i32 @read(i32 %13, i8* %4, i32 1)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %50
16: ; preds = %10
%17 = load i32*, i32** %2
%18 = getelementptr inbounds i32, i32* %17, i64 0
%19 = load i32, i32* %18
%20 = load i32, i32* @FIONREAD
%21 = call i32 @ioctl(i32 %19, i32 %20, i32* %5)
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %49
23: ; preds = %16
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %3
%27 = load i8, i8* %4
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i64 0
store i8 %27, i8* %29
%30 = load i32*, i32** %2
%31 = getelementptr inbounds i32, i32* %30, i64 0
%32 = load i32, i32* %31
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = load i32, i32* %5
%36 = call i32 @read(i32 %32, i8* %34, i32 %35)
%37 = load i32, i32* %5
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %48
39: ; preds = %23
%40 = load i8*, i8** %3
%41 = call i64 @writeMessageInFifo(i8* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i8*, i8** %3
%45 = call i32 @free(i8* %44)
%46 = call i32 @execl(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %47
47: ; preds = %43, %39
br label %48
48: ; preds = %47, %23
br label %49
49: ; preds = %48, %16
br label %10
50: ; preds = %10
ret void
}

LLVM IR (-O2):
@FIONREAD = global i32 0
@.str = [6 x i8] c"./Ej2\00"
@.str.1 = [4 x i8] c"Ej2\00"
define void @handleProcess2(i32* nocapture %0) {
%2 = alloca i8
%3 = alloca i32
store i8 0, i8* %2
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = getelementptr inbounds i32, i32* %0, i64 1
%6 = load i32, i32* %5
%7 = tail call i32 @close(i32 %6)
%8 = load i32, i32* %0
%9 = call i32 @read(i32 %8, i8* %2, i32 1)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %36
11: ; preds = %1, %32
%12 = load i32, i32* %0
%13 = load i32, i32* @FIONREAD
%14 = call i32 @ioctl(i32 %12, i32 %13, i32* %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %32, label %16
16: ; preds = %11
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
%19 = call i8* @malloc(i32 %18)
%20 = load i8, i8* %2
store i8 %20, i8* %19
%21 = load i32, i32* %0
%22 = getelementptr inbounds i8, i8* %19, i64 1
%23 = call i32 @read(i32 %21, i8* %22, i32 %17)
%24 = load i32, i32* %3
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %32
26: ; preds = %16
%27 = call i64 @writeMessageInFifo(i8* %19)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = call i32 @free(i8* %19)
%31 = call i32 @execl(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %32
32: ; preds = %16, %29, %26, %11
%33 = load i32, i32* %0
%34 = call i32 @read(i32 %33, i8* %2, i32 1)
%35 = icmp eq i32 %34, 1
br i1 %35, label %11, label %36
36: ; preds = %32, %1
ret void
}