#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 sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int ECONNRESET;
int errno;
int getpeername(int, struct sockaddr *, int /*<<< orphan*/ *);
int ruby_getpeername(int s, struct sockaddr *name, socklen_t *namelen) {
  int err = errno;
  errno = 0;
  s = getpeername(s, name, namelen);
  if (errno == ECONNRESET) {
    errno = 0;
    s = 0;
  } else if (errno == 0)
    errno = err;
  return s;
}


x86 assembly (clang, -O0):
<ruby_getpeername>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2ea2(%rip),%rax
movl   $0x0,(%rax)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <getpeername@plt>
mov    %eax,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    0x2e81(%rip),%rcx
cmp    (%rcx),%eax
jne    1180 <ruby_getpeername+0x70>
mov    0x2e6a(%rip),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11a1 <ruby_getpeername+0x91>
mov    0x2e51(%rip),%rax
cmpl   $0x0,(%rax)
jne    119c <ruby_getpeername+0x8c>
mov    -0x1c(%rbp),%ecx
mov    0x2e3e(%rip),%rax
mov    %ecx,(%rax)
jmp    11a1 <ruby_getpeername+0x91>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ruby_getpeername>:
push   %rbp
push   %rbx
push   %rax
mov    0x2ebe(%rip),%rbx
mov    (%rbx),%ebp
movl   $0x0,(%rbx)
call   1030 <getpeername@plt>
mov    (%rbx),%edx
mov    0x2eb0(%rip),%rdi
xor    %ecx,%ecx
mov    $0x0,%esi
cmp    (%rdi),%edx
je     1143 <ruby_getpeername+0x33>
mov    %ebp,%ecx
mov    %eax,%esi
test   %edx,%edx
jne    1147 <ruby_getpeername+0x37>
mov    %ecx,(%rbx)
mov    %esi,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i32 0
@ECONNRESET = global i32 0
define i32 @ruby_getpeername(i32 %0, %struct.sockaddr* %1, i32* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
store i32* %2, i32** %6
%8 = load i32, i32* @errno
store i32 %8, i32* %7
store i32 0, i32* @errno
%9 = load i32, i32* %4
%10 = load %struct.sockaddr*, %struct.sockaddr** %5
%11 = load i32*, i32** %6
%12 = call i32 @getpeername(i32 %9, %struct.sockaddr* %10, i32* %11)
store i32 %12, i32* %4
%13 = load i32, i32* @errno
%14 = load i32, i32* @ECONNRESET
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 0, i32* @errno
store i32 0, i32* %4
br label %23
17: ; preds = %3
%18 = load i32, i32* @errno
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i32, i32* %7
store i32 %21, i32* @errno
br label %22
22: ; preds = %20, %17
br label %23
23: ; preds = %22, %16
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i32 0
@ECONNRESET = global i32 0
define i32 @ruby_getpeername(i32 %0, %struct.sockaddr* %1, i32* %2) {
%4 = load i32, i32* @errno
store i32 0, i32* @errno
%5 = tail call i32 @getpeername(i32 %0, %struct.sockaddr* %1, i32* %2)
%6 = load i32, i32* @errno
%7 = load i32, i32* @ECONNRESET
%8 = icmp eq i32 %6, %7
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = icmp eq i32 %6, 0
br i1 %10, label %11, label %14
11: ; preds = %9, %3
%12 = phi i32 [ 0, %3 ], [ %4, %9 ]
%13 = phi i32 [ 0, %3 ], [ %5, %9 ]
store i32 %12, i32* @errno
br label %14
14: ; preds = %11, %9
%15 = phi i32 [ %5, %9 ], [ %13, %11 ]
ret i32 %15
}