#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*/ O_RDONLY;
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ SIGUSR2;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  int fd;
  int nb;
  char buf[80];
  int i;
  int pid_serveur;
  pid_serveur = atoi(argv[1]);
  if ((fd = open("/tmp/montube", O_RDONLY)) == -1) {
    perror("open");
    exit(1);
  }
  for (i = 0; i < 5; i++) {
    kill(pid_serveur, SIGUSR1);
    nb = read(fd, buf, 80);
    if (nb > 0) {
      printf("Il y a un nouveau message !\n");
      printf("Message lu : %s\n", buf);
    }
    kill(pid_serveur, SIGUSR2);
    nb = read(fd, buf, 80);
    if (nb > 0) {
      printf("Il y a un nouveau message !\n");
      printf("Message lu : %s\n", buf);
    }
    sleep(1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x78(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11f0 <main+0x60>
lea    0xe2c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x74(%rbp)
cmpl   $0x5,-0x74(%rbp)
jge    12b7 <main+0x127>
mov    -0x78(%rbp),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    -0x14(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1250 <main+0xc0>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x70(%rbp),%rsi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x78(%rbp),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    -0x14(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    129f <main+0x10f>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x70(%rbp),%rsi
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    11f7 <main+0x67>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r13d
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     129e <main+0xfe>
mov    %eax,%ebp
mov    $0x5,%ebx
mov    %rsp,%r14
lea    0xe3a(%rip),%r15
lea    0xe22(%rip),%r12
jmp    120f <main+0x6f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1,%edi
call   10c0 <sleep@plt>
add    $0xffffffff,%ebx
je     1286 <main+0xe6>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
call   1070 <kill@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x50,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1248 <main+0xa8>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
call   1070 <kill@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x50,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1200 <main+0x60>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1200 <main+0x60>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd68(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/tmp/montube\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SIGUSR1 = global i32 0
@.str.2 = [29 x i8] c"Il y a un nouveau message !\0A\00"
@.str.3 = [17 x i8] c"Message lu : %s\0A\00"
@SIGUSR2 = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [80 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @atoi(i8* %13)
store i32 %14, i32* %10
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
store i32 0, i32* %9
br label %22
22: ; preds = %53, %21
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 5
br i1 %24, label %25, label %56
25: ; preds = %22
%26 = load i32, i32* %10
%27 = load i32, i32* @SIGUSR1
%28 = call i32 @kill(i32 %26, i32 %27)
%29 = load i32, i32* %6
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 80)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %25
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %34, %25
%39 = load i32, i32* %10
%40 = load i32, i32* @SIGUSR2
%41 = call i32 @kill(i32 %39, i32 %40)
%42 = load i32, i32* %6
%43 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 80)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %38
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%49 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %47, %38
%52 = call i32 @sleep(i32 1)
br label %53
53: ; preds = %51
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %22
56: ; preds = %22
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"/tmp/montube\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SIGUSR1 = global i32 0
@.str.3 = [17 x i8] c"Message lu : %s\0A\00"
@SIGUSR2 = global i32 0
@str.4 = [28 x i8] c"Il y a un nouveau message !\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2, %31
%15 = phi i32 [ %33, %31 ], [ 0, %2 ]
%16 = load i32, i32* @SIGUSR1
%17 = call i32 @kill(i32 %7, i32 %16)
%18 = call i32 @read(i32 %9, i8* %4, i32 80)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.4, i64 0, i64 0))
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %23
23: ; preds = %20, %14
%24 = load i32, i32* @SIGUSR2
%25 = call i32 @kill(i32 %7, i32 %24)
%26 = call i32 @read(i32 %9, i8* %4, i32 80)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.4, i64 0, i64 0))
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %31
31: ; preds = %28, %23
%32 = call i32 @sleep(i32 1)
%33 = add nuw nsw i32 %15, 1
%34 = icmp eq i32 %33, 5
br i1 %34, label %35, label %14
35: ; preds = %31
%36 = call i32 @close(i32 %9)
ret i32 0
}