#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 int ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int read(int, char *, int);
scalar_t__ write_command(int, char *);
void detach(int fd) {
  if (write_command(fd, "ATTACH_DETACHED") != 0) {
    return;
  }
  char buf[256];
  ssize_t bytes;
  do {
    bytes = read(fd, buf, sizeof(buf));
  } while (bytes > 0 && buf[bytes - 1] != 0);
}


x86 assembly (clang, -O0):
<detach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    0xec8(%rip),%rsi
call   1030 <write_command@plt>
cmp    $0x0,%rax
je     114c <detach+0x2c>
jmp    11ad <detach+0x8d>
jmp    1151 <detach+0x31>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1040 <read@plt>
mov    %eax,-0x114(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x114(%rbp)
mov    %al,-0x115(%rbp)
jle    119f <detach+0x7f>
mov    -0x114(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
setne  %al
mov    %al,-0x115(%rbp)
mov    -0x115(%rbp),%al
test   $0x1,%al
jne    1151 <detach+0x31>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detach>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edi,%ebx
lea    0xecd(%rip),%rsi
call   1030 <write_command@plt>
test   %rax,%rax
jne    115c <detach+0x3c>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    115c <detach+0x3c>
add    $0xffffffff,%eax
cmpb   $0x0,(%rsp,%rax,1)
jne    1140 <detach+0x20>
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [16 x i8] c"ATTACH_DETACHED\00"
define void @detach(i32 %0) {
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i64 @write_command(i32 %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
br label %27
9: ; preds = %1
br label %10
10: ; preds = %25, %9
%11 = load i32, i32* %2
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%13 = call i32 @read(i32 %11, i8* %12, i32 256)
store i32 %13, i32* %4
br label %14
14: ; preds = %10
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i32, i32* %4
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br label %25
25: ; preds = %17, %14
%26 = phi i1 [ false, %14 ], [ %24, %17 ]
br i1 %26, label %10, label %27
27: ; preds = %8, %25
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"ATTACH_DETACHED\00"
define void @detach(i32 %0) {
%2 = alloca [256 x i8]
%3 = tail call i64 @write_command(i32 %0, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %17
5: ; preds = %1
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
br label %7
7: ; preds = %10, %5
%8 = call i32 @read(i32 %0, i8* %6, i32 256)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %7
%11 = add nsw i32 %8, -1
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %16, label %7
16: ; preds = %7, %10
br label %17
17: ; preds = %1, %16
ret void
}