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

/* Variables and functions */
void SIG_ERR(int);
int sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
void (*my_signal(int sig, void (*func)(int)))(int) {
  struct sigaction act, oact;
  act.sa_handler = func;
  sigemptyset(&act.sa_mask);
  act.sa_flags = 0;
  if (sigaction(sig, &act, &oact) == -1) {
    return SIG_ERR;
  }
  return oact.sa_handler;
}


x86 assembly (clang, -O0):
<my_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x10,%rdi
call   1040 <sigemptyset@plt>
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    1175 <my_signal+0x55>
mov    0x2e74(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    117d <my_signal+0x5d>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_signal>:
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebx
mov    %rsi,(%rsp)
lea    0x10(%rsp),%rdi
call   1040 <sigemptyset@plt>
movq   $0x0,0x8(%rsp)
mov    %rsp,%rsi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    115b <my_signal+0x3b>
mov    0x2e87(%rip),%rax
jmp    1160 <my_signal+0x40>
mov    0x18(%rsp),%rax
add    $0x30,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i64, i32 }
define void (i32)* @my_signal(i32 %0, void (i32)* %1) {
%3 = alloca %struct.sigaction
%4 = alloca %struct.sigaction
%5 = bitcast %struct.sigaction* %3 to i8*
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store void (i32)* %1, void (i32)** %7
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
%9 = call i32 @sigemptyset(i32* %8)
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i64 0, i64* %10
%11 = call i32 @sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %4)
%12 = icmp eq i32 %11, -1
%13 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
%14 = load void (i32)*, void (i32)** %13
%15 = select i1 %12, void (i32)* @SIG_ERR, void (i32)* %14
ret void (i32)* %15
}