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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fdopen(int, char *);
int socket(int, int, int);
int /*<<< orphan*/ tcpd_warn(char *);
FILE *fsocket(int domain, int type, int protocol) {
  int s;
  FILE *fp;
  if ((s = socket(domain, type, protocol)) < 0) {
    tcpd_warn("socket: %m");
    return (0);
  } else {
    if ((fp = fdopen(s, "r+")) == 0) {
      tcpd_warn("fdopen: %m");
      close(s);
    }
    return (fp);
  }
}


x86 assembly (clang, -O0):
<fsocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1184 <fsocket+0x44>
lea    0xe8e(%rip),%rdi
call   1040 <tcpd_warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    11bd <fsocket+0x7d>
mov    -0x18(%rbp),%edi
lea    0xe7d(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b5 <fsocket+0x75>
lea    0xe66(%rip),%rdi
call   1040 <tcpd_warn@plt>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsocket>:
push   %rbx
call   1060 <socket@plt>
test   %eax,%eax
js     1161 <fsocket+0x21>
mov    %eax,%ebx
lea    0xeb8(%rip),%rsi
mov    %eax,%edi
call   1050 <fdopen@plt>
test   %rax,%rax
je     116f <fsocket+0x2f>
pop    %rbx
ret
lea    0xe98(%rip),%rdi
call   1040 <tcpd_warn@plt>
jmp    1182 <fsocket+0x42>
lea    0xe98(%rip),%rdi
call   1040 <tcpd_warn@plt>
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [11 x i8] c"socket: %m\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [11 x i8] c"fdopen: %m\00"
define i32* @fsocket(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @tcpd_warn(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %4
br label %27
17: ; preds = %3
%18 = load i32, i32* %8
%19 = call i32* @fdopen(i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %9
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = call i32 @tcpd_warn(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* %8
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %21, %17
%26 = load i32*, i32** %9
store i32* %26, i32** %4
br label %27
27: ; preds = %25, %15
%28 = load i32*, i32** %4
ret i32* %28
}

LLVM IR (-O2):
@.str = [11 x i8] c"socket: %m\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [11 x i8] c"fdopen: %m\00"
define noalias i32* @fsocket(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @socket(i32 %0, i32 %1, i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @tcpd_warn(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %14
8: ; preds = %3
%9 = tail call i32* @fdopen(i32 %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @tcpd_warn(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @close(i32 %4)
br label %14
14: ; preds = %8, %11, %6
%15 = phi i32* [ null, %6 ], [ null, %11 ], [ %9, %8 ]
ret i32* %15
}