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

/* Variables and functions */
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGUSR2;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ *stdout;
scalar_t__ strcmp(char *, char *);
int figlioProduttore(pid_t pidProduttore, pid_t pidConsumatore) {
  FILE *myfile;
  char stringa[1024];
  if ((myfile = fopen("myfile.txt", "w")) == NULL) {
    fprintf(stderr, "Error file open.\n");
    return (-3);
  }
  sleep(1);
  fprintf(myfile, "%d\n", pidProduttore);
  fclose(myfile);
  kill(pidConsumatore, SIGUSR2);
  pause();
  while (1) {
    sleep(1);
    if ((myfile = fopen("myfile.txt", "w")) == NULL) {
      fprintf(stderr, "Error file open.\n");
      return (-4);
    }
    fprintf(stdout, "%s\n", "Inserisci una stringa: ");
    fscanf(stdin, "%s", stringa);
    if (strcmp(stringa, "end") == 0) {
      fclose(myfile);
      kill(pidConsumatore, SIGINT);
      return 0;
    }
    fprintf(myfile, "%s", stringa);
    fclose(myfile);
    kill(pidConsumatore, SIGUSR2);
    pause();
  }
  return 0;
}


x86 assembly (clang, -O0):
<figlioProduttore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe68(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d6 <figlioProduttore+0x56>
mov    0x2e17(%rip),%rax
movslq (%rax),%rdi
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1322 <figlioProduttore+0x1a2>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc(%rbp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
mov    $0x0,%al
call   1080 <pause@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0xdd9(%rip),%rdi
lea    0xddd(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1265 <figlioProduttore+0xe5>
mov    0x2d88(%rip),%rax
movslq (%rax),%rdi
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1322 <figlioProduttore+0x1a2>
mov    0x2d54(%rip),%rax
mov    (%rax),%rdi
lea    0xdad(%rip),%rsi
lea    0xdaa(%rip),%rdx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xda4(%rip),%rsi
call   1040 <fscanf@plt>
lea    -0x420(%rbp),%rdi
lea    0xd94(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12e3 <figlioProduttore+0x163>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc(%rbp),%edi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
movl   $0x0,-0x4(%rbp)
jmp    1322 <figlioProduttore+0x1a2>
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rdx
lea    0xd4a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc(%rbp),%edi
mov    0x2cb9(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
mov    $0x0,%al
call   1080 <pause@plt>
jmp    1216 <figlioProduttore+0x96>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<figlioProduttore>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
lea    0xe43(%rip),%rdi
lea    0xe47(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     131d <figlioProduttore+0x17d>
mov    %rax,%rbx
mov    $0x1,%edi
call   10c0 <sleep@plt>
lea    0xe39(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1080 <kill@plt>
xor    %eax,%eax
call   1090 <pause@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
lea    0xddd(%rip),%rdi
lea    0xde1(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12f6 <figlioProduttore+0x156>
mov    %rax,%r12
mov    %rsp,%rbx
lea    0xdfd(%rip),%r13
lea    0xdb4(%rip),%r14
lea    0xdb8(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x2d59(%rip),%rax
mov    (%rax),%rdi
lea    0xdb2(%rip),%rsi
lea    0xdaf(%rip),%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fscanf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1344 <figlioProduttore+0x1a4>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <fputs@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1080 <kill@plt>
xor    %eax,%eax
call   1090 <pause@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   10a0 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1260 <figlioProduttore+0xc0>
mov    0x2cd3(%rip),%rax
movslq (%rax),%rcx
lea    0xd06(%rip),%rdi
mov    $0x11,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    $0xfffffffc,%eax
jmp    135e <figlioProduttore+0x1be>
mov    0x2cac(%rip),%rax
movslq (%rax),%rcx
lea    0xcdf(%rip),%rdi
mov    $0x11,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    $0xfffffffd,%eax
jmp    135e <figlioProduttore+0x1be>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1080 <kill@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"myfile.txt\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [18 x i8] c"Error file open.\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@SIGUSR2 = global i32 0
@stdout = global i32* null
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [24 x i8] c"Inserisci una stringa: \00"
@stdin = global i32 0
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [4 x i8] c"end\00"
@SIGINT = global i32 0
define i32 @figlioProduttore(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [1024 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %6
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i32*
%14 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %3
br label %60
15: ; preds = %2
%16 = call i32 @sleep(i32 1)
%17 = load i32*, i32** %6
%18 = load i32, i32* %4
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = load i32*, i32** %6
%21 = call i32 @fclose(i32* %20)
%22 = load i32, i32* %5
%23 = load i32, i32* @SIGUSR2
%24 = call i32 @kill(i32 %22, i32 %23)
%25 = call i32 (...) @pause()
br label %26
26: ; preds = %15, %50
%27 = call i32 @sleep(i32 1)
%28 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %6
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = sext i32 %31 to i64
%33 = inttoptr i64 %32 to i32*
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 -4, i32* %3
br label %60
35: ; preds = %26
%36 = load i32*, i32** @stdout
%37 = call i32 (i32*, i8*, ...) @fprintf(i32* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%38 = load i32, i32* @stdin
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%40 = call i32 @fscanf(i32 %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %39)
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%42 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %35
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
%47 = load i32, i32* %5
%48 = load i32, i32* @SIGINT
%49 = call i32 @kill(i32 %47, i32 %48)
store i32 0, i32* %3
br label %60
50: ; preds = %35
%51 = load i32*, i32** %6
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%53 = call i32 (i32*, i8*, ...) @fprintf(i32* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %52)
%54 = load i32*, i32** %6
%55 = call i32 @fclose(i32* %54)
%56 = load i32, i32* %5
%57 = load i32, i32* @SIGUSR2
%58 = call i32 @kill(i32 %56, i32 %57)
%59 = call i32 (...) @pause()
br label %26
60: ; preds = %44, %30, %10
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [11 x i8] c"myfile.txt\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [18 x i8] c"Error file open.\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@SIGUSR2 = global i32 0
@stdout = global i32* null
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [24 x i8] c"Inserisci una stringa: \00"
@stdin = global i32 0
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [4 x i8] c"end\00"
@SIGINT = global i32 0
define i32 @figlioProduttore(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i32*
%11 = tail call i64 @fwrite(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 17, i64 1, i32* %10)
br label %48
12: ; preds = %2
%13 = tail call i32 @sleep(i32 1)
%14 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%15 = tail call i32 @fclose(i32* %5)
%16 = load i32, i32* @SIGUSR2
%17 = tail call i32 @kill(i32 %1, i32 %16)
%18 = tail call i32 (...) @pause()
%19 = call i32 @sleep(i32 1)
%20 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %27
22: ; preds = %39, %12
%23 = load i32, i32* @stderr
%24 = sext i32 %23 to i64
%25 = inttoptr i64 %24 to i32*
%26 = call i64 @fwrite(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 17, i64 1, i32* %25)
br label %48
27: ; preds = %12, %39
%28 = phi i32* [ %46, %39 ], [ %20, %12 ]
%29 = load i32*, i32** @stdout
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%31 = load i32, i32* @stdin
%32 = call i32 @fscanf(i32 %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%33 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %27
%36 = call i32 @fclose(i32* %28)
%37 = load i32, i32* @SIGINT
%38 = call i32 @kill(i32 %1, i32 %37)
br label %48
39: ; preds = %27
%40 = call i32 @fputs(i8* %4, i32* %28)
%41 = call i32 @fclose(i32* %28)
%42 = load i32, i32* @SIGUSR2
%43 = call i32 @kill(i32 %1, i32 %42)
%44 = call i32 (...) @pause()
%45 = call i32 @sleep(i32 1)
%46 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%47 = icmp eq i32* %46, null
br i1 %47, label %22, label %27
48: ; preds = %35, %22, %7
%49 = phi i32 [ -3, %7 ], [ -4, %22 ], [ 0, %35 ]
ret i32 %49
}