#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 event {
  int dummy;
};

/* Variables and functions */
int EVENT_SIGNAL(struct event *);
int /*<<< orphan*/ event_del(struct event *);
int /*<<< orphan*/ printf(char *, ...);
void signalhandler(int fd, short type, void *arg) {
  struct event *signal_event = (struct event *)arg;
  static int count = 0;
  printf("in function signal_cb...\n");
  printf("handle signal: %d\n", EVENT_SIGNAL(signal_event));
  if (count >= 3)
    event_del(signal_event);
  count++;
  struct event *ev = arg;
  printf("SIGINT triggered!!!\n");
}


x86 assembly (clang, -O0):
<signalhandler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <EVENT_SIGNAL@plt>
mov    %eax,%esi
lea    0xeac(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x3,0x2ec0(%rip)
jl     118b <signalhandler+0x5b>
mov    -0x18(%rbp),%rdi
call   1040 <event_del@plt>
mov    0x2eab(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2ea2(%rip)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signalhandler>:
push   %rbx
mov    %rdx,%rbx
lea    0xec8(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <EVENT_SIGNAL@plt>
lea    0xea1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2ed6(%rip),%eax
cmp    $0x3,%eax
jl     1181 <signalhandler+0x41>
mov    %rbx,%rdi
call   1050 <event_del@plt>
mov    0x2ec3(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2eba(%rip)
lea    0xe9b(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
%struct.event = type { i32 }
@signalhandler.count = internal global i32 0
@.str = [26 x i8] c"in function signal_cb...\0A\00"
@.str.1 = [19 x i8] c"handle signal: %d\0A\00"
@.str.2 = [21 x i8] c"SIGINT triggered!!!\0A\00"
define void @signalhandler(i32 %0, i16 signext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i16
%6 = alloca i8*
%7 = alloca %struct.event*
%8 = alloca %struct.event*
store i32 %0, i32* %4
store i16 %1, i16* %5
store i8* %2, i8** %6
%9 = load i8*, i8** %6
%10 = bitcast i8* %9 to %struct.event*
store %struct.event* %10, %struct.event** %7
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%12 = load %struct.event*, %struct.event** %7
%13 = call i32 @EVENT_SIGNAL(%struct.event* %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @signalhandler.count
%16 = icmp sge i32 %15, 3
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load %struct.event*, %struct.event** %7
%19 = call i32 @event_del(%struct.event* %18)
br label %20
20: ; preds = %17, %3
%21 = load i32, i32* @signalhandler.count
%22 = add nsw i32 %21, 1
store i32 %22, i32* @signalhandler.count
%23 = load i8*, i8** %6
%24 = bitcast i8* %23 to %struct.event*
store %struct.event* %24, %struct.event** %8
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.event = type { i32 }
@signalhandler.count = internal unnamed_addr global i32 0
@.str.1 = [19 x i8] c"handle signal: %d\0A\00"
@str = [25 x i8] c"in function signal_cb...\00"
@str.3 = [20 x i8] c"SIGINT triggered!!!\00"
define void @signalhandler(i32 %0, i16 signext %1, i8* %2) {
%4 = bitcast i8* %2 to %struct.event*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @EVENT_SIGNAL(%struct.event* %4)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @signalhandler.count
%9 = icmp sgt i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = tail call i32 @event_del(%struct.event* %4)
%12 = load i32, i32* @signalhandler.count
br label %13
13: ; preds = %10, %3
%14 = phi i32 [ %12, %10 ], [ %8, %3 ]
%15 = add nsw i32 %14, 1
store i32 %15, i32* @signalhandler.count
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.3, i64 0, i64 0))
ret void
}