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

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ LOG_ERR;
int O_NONBLOCK;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int, ...);
int set_socket_nonblocking(int socketfd) {
  int flag;
  if (0 > socketfd) {
    syslog(LOG_ERR, "[%d]:sfd invalid!\r\n", 372);
    return -1;
  }
  flag = fcntl(socketfd, F_GETFL, 0);
  if (-1 == flag) {
    syslog(LOG_ERR, "[%d]:fcntl get  flag error!socketfd:%d(%m)\r\n", 378,
           socketfd);
    return -1;
  }
  if (-1 == fcntl(socketfd, F_SETFL, flag | O_NONBLOCK)) {
    syslog(LOG_ERR, "[%d]:fcntl set nonblocking error!socketfd%d(%m)\r\n", 382,
           socketfd);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_socket_nonblocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
jle    115e <set_socket_nonblocking+0x3e>
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
lea    0xeba(%rip),%rsi
mov    $0x174,%edx
mov    $0x0,%al
call   1040 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <set_socket_nonblocking+0xeb>
mov    -0x8(%rbp),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    11ad <set_socket_nonblocking+0x8d>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%ecx
lea    0xe7f(%rip),%rsi
mov    $0x17a,%edx
mov    $0x0,%al
call   1040 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <set_socket_nonblocking+0xeb>
mov    -0x8(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2e25(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1204 <set_socket_nonblocking+0xe4>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%ecx
lea    0xe55(%rip),%rsi
mov    $0x17e,%edx
mov    $0x0,%al
call   1040 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <set_socket_nonblocking+0xeb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_socket_nonblocking>:
push   %rbp
push   %rbx
push   %rax
test   %edi,%edi
js     1177 <set_socket_nonblocking+0x57>
mov    %edi,%ebx
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1195 <set_socket_nonblocking+0x75>
mov    0x2e91(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e98(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11b8 <set_socket_nonblocking+0x98>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xed1(%rip),%rsi
mov    $0x17e,%edx
jmp    11aa <set_socket_nonblocking+0x8a>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
mov    $0x174,%edx
xor    %eax,%eax
call   1040 <syslog@plt>
jmp    11b3 <set_socket_nonblocking+0x93>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
mov    $0x17a,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <syslog@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [20 x i8] c"[%d]:sfd invalid!\0D\0A\00"
@F_GETFL = global i32 0
@.str.1 = [45 x i8] c"[%d]:fcntl get flag error!socketfd:%d(%m)\0D\0A\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [50 x i8] c"[%d]:fcntl set nonblocking error!socketfd%d(%m)\0D\0A\00"
define i32 @set_socket_nonblocking(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp sgt i32 0, %5
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @LOG_ERR
%9 = call i32 (i32, i8*, i32, ...) @syslog(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 372)
store i32 -1, i32* %2
br label %33
10: ; preds = %1
%11 = load i32, i32* %3
%12 = load i32, i32* @F_GETFL
%13 = call i32 @fcntl(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i32, i32* @LOG_ERR
%18 = load i32, i32* %3
%19 = call i32 (i32, i8*, i32, ...) @syslog(i32 %17, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 378, i32 %18)
store i32 -1, i32* %2
br label %33
20: ; preds = %10
%21 = load i32, i32* %3
%22 = load i32, i32* @F_SETFL
%23 = load i32, i32* %4
%24 = load i32, i32* @O_NONBLOCK
%25 = or i32 %23, %24
%26 = call i32 @fcntl(i32 %21, i32 %22, i32 %25)
%27 = icmp eq i32 -1, %26
br i1 %27, label %28, label %32
28: ; preds = %20
%29 = load i32, i32* @LOG_ERR
%30 = load i32, i32* %3
%31 = call i32 (i32, i8*, i32, ...) @syslog(i32 %29, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 382, i32 %30)
store i32 -1, i32* %2
br label %33
32: ; preds = %20
store i32 0, i32* %2
br label %33
33: ; preds = %32, %28, %16, %7
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [20 x i8] c"[%d]:sfd invalid!\0D\0A\00"
@F_GETFL = global i32 0
@.str.1 = [45 x i8] c"[%d]:fcntl get flag error!socketfd:%d(%m)\0D\0A\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [50 x i8] c"[%d]:fcntl set nonblocking error!socketfd%d(%m)\0D\0A\00"
define i32 @set_socket_nonblocking(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = load i32, i32* @LOG_ERR
%5 = tail call i32 (i32, i8*, i32, ...) @syslog(i32 %4, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 372)
br label %22
6: ; preds = %1
%7 = load i32, i32* @F_GETFL
%8 = tail call i32 @fcntl(i32 %0, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = load i32, i32* @LOG_ERR
%12 = tail call i32 (i32, i8*, i32, ...) @syslog(i32 %11, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 378, i32 %0)
br label %22
13: ; preds = %6
%14 = load i32, i32* @F_SETFL
%15 = load i32, i32* @O_NONBLOCK
%16 = or i32 %15, %8
%17 = tail call i32 @fcntl(i32 %0, i32 %14, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @LOG_ERR
%21 = tail call i32 (i32, i8*, i32, ...) @syslog(i32 %20, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 382, i32 %0)
br label %22
22: ; preds = %13, %19, %10, %3
%23 = phi i32 [ -1, %3 ], [ -1, %10 ], [ -1, %19 ], [ 0, %13 ]
ret i32 %23
}