#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*/ FIONREAD;
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) {
    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;
  }
  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    125a <handleCommands+0xfa>
cmpq   $0x0,-0x10(%rbp)
jne    11ad <handleCommands+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    125a <handleCommands+0xfa>
mov    -0x10(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d1 <handleCommands+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    125a <handleCommands+0xfa>
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    1207 <handleCommands+0xa7>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125a <handleCommands+0xfa>
mov    -0x20(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    124a <handleCommands+0xea>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <handleCommands+0xfa>
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,%ebp
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1050 <ioctl@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1191 <handleCommands+0x31>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%r15
test   %r15,%r15
je     1184 <handleCommands+0x24>
mov    %r15,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1184 <handleCommands+0x24>
mov    %rax,%r14
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %rax,%rax
js     11e4 <handleCommands+0x84>
lea    0xe3b(%rip),%rsi
mov    %r14,%rdi
call   1080 <strtok@plt>
lea    0xe2e(%rip),%rsi
mov    %rax,%rdi
call   1060 <strcmp@plt>
xor    %ebx,%ebx
test   %rax,%rax
sete   %bl
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1184 <handleCommands+0x24>

LLVM IR (-O0):
@FIONREAD = global i32 0
@.str = [2 x i8] c" \00"
@.str.1 = [9 x i8] c"shutdown\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 %45
13: ; preds = %1
%14 = load i64, i64* %4
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 -1, i32* %2
br label %45
17: ; preds = %13
%18 = load i64, i64* %4
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -1, i32* %2
br label %45
23: ; preds = %17
%24 = load i32, i32* %3
%25 = load i8*, i8** %6
%26 = load i64, i64* %4
%27 = call i64 @recv(i32 %24, i8* %25, i64 %26, i32 0)
store i64 %27, i64* %5
%28 = load i64, i64* %5
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = load i8*, i8** %6
%32 = call i32 @free(i8* %31)
store i32 -1, i32* %2
br label %45
33: ; preds = %23
%34 = load i8*, i8** %6
%35 = call i8* @strtok(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = load i8*, i8** %6
%41 = call i32 @free(i8* %40)
store i32 1, i32* %2
br label %45
42: ; preds = %33
%43 = load i8*, i8** %6
%44 = call i32 @free(i8* %43)
store i32 0, i32* %2
br label %45
45: ; preds = %42, %39, %30, %22, %16, %12
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@FIONREAD = global i32 0
@.str = [2 x i8] c" \00"
@.str.1 = [9 x i8] c"shutdown\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 %24
7: ; preds = %1
%8 = load i64, i64* %2
%9 = icmp eq i64 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %7
%11 = call i8* @malloc(i64 %8)
%12 = icmp eq i8* %11, null
br i1 %12, label %24, label %13
13: ; preds = %10
%14 = call i64 @recv(i32 %0, i8* %11, i64 %8, i32 0)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @free(i8* %11)
br label %24
18: ; preds = %13
%19 = call i8* @strtok(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
%22 = call i32 @free(i8* %11)
%23 = zext i1 %21 to i32
br label %24
24: ; preds = %18, %10, %7, %1, %16
%25 = phi i32 [ -1, %16 ], [ -1, %1 ], [ -1, %7 ], [ -1, %10 ], [ %23, %18 ]
ret i32 %25
}