#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*/ EXIT_FAILURE;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char const *, char *);
int get_socket(const char *type) {
  int sock;
  if (strcmp(type, "udp") == 0) {
    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  } else if (strcmp(type, "tcp") == 0) {
    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  }
  if (sock < 0) {
    perror("socket() failed.");
    exit(EXIT_FAILURE);
  }
  return sock;
}


x86 assembly (clang, -O0):
<get_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    118e <get_socket+0x4e>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0xc(%rbp)
jmp    11d0 <get_socket+0x90>
mov    -0x8(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    11cb <get_socket+0x8b>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0xc(%rbp)
jmp    11d0 <get_socket+0x90>
cmpl   $0x0,-0xc(%rbp)
jge    11f4 <get_socket+0xb4>
lea    0xe27(%rip),%rdi
call   1040 <perror@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_socket>:
push   %rbx
mov    %rdi,%rbx
lea    0xeb5(%rip),%rsi
call   1030 <strcmp@plt>
test   %rax,%rax
je     117c <get_socket+0x3c>
lea    0xea8(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
mov    %rax,%rcx
test   %rcx,%rcx
jne    11a0 <get_socket+0x60>
mov    0x2e75(%rip),%rax
mov    0x2e56(%rip),%rcx
jmp    118a <get_socket+0x4a>
mov    0x2e2d(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    0x2e37(%rip),%rdx
mov    (%rdx),%edi
mov    (%rcx),%esi
mov    (%rax),%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11a2 <get_socket+0x62>
pop    %rbx
ret
lea    0xe5f(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"udp\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [4 x i8] c"tcp\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [17 x i8] c"socket() failed.\00"
@EXIT_FAILURE = global i32 0
define i32 @get_socket(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_UDP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %3
br label %22
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @PF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = load i32, i32* @IPPROTO_TCP
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
store i32 %20, i32* %3
br label %21
21: ; preds = %16, %12
br label %22
22: ; preds = %21, %7
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %22
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [4 x i8] c"udp\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [4 x i8] c"tcp\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [17 x i8] c"socket() failed.\00"
@EXIT_FAILURE = global i32 0
define i32 @get_socket(i8* %0) {
%2 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i64 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %19
7: ; preds = %4, %1
%8 = phi i32* [ @SOCK_DGRAM, %1 ], [ @SOCK_STREAM, %4 ]
%9 = phi i32* [ @IPPROTO_UDP, %1 ], [ @IPPROTO_TCP, %4 ]
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* %8
%12 = load i32, i32* %9
%13 = tail call i32 @socket(i32 %10, i32 %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %7
%16 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %4, %7
%20 = phi i32 [ %13, %7 ], [ undef, %4 ]
ret i32 %20
}