#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_flags;
};

/* Variables and functions */
int /*<<< orphan*/ SA_RESTART;
void SIG_ERR(int);
scalar_t__ __sigaction(int, struct sigaction *, struct sigaction *);
void (*signal(int sig, void (*func)(int)))(int) {
  struct sigaction sa_old, sa = {.sa_handler = func, .sa_flags = SA_RESTART};
  if (__sigaction(sig, &sa, &sa_old) < 0)
    return SIG_ERR;
  return sa_old.sa_handler;
}


x86 assembly (clang, -O0):
<signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2eba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1030 <__sigaction@plt>
cmp    $0x0,%rax
jge    115d <signal+0x4d>
mov    0x2e84(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1165 <signal+0x55>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal>:
sub    $0x28,%rsp
mov    %rsi,0x8(%rsp)
mov    0x2ec8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
call   1030 <__sigaction@plt>
test   %rax,%rax
jns    1146 <signal+0x36>
mov    0x2e97(%rip),%rax
add    $0x28,%rsp
ret
mov    0x18(%rsp),%rax
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32 }
@SA_RESTART = global i32 0
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 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%9 = load void (i32)*, void (i32)** %5
store void (i32)* %9, void (i32)** %8
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
%11 = load i32, i32* @SA_RESTART
store i32 %11, i32* %10
%12 = load i32, i32* %4
%13 = call i64 @__sigaction(i32 %12, %struct.sigaction* %7, %struct.sigaction* %6)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store void (i32)* @SIG_ERR, void (i32)** %3
br label %19
16: ; preds = %2
%17 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
%18 = load void (i32)*, void (i32)** %17
store void (i32)* %18, void (i32)** %3
br label %19
19: ; preds = %16, %15
%20 = load void (i32)*, void (i32)** %3
ret void (i32)* %20
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32 }
@SA_RESTART = global i32 0
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 = bitcast %struct.sigaction* %4 to i8*
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store void (i32)* %1, void (i32)** %7
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
%9 = load i32, i32* @SA_RESTART
store i32 %9, i32* %8
%10 = call i64 @__sigaction(i32 %0, %struct.sigaction* %4, %struct.sigaction* %3)
%11 = icmp slt i64 %10, 0
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
%13 = load void (i32)*, void (i32)** %12
%14 = select i1 %11, void (i32)* @SIG_ERR, void (i32)* %13
ret void (i32)* %14
}