#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int divisable(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
scalar_t__ read(int, int *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int write(int, int *, int);
int vie_des_fils(FILE *fd, int pipefd[], int premier) {
  fprintf(fd, "%d\n", premier);
  fflush(fd);
  int pipefdtmp[2];
  int nb;
  int hasfork = 0;
  close(pipefd[1]);
  while ((read(pipefd[0], &nb, sizeof(int))) != 0) {
    if (divisable(nb, premier) == -1) {
      if (!hasfork) {
        hasfork = 1;
        if ((pipe(pipefdtmp)) == -1) {
          perror("pipe");
          return -1;
        }
        if (fork() == 0) {
          vie_des_fils(fd, pipefdtmp, nb);
          exit(EXIT_SUCCESS);
        }
      } else {
        if ((write(pipefdtmp[1], &nb, sizeof(int))) == -1) {
          perror("write premier");
          return -1;
        }
      }
    }
  }
  close(pipefdtmp[1]);
  wait(NULL);
  return 42;
}


x86 assembly (clang, -O0):
<vie_des_fils>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe1f(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <fflush@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
je     12e4 <vie_des_fils+0x124>
mov    -0x28(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1090 <divisable@plt>
cmp    $0xffffffff,%eax
jne    12df <vie_des_fils+0x11f>
cmpl   $0x0,-0x2c(%rbp)
jne    12a3 <vie_des_fils+0xe3>
movl   $0x1,-0x2c(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    126f <vie_des_fils+0xaf>
lea    0xda6(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fc <vie_des_fils+0x13c>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
jne    129e <vie_des_fils+0xde>
mov    -0x10(%rbp),%rdi
lea    -0x24(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   10e0 <vie_des_fils@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    12da <vie_des_fils+0x11a>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12d5 <vie_des_fils+0x115>
lea    0xd45(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fc <vie_des_fils+0x13c>
jmp    12da <vie_des_fils+0x11a>
jmp    12df <vie_des_fils+0x11f>
jmp    1202 <vie_des_fils+0x42>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
movl   $0x2a,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vie_des_fils>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xe22(%rip),%rsi
call   1070 <fprintf@plt>
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
call   1080 <fflush@plt>
mov    0x4(%rbx),%edi
call   1040 <close@plt>
mov    (%rbx),%edi
lea    0x4(%rsp),%r15
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
lea    0x8(%rsp),%r12
xor    %r14d,%r14d
jmp    1254 <vie_des_fils+0x94>
cs nopw 0x0(%rax,%rax,1)
mov    0xc(%rsp),%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    $0x1,%r14d
cmp    $0xffffffff,%eax
je     12ce <vie_des_fils+0x10e>
mov    (%rbx),%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
test   %rax,%rax
je     12aa <vie_des_fils+0xea>
mov    0x4(%rsp),%ebp
mov    %ebp,%edi
mov    %r13d,%esi
call   1090 <divisable@plt>
cmp    $0xffffffff,%eax
jne    1240 <vie_des_fils+0x80>
test   %r14d,%r14d
jne    1220 <vie_des_fils+0x60>
mov    %r12,%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     12d7 <vie_des_fils+0x117>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    $0x1,%r14d
test   %rax,%rax
jne    1240 <vie_des_fils+0x80>
lea    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdi
mov    %ebp,%edx
call   10e0 <vie_des_fils@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
mov    $0x2a,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd34(%rip),%rdi
jmp    12de <vie_des_fils+0x11e>
lea    0xd26(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    12bf <vie_des_fils+0xff>

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [5 x i8] c"pipe\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [14 x i8] c"write premier\00"
define i32 @vie_des_fils(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [2 x i32]
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%11 = load i32*, i32** %5
%12 = load i32, i32* %7
%13 = call i32 @fprintf(i32* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32*, i32** %5
%15 = call i32 @fflush(i32* %14)
store i32 0, i32* %10
%16 = load i32*, i32** %6
%17 = getelementptr inbounds i32, i32* %16, i64 1
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
br label %20
20: ; preds = %60, %3
%21 = load i32*, i32** %6
%22 = getelementptr inbounds i32, i32* %21, i64 0
%23 = load i32, i32* %22
%24 = call i64 @read(i32 %23, i32* %9, i32 4)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %61
26: ; preds = %20
%27 = load i32, i32* %9
%28 = load i32, i32* %7
%29 = call i32 @divisable(i32 %27, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %60
31: ; preds = %26
%32 = load i32, i32* %10
%33 = icmp ne i32 %32, 0
br i1 %33, label %51, label %34
34: ; preds = %31
store i32 1, i32* %10
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%36 = call i32 @pipe(i32* %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %66
40: ; preds = %34
%41 = call i64 (...) @fork()
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %40
%44 = load i32*, i32** %5
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%46 = load i32, i32* %9
%47 = call i32 @vie_des_fils(i32* %44, i32* %45, i32 %46)
%48 = load i32, i32* @EXIT_SUCCESS
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %40
br label %59
51: ; preds = %31
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%53 = load i32, i32* %52
%54 = call i32 @write(i32 %53, i32* %9, i32 4)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %66
58: ; preds = %51
br label %59
59: ; preds = %58, %50
br label %60
60: ; preds = %59, %26
br label %20
61: ; preds = %20
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = call i32 @wait(i32* null)
store i32 42, i32* %4
br label %66
66: ; preds = %61, %56, %38
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [5 x i8] c"pipe\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [14 x i8] c"write premier\00"
define i32 @vie_des_fils(i32* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = tail call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %2)
%7 = tail call i32 @fflush(i32* %0)
%8 = bitcast [2 x i32]* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = getelementptr inbounds i32, i32* %1, i64 1
%11 = load i32, i32* %10
%12 = tail call i32 @close(i32 %11)
%13 = load i32, i32* %1
%14 = call i64 @read(i32 %13, i32* %5, i32 4)
%15 = icmp ne i64 %14, 0
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
br label %18
18: ; preds = %3, %43
%19 = phi i32 [ 0, %3 ], [ %44, %43 ]
%20 = load i32, i32* %5
%21 = call i32 @divisable(i32 %20, i32 %2)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %43
23: ; preds = %18
%24 = icmp eq i32 %19, 0
br i1 %24, label %25, label %37
25: ; preds = %23
%26 = call i32 @pipe(i32* %17)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %52
30: ; preds = %25
%31 = call i64 (...) @fork()
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %43
33: ; preds = %30
%34 = call i32 @vie_des_fils(i32* %0, i32* %17, i32 %20)
%35 = load i32, i32* @EXIT_SUCCESS
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %23
%38 = load i32, i32* %16
%39 = call i32 @write(i32 %38, i32* %5, i32 4)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %52
43: ; preds = %30, %37, %18
%44 = phi i32 [ 1, %37 ], [ 1, %30 ], [ %19, %18 ]
%45 = load i32, i32* %1
%46 = call i64 @read(i32 %45, i32* %5, i32 4)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %18
48: ; preds = %43
%49 = load i32, i32* %16
%50 = call i32 @close(i32 %49)
%51 = call i32 @wait(i32* null)
br label %52
52: ; preds = %48, %41, %28
%53 = phi i32 [ -1, %41 ], [ -1, %28 ], [ 42, %48 ]
ret i32 %53
}