#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 */

/* Variables and functions */
int /*<<< orphan*/ cornet_debug(int);
int cornet_read(int, char *, int);
int /*<<< orphan*/ cornet_signal(int);
int /*<<< orphan*/ cornet_signalall();
int /*<<< orphan*/ cornet_write(int, char *, int);
int /*<<< orphan*/ printf(char *);
int snprintf(char *, int, char *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ strncmp(char *, char *, int);
void handler1(int fd) {
  char buf[256];
  int n, id;
  n = snprintf(buf, 256, "Welcome abroad (handler1)\n");
  cornet_write(fd, buf, n < 256 ? n : 256);
  n = snprintf(buf, 256,
               "Available commands are: 'debug'', 'signal N (where N equals "
               "task's id) and 'quit'\n\n");
  cornet_write(fd, buf, n < 256 ? n : 256);
  while ((n = cornet_read(fd, buf, 256 - 1)) > 0) {
    buf[n] = 0;
    if (!strncmp(buf, "quit", 4)) {
      cornet_write(fd, "handler1: quit\n", sizeof("handler1: quit\n") - 1);
      cornet_signalall();
      break;
    } else if (!strncmp(buf, "signal", 6)) {
      id = 0;
      sscanf(buf, "signal %d", &id);
      cornet_signal(id);
    } else if (!strncmp(buf, "debug", 5)) {
      cornet_debug(fd);
    } else if (n > 2) {
      cornet_write(fd, "unknown command: ", sizeof("unknown command: ") - 1);
      cornet_write(fd, buf, n);
    }
  }
  printf("Bye (handler1)\n");
}


x86 assembly (clang, -O0):
<handler1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
lea    0xe4f(%rip),%rdx
call   1080 <snprintf@plt>
mov    %eax,-0x114(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x124(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x120(%rbp)
cmpl   $0x100,-0x114(%rbp)
jge    11f4 <handler1+0x64>
mov    -0x114(%rbp),%eax
mov    %eax,-0x128(%rbp)
jmp    1204 <handler1+0x74>
mov    $0x100,%eax
mov    %eax,-0x128(%rbp)
jmp    1204 <handler1+0x74>
mov    -0x120(%rbp),%rsi
mov    -0x124(%rbp),%edi
mov    -0x128(%rbp),%edx
call   1050 <cornet_write@plt>
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
lea    0xdec(%rip),%rdx
call   1080 <snprintf@plt>
mov    %eax,-0x114(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x134(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x130(%rbp)
cmpl   $0x100,-0x114(%rbp)
jge    1272 <handler1+0xe2>
mov    -0x114(%rbp),%eax
mov    %eax,-0x138(%rbp)
jmp    1282 <handler1+0xf2>
mov    $0x100,%eax
mov    %eax,-0x138(%rbp)
jmp    1282 <handler1+0xf2>
mov    -0x130(%rbp),%rsi
mov    -0x134(%rbp),%edi
mov    -0x138(%rbp),%edx
call   1050 <cornet_write@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
call   10b0 <cornet_read@plt>
mov    %eax,-0x114(%rbp)
cmp    $0x0,%eax
jle    13da <handler1+0x24a>
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
lea    0xd95(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    130d <handler1+0x17d>
mov    -0x4(%rbp),%edi
lea    0xd7d(%rip),%rsi
mov    $0xf,%edx
call   1050 <cornet_write@plt>
mov    $0x0,%al
call   1040 <cornet_signalall@plt>
jmp    13da <handler1+0x24a>
lea    -0x110(%rbp),%rdi
lea    0xd69(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1362 <handler1+0x1d2>
movl   $0x0,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xd45(%rip),%rsi
lea    -0x118(%rbp),%rdx
call   10a0 <sscanf@plt>
mov    -0x118(%rbp),%edi
call   1090 <cornet_signal@plt>
jmp    13d0 <handler1+0x240>
lea    -0x110(%rbp),%rdi
lea    0xd25(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1390 <handler1+0x200>
mov    -0x4(%rbp),%edi
call   1070 <cornet_debug@plt>
jmp    13cb <handler1+0x23b>
cmpl   $0x2,-0x114(%rbp)
jle    13c6 <handler1+0x236>
mov    -0x4(%rbp),%edi
lea    0xcf4(%rip),%rsi
mov    $0x11,%edx
call   1050 <cornet_write@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x114(%rbp),%edx
call   1050 <cornet_write@plt>
jmp    13cb <handler1+0x23b>
jmp    13d0 <handler1+0x240>
jmp    13d5 <handler1+0x245>
jmp    129a <handler1+0x10a>
lea    0xccc(%rip),%rdi
call   1060 <printf@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handler1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edi,%ebp
lea    0xe56(%rip),%rdx
lea    0x10(%rsp),%r14
mov    $0x100,%ebx
mov    %r14,%rdi
mov    $0x100,%esi
call   1080 <snprintf@plt>
cmp    $0x100,%eax
cmovge %ebx,%eax
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <cornet_write@plt>
lea    0xe3f(%rip),%rdx
mov    %r14,%rdi
mov    $0x100,%esi
call   1080 <snprintf@plt>
cmp    $0x100,%eax
cmovl  %eax,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1060 <cornet_write@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xff,%edx
call   10b0 <cornet_read@plt>
test   %eax,%eax
jle    1316 <handler1+0x186>
mov    %eax,%r13d
lea    0xe51(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xe5a(%rip),%r15
lea    0xe64(%rip),%r12
jmp    127f <handler1+0xef>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movl   $0x0,0xc(%rsp)
mov    %rbx,%rdi
lea    0xe39(%rip),%rsi
lea    0xc(%rsp),%rdx
call   10a0 <sscanf@plt>
mov    0xc(%rsp),%edi
call   1090 <cornet_signal@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   10b0 <cornet_read@plt>
mov    %eax,%r13d
test   %eax,%eax
jle    1316 <handler1+0x186>
mov    %r13d,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     12fc <handler1+0x16c>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     1240 <handler1+0xb0>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     12f0 <handler1+0x160>
cmp    $0x3,%r13d
jb     1265 <handler1+0xd5>
mov    %ebp,%edi
lea    0xdc9(%rip),%rsi
mov    $0x11,%edx
call   1060 <cornet_write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1060 <cornet_write@plt>
jmp    1265 <handler1+0xd5>
xchg   %ax,%ax
mov    %ebp,%edi
call   1070 <cornet_debug@plt>
jmp    1265 <handler1+0xd5>
lea    0xd71(%rip),%rsi
mov    %ebp,%edi
mov    $0xf,%edx
call   1060 <cornet_write@plt>
xor    %eax,%eax
call   1050 <cornet_signalall@plt>
lea    0xd90(%rip),%rdi
call   1040 <puts@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Welcome abroad (handler1)\0A\00"
@.str.1 = [84 x i8] c"Available commands are: 'debug'', 'signal N (where N equals task's id) and 'quit'\0A\0A\00"
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [16 x i8] c"handler1: quit\0A\00"
@.str.4 = [7 x i8] c"signal\00"
@.str.5 = [10 x i8] c"signal %d\00"
@.str.6 = [6 x i8] c"debug\00"
@.str.7 = [18 x i8] c"unknown command: \00"
@.str.8 = [16 x i8] c"Bye (handler1)\0A\00"
define void @handler1(i32 %0) {
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 %7, i32* %4
%8 = load i32, i32* %2
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 256
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = load i32, i32* %4
br label %15
14: ; preds = %1
br label %15
15: ; preds = %14, %12
%16 = phi i32 [ %13, %12 ], [ 256, %14 ]
%17 = call i32 @cornet_write(i32 %8, i8* %9, i32 %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%19 = call i32 @snprintf(i8* %18, i32 256, i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.1, i64 0, i64 0))
store i32 %19, i32* %4
%20 = load i32, i32* %2
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 256
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = load i32, i32* %4
br label %27
26: ; preds = %15
br label %27
27: ; preds = %26, %24
%28 = phi i32 [ %25, %24 ], [ 256, %26 ]
%29 = call i32 @cornet_write(i32 %20, i8* %21, i32 %28)
br label %30
30: ; preds = %75, %27
%31 = load i32, i32* %2
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%33 = call i32 @cornet_read(i32 %31, i8* %32, i32 255)
store i32 %33, i32* %4
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %76
35: ; preds = %30
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %37
store i8 0, i8* %38
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%40 = call i32 @strncmp(i8* %39, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%41 = icmp ne i32 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %35
%43 = load i32, i32* %2
%44 = call i32 @cornet_write(i32 %43, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 15)
%45 = call i32 (...) @cornet_signalall()
br label %76
46: ; preds = %35
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%48 = call i32 @strncmp(i8* %47, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
%49 = icmp ne i32 %48, 0
br i1 %49, label %55, label %50
50: ; preds = %46
store i32 0, i32* %5
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%52 = call i32 @sscanf(i8* %51, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32* %5)
%53 = load i32, i32* %5
%54 = call i32 @cornet_signal(i32 %53)
br label %74
55: ; preds = %46
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%57 = call i32 @strncmp(i8* %56, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 5)
%58 = icmp ne i32 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %55
%60 = load i32, i32* %2
%61 = call i32 @cornet_debug(i32 %60)
br label %73
62: ; preds = %55
%63 = load i32, i32* %4
%64 = icmp sgt i32 %63, 2
br i1 %64, label %65, label %72
65: ; preds = %62
%66 = load i32, i32* %2
%67 = call i32 @cornet_write(i32 %66, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i32 17)
%68 = load i32, i32* %2
%69 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%70 = load i32, i32* %4
%71 = call i32 @cornet_write(i32 %68, i8* %69, i32 %70)
br label %72
72: ; preds = %65, %62
br label %73
73: ; preds = %72, %59
br label %74
74: ; preds = %73, %50
br label %75
75: ; preds = %74
br label %30
76: ; preds = %42, %30
%77 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"Welcome abroad (handler1)\0A\00"
@.str.1 = [84 x i8] c"Available commands are: 'debug'', 'signal N (where N equals task's id) and 'quit'\0A\0A\00"
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [16 x i8] c"handler1: quit\0A\00"
@.str.4 = [7 x i8] c"signal\00"
@.str.5 = [10 x i8] c"signal %d\00"
@.str.6 = [6 x i8] c"debug\00"
@.str.7 = [18 x i8] c"unknown command: \00"
@str = [15 x i8] c"Bye (handler1)\00"
define void @handler1(i32 %0) {
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%7 = icmp slt i32 %6, 256
%8 = select i1 %7, i32 %6, i32 256
%9 = call i32 @cornet_write(i32 %0, i8* %4, i32 %8)
%10 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp slt i32 %10, 256
%12 = select i1 %11, i32 %10, i32 256
%13 = call i32 @cornet_write(i32 %0, i8* %4, i32 %12)
%14 = call i32 @cornet_read(i32 %0, i8* %4, i32 255)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %45
16: ; preds = %1, %42
%17 = phi i32 [ %43, %42 ], [ %14, %1 ]
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i32 @strncmp(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @cornet_write(i32 %0, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 15)
%24 = call i32 (...) @cornet_signalall()
br label %45
25: ; preds = %16
%26 = call i32 @strncmp(i8* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %25
store i32 0, i32* %3
%29 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%30 = load i32, i32* %3
%31 = call i32 @cornet_signal(i32 %30)
br label %42
32: ; preds = %25
%33 = call i32 @strncmp(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 5)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @cornet_debug(i32 %0)
br label %42
37: ; preds = %32
%38 = icmp ugt i32 %17, 2
br i1 %38, label %39, label %42
39: ; preds = %37
%40 = call i32 @cornet_write(i32 %0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i32 17)
%41 = call i32 @cornet_write(i32 %0, i8* %4, i32 %17)
br label %42
42: ; preds = %28, %37, %39, %35
%43 = call i32 @cornet_read(i32 %0, i8* %4, i32 255)
%44 = icmp sgt i32 %43, 0
br i1 %44, label %16, label %45
45: ; preds = %42, %1, %22
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
ret void
}