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

/* Variables and functions */
int EINTR;
int /*<<< orphan*/ MSG_DONTWAIT;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_PASSCRED;
int errno;
int /*<<< orphan*/ printf(char *, char *);
int send(int, char const *, int, int /*<<< orphan*/);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
char *strerror(int);
int /*<<< orphan*/ usleep(int);
int eslib_sock_send_cred(int sock) {
  int r, i, e;
  int optval = 1;
  const char msg = 'C';
  if (sock == -1)
    return -1;
  e = 0;
  errno = 0;
  if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval))) {
    printf("couldn't set socket option: %s\n", strerror(errno));
    return -1;
  }
  for (i = 0; i < 100; ++i) {
    r = send(sock, &msg, 1, MSG_DONTWAIT);
    if (r == -1 && errno == EINTR) {
      usleep(50);
      continue;
    } else {
      e = errno;
      break;
    }
  }
  optval = 0;
  if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)))
    printf("couldn't set socket option: %s\n", strerror(errno));
  if (r != 1) {
    printf("send_cred(): %s\n", strerror(e));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<eslib_sock_send_cred>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x18(%rbp)
movb   $0x43,-0x19(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    117c <eslib_sock_send_cred+0x2c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12eb <eslib_sock_send_cred+0x19b>
movl   $0x0,-0x14(%rbp)
mov    0x2e46(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11e7 <eslib_sock_send_cred+0x97>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe2a(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12eb <eslib_sock_send_cred+0x19b>
movl   $0x0,-0x10(%rbp)
cmpl   $0x64,-0x10(%rbp)
jge    1265 <eslib_sock_send_cred+0x115>
mov    -0x8(%rbp),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%ecx
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <send@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1246 <eslib_sock_send_cred+0xf6>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    0x2d91(%rip),%rcx
cmp    (%rcx),%eax
jne    1246 <eslib_sock_send_cred+0xf6>
mov    $0x32,%edi
call   1070 <usleep@plt>
jmp    1257 <eslib_sock_send_cred+0x107>
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1265 <eslib_sock_send_cred+0x115>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11ee <eslib_sock_send_cred+0x9e>
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     12b7 <eslib_sock_send_cred+0x167>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xd4e(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x1,-0xc(%rbp)
je     12e4 <eslib_sock_send_cred+0x194>
mov    -0x14(%rbp),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xd4d(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12eb <eslib_sock_send_cred+0x19b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eslib_sock_send_cred>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x1,0x4(%rsp)
movb   $0x43,0x3(%rsp)
mov    $0xffffffff,%eax
cmp    $0xffffffff,%edi
je     1281 <eslib_sock_send_cred+0x131>
mov    %edi,%r14d
mov    0x2e50(%rip),%rbp
movl   $0x0,0x0(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     11bd <eslib_sock_send_cred+0x6d>
mov    0x0(%rbp),%ebp
lea    0xe48(%rip),%rbx
jmp    126a <eslib_sock_send_cred+0x11a>
mov    $0x64,%r13d
mov    0x2df6(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
mov    %r14d,%edi
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1040 <send@plt>
mov    %eax,%ebx
mov    %rbp,%r15
mov    0x0(%rbp),%ebp
cmp    $0xffffffff,%eax
jne    1213 <eslib_sock_send_cred+0xc3>
cmp    (%r12),%ebp
jne    1213 <eslib_sock_send_cred+0xc3>
mov    $0x32,%edi
call   1070 <usleep@plt>
add    $0xffffffff,%r13d
mov    %r15,%rbp
jne    11d0 <eslib_sock_send_cred+0x80>
xor    %ebp,%ebp
movl   $0x0,0x4(%rsp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     125c <eslib_sock_send_cred+0x10c>
mov    (%r15),%edi
call   1060 <strerror@plt>
lea    0xdac(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
cmp    $0x1,%ebx
je     1281 <eslib_sock_send_cred+0x131>
lea    0xdb6(%rip),%rbx
mov    %ebp,%edi
call   1060 <strerror@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_PASSCRED = global i32 0
@.str = [32 x i8] c"couldn't set socket option: %s\0A\00"
@MSG_DONTWAIT = global i32 0
@EINTR = global i32 0
@.str.1 = [17 x i8] c"send_cred(): %s\0A\00"
define i32 @eslib_sock_send_cred(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i32 %0, i32* %3
store i32 1, i32* %7
store i8 67, i8* %8
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %61
12: ; preds = %1
store i32 0, i32* %6
store i32 0, i32* @errno
%13 = load i32, i32* %3
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_PASSCRED
%16 = call i64 @setsockopt(i32 %13, i32 %14, i32 %15, i32* %7, i32 4)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 -1, i32* %2
br label %61
22: ; preds = %12
store i32 0, i32* %5
br label %23
23: ; preds = %40, %22
%24 = load i32, i32* %5
%25 = icmp slt i32 %24, 100
br i1 %25, label %26, label %43
26: ; preds = %23
%27 = load i32, i32* %3
%28 = load i32, i32* @MSG_DONTWAIT
%29 = call i32 @send(i32 %27, i8* %8, i32 1, i32 %28)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %38
32: ; preds = %26
%33 = load i32, i32* @errno
%34 = load i32, i32* @EINTR
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @usleep(i32 50)
br label %40
38: ; preds = %32, %26
%39 = load i32, i32* @errno
store i32 %39, i32* %6
br label %43
40: ; preds = %36
%41 = load i32, i32* %5
%42 = add nsw i32 %41, 1
store i32 %42, i32* %5
br label %23
43: ; preds = %38, %23
store i32 0, i32* %7
%44 = load i32, i32* %3
%45 = load i32, i32* @SOL_SOCKET
%46 = load i32, i32* @SO_PASSCRED
%47 = call i64 @setsockopt(i32 %44, i32 %45, i32 %46, i32* %7, i32 4)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %43
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %49, %43
%54 = load i32, i32* %4
%55 = icmp ne i32 %54, 1
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = load i32, i32* %6
%58 = call i8* @strerror(i32 %57)
%59 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %58)
store i32 -1, i32* %2
br label %61
60: ; preds = %53
store i32 0, i32* %2
br label %61
61: ; preds = %60, %56, %18, %11
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_PASSCRED = global i32 0
@.str = [32 x i8] c"couldn't set socket option: %s\0A\00"
@MSG_DONTWAIT = global i32 0
@EINTR = global i32 0
@.str.1 = [17 x i8] c"send_cred(): %s\0A\00"
define i32 @eslib_sock_send_cred(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = bitcast i32* %2 to i8*
store i32 1, i32* %2
store i8 67, i8* %3
%5 = icmp eq i32 %0, -1
br i1 %5, label %43, label %6
6: ; preds = %1
store i32 0, i32* @errno
%7 = load i32, i32* @SOL_SOCKET
%8 = load i32, i32* @SO_PASSCRED
%9 = call i64 @setsockopt(i32 %0, i32 %7, i32 %8, i32* %2, i32 4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %6
%12 = load i32, i32* @errno
br label %38
13: ; preds = %6, %22
%14 = phi i32 [ %24, %22 ], [ 0, %6 ]
%15 = load i32, i32* @MSG_DONTWAIT
%16 = call i32 @send(i32 %0, i8* %3, i32 1, i32 %15)
%17 = icmp eq i32 %16, -1
%18 = load i32, i32* @errno
%19 = load i32, i32* @EINTR
%20 = icmp eq i32 %18, %19
%21 = select i1 %17, i1 %20, i1 false
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = call i32 @usleep(i32 50)
%24 = add nuw nsw i32 %14, 1
%25 = icmp eq i32 %24, 100
br i1 %25, label %26, label %13
26: ; preds = %22, %13
%27 = phi i32 [ %18, %13 ], [ 0, %22 ]
store i32 0, i32* %2
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_PASSCRED
%30 = call i64 @setsockopt(i32 %0, i32 %28, i32 %29, i32* %2, i32 4)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %26
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %32, %26
%37 = icmp eq i32 %16, 1
br i1 %37, label %43, label %38
38: ; preds = %36, %11
%39 = phi i32 [ %12, %11 ], [ %27, %36 ]
%40 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), %11 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %36 ]
%41 = call i8* @strerror(i32 %39)
%42 = call i32 @printf(i8* %40, i8* %41)
br label %43
43: ; preds = %38, %36, %1
%44 = phi i32 [ -1, %1 ], [ 0, %36 ], [ -1, %38 ]
ret i32 %44
}