#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 wpa_ctrl {
  int dummy;
};

/* Variables and functions */
scalar_t__ memcmp(char *, char *, int);
int wpa_ctrl_request(struct wpa_ctrl *, char *, int, char *, size_t *,
                     int /*<<< orphan*/ *);
int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach) {
  char buf[10];
  int ret;
  size_t len = 10;
  ret =
      wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6, buf, &len, NULL);
  if (ret < 0)
    return ret;
  if (len == 3 && memcmp(buf, "OK\n", 3) == 0)
    return 0;
  return -1;
}


x86 assembly (clang, -O0):
<wpa_ctrl_attach_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0xa,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%ecx
lea    0xec2(%rip),%rsi
lea    0xeb4(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    -0x1e(%rbp),%rcx
mov    $0x6,%edx
lea    -0x30(%rbp),%r8
xor    %eax,%eax
mov    %eax,%r9d
call   1040 <wpa_ctrl_request@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1182 <wpa_ctrl_attach_helper+0x62>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <wpa_ctrl_attach_helper+0x9f>
cmpq   $0x3,-0x30(%rbp)
jne    11b8 <wpa_ctrl_attach_helper+0x98>
lea    -0x1e(%rbp),%rdi
lea    0xe76(%rip),%rsi
mov    $0x3,%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
jne    11b8 <wpa_ctrl_attach_helper+0x98>
movl   $0x0,-0x4(%rbp)
jmp    11bf <wpa_ctrl_attach_helper+0x9f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wpa_ctrl_attach_helper>:
sub    $0x18,%rsp
movq   $0xa,(%rsp)
test   %esi,%esi
lea    0xed2(%rip),%rax
lea    0xec4(%rip),%rsi
cmove  %rax,%rsi
lea    0xe(%rsp),%rcx
mov    %rsp,%r8
mov    $0x6,%edx
xor    %r9d,%r9d
call   1040 <wpa_ctrl_request@plt>
test   %eax,%eax
js     1180 <wpa_ctrl_attach_helper+0x60>
cmpq   $0x3,(%rsp)
jne    117b <wpa_ctrl_attach_helper+0x5b>
lea    0xea7(%rip),%rsi
lea    0xe(%rsp),%rdi
mov    $0x3,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
je     1185 <wpa_ctrl_attach_helper+0x65>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.wpa_ctrl = type { i32 }
@.str = [7 x i8] c"ATTACH\00"
@.str.1 = [7 x i8] c"DETACH\00"
@.str.2 = [4 x i8] c"OK\0A\00"
define i32 @wpa_ctrl_attach_helper(%struct.wpa_ctrl* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.wpa_ctrl*
%5 = alloca i32
%6 = alloca [10 x i8]
%7 = alloca i32
%8 = alloca i64
store %struct.wpa_ctrl* %0, %struct.wpa_ctrl** %4
store i32 %1, i32* %5
store i64 10, i64* %8
%9 = load %struct.wpa_ctrl*, %struct.wpa_ctrl** %4
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
%12 = zext i1 %11 to i64
%13 = select i1 %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%15 = call i32 @wpa_ctrl_request(%struct.wpa_ctrl* %9, i8* %13, i32 6, i8* %14, i64* %8, i32* null)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = load i32, i32* %7
store i32 %19, i32* %3
br label %29
20: ; preds = %2
%21 = load i64, i64* %8
%22 = icmp eq i64 %21, 3
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%25 = call i64 @memcmp(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 0, i32* %3
br label %29
28: ; preds = %23, %20
store i32 -1, i32* %3
br label %29
29: ; preds = %28, %27, %18
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.wpa_ctrl = type { i32 }
@.str = [7 x i8] c"ATTACH\00"
@.str.1 = [7 x i8] c"DETACH\00"
@.str.2 = [4 x i8] c"OK\0A\00"
define i32 @wpa_ctrl_attach_helper(%struct.wpa_ctrl* %0, i32 %1) {
%3 = alloca [10 x i8]
%4 = alloca i64
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%6 = bitcast i64* %4 to i8*
store i64 10, i64* %4
%7 = icmp eq i32 %1, 0
%8 = select i1 %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0)
%9 = call i32 @wpa_ctrl_request(%struct.wpa_ctrl* %0, i8* %8, i32 6, i8* %5, i64* %4, i32* null)
%10 = icmp slt i32 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %2
%12 = load i64, i64* %4
%13 = icmp eq i64 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i64 @memcmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %14, %11
br label %18
18: ; preds = %14, %2, %17
%19 = phi i32 [ -1, %17 ], [ %9, %2 ], [ 0, %14 ]
ret i32 %19
}