#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 shutdown(int, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, int, ...);
int rclose(int *piSocket, int iMode) {
  int iSocket;
  int iRC;
  int iDebug = 0;
  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;
      } else if (iDebug)
        printf("    %s: shutdown(%d) successfull\n", cModule, iMode);
    }
    if (iClose) {
      iRC = close(iSocket);
      if (iRC) {
        sprintf(cMsg, "-E- %s: close rc = %d", cModule, iRC);
        perror(cMsg);
        iError = -2;
      } else if (iDebug)
        printf("    %s: connection closed\n", cModule);
    }
  } 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   $0x0,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0xe72(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe6f(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe6c(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe69(%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    11df <rclose+0x7f>
xor    %eax,%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11f3 <rclose+0x93>
cmpl   $0x3,-0xc(%rbp)
jle    1221 <rclose+0xc1>
cmpl   $0x0,-0x20(%rbp)
jne    1205 <rclose+0xa5>
xor    %eax,%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x40(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1335 <rclose+0x1d5>
cmpl   $0x3,-0xc(%rbp)
jge    12b7 <rclose+0x157>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <shutdown@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     128e <rclose+0x12e>
lea    -0xc0(%rbp),%rdi
lea    -0x40(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    -0x14(%rbp),%r8d
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0xc0(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12b2 <rclose+0x152>
cmpl   $0x0,-0x18(%rbp)
je     12ad <rclose+0x14d>
lea    -0x40(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b2 <rclose+0x152>
jmp    12b7 <rclose+0x157>
cmpl   $0x0,-0x20(%rbp)
je     1330 <rclose+0x1d0>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     130a <rclose+0x1aa>
lea    -0xc0(%rbp),%rdi
lea    -0x40(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xd97(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0xc0(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xfffffffe,-0x1c(%rbp)
jmp    132b <rclose+0x1cb>
cmpl   $0x0,-0x18(%rbp)
je     1326 <rclose+0x1c6>
lea    -0x40(%rbp),%rsi
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132b <rclose+0x1cb>
jmp    1330 <rclose+0x1d0>
jmp    1351 <rclose+0x1f1>
lea    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%edx
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x1c(%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     11d6 <rclose+0x86>
lea    0xe5e(%rip),%rdi
mov    %rsp,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%r15d
mov    %ebx,%ebp
mov    (%r14),%r12d
test   %r12d,%r12d
jle    122c <rclose+0xdc>
cmp    $0x2,%ebp
jg     11f1 <rclose+0xa1>
mov    %r12d,%edi
mov    %ebp,%esi
call   1030 <shutdown@plt>
test   %eax,%eax
jne    1260 <rclose+0x110>
test   %ebx,%ebx
js     1246 <rclose+0xf6>
mov    %r12d,%edi
call   1050 <close@plt>
test   %eax,%eax
je     1246 <rclose+0xf6>
lea    0xe58(%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    1246 <rclose+0xf6>
lea    0xe43(%rip),%rdi
mov    %rsp,%rsi
mov    %r12d,%edx
xor    %eax,%eax
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    0xddc(%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    11f5 <rclose+0xa5>
jmp    1246 <rclose+0xf6>

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 = [34 x i8] c" %s: shutdown(%d) successfull\0A\00"
@.str.3 = [22 x i8] c"-E- %s: close rc = %d\00"
@.str.4 = [27 x i8] c" %s: connection closed\0A\00"
@.str.5 = [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 i32
%10 = alloca [32 x i8]
%11 = alloca [128 x i8]
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %7
store i32 0, i32* %8
store i32 1, i32* %9
%12 = bitcast [32 x i8]* %10 to i8*
%13 = bitcast [128 x i8]* %11 to i8*
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* %4
%18 = sub nsw i32 0, %17
store i32 %18, i32* %4
store i32 0, i32* %9
br label %19
19: ; preds = %16, %2
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 3
br i1 %24, label %25, label %35
25: ; preds = %22, %19
%26 = load i32, i32* %9
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* %4
%30 = sub nsw i32 0, %29
store i32 %30, i32* %4
br label %31
31: ; preds = %28, %25
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%33 = load i32, i32* %4
%34 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %32, i32 %33)
store i32 2, i32* %8
br label %35
35: ; preds = %31, %22
%36 = load i32*, i32** %3
%37 = load i32, i32* %36
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %90
40: ; preds = %35
%41 = load i32, i32* %4
%42 = icmp slt i32 %41, 3
br i1 %42, label %43, label %66
43: ; preds = %40
%44 = load i32, i32* %5
%45 = load i32, i32* %4
%46 = call i32 @shutdown(i32 %44, i32 %45)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %43
%50 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%51 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%52 = load i32, i32* %4
%53 = load i32, i32* %6
%54 = call i32 (i8*, i8*, i8*, i32, ...) @sprintf(i8* %50, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %51, i32 %52, i32 %53)
%55 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%56 = call i32 @perror(i8* %55)
store i32 -1, i32* %8
br label %65
57: ; preds = %43
%58 = load i32, i32* %7
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%62 = load i32, i32* %4
%63 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %61, i32 %62)
br label %64
64: ; preds = %60, %57
br label %65
65: ; preds = %64, %49
br label %66
66: ; preds = %65, %40
%67 = load i32, i32* %9
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %89
69: ; preds = %66
%70 = load i32, i32* %5
%71 = call i32 @close(i32 %70)
store i32 %71, i32* %6
%72 = load i32, i32* %6
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %81
74: ; preds = %69
%75 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%76 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%77 = load i32, i32* %6
%78 = call i32 (i8*, i8*, i8*, i32, ...) @sprintf(i8* %75, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %76, i32 %77)
%79 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%80 = call i32 @perror(i8* %79)
store i32 -2, i32* %8
br label %88
81: ; preds = %69
%82 = load i32, i32* %7
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %87
84: ; preds = %81
%85 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%86 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %85)
br label %87
87: ; preds = %84, %81
br label %88
88: ; preds = %87, %74
br label %89
89: ; preds = %88, %66
br label %94
90: ; preds = %35
%91 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%92 = load i32, i32* %5
%93 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %91, i32 %92)
store i32 1, i32* %8
br label %94
94: ; preds = %90, %89
%95 = load i32*, i32** %3
store i32 0, i32* %95
%96 = load i32, i32* %8
ret i32 %96
}

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.3 = [22 x i8] c"-E- %s: close rc = %d\00"
@.str.5 = [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 (i8*, i8*, ...) @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, %18 ], [ %15, %20 ]
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.3, i64 0, i64 0), i8* %5, i32 %29)
%33 = call i32 @perror(i8* %6)
br label %36
34: ; preds = %13
%35 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %5, i32 %16)
br label %36
36: ; preds = %28, %26, %31, %34
%37 = phi i32 [ -2, %31 ], [ %27, %26 ], [ 1, %34 ], [ %27, %28 ]
store i32 0, i32* %0
ret i32 %37
}