#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execlp(char *, char *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  pid_t pid;
  int readbytes;
  char buffer[512];
  printf("-->Arranque de programa de deteción de Beacons\n");
  int fd[2];
  pipe(fd);
  pid = fork();
  char str[2];
  switch (pid) {
  case -1:
    perror("No se ha podido crear el proceso\n");
    break;
  case 0:
    close(fd[0]);
    sprintf(str, "%d", fd[1]);
    execlp("./filter", "filter", str, (char *)NULL);
    break;
  default:
    close(fd[1]);
    while (1) {
      while ((readbytes = read(fd[0], buffer, 512)) > 0) {
        write(1, buffer, readbytes);
      }
    }
    close(fd[0]);
    break;
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe40(%rip),%rdi
call   1040 <printf@plt>
lea    -0x228(%rbp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x230(%rbp)
sub    $0xffffffff,%eax
je     1205 <main+0x65>
jmp    11f2 <main+0x52>
mov    -0x230(%rbp),%eax
test   %eax,%eax
je     1216 <main+0x76>
jmp    125d <main+0xbd>
lea    0xe25(%rip),%rdi
call   1080 <perror@plt>
jmp    12ae <main+0x10e>
mov    -0x228(%rbp),%edi
call   1050 <close@plt>
lea    -0x22a(%rbp),%rdi
mov    -0x224(%rbp),%edx
lea    0xe1e(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x22a(%rbp),%rdx
lea    0xe0e(%rip),%rdi
lea    0xe09(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   10b0 <execlp@plt>
jmp    12ae <main+0x10e>
mov    -0x224(%rbp),%edi
call   1050 <close@plt>
jmp    126d <main+0xcd>
mov    -0x228(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    12a9 <main+0x109>
lea    -0x220(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    126d <main+0xcd>
jmp    1268 <main+0xc8>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x210,%rsp
lea    0xe7f(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     1226 <main+0x86>
test   %eax,%eax
je     1239 <main+0x99>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0x10(%rsp),%rbx
nopl   0x0(%rax)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    11e0 <main+0x40>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     1200 <main+0x60>
jmp    11e0 <main+0x40>
lea    0xdd3(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edx
lea    0xdd5(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <sprintf@plt>
lea    0xdc4(%rip),%rdi
lea    0xdbf(%rip),%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   10b0 <execlp@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [49 x i8] c"-->Arranque de programa de deteci\C3\B3n de Beacons\0A\00"
@.str.1 = [34 x i8] c"No se ha podido crear el proceso\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [9 x i8] c"./filter\00"
@.str.4 = [7 x i8] c"filter\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca [2 x i32]
%10 = alloca [2 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = call i32 (...) @fork()
store i32 %14, i32* %6
%15 = load i32, i32* %6
switch i32 %15, label %28 [
i32 -1, label %16
i32 0, label %18
]
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %44
18: ; preds = %2
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%27 = call i32 @execlp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %26, i8* null)
br label %44
28: ; preds = %2
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %28, %43
br label %33
33: ; preds = %39, %32
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%37 = call i32 @read(i32 %35, i8* %36, i32 512)
store i32 %37, i32* %7
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %33
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%41 = load i32, i32* %7
%42 = call i32 @write(i32 1, i8* %40, i32 %41)
br label %33
43: ; preds = %33
br label %32
44: ; preds = %18, %16
%45 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [34 x i8] c"No se ha podido crear el proceso\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [9 x i8] c"./filter\00"
@.str.4 = [7 x i8] c"filter\00"
@str = [48 x i8] c"-->Arranque de programa de deteci\C3\B3n de Beacons\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [512 x i8]
%4 = alloca [2 x i32]
%5 = alloca [2 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
%8 = bitcast [2 x i32]* %4 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = call i32 (...) @fork()
%12 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
switch i32 %11, label %22 [
i32 -1, label %13
i32 0, label %15
]
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %37
15: ; preds = %2
%16 = load i32, i32* %9
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = call i32 @execlp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %12, i8* null)
br label %37
22: ; preds = %2
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %30, %22
%27 = load i32, i32* %9
%28 = call i32 @read(i32 %27, i8* %6, i32 512)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %31, %26
br label %26
31: ; preds = %26, %31
%32 = phi i32 [ %35, %31 ], [ %28, %26 ]
%33 = call i32 @write(i32 1, i8* %6, i32 %32)
%34 = load i32, i32* %9
%35 = call i32 @read(i32 %34, i8* %6, i32 512)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %31, label %30
37: ; preds = %15, %13
%38 = call i32 @exit(i32 0)
unreachable
}