#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 O_APPEND;
int O_CREAT;
int O_RDWR;
int SIGINT;
int SIGUSR1;
int SIGUSR2;
int /*<<< orphan*/ STDOUT_FILENO;
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, char *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int *);
void signalhandler(int signo) {
  if (signo == SIGUSR1) {
    printf("who... \n");
    int fd = open("whooutput", O_RDWR | O_APPEND | O_CREAT, 0666);
    int redirstdout = dup(STDOUT_FILENO);
    dup2(fd, STDOUT_FILENO);
    int pid = fork();
    if (pid == 0) {
      execl("/usr/bin/who", "who", NULL);
    }
    dup2(redirstdout, STDOUT_FILENO);
  } else if (signo == SIGUSR2) {
    printf("ps... \n");
    int fd = open("psoutput", O_RDWR | O_APPEND | O_CREAT, 0666);
    int redirstdout = dup(STDOUT_FILENO);
    dup2(fd, STDOUT_FILENO);
    int pid = fork();
    if (pid == 0) {
      execl("/bin/ps", "ps", NULL);
    }
    dup2(redirstdout, STDOUT_FILENO);
  } else if (signo == SIGINT) {
    int s;
    int pid = fork();
    if (pid == 0) {
      execl("/bin/cat", "cat", "whooutput", NULL);
      exit(0);
    } else {
      wait(&s);
    }
    int pid2 = fork();
    if (pid2 == 0) {
      execl("/bin/cat", "cat", "psoutput", NULL);
      exit(0);
    } else {
      wait(&s);
    }
    exit(0);
  }
}


