#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*/ IPPROTO_TCP;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOCK_STREAM;
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sock_create(int *fd, int is_stream) {
  (*fd) = (is_stream ? socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)
                     : socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP));
  return (((*fd) == -1) ? -1 : 0);
}


x86 assembly (clang, -O0):
<sock_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1151 <sock_create+0x41>
mov    0x2e98(%rip),%rax
mov    (%rax),%edi
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
mov    0x2ea6(%rip),%rax
mov    (%rax),%edx
call   1030 <socket@plt>
mov    %eax,-0x10(%rbp)
jmp    1174 <sock_create+0x64>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
call   1030 <socket@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0xffffffff,%edx
cmove  %ecx,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_create>:
push   %rbx
mov    %rdi,%rbx
test   %esi,%esi
mov    0x2eb3(%rip),%rax
cmove  0x2ebb(%rip),%rax
mov    0x2e9c(%rip),%rcx
mov    (%rcx),%edi
mov    (%rax),%esi
mov    0x2eb1(%rip),%rax
cmove  0x2e79(%rip),%rax
mov    (%rax),%edx
call   1030 <socket@plt>
mov    %eax,(%rbx)
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
sete   %cl
neg    %ecx
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
define i32 @sock_create(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = load i32, i32* @IPPROTO_TCP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
br label %17
12: ; preds = %2
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = load i32, i32* @IPPROTO_UDP
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
br label %17
17: ; preds = %12, %7
%18 = phi i32 [ %11, %7 ], [ %16, %12 ]
%19 = load i32*, i32** %3
store i32 %18, i32* %19
%20 = load i32*, i32** %3
%21 = load i32, i32* %20
%22 = icmp eq i32 %21, -1
%23 = zext i1 %22 to i64
%24 = select i1 %22, i32 -1, i32 0
ret i32 %24
}

LLVM IR (-O2):
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
define i32 @sock_create(i32* nocapture writeonly %0, i32 %1) {
%3 = icmp eq i32 %1, 0
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = load i32, i32* @SOCK_STREAM
%7 = select i1 %3, i32 %5, i32 %6
%8 = load i32, i32* @IPPROTO_UDP
%9 = load i32, i32* @IPPROTO_TCP
%10 = select i1 %3, i32 %8, i32 %9
%11 = tail call i32 @socket(i32 %4, i32 %7, i32 %10)
store i32 %11, i32* %0
%12 = icmp eq i32 %11, -1
%13 = sext i1 %12 to i32
ret i32 %13
}