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

/* Variables and functions */
int /*<<< orphan*/ SHUT_WR;
scalar_t__ close(int);
scalar_t__ qio_read(int, char *, int, int);
scalar_t__ shutdown(int, int /*<<< orphan*/);
bool qsocket_close(int sockfd, int timeoutms) {
  if (timeoutms >= 0 && shutdown(sockfd, SHUT_WR) == 0) {
    char buf[1024];
    while (true) {
      ssize_t read = qio_read(sockfd, buf, sizeof(buf), timeoutms);
      if (read <= 0)
        break;
      ;
    }
  }
  if (close(sockfd) == 0)
    return true;
  return false;
}


x86 assembly (clang, -O0):
<qsocket_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11a6 <qsocket_close+0x76>
mov    -0x8(%rbp),%edi
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
cmp    $0x0,%rax
jne    11a6 <qsocket_close+0x76>
jmp    116b <qsocket_close+0x3b>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0xc(%rbp),%ecx
mov    $0x400,%edx
call   1030 <qio_read@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jg     119c <qsocket_close+0x6c>
jmp    11a1 <qsocket_close+0x71>
jmp    116b <qsocket_close+0x3b>
jmp    11a6 <qsocket_close+0x76>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jne    11c4 <qsocket_close+0x94>
movl   $0x1,-0x4(%rbp)
jmp    11cb <qsocket_close+0x9b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qsocket_close>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %edi,%ebx
test   %esi,%esi
js     1176 <qsocket_close+0x46>
mov    %esi,%ebp
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <shutdown@plt>
test   %rax,%rax
jne    1176 <qsocket_close+0x46>
mov    %rsp,%r14
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
mov    %ebp,%ecx
call   1030 <qio_read@plt>
test   %rax,%rax
jg     1160 <qsocket_close+0x30>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
mov    %ecx,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SHUT_WR = global i32 0
define i32 @qsocket_close(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp sge i32 %8, 0
br i1 %9, label %10, label %26
10: ; preds = %2
%11 = load i32, i32* %4
%12 = load i32, i32* @SHUT_WR
%13 = call i64 @shutdown(i32 %11, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %26
15: ; preds = %10
br label %16
16: ; preds = %15, %24
%17 = load i32, i32* %4
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%19 = load i32, i32* %5
%20 = call i64 @qio_read(i32 %17, i8* %18, i32 1024, i32 %19)
store i64 %20, i64* %7
%21 = load i64, i64* %7
%22 = icmp sle i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %16
br label %25
24: ; preds = %16
br label %16
25: ; preds = %23
br label %26
26: ; preds = %25, %10, %2
%27 = load i32, i32* %4
%28 = call i64 @close(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 1, i32* %3
br label %32
31: ; preds = %26
store i32 0, i32* %3
br label %32
32: ; preds = %31, %30
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@SHUT_WR = global i32 0
define i32 @qsocket_close(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = icmp sgt i32 %1, -1
br i1 %4, label %5, label %15
5: ; preds = %2
%6 = load i32, i32* @SHUT_WR
%7 = tail call i64 @shutdown(i32 %0, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %5
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %11
11: ; preds = %11, %9
%12 = call i64 @qio_read(i32 %0, i8* %10, i32 1024, i32 %1)
%13 = icmp slt i64 %12, 1
br i1 %13, label %14, label %11
14: ; preds = %11
br label %15
15: ; preds = %14, %5, %2
%16 = call i64 @close(i32 %0)
%17 = icmp eq i64 %16, 0
%18 = zext i1 %17 to i32
ret i32 %18
}