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

/* Variables and functions */
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ log_message(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int if_setsockopt_rcvbuf(int *sd, int val) {
  int ret;
  if (*sd < 0)
    return -1;
  ret = setsockopt(*sd, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val));
  if (ret < 0) {
    log_message(LOG_INFO, "cant set SO_RCVBUF IP option. errno=%d (%m)", errno);
    close(*sd);
    *sd = -1;
  }
  return *sd;
}


x86 assembly (clang, -O0):
<if_setsockopt_rcvbuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1158 <if_setsockopt_rcvbuf+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <if_setsockopt_rcvbuf+0x98>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bf <if_setsockopt_rcvbuf+0x8f>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
lea    0xe5b(%rip),%rsi
call   1050 <log_message@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_setsockopt_rcvbuf>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %esi,0x4(%rsp)
mov    (%rdi),%edi
mov    $0xffffffff,%ebp
test   %edi,%edi
js     119a <if_setsockopt_rcvbuf+0x6a>
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     116f <if_setsockopt_rcvbuf+0x3f>
mov    (%rbx),%ebp
jmp    119a <if_setsockopt_rcvbuf+0x6a>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
lea    0xe78(%rip),%rsi
call   1050 <log_message@plt>
mov    (%rbx),%edi
call   1040 <close@plt>
movl   $0xffffffff,(%rbx)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@LOG_INFO = global i32 0
@.str = [44 x i8] c"cant set SO_RCVBUF IP option. errno=%d (%m)\00"
@errno = global i32 0
define i32 @if_setsockopt_rcvbuf(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%7 = load i32*, i32** %4
%8 = load i32, i32* %7
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %30
11: ; preds = %2
%12 = load i32*, i32** %4
%13 = load i32, i32* %12
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_RCVBUF
%16 = call i32 @setsockopt(i32 %13, i32 %14, i32 %15, i32* %5, i32 4)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %11
%20 = load i32, i32* @LOG_INFO
%21 = load i32, i32* @errno
%22 = call i32 @log_message(i32 %20, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32*, i32** %4
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32*, i32** %4
store i32 -1, i32* %26
br label %27
27: ; preds = %19, %11
%28 = load i32*, i32** %4
%29 = load i32, i32* %28
store i32 %29, i32* %3
br label %30
30: ; preds = %27, %10
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@LOG_INFO = global i32 0
@.str = [44 x i8] c"cant set SO_RCVBUF IP option. errno=%d (%m)\00"
@errno = global i32 0
define i32 @if_setsockopt_rcvbuf(i32* nocapture %0, i32 %1) {
%3 = alloca i32
store i32 %1, i32* %3
%4 = load i32, i32* %0
%5 = icmp slt i32 %4, 0
br i1 %5, label %19, label %6
6: ; preds = %2
%7 = load i32, i32* @SOL_SOCKET
%8 = load i32, i32* @SO_RCVBUF
%9 = call i32 @setsockopt(i32 %4, i32 %7, i32 %8, i32* %3, i32 4)
%10 = icmp slt i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %6
%12 = load i32, i32* %0
br label %19
13: ; preds = %6
%14 = load i32, i32* @LOG_INFO
%15 = load i32, i32* @errno
%16 = call i32 @log_message(i32 %14, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %0
%18 = call i32 @close(i32 %17)
store i32 -1, i32* %0
br label %19
19: ; preds = %13, %11, %2
%20 = phi i32 [ -1, %2 ], [ %12, %11 ], [ -1, %13 ]
ret i32 %20
}