#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 */
struct pollfd {
  int fd;
  int events;
  int revents;
};
typedef int ssize_t;

/* Variables and functions */
int POLLHUP;
int POLLIN;
int poll(struct pollfd *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ strlen(char const *);
int write(int, char const *, int /*<<< orphan*/);
ssize_t wpa_cmd(int fd, const char *cmd, char *buffer, size_t len) {
  int retval;
  ssize_t bytes;
  struct pollfd pfd;
  if (buffer)
    *buffer = '\0';
  bytes = write(fd, cmd, strlen(cmd));
  if (bytes == -1)
    return -1;
  if (buffer == NULL || len == 0)
    return 0;
  pfd.fd = fd;
  pfd.events = POLLIN | POLLHUP;
  pfd.revents = 0;
  retval = poll(&pfd, 1, 2000);
  if (retval == -1)
    return -1;
  if (retval == 0 || !(pfd.revents & (POLLIN | POLLHUP)))
    return -1;
  bytes = read(fd, buffer, len == 1 ? 1 : len - 1);
  if (bytes != -1)
    buffer[bytes] = '\0';
  return bytes;
}


x86 assembly (clang, -O0):
<wpa_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1169 <wpa_cmd+0x29>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11a7 <wpa_cmd+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <wpa_cmd+0x171>
cmpq   $0x0,-0x18(%rbp)
je     11bd <wpa_cmd+0x7d>
cmpq   $0x0,-0x20(%rbp)
jne    11c9 <wpa_cmd+0x89>
movl   $0x0,-0x4(%rbp)
jmp    12b1 <wpa_cmd+0x171>
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    0x2e09(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x30(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x1,%esi
mov    $0x7d0,%edx
call   1060 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1217 <wpa_cmd+0xd7>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <wpa_cmd+0x171>
cmpl   $0x0,-0x24(%rbp)
je     1241 <wpa_cmd+0x101>
mov    -0x30(%rbp),%eax
mov    0x2db5(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2db4(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    124d <wpa_cmd+0x10d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <wpa_cmd+0x171>
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x1,-0x20(%rbp)
jne    1274 <wpa_cmd+0x134>
mov    $0x1,%eax
mov    %rax,-0x60(%rbp)
jmp    1280 <wpa_cmd+0x140>
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x54(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    %eax,%edx
call   1050 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     12ab <wpa_cmd+0x16b>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wpa_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
test   %rdx,%rdx
je     1160 <wpa_cmd+0x20>
movb   $0x0,(%rbx)
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11c5 <wpa_cmd+0x85>
xor    %eax,%eax
test   %rbx,%rbx
je     11ca <wpa_cmd+0x8a>
test   %r14,%r14
je     11ca <wpa_cmd+0x8a>
mov    %r15d,(%rsp)
mov    0x2e4f(%rip),%r12
mov    0x2e50(%rip),%rbp
mov    0x0(%rbp),%eax
or     (%r12),%eax
mov    %eax,0x4(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x7d0,%edx
call   1060 <poll@plt>
add    $0x1,%eax
cmp    $0x2,%eax
jae    11d7 <wpa_cmd+0x97>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%rbp),%eax
or     (%r12),%eax
test   %eax,0x8(%rsp)
je     11c5 <wpa_cmd+0x85>
lea    -0x1(%r14),%eax
cmp    $0x1,%r14
mov    $0x1,%edx
cmovne %eax,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1050 <read@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     11ca <wpa_cmd+0x8a>
movslq %ecx,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %ecx,%eax
jmp    11ca <wpa_cmd+0x8a>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLHUP = global i32 0
define i32 @wpa_cmd(i32 %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.pollfd
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%13 = load i8*, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = load i8*, i8** %8
store i8 0, i8* %16
br label %17
17: ; preds = %15, %4
%18 = load i32, i32* %6
%19 = load i8*, i8** %7
%20 = load i8*, i8** %7
%21 = call i32 @strlen(i8* %20)
%22 = call i32 @write(i32 %18, i8* %19, i32 %21)
store i32 %22, i32* %11
%23 = load i32, i32* %11
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %17
store i32 -1, i32* %5
br label %79
26: ; preds = %17
%27 = load i8*, i8** %8
%28 = icmp eq i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i64, i64* %9
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29, %26
store i32 0, i32* %5
br label %79
33: ; preds = %29
%34 = load i32, i32* %6
%35 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* @POLLIN
%37 = load i32, i32* @POLLHUP
%38 = or i32 %36, %37
%39 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 1
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 2
store i32 0, i32* %40
%41 = call i32 @poll(%struct.pollfd* %12, i32 1, i32 2000)
store i32 %41, i32* %10
%42 = load i32, i32* %10
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %33
store i32 -1, i32* %5
br label %79
45: ; preds = %33
%46 = load i32, i32* %10
%47 = icmp eq i32 %46, 0
br i1 %47, label %56, label %48
48: ; preds = %45
%49 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 2
%50 = load i32, i32* %49
%51 = load i32, i32* @POLLIN
%52 = load i32, i32* @POLLHUP
%53 = or i32 %51, %52
%54 = and i32 %50, %53
%55 = icmp ne i32 %54, 0
br i1 %55, label %57, label %56
56: ; preds = %48, %45
store i32 -1, i32* %5
br label %79
57: ; preds = %48
%58 = load i32, i32* %6
%59 = load i8*, i8** %8
%60 = load i64, i64* %9
%61 = icmp eq i64 %60, 1
br i1 %61, label %62, label %63
62: ; preds = %57
br label %66
63: ; preds = %57
%64 = load i64, i64* %9
%65 = sub i64 %64, 1
br label %66
66: ; preds = %63, %62
%67 = phi i64 [ 1, %62 ], [ %65, %63 ]
%68 = trunc i64 %67 to i32
%69 = call i32 @read(i32 %58, i8* %59, i32 %68)
store i32 %69, i32* %11
%70 = load i32, i32* %11
%71 = icmp ne i32 %70, -1
br i1 %71, label %72, label %77
72: ; preds = %66
%73 = load i8*, i8** %8
%74 = load i32, i32* %11
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
store i8 0, i8* %76
br label %77
77: ; preds = %72, %66
%78 = load i32, i32* %11
store i32 %78, i32* %5
br label %79
79: ; preds = %77, %56, %44, %32, %25
%80 = load i32, i32* %5
ret i32 %80
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLHUP = global i32 0
define i32 @wpa_cmd(i32 %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca %struct.pollfd
%6 = bitcast %struct.pollfd* %5 to i8*
%7 = icmp eq i8* %2, null
br i1 %7, label %9, label %8
8: ; preds = %4
store i8 0, i8* %2
br label %9
9: ; preds = %8, %4
%10 = tail call i32 @strlen(i8* %1)
%11 = tail call i32 @write(i32 %0, i8* %1, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %41, label %13
13: ; preds = %9
%14 = icmp eq i64 %3, 0
%15 = or i1 %7, %14
br i1 %15, label %41, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %0, i32* %17
%18 = load i32, i32* @POLLIN
%19 = load i32, i32* @POLLHUP
%20 = or i32 %19, %18
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 2
store i32 0, i32* %22
%23 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 2000)
switch i32 %23, label %24 [
i32 -1, label %41
i32 0, label %41
]
24: ; preds = %16
%25 = load i32, i32* %22
%26 = load i32, i32* @POLLIN
%27 = load i32, i32* @POLLHUP
%28 = or i32 %27, %26
%29 = and i32 %28, %25
%30 = icmp eq i32 %29, 0
br i1 %30, label %41, label %31
31: ; preds = %24
%32 = icmp eq i64 %3, 1
%33 = trunc i64 %3 to i32
%34 = add i32 %33, -1
%35 = select i1 %32, i32 1, i32 %34
%36 = call i32 @read(i32 %0, i8* %2, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %41, label %38
38: ; preds = %31
%39 = sext i32 %36 to i64
%40 = getelementptr inbounds i8, i8* %2, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %31, %38, %24, %16, %16, %13, %9
%42 = phi i32 [ -1, %9 ], [ 0, %13 ], [ -1, %16 ], [ -1, %16 ], [ -1, %24 ], [ %36, %38 ], [ -1, %31 ]
ret i32 %42
}