#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 = {.sa_handler = func, .sa_flags = SA_RESTART};
  if (__sigaction(sig, &sa, &sa) < 0)
    return SIG_ERR;
  return sa.sa_handler;
}


x86 assembly (clang, -O0):
<signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2eba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rdx
mov    %rdx,%rsi
call   1030 <__sigaction@plt>
cmp    $0x0,%rax
jge    115c <signal+0x4c>
mov    0x2e85(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1164 <signal+0x54>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal>:
sub    $0x18,%rsp
mov    %rsi,0x8(%rsp)
mov    0x2ec8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rdx
mov    %rdx,%rsi
call   1030 <__sigaction@plt>
test   %rax,%rax
jns    1144 <signal+0x34>
mov    0x2e99(%rip),%rax
add    $0x18,%rsp
ret
mov    0x8(%rsp),%rax
add    $0x18,%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
store i32 %0, i32* %4
store void (i32)* %1, void (i32)** %5
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
%8 = load void (i32)*, void (i32)** %5
store void (i32)* %8, void (i32)** %7
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
%10 = load i32, i32* @SA_RESTART
store i32 %10, i32* %9
%11 = load i32, i32* %4
%12 = call i64 @__sigaction(i32 %11, %struct.sigaction* %6, %struct.sigaction* %6)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store void (i32)* @SIG_ERR, void (i32)** %3
br label %18
15: ; preds = %2
%16 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
%17 = load void (i32)*, void (i32)** %16
store void (i32)* %17, void (i32)** %3
br label %18
18: ; preds = %15, %14
%19 = load void (i32)*, void (i32)** %3
ret void (i32)* %19
}

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 = bitcast %struct.sigaction* %3 to i8*
%5 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store void (i32)* %1, void (i32)** %5
%6 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
%7 = load i32, i32* @SA_RESTART
store i32 %7, i32* %6
%8 = call i64 @__sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %3)
%9 = icmp slt i64 %8, 0
%10 = load void (i32)*, void (i32)** %5
%11 = select i1 %9, void (i32)* @SIG_ERR, void (i32)* %10
ret void (i32)* %11
}