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

/* Variables and functions */
void SIG_ERR(int);
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
void (*signal(int signum, void (*handler)(int)))(int) {
  struct sigaction action;
  action.sa_handler = handler;
  action.sa_flags = 0;
  sigemptyset(&action.sa_mask);
  struct sigaction old;
  if (sigaction(signum, &action, &old) < 0) {
    return SIG_ERR;
  }
  return old.sa_handler;
}


x86 assembly (clang, -O0):
<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)
movq   $0x0,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
call   1040 <sigemptyset@plt>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    1176 <signal+0x56>
mov    0x2e73(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    117e <signal+0x5e>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

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

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32, i64 }
define void (i32)* @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
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
%12 = call i32 @sigemptyset(i32* %11)
%13 = load i32, i32* %4
%14 = call i64 @sigaction(i32 %13, %struct.sigaction* %6, %struct.sigaction* %7)
%15 = icmp slt i64 %14, 0
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)*, i32, i64 }
define void (i32)* @signal(i32 %0, void (i32)* %1) {
%3 = alloca %struct.sigaction
%4 = alloca %struct.sigaction
%5 = bitcast %struct.sigaction* %3 to i8*
%6 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store void (i32)* %1, void (i32)** %6
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
%9 = call i32 @sigemptyset(i32* %8)
%10 = bitcast %struct.sigaction* %4 to i8*
%11 = call i64 @sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %4)
%12 = icmp slt i64 %11, 0
%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
}