#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*/ 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;
  char *cmd;
  if (ioctl(sd, FIONREAD, &size) != 0) {
    return -1;
  }
  if (size == 0) {
    return -1;
  }
  cmd = malloc(size);
  if (cmd == NULL) {
    return -1;
  }
  if (recv(sd, cmd, size, 0) < 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    $0x20,%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    1255 <handleCommands+0xf5>
cmpq   $0x0,-0x10(%rbp)
jne    11ad <handleCommands+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <handleCommands+0xf5>
mov    -0x10(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d1 <handleCommands+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <handleCommands+0xf5>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <recv@plt>
cmp    $0x0,%rax
jge    1202 <handleCommands+0xa2>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <handleCommands+0xf5>
mov    -0x18(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xde1(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1245 <handleCommands+0xe5>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1255 <handleCommands+0xf5>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%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 i8*
%6 = alloca i8*
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = load i32, i32* @FIONREAD
%9 = call i64 @ioctl(i32 %7, i32 %8, i64* %4)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %43
12: ; preds = %1
%13 = load i64, i64* %4
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 -1, i32* %2
br label %43
16: ; preds = %12
%17 = load i64, i64* %4
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -1, i32* %2
br label %43
22: ; preds = %16
%23 = load i32, i32* %3
%24 = load i8*, i8** %5
%25 = load i64, i64* %4
%26 = call i64 @recv(i32 %23, i8* %24, i64 %25, i32 0)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i8*, i8** %5
%30 = call i32 @free(i8* %29)
store i32 -1, i32* %2
br label %43
31: ; preds = %22
%32 = load i8*, i8** %5
%33 = call i8* @strtok(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
store i32 1, i32* %2
br label %43
40: ; preds = %31
%41 = load i8*, i8** %5
%42 = call i32 @free(i8* %41)
store i32 0, i32* %2
br label %43
43: ; preds = %40, %37, %28, %21, %15, %11
%44 = load i32, i32* %2
ret i32 %44
}

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
}