#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*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ net_err(char *, char *, int /*<<< orphan*/);
int reuse(char *, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int newsocket(char *err, int domain) {
  int fd = -1;
  if ((fd == socket(domain, SOCK_STREAM, 0)) == -1) {
    net_err(err, "socket: %s", strerror(errno));
    return -1;
  }
  if (reuse(err, fd) == -1) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<newsocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
cmp    $0xffffffff,%eax
jne    11cb <newsocket+0x7b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xe46(%rip),%rsi
call   1050 <net_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <newsocket+0xaa>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1040 <reuse@plt>
cmp    $0xffffffff,%eax
jne    11f4 <newsocket+0xa4>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <newsocket+0xaa>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newsocket>:
push   %rbx
mov    %esi,%eax
mov    %rdi,%rbx
mov    0x2ea3(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %rbx,%rdi
mov    $0xffffffff,%esi
call   1040 <reuse@plt>
cmp    $0xffffffff,%eax
jne    1164 <newsocket+0x34>
mov    $0xffffffff,%edi
call   1030 <close@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@SOCK_STREAM = global i32 0
@errno = global i32 0
define i32 @newsocket(i8* %0, i32 %1) {
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %1, i32 %3, i32 0)
%5 = tail call i32 @reuse(i8* %0, i32 -1)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @close(i32 -1)
br label %9
9: ; preds = %2, %7
ret i32 -1
}