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

/* Variables and functions */
int /*<<< orphan*/ LOG_CRIT;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
int /*<<< orphan*/ logger(int /*<<< orphan*/, char *, int, int,
                          int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int);
int sock_get_error(int sock) {
  int errv, errlen = sizeof(errv);
  if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &errv, &errlen) < 0) {
    logger(LOG_CRIT, "getsockopt(SO_ERROR) failed on socket(%d) %d: %s", sock,
           errv, strerror(errv));
    return -1;
  }
  return errv;
}


x86 assembly (clang, -O0):
<sock_get_error>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1030 <getsockopt@plt>
cmp    $0x0,%rax
jge    11b2 <sock_get_error+0x82>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    %eax,%r8d
lea    0xe5f(%rip),%rsi
call   1040 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <sock_get_error+0x88>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_get_error>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movl   $0x4,0xc(%rsp)
mov    0x2e9f(%rip),%rax
mov    (%rax),%esi
mov    0x2e7e(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
lea    0xc(%rsp),%r8
call   1030 <getsockopt@plt>
test   %rax,%rax
js     116e <sock_get_error+0x3e>
mov    0x8(%rsp),%eax
jmp    119e <sock_get_error+0x6e>
mov    0x2e6b(%rip),%rax
mov    (%rax),%r14d
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1050 <strerror@plt>
lea    0xe76(%rip),%rsi
mov    %r14d,%edi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
call   1040 <logger@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@LOG_CRIT = global i32 0
@.str = [49 x i8] c"getsockopt(SO_ERROR) failed on socket(%d) %d: %s\00"
define i32 @sock_get_error(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 4, i32* %5
%6 = load i32, i32* %3
%7 = load i32, i32* @SOL_SOCKET
%8 = load i32, i32* @SO_ERROR
%9 = call i64 @getsockopt(i32 %6, i32 %7, i32 %8, i32* %4, i32* %5)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %1
%12 = load i32, i32* @LOG_CRIT
%13 = load i32, i32* %3
%14 = load i32, i32* %4
%15 = load i32, i32* %4
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @logger(i32 %12, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14, i32 %16)
store i32 -1, i32* %2
br label %20
18: ; preds = %1
%19 = load i32, i32* %4
store i32 %19, i32* %2
br label %20
20: ; preds = %18, %11
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@LOG_CRIT = global i32 0
@.str = [49 x i8] c"getsockopt(SO_ERROR) failed on socket(%d) %d: %s\00"
define i32 @sock_get_error(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%6 = load i32, i32* @SOL_SOCKET
%7 = load i32, i32* @SO_ERROR
%8 = call i64 @getsockopt(i32 %0, i32 %6, i32 %7, i32* %2, i32* %3)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @LOG_CRIT
%12 = load i32, i32* %2
%13 = call i32 @strerror(i32 %12)
%14 = call i32 @logger(i32 %11, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %12, i32 %13)
br label %17
15: ; preds = %1
%16 = load i32, i32* %2
br label %17
17: ; preds = %15, %10
%18 = phi i32 [ -1, %10 ], [ %16, %15 ]
ret i32 %18
}