#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 cornet_read(int, char *, int);
int /*<<< orphan*/ cornet_write(int, char *, int);
int /*<<< orphan*/ printf(char *);
int snprintf(char *, int, char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
void handler2(int fd) {
  char buf[256];
  int n;
  n = snprintf(buf, 256, "Welcome abroad (handler2)\n\n");
  cornet_write(fd, buf, n < 256 ? n : 256);
  while ((n = cornet_read(fd, buf, 256)) > 0) {
    buf[n] = 0;
    if (!strncmp(buf, "quit", 4)) {
      break;
    }
    cornet_write(fd, buf, n);
  }
  printf("Bye (handler2)\n");
}


x86 assembly (clang, -O0):
<handler2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
lea    0xe8f(%rip),%rdx
call   1060 <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    11b4 <handler2+0x64>
mov    -0x114(%rbp),%eax
mov    %eax,-0x128(%rbp)
jmp    11c4 <handler2+0x74>
mov    $0x100,%eax
mov    %eax,-0x128(%rbp)
jmp    11c4 <handler2+0x74>
mov    -0x120(%rbp),%rsi
mov    -0x124(%rbp),%edi
mov    -0x128(%rbp),%edx
call   1040 <cornet_write@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1070 <cornet_read@plt>
mov    %eax,-0x114(%rbp)
cmp    $0x0,%eax
jle    124e <handler2+0xfe>
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
lea    0xe00(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1234 <handler2+0xe4>
jmp    124e <handler2+0xfe>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x114(%rbp),%edx
call   1040 <cornet_write@plt>
jmp    11dc <handler2+0x8c>
lea    0xdcc(%rip),%rdi
call   1050 <printf@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handler2>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edi,%r15d
lea    0xe99(%rip),%rdx
mov    %rsp,%rbx
mov    $0x100,%ebp
mov    %rbx,%rdi
mov    $0x100,%esi
call   1060 <snprintf@plt>
cmp    $0x100,%eax
cmovge %ebp,%eax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <cornet_write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1070 <cornet_read@plt>
test   %eax,%eax
jle    11fd <handler2+0xad>
mov    %eax,%ebp
lea    0xe6e(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     11fd <handler2+0xad>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <cornet_write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1070 <cornet_read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11c0 <handler2+0x70>
lea    0xe1d(%rip),%rdi
call   1040 <puts@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Welcome abroad (handler2)\0A\0A\00"
@.str.1 = [5 x i8] c"quit\00"
@.str.2 = [16 x i8] c"Bye (handler2)\0A\00"
define void @handler2(i32 %0) {
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i32
store i32 %0, i32* %2
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 %6, i32* %4
%7 = load i32, i32* %2
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 256
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i32, i32* %4
br label %14
13: ; preds = %1
br label %14
14: ; preds = %13, %11
%15 = phi i32 [ %12, %11 ], [ 256, %13 ]
%16 = call i32 @cornet_write(i32 %7, i8* %8, i32 %15)
br label %17
17: ; preds = %30, %14
%18 = load i32, i32* %2
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%20 = call i32 @cornet_read(i32 %18, i8* %19, i32 256)
store i32 %20, i32* %4
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %17
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %24
store i8 0, i8* %25
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%27 = call i32 @strncmp(i8* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %22
br label %35
30: ; preds = %22
%31 = load i32, i32* %2
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%33 = load i32, i32* %4
%34 = call i32 @cornet_write(i32 %31, i8* %32, i32 %33)
br label %17
35: ; preds = %29, %17
%36 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"Welcome abroad (handler2)\0A\0A\00"
@.str.1 = [5 x i8] c"quit\00"
@str = [15 x i8] c"Bye (handler2)\00"
define void @handler2(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 256, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%5 = icmp slt i32 %4, 256
%6 = select i1 %5, i32 %4, i32 256
%7 = call i32 @cornet_write(i32 %0, i8* %3, i32 %6)
%8 = call i32 @cornet_read(i32 %0, i8* %3, i32 256)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %1, %16
%11 = phi i32 [ %18, %16 ], [ %8, %1 ]
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @strncmp(i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %10
%17 = call i32 @cornet_write(i32 %0, i8* %3, i32 %11)
%18 = call i32 @cornet_read(i32 %0, i8* %3, i32 256)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %10, label %20
20: ; preds = %16, %10, %1
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
ret void
}