#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 */

/* Variables and functions */
int close(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int shutdown(int, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, int, ...);
int rclose(int *piSocket, int iMode) {
  int iSocket;
  int iRC;
  int iError = 0;
  int iClose = 1;
  char cModule[32] = "rclose";
  char cMsg[128] = "";
  if (iMode < 0) {
    iMode = -iMode;
    iClose = 0;
  }
  if ((iMode < 0) || (iMode > 3)) {
    if (iClose == 0)
      iMode = -iMode;
    printf("-E- %s: invalid shutdown mode: %d\n", cModule, iMode);
    iError = 2;
  }
  iSocket = *piSocket;
  if (iSocket > 0) {
    if (iMode < 3) {
      iRC = shutdown(iSocket, iMode);
      if (iRC) {
        sprintf(cMsg, "-E- %s: shutdown(%d) rc = %d", cModule, iMode, iRC);
        perror(cMsg);
        iError = -1;
      }
    }
    if (iClose) {
      iRC = close(iSocket);
      if (iRC) {
        sprintf(cMsg, "-E- %s: close rc = %d", cModule, iRC);
        perror(cMsg);
        iError = -2;
      }
    }
  } else {
    printf("-E- %s: invalid socket: %d\n", cModule, iSocket);
    iError = 1;
  }
  *piSocket = 0;
  return (iError);
}


x86 assembly (clang, -O0):
<rclose>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0xe79(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe76(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe73(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe70(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
cmpl   $0x0,-0xc(%rbp)
jge    11d8 <rclose+0x78>
xor    %eax,%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11ec <rclose+0x8c>
cmpl   $0x3,-0xc(%rbp)
jle    1218 <rclose+0xb8>
cmpl   $0x0,-0x1c(%rbp)
jne    11fe <rclose+0x9e>
xor    %eax,%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x40(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    12dd <rclose+0x17d>
cmpl   $0x3,-0xc(%rbp)
jge    1285 <rclose+0x125>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <shutdown@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1280 <rclose+0x120>
lea    -0xc0(%rbp),%rdi
lea    -0x40(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    -0x14(%rbp),%r8d
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0xc0(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x18(%rbp)
jmp    1285 <rclose+0x125>
cmpl   $0x0,-0x1c(%rbp)
je     12d8 <rclose+0x178>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12d3 <rclose+0x173>
lea    -0xc0(%rbp),%rdi
lea    -0x40(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0xc0(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xfffffffe,-0x18(%rbp)
jmp    12d8 <rclose+0x178>
jmp    12f7 <rclose+0x197>
lea    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%edx
lea    0xd8b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rclose>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xa0,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
movaps 0xea5(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe89(%rip),%xmm0
movaps %xmm0,(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
mov    %esi,%ebp
neg    %ebp
cmovs  %esi,%ebp
xor    %r15d,%r15d
cmp    $0x4,%ebp
jb     11d4 <rclose+0x84>
lea    0xe5e(%rip),%rdi
mov    %rsp,%rsi
mov    %ebx,%edx
call   1040 <printf@plt>
mov    $0x2,%r15d
mov    %ebx,%ebp
mov    (%r14),%r12d
test   %r12d,%r12d
jle    122a <rclose+0xda>
cmp    $0x2,%ebp
jg     11ef <rclose+0x9f>
mov    %r12d,%edi
mov    %ebp,%esi
call   1030 <shutdown@plt>
test   %eax,%eax
jne    125c <rclose+0x10c>
test   %ebx,%ebx
js     1242 <rclose+0xf2>
mov    %r12d,%edi
call   1050 <close@plt>
test   %eax,%eax
je     1242 <rclose+0xf2>
lea    0xe5a(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1060 <perror@plt>
mov    $0xfffffffe,%r15d
jmp    1242 <rclose+0xf2>
lea    0xe45(%rip),%rdi
mov    %rsp,%rsi
mov    %r12d,%edx
call   1040 <printf@plt>
mov    $0x1,%r15d
movl   $0x0,(%r14)
mov    %r15d,%eax
add    $0xa0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde0(%rip),%rsi
lea    0x20(%rsp),%r15
mov    %rsp,%rdx
mov    %r15,%rdi
mov    %ebp,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %r15,%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r15d
test   %ebx,%ebx
jns    11f3 <rclose+0xa3>
jmp    1242 <rclose+0xf2>

LLVM IR (-O0):
@__const.rclose.cModule = [32 x i8] c"rclose\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [35 x i8] c"-E- %s: invalid shutdown mode: %d\0A\00"
@.str.1 = [29 x i8] c"-E- %s: shutdown(%d) rc = %d\00"
@.str.2 = [22 x i8] c"-E- %s: close rc = %d\00"
@.str.3 = [28 x i8] c"-E- %s: invalid socket: %d\0A\00"
define i32 @rclose(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [32 x i8]
%10 = alloca [128 x i8]
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %7
store i32 1, i32* %8
%11 = bitcast [32 x i8]* %9 to i8*
%12 = bitcast [128 x i8]* %10 to i8*
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* %4
%17 = sub nsw i32 0, %16
store i32 %17, i32* %4
store i32 0, i32* %8
br label %18
18: ; preds = %15, %2
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* %4
%23 = icmp sgt i32 %22, 3
br i1 %23, label %24, label %34
24: ; preds = %21, %18
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %4
%29 = sub nsw i32 0, %28
store i32 %29, i32* %4
br label %30
30: ; preds = %27, %24
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%32 = load i32, i32* %4
%33 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %31, i32 %32)
store i32 2, i32* %7
br label %34
34: ; preds = %30, %21
%35 = load i32*, i32** %3
%36 = load i32, i32* %35
store i32 %36, i32* %5
%37 = load i32, i32* %5
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %74
39: ; preds = %34
%40 = load i32, i32* %4
%41 = icmp slt i32 %40, 3
br i1 %41, label %42, label %57
42: ; preds = %39
%43 = load i32, i32* %5
%44 = load i32, i32* %4
%45 = call i32 @shutdown(i32 %43, i32 %44)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %56
48: ; preds = %42
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%50 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%51 = load i32, i32* %4
%52 = load i32, i32* %6
%53 = call i32 (i8*, i8*, i8*, i32, ...) @sprintf(i8* %49, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %50, i32 %51, i32 %52)
%54 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%55 = call i32 @perror(i8* %54)
store i32 -1, i32* %7
br label %56
56: ; preds = %48, %42
br label %57
57: ; preds = %56, %39
%58 = load i32, i32* %8
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %73
60: ; preds = %57
%61 = load i32, i32* %5
%62 = call i32 @close(i32 %61)
store i32 %62, i32* %6
%63 = load i32, i32* %6
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %72
65: ; preds = %60
%66 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%67 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%68 = load i32, i32* %6
%69 = call i32 (i8*, i8*, i8*, i32, ...) @sprintf(i8* %66, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %67, i32 %68)
%70 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%71 = call i32 @perror(i8* %70)
store i32 -2, i32* %7
br label %72
72: ; preds = %65, %60
br label %73
73: ; preds = %72, %57
br label %78
74: ; preds = %34
%75 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%76 = load i32, i32* %5
%77 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %75, i32 %76)
store i32 1, i32* %7
br label %78
78: ; preds = %74, %73
%79 = load i32*, i32** %3
store i32 0, i32* %79
%80 = load i32, i32* %7
ret i32 %80
}

LLVM IR (-O2):
@__const.rclose.cModule = [32 x i8] c"rclose\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [35 x i8] c"-E- %s: invalid shutdown mode: %d\0A\00"
@.str.1 = [29 x i8] c"-E- %s: shutdown(%d) rc = %d\00"
@.str.2 = [22 x i8] c"-E- %s: close rc = %d\00"
@.str.3 = [28 x i8] c"-E- %s: invalid socket: %d\0A\00"
define i32 @rclose(i32* nocapture %0, i32 %1) {
%3 = alloca [32 x i8]
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %1, 0
%8 = sub nsw i32 0, %1
%9 = select i1 %7, i32 %8, i32 %1
%10 = icmp ugt i32 %9, 3
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %5, i32 %1)
br label %13
13: ; preds = %2, %11
%14 = phi i32 [ %1, %11 ], [ %9, %2 ]
%15 = phi i32 [ 2, %11 ], [ 0, %2 ]
%16 = load i32, i32* %0
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %34
18: ; preds = %13
%19 = icmp slt i32 %14, 3
br i1 %19, label %20, label %26
20: ; preds = %18
%21 = call i32 @shutdown(i32 %16, i32 %14)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = call i32 (i8*, i8*, i8*, i32, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %5, i32 %14, i32 %21)
%25 = call i32 @perror(i8* %6)
br label %26
26: ; preds = %20, %23, %18
%27 = phi i32 [ -1, %23 ], [ %15, %20 ], [ %15, %18 ]
br i1 %7, label %36, label %28
28: ; preds = %26
%29 = call i32 @close(i32 %16)
%30 = icmp eq i32 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %28
%32 = call i32 (i8*, i8*, i8*, i32, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %5, i32 %29)
%33 = call i32 @perror(i8* %6)
br label %36
34: ; preds = %13
%35 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %5, i32 %16)
br label %36
36: ; preds = %26, %31, %28, %34
%37 = phi i32 [ -2, %31 ], [ %27, %28 ], [ %27, %26 ], [ 1, %34 ]
store i32 0, i32* %0
ret i32 %37
}