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

/* Variables and functions */
int /*<<< orphan*/ SA_RESTART;
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
void *my_signal(int signum, void *handler, int restartsyscall) {
  struct sigaction sa, osa;
  sa.sa_handler = handler;
  sigemptyset(&sa.sa_mask);
  if (restartsyscall) {
    sa.sa_flags = SA_RESTART;
  } else {
  }
  if (sigaction(signum, &sa, &osa) < 0)
    return (void *)-1;
  return osa.sa_handler;
}


x86 assembly (clang, -O0):
<my_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
add    $0xc,%rdi
call   1040 <sigemptyset@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1162 <my_signal+0x42>
mov    0x2e90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    1167 <my_signal+0x47>
jmp    1167 <my_signal+0x47>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    1191 <my_signal+0x71>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1199 <my_signal+0x79>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_signal>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebp
mov    %edi,%ebx
mov    %rsi,0x8(%rsp)
lea    0x14(%rsp),%rdi
call   1040 <sigemptyset@plt>
test   %ebp,%ebp
je     114a <my_signal+0x2a>
mov    0x2ea4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   1030 <sigaction@plt>
mov    %rax,%rcx
mov    $0xffffffffffffffff,%rax
test   %rcx,%rcx
js     116f <my_signal+0x4f>
mov    0x18(%rsp),%rax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { i8*, i32, i32 }
@SA_RESTART = global i32 0
define i8* @my_signal(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sigaction
%9 = alloca %struct.sigaction
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %8, i32 0, i32 0
store i8* %10, i8** %11
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %8, i32 0, i32 2
%13 = call i32 @sigemptyset(i32* %12)
%14 = load i32, i32* %7
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i32, i32* @SA_RESTART
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %8, i32 0, i32 1
store i32 %17, i32* %18
br label %20
19: ; preds = %3
br label %20
20: ; preds = %19, %16
%21 = load i32, i32* %5
%22 = call i64 @sigaction(i32 %21, %struct.sigaction* %8, %struct.sigaction* %9)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i8* inttoptr (i64 -1 to i8*), i8** %4
br label %28
25: ; preds = %20
%26 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 0
%27 = load i8*, i8** %26
store i8* %27, i8** %4
br label %28
28: ; preds = %25, %24
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
%struct.sigaction = type { i8*, i32, i32 }
@SA_RESTART = global i32 0
define i8* @my_signal(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sigaction
%5 = alloca %struct.sigaction
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = bitcast %struct.sigaction* %5 to i8*
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store i8* %1, i8** %8
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
%10 = call i32 @sigemptyset(i32* %9)
%11 = icmp eq i32 %2, 0
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i32, i32* @SA_RESTART
%14 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i32 %13, i32* %14
br label %15
15: ; preds = %3, %12
%16 = call i64 @sigaction(i32 %0, %struct.sigaction* %4, %struct.sigaction* %5)
%17 = icmp slt i64 %16, 0
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = select i1 %17, i8* inttoptr (i64 -1 to i8*), i8* %19
ret i8* %20
}