x86 assembly (clang, -O0):
<signalhandler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%eax
jne    123d <signalhandler+0xbd>
lea    0xe5c(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
or     (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
lea    0xe3e(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1050 <dup@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1227 <signalhandler+0xa7>
lea    0xdfe(%rip),%rdi
lea    0xe00(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   1080 <execl@plt>
mov    -0xc(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
jmp    139a <signalhandler+0x21a>
mov    -0x4(%rbp),%eax
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%eax
jne    12ef <signalhandler+0x16f>
lea    0xdca(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    0x2d3d(%rip),%rax
or     (%rax),%esi
mov    0x2d84(%rip),%rax
or     (%rax),%esi
lea    0xdab(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   1050 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    12d9 <signalhandler+0x159>
lea    0xd6a(%rip),%rdi
lea    0xd68(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   1080 <execl@plt>
mov    -0x18(%rbp),%edi
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
jmp    1395 <signalhandler+0x215>
mov    -0x4(%rbp),%eax
mov    0x2ccf(%rip),%rcx
cmp    (%rcx),%eax
jne    1390 <signalhandler+0x210>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    133c <signalhandler+0x1bc>
lea    0xd1d(%rip),%rdi
lea    0xd1b(%rip),%rsi
lea    0xcdf(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   1080 <execl@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    -0x20(%rbp),%rdi
call   1090 <wait@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1380 <signalhandler+0x200>
lea    0xcd9(%rip),%rdi
lea    0xcd7(%rip),%rsi
lea    0xcba(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   1080 <execl@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    -0x20(%rbp),%rdi
call   1090 <wait@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1395 <signalhandler+0x215>
jmp    139a <signalhandler+0x21a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signalhandler>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e41(%rip),%rax
cmp    %edi,(%rax)
jne    1206 <signalhandler+0x86>
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e32(%rip),%rax
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
lea    0xe3f(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,%ebp
mov    0x2e14(%rip),%rbx
mov    (%rbx),%edi
call   1050 <dup@plt>
mov    %eax,%r14d
mov    (%rbx),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    128b <signalhandler+0x10b>
lea    0xe0d(%rip),%rdi
lea    0xe0f(%rip),%rsi
jmp    1282 <signalhandler+0x102>
mov    0x2dab(%rip),%rax
cmp    %edi,(%rax)
jne    12a4 <signalhandler+0x124>
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
mov    0x2db0(%rip),%rax
mov    0x2d79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dbe(%rip),%rax
or     (%rax),%esi
lea    0xdd4(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,%ebp
mov    0x2d92(%rip),%rbx
mov    (%rbx),%edi
call   1050 <dup@plt>
mov    %eax,%r14d
mov    (%rbx),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    128b <signalhandler+0x10b>
lea    0xda5(%rip),%rdi
lea    0xda3(%rip),%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1080 <execl@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <dup2@plt>
mov    0x2d1d(%rip),%rax
cmp    %edi,(%rax)
je     12b8 <signalhandler+0x138>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    12da <signalhandler+0x15a>
lea    0xd5e(%rip),%rdi
lea    0xd5c(%rip),%rsi
lea    0xd28(%rip),%rdx
jmp    1304 <signalhandler+0x184>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    1314 <signalhandler+0x194>
lea    0xd32(%rip),%rdi
lea    0xd30(%rip),%rsi
lea    0xd13(%rip),%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1080 <execl@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@SIGUSR1 = global i32 0
@.str = [9 x i8] c"who... \0A\00"
@.str.1 = [10 x i8] c"whooutput\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [13 x i8] c"/usr/bin/who\00"
@.str.3 = [4 x i8] c"who\00"
@SIGUSR2 = global i32 0
@.str.4 = [8 x i8] c"ps... \0A\00"
@.str.5 = [9 x i8] c"psoutput\00"
@.str.6 = [8 x i8] c"/bin/ps\00"
@.str.7 = [3 x i8] c"ps\00"
@SIGINT = global i32 0
@.str.8 = [9 x i8] c"/bin/cat\00"
@.str.9 = [4 x i8] c"cat\00"
define void @signalhandler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %2
%12 = load i32, i32* %2
%13 = load i32, i32* @SIGUSR1
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %37
15: ; preds = %1
%16 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_APPEND
%19 = or i32 %17, %18
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 438)
store i32 %22, i32* %3
%23 = load i32, i32* @STDOUT_FILENO
%24 = call i32 @dup(i32 %23)
store i32 %24, i32* %4
%25 = load i32, i32* %3
%26 = load i32, i32* @STDOUT_FILENO
%27 = call i32 @dup2(i32 %25, i32 %26)
%28 = call i32 (...) @fork()
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %15
%32 = call i32 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* null)
br label %33
33: ; preds = %31, %15
%34 = load i32, i32* %4
%35 = load i32, i32* @STDOUT_FILENO
%36 = call i32 @dup2(i32 %34, i32 %35)
br label %89
37: ; preds = %1
%38 = load i32, i32* %2
%39 = load i32, i32* @SIGUSR2
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %63
41: ; preds = %37
%42 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%43 = load i32, i32* @O_RDWR
%44 = load i32, i32* @O_APPEND
%45 = or i32 %43, %44
%46 = load i32, i32* @O_CREAT
%47 = or i32 %45, %46
%48 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %47, i32 438)
store i32 %48, i32* %6
%49 = load i32, i32* @STDOUT_FILENO
%50 = call i32 @dup(i32 %49)
store i32 %50, i32* %7
%51 = load i32, i32* %6
%52 = load i32, i32* @STDOUT_FILENO
%53 = call i32 @dup2(i32 %51, i32 %52)
%54 = call i32 (...) @fork()
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %41
%58 = call i32 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* null)
br label %59
59: ; preds = %57, %41
%60 = load i32, i32* %7
%61 = load i32, i32* @STDOUT_FILENO
%62 = call i32 @dup2(i32 %60, i32 %61)
br label %88
63: ; preds = %37
%64 = load i32, i32* %2
%65 = load i32, i32* @SIGINT
%66 = icmp eq i32 %64, %65
br i1 %66, label %67, label %87
67: ; preds = %63
%68 = call i32 (...) @fork()
store i32 %68, i32* %10
%69 = load i32, i32* %10
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %67
%72 = call i32 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* null)
%73 = call i32 @exit(i32 0)
unreachable
74: ; preds = %67
%75 = call i32 @wait(i32* %9)
br label %76
76: ; preds = %74
%77 = call i32 (...) @fork()
store i32 %77, i32* %11
%78 = load i32, i32* %11
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %76
%81 = call i32 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* null)
%82 = call i32 @exit(i32 0)
unreachable
83: ; preds = %76
%84 = call i32 @wait(i32* %9)
br label %85
85: ; preds = %83
%86 = call i32 @exit(i32 0)
unreachable
87: ; preds = %63
br label %88
88: ; preds = %87, %59
br label %89
89: ; preds = %88, %33
ret void
}

LLVM IR (-O2):
@SIGUSR1 = global i32 0
@.str.1 = [10 x i8] c"whooutput\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [13 x i8] c"/usr/bin/who\00"
@.str.3 = [4 x i8] c"who\00"
@SIGUSR2 = global i32 0
@.str.5 = [9 x i8] c"psoutput\00"
@.str.6 = [8 x i8] c"/bin/ps\00"
@.str.7 = [3 x i8] c"ps\00"
@SIGINT = global i32 0
@.str.8 = [9 x i8] c"/bin/cat\00"
@.str.9 = [4 x i8] c"cat\00"
@str = [7 x i8] c"ps... \00"
@str.10 = [8 x i8] c"who... \00"
define void @signalhandler(i32 %0) {
%2 = alloca i32
%3 = load i32, i32* @SIGUSR1
%4 = icmp eq i32 %3, %0
br i1 %4, label %5, label %21
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.10, i64 0, i64 0))
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_APPEND
%9 = or i32 %8, %7
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 438)
%13 = load i32, i32* @STDOUT_FILENO
%14 = tail call i32 @dup(i32 %13)
%15 = load i32, i32* @STDOUT_FILENO
%16 = tail call i32 @dup2(i32 %12, i32 %15)
%17 = tail call i32 (...) @fork()
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %60
19: ; preds = %5
%20 = tail call i32 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* null)
br label %60
21: ; preds = %1
%22 = load i32, i32* @SIGUSR2
%23 = icmp eq i32 %22, %0
br i1 %23, label %24, label %40
24: ; preds = %21
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%26 = load i32, i32* @O_RDWR
%27 = load i32, i32* @O_APPEND
%28 = or i32 %27, %26
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %30, i32 438)
%32 = load i32, i32* @STDOUT_FILENO
%33 = tail call i32 @dup(i32 %32)
%34 = load i32, i32* @STDOUT_FILENO
%35 = tail call i32 @dup2(i32 %31, i32 %34)
%36 = tail call i32 (...) @fork()
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %60
38: ; preds = %24
%39 = tail call i32 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* null)
br label %60
40: ; preds = %21
%41 = load i32, i32* @SIGINT
%42 = icmp eq i32 %41, %0
br i1 %42, label %43, label %64
43: ; preds = %40
%44 = bitcast i32* %2 to i8*
%45 = tail call i32 (...) @fork()
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = tail call i32 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* null)
%49 = tail call i32 @exit(i32 0)
unreachable
50: ; preds = %43
%51 = call i32 @wait(i32* %2)
%52 = call i32 (...) @fork()
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = call i32 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* null)
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %50
%58 = call i32 @wait(i32* %2)
%59 = call i32 @exit(i32 0)
unreachable
60: ; preds = %24, %38, %5, %19
%61 = phi i32 [ %14, %19 ], [ %14, %5 ], [ %33, %38 ], [ %33, %24 ]
%62 = load i32, i32* @STDOUT_FILENO
%63 = tail call i32 @dup2(i32 %61, i32 %62)
br label %64
64: ; preds = %60, %40
ret void
}