#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ ENOMSG;
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, size_t *);
char *malloc(size_t);
scalar_t__ recv(int, char *, size_t, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
char *strtok(char *, char *);
int handleCommands(int sd) {
  size_t size;
  ssize_t rc;
  char *cmd;
  if (ioctl(sd, FIONREAD, &size) != 0) {
    return -1;
  }
  if (size == 0) {
    errno = ENOMSG;
    return -1;
  }
  cmd = malloc(size);
  if (cmd == NULL) {
    return -1;
  }
  rc = recv(sd, cmd, size, 0);
  if (rc < 0) {
    free(cmd);
    return -1;
  }
  char *s = strtok(cmd, " ");
  if (strcmp(s, "shutdown") == 0) {
    free(cmd);
    return 1;
  }
  if (strcmp(s, "setbcast") == 0) {
    s = strtok(NULL, " ");
    if (strcmp(s, "1") == 0) {
      free(cmd);
      return 2;
    }
    if (strcmp(s, "0") == 0) {
      free(cmd);
      return 3;
    }
  }
  free(cmd);
  return 0;
}


x86 assembly (clang, -O0):
<handleCommands>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     1196 <handleCommands+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fd <handleCommands+0x19d>
cmpq   $0x0,-0x10(%rbp)
jne    11bf <handleCommands+0x5f>
mov    0x2e28(%rip),%rax
mov    (%rax),%ecx
mov    0x2e27(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12fd <handleCommands+0x19d>
mov    -0x10(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e3 <handleCommands+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fd <handleCommands+0x19d>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1219 <handleCommands+0xb9>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fd <handleCommands+0x19d>
mov    -0x20(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdca(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    125c <handleCommands+0xfc>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fd <handleCommands+0x19d>
mov    -0x28(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12ed <handleCommands+0x18d>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd7f(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xd7f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12b9 <handleCommands+0x159>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x2,-0x4(%rbp)
jmp    12fd <handleCommands+0x19d>
mov    -0x28(%rbp),%rdi
lea    0xd52(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12e8 <handleCommands+0x188>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x3,-0x4(%rbp)
jmp    12fd <handleCommands+0x19d>
jmp    12ed <handleCommands+0x18d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handleCommands>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1050 <ioctl@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1193 <handleCommands+0x33>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rbp
test   %rbp,%rbp
je     120a <handleCommands+0xaa>
mov    %rbp,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1185 <handleCommands+0x25>
mov    %rax,%r14
mov    %ebx,%edi
mov    %rax,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %rax,%rax
js     1221 <handleCommands+0xc1>
lea    0xe39(%rip),%rsi
mov    %r14,%rdi
call   1080 <strtok@plt>
mov    %rax,%rbx
lea    0xe29(%rip),%rsi
mov    %rax,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     122e <handleCommands+0xce>
lea    0xe1e(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1241 <handleCommands+0xe1>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    1185 <handleCommands+0x25>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    0x2dbe(%rip),%rcx
mov    %eax,(%rcx)
jmp    1185 <handleCommands+0x25>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1185 <handleCommands+0x25>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x1,%r15d
jmp    1185 <handleCommands+0x25>
lea    0xdb8(%rip),%rsi
xor    %edi,%edi
call   1080 <strtok@plt>
mov    %rax,%rbx
lea    0xdbb(%rip),%rsi
mov    %rax,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     128d <handleCommands+0x12d>
lea    0xda9(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11fa <handleCommands+0x9a>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x3,%r15d
jmp    1185 <handleCommands+0x25>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x2,%r15d
jmp    1185 <handleCommands+0x25>

LLVM IR (-O0):
@FIONREAD = global i32 0
@ENOMSG = global i32 0
@errno = global i32 0
@.str = [2 x i8] c" \00"
@.str.1 = [9 x i8] c"shutdown\00"
@.str.2 = [9 x i8] c"setbcast\00"
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [2 x i8] c"0\00"
define i32 @handleCommands(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = load i32, i32* @FIONREAD
%10 = call i64 @ioctl(i32 %8, i32 %9, i64* %4)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %66
13: ; preds = %1
%14 = load i64, i64* %4
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i32, i32* @ENOMSG
store i32 %17, i32* @errno
store i32 -1, i32* %2
br label %66
18: ; preds = %13
%19 = load i64, i64* %4
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 -1, i32* %2
br label %66
24: ; preds = %18
%25 = load i32, i32* %3
%26 = load i8*, i8** %6
%27 = load i64, i64* %4
%28 = call i64 @recv(i32 %25, i8* %26, i64 %27, i32 0)
store i64 %28, i64* %5
%29 = load i64, i64* %5
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i8*, i8** %6
%33 = call i32 @free(i8* %32)
store i32 -1, i32* %2
br label %66
34: ; preds = %24
%35 = load i8*, i8** %6
%36 = call i8* @strtok(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = load i8*, i8** %6
%42 = call i32 @free(i8* %41)
store i32 1, i32* %2
br label %66
43: ; preds = %34
%44 = load i8*, i8** %7
%45 = call i64 @strcmp(i8* %44, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %63
47: ; preds = %43
%48 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %48, i8** %7
%49 = load i8*, i8** %7
%50 = call i64 @strcmp(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i8*, i8** %6
%54 = call i32 @free(i8* %53)
store i32 2, i32* %2
br label %66
55: ; preds = %47
%56 = load i8*, i8** %7
%57 = call i64 @strcmp(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = load i8*, i8** %6
%61 = call i32 @free(i8* %60)
store i32 3, i32* %2
br label %66
62: ; preds = %55
br label %63
63: ; preds = %62, %43
%64 = load i8*, i8** %6
%65 = call i32 @free(i8* %64)
store i32 0, i32* %2
br label %66
66: ; preds = %63, %59, %52, %40, %31, %23, %16, %12
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
@FIONREAD = global i32 0
@ENOMSG = global i32 0
@errno = global i32 0
@.str = [2 x i8] c" \00"
@.str.1 = [9 x i8] c"shutdown\00"
@.str.2 = [9 x i8] c"setbcast\00"
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [2 x i8] c"0\00"
define i32 @handleCommands(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = load i32, i32* @FIONREAD
%5 = call i64 @ioctl(i32 %0, i32 %4, i64* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %42
7: ; preds = %1
%8 = load i64, i64* %2
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = load i32, i32* @ENOMSG
store i32 %11, i32* @errno
br label %42
12: ; preds = %7
%13 = call i8* @malloc(i64 %8)
%14 = icmp eq i8* %13, null
br i1 %14, label %42, label %15
15: ; preds = %12
%16 = call i64 @recv(i32 %0, i8* %13, i64 %8, i32 0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @free(i8* %13)
br label %42
20: ; preds = %15
%21 = call i8* @strtok(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @free(i8* %13)
br label %42
26: ; preds = %20
%27 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %40
29: ; preds = %26
%30 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @free(i8* %13)
br label %42
35: ; preds = %29
%36 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @free(i8* %13)
br label %42
40: ; preds = %35, %26
%41 = call i32 @free(i8* %13)
br label %42
42: ; preds = %24, %33, %38, %40, %12, %1, %18, %10
%43 = phi i32 [ -1, %10 ], [ -1, %18 ], [ -1, %1 ], [ -1, %12 ], [ 1, %24 ], [ 2, %33 ], [ 3, %38 ], [ 0, %40 ]
ret i32 %43
}