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

/* Variables and functions */
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ SIGUSR2;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ asprintf(char **, char *, int);
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ sigwait(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  if (argc == 2) {
    int receiverPid = atoi(argv[1]);
    char *path;
    asprintf(&path, "/tmp/giro%d", receiverPid);
    char *buffer;
    do {
      buffer = malloc(200);
      buffer = fgets(buffer, 200, stdin);
      if (buffer != NULL) {
        FILE *file = fopen(path, "w");
        if (file != NULL) {
          fputs(buffer, file);
          free(buffer);
          fclose(file);
          kill(receiverPid, SIGUSR1);
          sigset_t mask;
          sigemptyset(&mask);
          sigaddset(&mask, SIGUSR2);
          sigprocmask(SIG_BLOCK, &mask, NULL);
          int sig;
          sigwait(&mask, &sig);
        } else {
          printf("Error opening the file %s \n", path);
        }
      }
    } while (buffer != NULL);
    kill(receiverPid, SIGUSR2);
  } else {
    printf("Parameters error: pid required\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1326 <main+0x146>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   10c0 <asprintf@plt>
mov    $0xc8,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
mov    $0xc8,%esi
call   1080 <fgets@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1300 <main+0x120>
mov    -0x20(%rbp),%rdi
lea    0xdaa(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12e9 <main+0x109>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <fputs@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x14(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
call   10a0 <kill@plt>
lea    -0x34(%rbp),%rdi
call   1090 <sigemptyset@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
lea    -0x34(%rbp),%rdi
call   1100 <sigaddset@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
lea    -0x34(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10f0 <sigwait@plt>
jmp    12fb <main+0x11b>
mov    -0x20(%rbp),%rsi
lea    0xd1a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1300 <main+0x120>
jmp    1305 <main+0x125>
cmpq   $0x0,-0x28(%rbp)
jne    1223 <main+0x43>
mov    -0x14(%rbp),%edi
mov    0x2cb6(%rip),%rax
mov    (%rax),%esi
call   10a0 <kill@plt>
jmp    1334 <main+0x154>
lea    0xcfd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    1329 <main+0x139>
mov    0x8(%rsi),%rdi
call   10f0 <atoi@plt>
mov    %eax,%r13d
lea    0xde6(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %eax,%edx
call   10d0 <asprintf@plt>
mov    $0xc8,%edi
call   10c0 <malloc@plt>
mov    0x2da9(%rip),%r12
mov    (%r12),%edx
mov    %rax,%rdi
mov    $0xc8,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     1337 <main+0x147>
mov    %rax,%rbp
lea    0xdb1(%rip),%r14
lea    0x14(%rsp),%rbx
jmp    12aa <main+0xba>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rsp),%rsi
lea    0xd92(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xc8,%edi
call   10c0 <malloc@plt>
mov    (%r12),%edx
mov    %rax,%rdi
mov    $0xc8,%esi
call   1090 <fgets@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1337 <main+0x147>
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1270 <main+0x80>
mov    %rax,%r15
mov    %rbp,%rdi
mov    %rax,%rsi
call   1080 <fputs@plt>
mov    %rbp,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
call   10b0 <kill@plt>
mov    %rbx,%rdi
call   10a0 <sigemptyset@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1110 <sigaddset@plt>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    %rbx,%rdi
lea    0x10(%rsp),%rsi
call   1100 <sigwait@plt>
jmp    1283 <main+0x93>
lea    0xcfa(%rip),%rdi
call   1050 <puts@plt>
jmp    1348 <main+0x158>
mov    0x2c92(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
call   10b0 <kill@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/tmp/giro%d\00"
@stdin = global i32 0
@.str.1 = [2 x i8] c"w\00"
@SIGUSR1 = global i32 0
@SIGUSR2 = global i32 0
@SIG_BLOCK = global i32 0
@.str.2 = [28 x i8] c"Error opening the file %s \0A\00"
@.str.3 = [32 x i8] c"Parameters error: pid required\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 2
br i1 %13, label %14, label %62
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %55, %14
%22 = call i8* @malloc(i32 200)
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = load i32, i32* @stdin
%25 = call i8* @fgets(i8* %23, i32 200, i32 %24)
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %54
28: ; preds = %21
%29 = load i8*, i8** %7
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %30, i32** %9
%31 = load i32*, i32** %9
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %50
33: ; preds = %28
%34 = load i8*, i8** %8
%35 = load i32*, i32** %9
%36 = call i32 @fputs(i8* %34, i32* %35)
%37 = load i8*, i8** %8
%38 = call i32 @free(i8* %37)
%39 = load i32*, i32** %9
%40 = call i32 @fclose(i32* %39)
%41 = load i32, i32* %6
%42 = load i32, i32* @SIGUSR1
%43 = call i32 @kill(i32 %41, i32 %42)
%44 = call i32 @sigemptyset(i32* %10)
%45 = load i32, i32* @SIGUSR2
%46 = call i32 @sigaddset(i32* %10, i32 %45)
%47 = load i32, i32* @SIG_BLOCK
%48 = call i32 @sigprocmask(i32 %47, i32* %10, i32* null)
%49 = call i32 @sigwait(i32* %10, i32* %11)
br label %53
50: ; preds = %28
%51 = load i8*, i8** %7
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %50, %33
br label %54
54: ; preds = %53, %21
br label %55
55: ; preds = %54
%56 = load i8*, i8** %8
%57 = icmp ne i8* %56, null
br i1 %57, label %21, label %58
58: ; preds = %55
%59 = load i32, i32* %6
%60 = load i32, i32* @SIGUSR2
%61 = call i32 @kill(i32 %59, i32 %60)
br label %64
62: ; preds = %2
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %64
64: ; preds = %62, %58
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"/tmp/giro%d\00"
@stdin = global i32 0
@.str.1 = [2 x i8] c"w\00"
@SIGUSR1 = global i32 0
@SIGUSR2 = global i32 0
@SIG_BLOCK = global i32 0
@.str.2 = [28 x i8] c"Error opening the file %s \0A\00"
@str = [31 x i8] c"Parameters error: pid required\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = icmp eq i32 %0, 2
br i1 %6, label %7, label %48
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = bitcast i8** %3 to i8*
%12 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
%13 = call dereferenceable_or_null(200) i8* @malloc(i32 200)
%14 = load i32, i32* @stdin
%15 = call i8* @fgets(i8* %13, i32 200, i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %45, label %17
17: ; preds = %7
%18 = bitcast i32* %4 to i8*
%19 = bitcast i32* %5 to i8*
br label %20
20: ; preds = %17, %40
%21 = phi i8* [ %15, %17 ], [ %43, %40 ]
%22 = load i8*, i8** %3
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %37, label %25
25: ; preds = %20
%26 = call i32 @fputs(i8* %21, i32* %23)
%27 = call i32 @free(i8* %21)
%28 = call i32 @fclose(i32* %23)
%29 = load i32, i32* @SIGUSR1
%30 = call i32 @kill(i32 %10, i32 %29)
%31 = call i32 @sigemptyset(i32* %4)
%32 = load i32, i32* @SIGUSR2
%33 = call i32 @sigaddset(i32* %4, i32 %32)
%34 = load i32, i32* @SIG_BLOCK
%35 = call i32 @sigprocmask(i32 %34, i32* %4, i32* null)
%36 = call i32 @sigwait(i32* %4, i32* %5)
br label %40
37: ; preds = %20
%38 = load i8*, i8** %3
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %25, %37
%41 = call dereferenceable_or_null(200) i8* @malloc(i32 200)
%42 = load i32, i32* @stdin
%43 = call i8* @fgets(i8* %41, i32 200, i32 %42)
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %20
45: ; preds = %40, %7
%46 = load i32, i32* @SIGUSR2
%47 = call i32 @kill(i32 %10, i32 %46)
br label %50
48: ; preds = %2
%49 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %50
50: ; preds = %48, %45
ret i32 0
}