#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*/ value;
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_SNDBUF;
int errno;
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int fd_inc_sndbuf(int fd, size_t n) {
  int r, value;
  socklen_t l = sizeof(value);
  r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
  if (r >= 0 && l == sizeof(value) && (size_t)value >= n * 2)
    return 0;
  value = (int)n;
  r = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value));
  if (r < 0)
    return -errno;
  return 1;
}


x86 assembly (clang, -O0):
<fd_inc_sndbuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
mov    0x2e97(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1040 <getsockopt@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1194 <fd_inc_sndbuf+0x74>
movslq -0x1c(%rbp),%rax
cmp    $0x4,%rax
jne    1194 <fd_inc_sndbuf+0x74>
movslq -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
shl    $0x1,%rcx
cmp    %rcx,%rax
jb     1194 <fd_inc_sndbuf+0x74>
movl   $0x0,-0x4(%rbp)
jmp    11e6 <fd_inc_sndbuf+0xc6>
mov    -0x10(%rbp),%rax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11df <fd_inc_sndbuf+0xbf>
xor    %eax,%eax
mov    0x2dfb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e6 <fd_inc_sndbuf+0xc6>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_inc_sndbuf>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
mov    0x2eac(%rip),%r15
mov    (%r15),%esi
mov    0x2e9a(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%rcx
lea    0x4(%rsp),%r8
call   1040 <getsockopt@plt>
test   %eax,%eax
js     1171 <fd_inc_sndbuf+0x51>
cmpl   $0x4,0x4(%rsp)
jne    1171 <fd_inc_sndbuf+0x51>
movslq (%rsp),%rcx
lea    (%r14,%r14,1),%rdx
xor    %eax,%eax
cmp    %rcx,%rdx
jbe    11a2 <fd_inc_sndbuf+0x82>
mov    %r14d,(%rsp)
mov    (%r15),%esi
mov    (%r12),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jns    11a2 <fd_inc_sndbuf+0x82>
mov    0x2e32(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@errno = global i32 0
define i32 @fd_inc_sndbuf(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32 4, i32* %8
%9 = load i32, i32* %4
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_SNDBUF
%12 = call i32 @getsockopt(i32 %9, i32 %10, i32 %11, i32* %7, i32* %8)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %26
15: ; preds = %2
%16 = load i32, i32* %8
%17 = sext i32 %16 to i64
%18 = icmp eq i64 %17, 4
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = load i64, i64* %5
%23 = mul i64 %22, 2
%24 = icmp uge i64 %21, %23
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 0, i32* %3
br label %39
26: ; preds = %19, %15, %2
%27 = load i64, i64* %5
%28 = trunc i64 %27 to i32
store i32 %28, i32* %7
%29 = load i32, i32* %4
%30 = load i32, i32* @SOL_SOCKET
%31 = load i32, i32* @SO_SNDBUF
%32 = call i32 @setsockopt(i32 %29, i32 %30, i32 %31, i32* %7, i32 4)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %26
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
store i32 %37, i32* %3
br label %39
38: ; preds = %26
store i32 1, i32* %3
br label %39
39: ; preds = %38, %35, %25
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@errno = global i32 0
define i32 @fd_inc_sndbuf(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%7 = load i32, i32* @SOL_SOCKET
%8 = load i32, i32* @SO_SNDBUF
%9 = call i32 @getsockopt(i32 %0, i32 %7, i32 %8, i32* %3, i32* %4)
%10 = icmp sgt i32 %9, -1
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 4
%13 = select i1 %10, i1 %12, i1 false
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = shl i64 %1, 1
%18 = icmp ugt i64 %17, %16
br i1 %18, label %19, label %28
19: ; preds = %14, %2
%20 = trunc i64 %1 to i32
store i32 %20, i32* %3
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_SNDBUF
%23 = call i32 @setsockopt(i32 %0, i32 %21, i32 %22, i32* %3, i32 4)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
br label %28
28: ; preds = %19, %14, %25
%29 = phi i32 [ %27, %25 ], [ 0, %14 ], [ 1, %19 ]
ret i32 %29
}