#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 /*<<< orphan*/ tmp;

/* Variables and functions */
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, char, int);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
scalar_t__ write(int, char *, int);
int sock_close(int sockfd) {
  char tmp[2];
  memset(tmp, '\0', sizeof(tmp));
  while (1) {
    if (read(sockfd, tmp, 1) <= 0)
      break;
    if (write(sockfd, tmp, 1) <= 0)
      break;
  }
  shutdown(sockfd, SHUT_RDWR);
  close(sockfd);
  return (0);
}


x86 assembly (clang, -O0):
<sock_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x6(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x6(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jg     118b <sock_close+0x3b>
jmp    11b0 <sock_close+0x60>
mov    -0x4(%rbp),%edi
lea    -0x6(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jg     11ab <sock_close+0x5b>
jmp    11b0 <sock_close+0x60>
jmp    116b <sock_close+0x1b>
mov    -0x4(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_close>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x6(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1050 <memset@plt>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    1198 <sock_close+0x48>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %rax,%rax
jg     1170 <sock_close+0x20>
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <shutdown@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@SHUT_RDWR = global i32 0
define i32 @sock_close(i32 %0) {
%2 = alloca i32
%3 = alloca [2 x i8]
store i32 %0, i32* %2
%4 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i8 signext 0, i32 2)
br label %6
6: ; preds = %1, %18
%7 = load i32, i32* %2
%8 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%9 = call i64 @read(i32 %7, i8* %8, i32 1)
%10 = icmp sle i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %6
br label %19
12: ; preds = %6
%13 = load i32, i32* %2
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%15 = call i64 @write(i32 %13, i8* %14, i32 1)
%16 = icmp sle i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
br label %19
18: ; preds = %12
br label %6
19: ; preds = %17, %11
%20 = load i32, i32* %2
%21 = load i32, i32* @SHUT_RDWR
%22 = call i32 @shutdown(i32 %20, i32 %21)
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
ret i32 0
}

LLVM IR (-O2):
@SHUT_RDWR = global i32 0
define i32 @sock_close(i32 %0) {
%2 = alloca [2 x i8]
%3 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i8 signext 0, i32 2)
br label %5
5: ; preds = %8, %1
%6 = call i64 @read(i32 %0, i8* %3, i32 1)
%7 = icmp slt i64 %6, 1
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = call i64 @write(i32 %0, i8* %3, i32 1)
%10 = icmp slt i64 %9, 1
br i1 %10, label %11, label %5
11: ; preds = %8, %5
%12 = load i32, i32* @SHUT_RDWR
%13 = call i32 @shutdown(i32 %0, i32 %12)
%14 = call i32 @close(i32 %0)
ret i32 0
}