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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int getsockopt(int, int, int, char *, int *);
int socket_getoption(int fd, int domain, int option, int defaut) {
  int ret;
  while (1) {
    int opt = -1;
    socklen_t optlen = sizeof(opt);
    ret = getsockopt(fd, domain, option, (char *)&opt, &optlen);
    if (ret == 0)
      return (int)opt;
    if (errno != EINTR)
      return defaut;
  }
}


x86 assembly (clang, -O0):
<socket_getoption>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x4,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    -0x1c(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1030 <getsockopt@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1160 <socket_getoption+0x50>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118a <socket_getoption+0x7a>
mov    0x2e79(%rip),%rax
mov    (%rax),%rax
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
je     1185 <socket_getoption+0x75>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118a <socket_getoption+0x7a>
jmp    1124 <socket_getoption+0x14>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_getoption>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x10(%rsp)
mov    %edx,%r15d
mov    %esi,%ebx
mov    %edi,%ebp
lea    0x14(%rsp),%r13
mov    0x2eab(%rip),%r14
mov    0x2e9c(%rip),%r12
nopl   0x0(%rax)
movl   $0xffffffff,0xc(%rsp)
ff
movl   $0x4,0x14(%rsp)
mov    %ebp,%edi
mov    %ebx,%esi
mov    %r15d,%edx
lea    0xc(%rsp),%rcx
mov    %r13,%r8
call   1030 <getsockopt@plt>
test   %eax,%eax
je     1177 <socket_getoption+0x67>
mov    (%r14),%rax
cmp    (%r12),%rax
je     1140 <socket_getoption+0x30>
mov    0x10(%rsp),%eax
jmp    117b <socket_getoption+0x6b>
mov    0xc(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @socket_getoption(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%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 i32 %3, i32* %9
br label %13
13: ; preds = %4, %29
store i32 -1, i32* %11
store i32 4, i32* %12
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = bitcast i32* %11 to i8*
%18 = call i32 @getsockopt(i32 %14, i32 %15, i32 %16, i8* %17, i32* %12)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = load i32, i32* %11
store i32 %22, i32* %5
br label %30
23: ; preds = %13
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i32, i32* %9
store i32 %28, i32* %5
br label %30
29: ; preds = %23
br label %13
30: ; preds = %27, %21
%31 = load i32, i32* %5
ret i32 %31
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @socket_getoption(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %5 to i8*
%8 = bitcast i32* %6 to i8*
br label %9
9: ; preds = %14, %4
store i32 -1, i32* %5
store i32 4, i32* %6
%10 = call i32 @getsockopt(i32 %0, i32 %1, i32 %2, i8* %7, i32* %6)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = load i32, i32* %5
br label %18
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %9, label %18
18: ; preds = %14, %12
%19 = phi i32 [ %13, %12 ], [ %3, %14 ]
ret i32 %19
}