#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 */
struct signalfd_siginfo {
  scalar_t__ ssi_signo;
  int /*<<< orphan*/ ssi_pid;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};
typedef int /*<<< orphan*/ sigset_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int POLLIN;
scalar_t__ SIGUSR1;
scalar_t__ SIGUSR2;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ asprintf(char **, char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, scalar_t__);
char *malloc(int);
int /*<<< orphan*/ poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, struct signalfd_siginfo *, int);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, scalar_t__);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int signalfd(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int pid = getpid();
  printf("pid: %d\n", pid);
  char *path;
  asprintf(&path, "/tmp/giro%d", pid);
  sigset_t mask;
  sigemptyset(&mask);
  sigaddset(&mask, SIGUSR1);
  sigaddset(&mask, SIGUSR2);
  sigprocmask(SIG_BLOCK, &mask, NULL);
  int fd;
  fd = signalfd(-1, &mask, 0);
  struct pollfd polls = {.fd = fd, .events = POLLIN};
  int sigusr2 = 0;
  while (!sigusr2) {
    poll(&polls, 1, -1);
    if (polls.revents & POLLIN) {
      struct signalfd_siginfo siginfo;
      read(fd, &siginfo, sizeof(struct signalfd_siginfo));
      if (siginfo.ssi_signo == SIGUSR1) {
        FILE *file = fopen(path, "r");
        if (file != NULL) {
          char *buffer = malloc(50);
          fgets(buffer, 50, file);
          printf("%s", buffer);
          free(buffer);
          fclose(file);
          remove(path);
          kill(siginfo.ssi_pid, SIGUSR2);
        } else {
          printf("Error opening the file %s \n", path);
        }
      } else if (siginfo.ssi_signo == SIGUSR2) {
        sigusr2 = 1;
      }
      polls.revents = 0;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rdi
lea    0xdca(%rip),%rsi
call   10f0 <asprintf@plt>
lea    -0x24(%rbp),%rdi
call   10c0 <sigemptyset@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%rsi
lea    -0x24(%rbp),%rdi
call   1120 <sigaddset@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%rsi
lea    -0x24(%rbp),%rdi
call   1120 <sigaddset@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    $0xffffffff,%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1090 <signalfd@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12d2 <main+0xd2>
jmp    13e5 <main+0x1e5>
lea    -0x38(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1100 <poll@plt>
mov    -0x30(%rbp),%eax
mov    0x2cf1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     13e0 <main+0x1e0>
mov    -0x28(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <read@plt>
mov    -0x50(%rbp),%rax
mov    0x2cd2(%rip),%rcx
cmp    (%rcx),%rax
jne    13b9 <main+0x1b9>
mov    -0x20(%rbp),%rdi
lea    0xceb(%rip),%rsi
call   1110 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     13a2 <main+0x1a2>
mov    $0x32,%edi
call   10e0 <malloc@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    $0x32,%esi
call   10b0 <fgets@plt>
mov    -0x60(%rbp),%rsi
lea    0xcae(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x60(%rbp),%rdi
call   1040 <free@plt>
mov    -0x58(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <remove@plt>
mov    -0x48(%rbp),%edi
mov    0x2c3b(%rip),%rax
mov    (%rax),%rsi
call   10d0 <kill@plt>
jmp    13b4 <main+0x1b4>
mov    -0x20(%rbp),%rsi
lea    0xc6d(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    13d9 <main+0x1d9>
mov    -0x50(%rbp),%rax
mov    0x2c0c(%rip),%rcx
cmp    (%rcx),%rax
jne    13d4 <main+0x1d4>
movl   $0x1,-0x3c(%rbp)
jmp    13d9 <main+0x1d9>
movl   $0x0,-0x30(%rbp)
jmp    12bc <main+0xbc>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %eax,%ebx
lea    0xde2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xddb(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %ebx,%edx
call   10f0 <asprintf@plt>
lea    0x34(%rsp),%r14
mov    %r14,%rdi
call   10c0 <sigemptyset@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%rsi
mov    %r14,%rdi
call   1120 <sigaddset@plt>
mov    0x2d70(%rip),%rbx
mov    (%rbx),%rsi
mov    %r14,%rdi
call   1120 <sigaddset@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    $0xffffffff,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1090 <signalfd@plt>
mov    %eax,%r12d
mov    %eax,(%rsp)
mov    0x2d46(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rsp,%rbp
lea    0x20(%rsp),%r13
mov    %r12d,0x1c(%rsp)
jmp    12db <main+0xdb>
nopl   0x0(%rax,%rax,1)
xor    %r15d,%r15d
cmp    (%rbx),%rax
sete   %r15b
movl   $0x0,0x8(%rsp)
test   %r15d,%r15d
jne    13b7 <main+0x1b7>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1100 <poll@plt>
mov    (%r14),%eax
mov    $0x0,%r15d
test   %eax,0x8(%rsp)
je     12d2 <main+0xd2>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x10,%edx
call   10a0 <read@plt>
mov    0x20(%rsp),%rax
mov    0x2cd0(%rip),%rcx
cmp    (%rcx),%rax
jne    12c0 <main+0xc0>
mov    0x10(%rsp),%rdi
lea    0xcec(%rip),%rsi
call   1110 <fopen@plt>
test   %rax,%rax
je     1394 <main+0x194>
mov    %rax,%r13
mov    $0x32,%edi
call   10e0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x32,%esi
mov    %r13,%rdx
call   10b0 <fgets@plt>
xor    %r15d,%r15d
lea    0xcba(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbp,%rdi
mov    0x1c(%rsp),%r12d
call   1040 <free@plt>
mov    %r13,%rdi
call   1070 <fclose@plt>
mov    0x10(%rsp),%rdi
call   1050 <remove@plt>
mov    0x28(%rsp),%edi
mov    (%rbx),%rsi
call   10d0 <kill@plt>
jmp    13aa <main+0x1aa>
mov    0x10(%rsp),%rsi
xor    %r15d,%r15d
lea    0xc77(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rsp,%rbp
lea    0x20(%rsp),%r13
jmp    12ca <main+0xca>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
%struct.signalfd_siginfo = type { i64, i32 }
@.str = [9 x i8] c"pid: %d\0A\00"
@.str.1 = [12 x i8] c"/tmp/giro%d\00"
@SIGUSR1 = global i64 0
@SIGUSR2 = global i64 0
@SIG_BLOCK = global i32 0
@POLLIN = global i32 0
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [28 x i8] c"Error opening the file %s \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 i32
%9 = alloca i32
%10 = alloca %struct.pollfd
%11 = alloca i32
%12 = alloca %struct.signalfd_siginfo
%13 = alloca i32*
%14 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i32 (...) @getpid()
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %6
%19 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call i32 @sigemptyset(i32* %8)
%21 = load i64, i64* @SIGUSR1
%22 = call i32 @sigaddset(i32* %8, i64 %21)
%23 = load i64, i64* @SIGUSR2
%24 = call i32 @sigaddset(i32* %8, i64 %23)
%25 = load i32, i32* @SIG_BLOCK
%26 = call i32 @sigprocmask(i32 %25, i32* %8, i32* null)
%27 = call i32 @signalfd(i32 -1, i32* %8, i32 0)
store i32 %27, i32* %9
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
%29 = load i32, i32* %9
store i32 %29, i32* %28
%30 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
%31 = load i32, i32* @POLLIN
store i32 %31, i32* %30
%32 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
store i32 0, i32* %32
store i32 0, i32* %11
br label %33
33: ; preds = %86, %2
%34 = load i32, i32* %11
%35 = icmp ne i32 %34, 0
%36 = xor i1 %35, true
br i1 %36, label %37, label %87
37: ; preds = %33
%38 = call i32 @poll(%struct.pollfd* %10, i32 1, i32 -1)
%39 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
%40 = load i32, i32* %39
%41 = load i32, i32* @POLLIN
%42 = and i32 %40, %41
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %86
44: ; preds = %37
%45 = load i32, i32* %9
%46 = call i32 @read(i32 %45, %struct.signalfd_siginfo* %12, i32 16)
%47 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %12, i32 0, i32 0
%48 = load i64, i64* %47
%49 = load i64, i64* @SIGUSR1
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %77
51: ; preds = %44
%52 = load i8*, i8** %7
%53 = call i32* @fopen(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %53, i32** %13
%54 = load i32*, i32** %13
%55 = icmp ne i32* %54, null
br i1 %55, label %56, label %73
56: ; preds = %51
%57 = call i8* @malloc(i32 50)
store i8* %57, i8** %14
%58 = load i8*, i8** %14
%59 = load i32*, i32** %13
%60 = call i32 @fgets(i8* %58, i32 50, i32* %59)
%61 = load i8*, i8** %14
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %61)
%63 = load i8*, i8** %14
%64 = call i32 @free(i8* %63)
%65 = load i32*, i32** %13
%66 = call i32 @fclose(i32* %65)
%67 = load i8*, i8** %7
%68 = call i32 @remove(i8* %67)
%69 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %12, i32 0, i32 1
%70 = load i32, i32* %69
%71 = load i64, i64* @SIGUSR2
%72 = call i32 @kill(i32 %70, i64 %71)
br label %76
73: ; preds = %51
%74 = load i8*, i8** %7
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %74)
br label %76
76: ; preds = %73, %56
br label %84
77: ; preds = %44
%78 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %12, i32 0, i32 0
%79 = load i64, i64* %78
%80 = load i64, i64* @SIGUSR2
%81 = icmp eq i64 %79, %80
br i1 %81, label %82, label %83
82: ; preds = %77
store i32 1, i32* %11
br label %83
83: ; preds = %82, %77
br label %84
84: ; preds = %83, %76
%85 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
store i32 0, i32* %85
br label %86
86: ; preds = %84, %37
br label %33
87: ; preds = %33
ret i32 0
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
%struct.signalfd_siginfo = type { i64, i32 }
@.str = [9 x i8] c"pid: %d\0A\00"
@.str.1 = [12 x i8] c"/tmp/giro%d\00"
@SIGUSR1 = global i64 0
@SIGUSR2 = global i64 0
@SIG_BLOCK = global i32 0
@POLLIN = global i32 0
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [28 x i8] c"Error opening the file %s \0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.pollfd
%6 = alloca %struct.signalfd_siginfo
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = bitcast i8** %3 to i8*
%10 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%11 = bitcast i32* %4 to i8*
%12 = call i32 @sigemptyset(i32* %4)
%13 = load i64, i64* @SIGUSR1
%14 = call i32 @sigaddset(i32* %4, i64 %13)
%15 = load i64, i64* @SIGUSR2
%16 = call i32 @sigaddset(i32* %4, i64 %15)
%17 = load i32, i32* @SIG_BLOCK
%18 = call i32 @sigprocmask(i32 %17, i32* %4, i32* null)
%19 = call i32 @signalfd(i32 -1, i32* %4, i32 0)
%20 = bitcast %struct.pollfd* %5 to i8*
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %19, i32* %21
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
%23 = load i32, i32* @POLLIN
store i32 %23, i32* %22
%24 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 2
store i32 0, i32* %24
%25 = bitcast %struct.signalfd_siginfo* %6 to i8*
%26 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %6, i64 0, i32 0
%27 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %6, i64 0, i32 1
br label %28
28: ; preds = %2, %63
%29 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 -1)
%30 = load i32, i32* %24
%31 = load i32, i32* @POLLIN
%32 = and i32 %31, %30
%33 = icmp eq i32 %32, 0
br i1 %33, label %63, label %34
34: ; preds = %28
%35 = call i32 @read(i32 %19, %struct.signalfd_siginfo* %6, i32 16)
%36 = load i64, i64* %26
%37 = load i64, i64* @SIGUSR1
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %57
39: ; preds = %34
%40 = load i8*, i8** %3
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%42 = icmp eq i32* %41, null
br i1 %42, label %54, label %43
43: ; preds = %39
%44 = call dereferenceable_or_null(50) i8* @malloc(i32 50)
%45 = call i32 @fgets(i8* %44, i32 50, i32* %41)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %44)
%47 = call i32 @free(i8* %44)
%48 = call i32 @fclose(i32* %41)
%49 = load i8*, i8** %3
%50 = call i32 @remove(i8* %49)
%51 = load i32, i32* %27
%52 = load i64, i64* @SIGUSR2
%53 = call i32 @kill(i32 %51, i64 %52)
br label %61
54: ; preds = %39
%55 = load i8*, i8** %3
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %55)
br label %61
57: ; preds = %34
%58 = load i64, i64* @SIGUSR2
%59 = icmp eq i64 %36, %58
%60 = zext i1 %59 to i32
br label %61
61: ; preds = %57, %43, %54
%62 = phi i32 [ 0, %54 ], [ 0, %43 ], [ %60, %57 ]
store i32 0, i32* %24
br label %63
63: ; preds = %61, %28
%64 = phi i32 [ %62, %61 ], [ 0, %28 ]
%65 = icmp eq i32 %64, 0
br i1 %65, label %28, label %66
66: ; preds = %63
ret i32 0
}