#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 sigaction {
  int /*<<< orphan*/ sa_mask;
  scalar_t__ sa_flags;
  scalar_t__ sa_handler;
};

/* Variables and functions */
int /*<<< orphan*/ SIGUSR1;
int getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int sigaction(int /*<<< orphan*/, struct sigaction *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int main() {
  struct sigaction sa;
  int ret;
  ret = open("does-not-exist1", 0);
  if (ret != getpid()) {
    printf("open() system call not changed, ret = %d\n", ret);
    return 1;
  }
  sa.sa_handler = 0;
  sa.sa_flags = 0;
  sigemptyset(&sa.sa_mask);
  ret = sigaction(SIGUSR1, &sa, 0);
  if (ret != getpid()) {
    printf("sigaction() system call not changed, ret = %d\n", ret);
    return 1;
  }
  open("does-not-exist2", 0);
  printf("open was not changed to exit\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
je     11a7 <main+0x57>
mov    -0x24(%rbp),%esi
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1231 <main+0xe1>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <sigemptyset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
je     120e <main+0xbe>
mov    -0x24(%rbp),%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1231 <main+0xe1>
lea    0xe54(%rip),%rdi
xor    %esi,%esi
call   1070 <open@plt>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0xe94(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
cmp    %eax,%ebx
jne    11d1 <main+0x71>
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sigemptyset@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
cmp    %eax,%ebx
jne    11da <main+0x7a>
lea    0xead(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
lea    0xeaf(%rip),%rdi
call   1030 <puts@plt>
jmp    11ea <main+0x8a>
lea    0xe38(%rip),%rdi
jmp    11e1 <main+0x81>
lea    0xe59(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sigaction = type { i32, i64, i64 }
@.str = [16 x i8] c"does-not-exist1\00"
@.str.1 = [42 x i8] c"open() system call not changed, ret = %d\0A\00"
@SIGUSR1 = global i32 0
@.str.2 = [47 x i8] c"sigaction() system call not changed, ret = %d\0A\00"
@.str.3 = [16 x i8] c"does-not-exist2\00"
@.str.4 = [30 x i8] c"open was not changed to exit\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sigaction
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = call i32 (...) @getpid()
%7 = icmp ne i32 %5, %6
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* %3
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %9)
store i32 1, i32* %1
br label %27
11: ; preds = %0
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 2
store i64 0, i64* %12
%13 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 1
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 0
%15 = call i32 @sigemptyset(i32* %14)
%16 = load i32, i32* @SIGUSR1
%17 = call i32 @sigaction(i32 %16, %struct.sigaction* %2, i32 0)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = call i32 (...) @getpid()
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %11
%22 = load i32, i32* %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %22)
store i32 1, i32* %1
br label %27
24: ; preds = %11
%25 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 0)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %27
27: ; preds = %24, %21, %8
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i64, i64 }
@.str = [16 x i8] c"does-not-exist1\00"
@.str.1 = [42 x i8] c"open() system call not changed, ret = %d\0A\00"
@SIGUSR1 = global i32 0
@.str.2 = [47 x i8] c"sigaction() system call not changed, ret = %d\0A\00"
@.str.3 = [16 x i8] c"does-not-exist2\00"
@str = [29 x i8] c"open was not changed to exit\00"
define i32 @main() {
%1 = alloca %struct.sigaction
%2 = bitcast %struct.sigaction* %1 to i8*
%3 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = tail call i32 (...) @getpid()
%5 = icmp eq i32 %3, %4
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %3)
br label %22
8: ; preds = %0
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 1
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 0
%11 = bitcast i64* %9 to i8*
%12 = call i32 @sigemptyset(i32* %10)
%13 = load i32, i32* @SIGUSR1
%14 = call i32 @sigaction(i32 %13, %struct.sigaction* %1, i32 0)
%15 = call i32 (...) @getpid()
%16 = icmp eq i32 %14, %15
br i1 %16, label %19, label %17
17: ; preds = %8
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %22
19: ; preds = %8
%20 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 0)
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %19, %17, %6
ret i32 1
}