#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char **, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
void forkConnection(int dataSocket, int *pipe) {
  pid_t childPID;
  childPID = fork();
  if (childPID == 0) {
    close(pipe[1]);
    printf("child\n");
    while (1) {
      char readBuff[3];
      printf("binging read\n");
      read(pipe[0], &readBuff, 4);
      printf("ending read\n");
      write(dataSocket, readBuff, 4);
      printf("write finished\n");
      if ((readBuff[0] == '#') && (readBuff[1] == 'F') &&
          (readBuff[2] == 'I') && (readBuff[3] == 'N')) {
        break;
      }
    }
    close(pipe[0]);
    _exit(EXIT_SUCCESS);
  }
  wait(NULL);
}


x86 assembly (clang, -O0):
<forkConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    124d <forkConnection+0xdd>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <close@plt>
lea    0xe58(%rip),%rdi
call   1050 <printf@plt>
lea    0xe53(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x1b(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
lea    0xe41(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x1b(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
lea    0xe31(%rip),%rdi
call   1050 <printf@plt>
movsbl -0x1b(%rbp),%eax
cmp    $0x23,%eax
jne    122f <forkConnection+0xbf>
movsbl -0x1a(%rbp),%eax
cmp    $0x46,%eax
jne    122f <forkConnection+0xbf>
movsbl -0x19(%rbp),%eax
cmp    $0x49,%eax
jne    122f <forkConnection+0xbf>
movsbl -0x18(%rbp),%eax
cmp    $0x4e,%eax
jne    122f <forkConnection+0xbf>
jmp    1234 <forkConnection+0xc4>
jmp    11ad <forkConnection+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forkConnection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11a1 <forkConnection+0x31>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <wait@plt>
mov    0x4(%rbx),%edi
call   1060 <close@plt>
lea    0xe50(%rip),%rdi
call   1040 <puts@plt>
lea    0xe4a(%rip),%r14
lea    0x5(%rsp),%r15
lea    0xe4b(%rip),%r12
lea    0xe50(%rip),%r13
nop
mov    %r14,%rdi
call   1040 <puts@plt>
mov    (%rbx),%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    %r12,%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1050 <write@plt>
mov    %r13,%rdi
call   1040 <puts@plt>
cmpb   $0x23,0x5(%rsp)
jne    11d0 <forkConnection+0x60>
cmpb   $0x46,0x6(%rsp)
jne    11d0 <forkConnection+0x60>
cmpb   $0x49,0x7(%rsp)
jne    11d0 <forkConnection+0x60>
cmpb   $0x4e,0x8(%rsp)
jne    11d0 <forkConnection+0x60>
mov    (%rbx),%edi
call   1060 <close@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"child\0A\00"
@.str.1 = [14 x i8] c"binging read\0A\00"
@.str.2 = [13 x i8] c"ending read\0A\00"
@.str.3 = [16 x i8] c"write finished\0A\00"
@EXIT_SUCCESS = global i32 0
define void @forkConnection(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca [3 x i8]
store i32 %0, i32* %3
store i32* %1, i32** %4
%7 = call i64 (...) @fork()
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %56
10: ; preds = %2
%11 = load i32*, i32** %4
%12 = getelementptr inbounds i32, i32* %11, i64 1
%13 = load i32, i32* %12
%14 = call i32 @close(i32 %13)
%15 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %10, %48
%17 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32*, i32** %4
%19 = getelementptr inbounds i32, i32* %18, i64 0
%20 = load i32, i32* %19
%21 = bitcast [3 x i8]* %6 to i8**
%22 = call i32 @read(i32 %20, i8** %21, i32 4)
%23 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* %3
%25 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%26 = call i32 @write(i32 %24, i8* %25, i32 4)
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 35
br i1 %31, label %32, label %48
32: ; preds = %16
%33 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 1
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 70
br i1 %36, label %37, label %48
37: ; preds = %32
%38 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 2
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 73
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 3
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 78
br i1 %46, label %47, label %48
47: ; preds = %42
br label %49
48: ; preds = %42, %37, %32, %16
br label %16
49: ; preds = %47
%50 = load i32*, i32** %4
%51 = getelementptr inbounds i32, i32* %50, i64 0
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* @EXIT_SUCCESS
%55 = call i32 @_exit(i32 %54)
unreachable
56: ; preds = %2
%57 = call i32 @wait(i32* null)
ret void
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@str = [6 x i8] c"child\00"
@str.4 = [13 x i8] c"binging read\00"
@str.5 = [12 x i8] c"ending read\00"
@str.6 = [15 x i8] c"write finished\00"
define void @forkConnection(i32 %0, i32* nocapture %1) {
%3 = alloca [3 x i8]
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %40
6: ; preds = %2
%7 = getelementptr inbounds i32, i32* %1, i64 1
%8 = load i32, i32* %7
%9 = tail call i32 @close(i32 %8)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%11 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 3
%12 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%13 = bitcast [3 x i8]* %3 to i8**
%14 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
%15 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
br label %16
16: ; preds = %34, %6
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.4, i64 0, i64 0))
%18 = load i32, i32* %1
%19 = call i32 @read(i32 %18, i8** %13, i32 4)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
%21 = call i32 @write(i32 %0, i8* %12, i32 4)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.6, i64 0, i64 0))
%23 = load i8, i8* %12
%24 = icmp eq i8 %23, 35
%25 = load i8, i8* %14
%26 = icmp eq i8 %25, 70
%27 = select i1 %24, i1 %26, i1 false
%28 = load i8, i8* %15
%29 = icmp eq i8 %28, 73
%30 = select i1 %27, i1 %29, i1 false
br i1 %30, label %31, label %34
31: ; preds = %16
%32 = load i8, i8* %11
%33 = icmp eq i8 %32, 78
br i1 %33, label %35, label %34
34: ; preds = %16, %31
br label %16
35: ; preds = %31
%36 = load i32, i32* %1
%37 = tail call i32 @close(i32 %36)
%38 = load i32, i32* @EXIT_SUCCESS
%39 = tail call i32 @_exit(i32 %38)
unreachable
40: ; preds = %2
%41 = tail call i32 @wait(i32* null)
ret void
}