#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 timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ SIOCOUTQ;
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          struct timeval *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
void gracefulshut(int handle) {
  struct timeval tv;
  int i, size;
  for (i = 0; i < 10; i++) {
    ioctl(handle, SIOCOUTQ, &size);
    if (size == 0)
      break;
    tv.tv_usec = 100 * 1000;
    tv.tv_sec = 0;
    select(0, NULL, NULL, NULL, &tv);
  }
  shutdown(handle, SHUT_WR);
}


x86 assembly (clang, -O0):
<gracefulshut>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xa,-0x1c(%rbp)
jge    11a2 <gracefulshut+0x72>
mov    -0x4(%rbp),%edi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmpl   $0x0,-0x20(%rbp)
jne    1170 <gracefulshut+0x40>
jmp    11a2 <gracefulshut+0x72>
movl   $0x186a0,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1050 <select@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1142 <gracefulshut+0x12>
mov    -0x4(%rbp),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gracefulshut>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    $0xa,%ebp
mov    0x2e8e(%rip),%r12
lea    0xc(%rsp),%r14
lea    0x10(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <ioctl@plt>
cmpl   $0x0,0xc(%rsp)
je     119b <gracefulshut+0x6b>
movl   $0x186a0,0x10(%rsp)
movq   $0x0,0x18(%rsp)
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1050 <select@plt>
add    $0xffffffff,%ebp
jne    1160 <gracefulshut+0x30>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <shutdown@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@SIOCOUTQ = global i32 0
@SHUT_WR = global i32 0
define void @gracefulshut(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
br label %6
6: ; preds = %20, %1
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 10
br i1 %8, label %9, label %23
9: ; preds = %6
%10 = load i32, i32* %2
%11 = load i32, i32* @SIOCOUTQ
%12 = call i32 @ioctl(i32 %10, i32 %11, i32* %5)
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %9
br label %23
16: ; preds = %9
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 100000, i32* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %18
%19 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %3)
br label %20
20: ; preds = %16
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %6
23: ; preds = %15, %6
%24 = load i32, i32* %2
%25 = load i32, i32* @SHUT_WR
%26 = call i32 @shutdown(i32 %24, i32 %25)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@SIOCOUTQ = global i32 0
@SHUT_WR = global i32 0
define void @gracefulshut(i32 %0) {
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
br label %8
8: ; preds = %1, %14
%9 = phi i32 [ 0, %1 ], [ %16, %14 ]
%10 = load i32, i32* @SIOCOUTQ
%11 = call i32 @ioctl(i32 %0, i32 %10, i32* %3)
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %8
store i32 100000, i32* %6
store i64 0, i64* %7
%15 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %2)
%16 = add nuw nsw i32 %9, 1
%17 = icmp eq i32 %16, 10
br i1 %17, label %18, label %8
18: ; preds = %8, %14
%19 = load i32, i32* @SHUT_WR
%20 = call i32 @shutdown(i32 %0, i32 %19)
ret void
}