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

/* Variables and functions */
int /*<<< orphan*/ ENOPROTOOPT;
int SOL_SOCKET;
int /*<<< orphan*/ errno;
struct socket_info *find_socket_info(int);
int getsockopt(int, int, int, void *, int /*<<< orphan*/ *);
int swrap_getsockopt(int s, int level, int optname, void *optval,
                     socklen_t *optlen) {
  struct socket_info *si = find_socket_info(s);
  if (!si) {
    return getsockopt(s, level, optname, optval, optlen);
  }
  if (level == SOL_SOCKET) {
    return getsockopt(s, level, optname, optval, optlen);
  }
  errno = ENOPROTOOPT;
  return -1;
}


x86 assembly (clang, -O0):
<swrap_getsockopt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <find_socket_info@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    116e <swrap_getsockopt+0x4e>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
call   1040 <getsockopt@plt>
mov    %eax,-0x4(%rbp)
jmp    11b7 <swrap_getsockopt+0x97>
mov    -0xc(%rbp),%eax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%eax
jne    119e <swrap_getsockopt+0x7e>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
call   1040 <getsockopt@plt>
mov    %eax,-0x4(%rbp)
jmp    11b7 <swrap_getsockopt+0x97>
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e22(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swrap_getsockopt>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r14
mov    %rcx,%r15
mov    %edx,%r12d
mov    %esi,%ebx
mov    %edi,%ebp
call   1030 <find_socket_info@plt>
test   %rax,%rax
je     114a <swrap_getsockopt+0x2a>
mov    0x2ea2(%rip),%rax
cmp    %ebx,(%rax)
jne    1164 <swrap_getsockopt+0x44>
mov    %ebp,%edi
mov    %ebx,%esi
mov    %r12d,%edx
mov    %r15,%rcx
mov    %r14,%r8
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <getsockopt@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    0x2e5c(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.socket_info = type { i32 }
@SOL_SOCKET = global i32 0
@ENOPROTOOPT = global i32 0
@errno = global i32 0
define i32 @swrap_getsockopt(i32 %0, i32 %1, i32 %2, i8* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca %struct.socket_info*
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32* %4, i32** %11
%13 = load i32, i32* %7
%14 = call %struct.socket_info* @find_socket_info(i32 %13)
store %struct.socket_info* %14, %struct.socket_info** %12
%15 = load %struct.socket_info*, %struct.socket_info** %12
%16 = icmp ne %struct.socket_info* %15, null
br i1 %16, label %24, label %17
17: ; preds = %5
%18 = load i32, i32* %7
%19 = load i32, i32* %8
%20 = load i32, i32* %9
%21 = load i8*, i8** %10
%22 = load i32*, i32** %11
%23 = call i32 @getsockopt(i32 %18, i32 %19, i32 %20, i8* %21, i32* %22)
store i32 %23, i32* %6
br label %37
24: ; preds = %5
%25 = load i32, i32* %8
%26 = load i32, i32* @SOL_SOCKET
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %35
28: ; preds = %24
%29 = load i32, i32* %7
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = load i8*, i8** %10
%33 = load i32*, i32** %11
%34 = call i32 @getsockopt(i32 %29, i32 %30, i32 %31, i8* %32, i32* %33)
store i32 %34, i32* %6
br label %37
35: ; preds = %24
%36 = load i32, i32* @ENOPROTOOPT
store i32 %36, i32* @errno
store i32 -1, i32* %6
br label %37
37: ; preds = %35, %28, %17
%38 = load i32, i32* %6
ret i32 %38
}

LLVM IR (-O2):
%struct.socket_info = type { i32 }
@SOL_SOCKET = global i32 0
@ENOPROTOOPT = global i32 0
@errno = global i32 0
define i32 @swrap_getsockopt(i32 %0, i32 %1, i32 %2, i8* %3, i32* %4) {
%6 = tail call %struct.socket_info* @find_socket_info(i32 %0)
%7 = icmp eq %struct.socket_info* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @getsockopt(i32 %0, i32 %1, i32 %2, i8* %3, i32* %4)
br label %17
10: ; preds = %5
%11 = load i32, i32* @SOL_SOCKET
%12 = icmp eq i32 %11, %1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @getsockopt(i32 %0, i32 %1, i32 %2, i8* %3, i32* %4)
br label %17
15: ; preds = %10
%16 = load i32, i32* @ENOPROTOOPT
store i32 %16, i32* @errno
br label %17
17: ; preds = %15, %13, %8
%18 = phi i32 [ %14, %13 ], [ -1, %15 ], [ %9, %8 ]
ret i32 %18
}