#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 addrinfo {
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
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(struct addrinfo *ai) {
  int sfd;
  int flags;
  if ((sfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) {
    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    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    117d <new_socket+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <new_socket+0xa8>
mov    -0x14(%rbp),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jl     11c2 <new_socket+0x82>
mov    -0x14(%rbp),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e36(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11e2 <new_socket+0xa2>
lea    0xe37(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <new_socket+0xa8>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_socket>:
push   %rbp
push   %rbx
push   %rax
mov    0x8(%rdi),%eax
mov    (%rdi),%edx
mov    0x4(%rdi),%esi
mov    %eax,%edi
call   1060 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1195 <new_socket+0x55>
mov    %eax,%ebp
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     119e <new_socket+0x5e>
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e64(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     119e <new_socket+0x5e>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5b(%rip),%rdi
call   1050 <perror@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1195 <new_socket+0x55>

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

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