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

/* Variables and functions */
int errno;
int setsockopt(int, int, int, void const *, int /*<<< orphan*/);
int __sulong_posix_setsockopt(int sockfd, int level, int optname,
                              const void *optval, socklen_t optlen) {
  {
    int native_errno = errno;
    int result = setsockopt(sockfd, level, optname, optval, optlen);
    if (result == (int)-1) {
      result = (int)(long)-errno;
    }
    errno = native_errno;
    return result;
  };
}


x86 assembly (clang, -O0):
<__sulong_posix_setsockopt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    0x2eb0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1168 <__sulong_posix_setsockopt+0x58>
xor    %eax,%eax
mov    0x2e7f(%rip),%rcx
sub    (%rcx),%eax
cltq
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%ecx
mov    0x2e6e(%rip),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__sulong_posix_setsockopt>:
push   %rbp
push   %rbx
push   %rax
mov    0x2ec6(%rip),%rbx
mov    (%rbx),%ebp
call   1030 <setsockopt@plt>
xor    %ecx,%ecx
sub    (%rbx),%ecx
cmp    $0xffffffff,%eax
cmove  %ecx,%eax
mov    %ebp,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @__sulong_posix_setsockopt(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%13 = load i32, i32* @errno
store i32 %13, i32* %11
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = load i8*, i8** %9
%18 = load i32, i32* %10
%19 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i8* %17, i32 %18)
store i32 %19, i32* %12
%20 = load i32, i32* %12
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %5
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
%25 = sext i32 %24 to i64
%26 = trunc i64 %25 to i32
store i32 %26, i32* %12
br label %27
27: ; preds = %22, %5
%28 = load i32, i32* %11
store i32 %28, i32* @errno
%29 = load i32, i32* %12
ret i32 %29
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @__sulong_posix_setsockopt(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = load i32, i32* @errno
%7 = tail call i32 @setsockopt(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4)
%8 = icmp eq i32 %7, -1
%9 = load i32, i32* @errno
%10 = sub nsw i32 0, %9
%11 = select i1 %8, i32 %10, i32 %7
store i32 %6, i32* @errno
ret i32 %11
}