#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*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int new_socket() {
  int sfd;
  int flags;
  if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket()");
    return -1;
  }
  if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 ||
      fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
    perror("setting O_NONBLOCK");
    close(sfd);
    return -1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<new_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1185 <new_socket+0x45>
lea    0xe8c(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <new_socket+0xb0>
mov    -0x8(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jl     11ca <new_socket+0x8a>
mov    -0x8(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2e26(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11ea <new_socket+0xaa>
lea    0xe38(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <new_socket+0xb0>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_socket>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%edi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11af <new_socket+0x6f>
mov    %eax,%ebx
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     119a <new_socket+0x5a>
mov    0x2e52(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e59(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     119a <new_socket+0x5a>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe68(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11bb <new_socket+0x7b>
lea    0xe4a(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1196 <new_socket+0x56>

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [19 x i8] c"setting O_NONBLOCK\00"
define i32 @new_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %29
10: ; preds = %0
%11 = load i32, i32* %2
%12 = load i32, i32* @F_GETFL
%13 = call i32 @fcntl(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %10
%16 = load i32, i32* %2
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* %3
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %18, %19
%21 = call i32 @fcntl(i32 %16, i32 %17, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %15, %10
%24 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %1
br label %29
27: ; preds = %15
%28 = load i32, i32* %2
store i32 %28, i32* %1
br label %29
29: ; preds = %27, %23, %8
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [19 x i8] c"setting O_NONBLOCK\00"
define i32 @new_socket() {
%1 = load i32, i32* @AF_INET
%2 = load i32, i32* @SOCK_STREAM
%3 = tail call i32 @socket(i32 %1, i32 %2, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %20
7: ; preds = %0
%8 = load i32, i32* @F_GETFL
%9 = tail call i32 @fcntl(i32 %3, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %7
%12 = load i32, i32* @F_SETFL
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %13, %9
%15 = tail call i32 @fcntl(i32 %3, i32 %12, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11, %7
%18 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @close(i32 %3)
br label %20
20: ; preds = %11, %17, %5
%21 = phi i32 [ -1, %5 ], [ -1, %17 ], [ %3, %11 ]
ret i32 %21
}