#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 protoent {
  int p_proto;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOCK_STREAM;
struct protoent *getprotobyname(char const *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcmp(char const *, char *);
int sock_create(const char *proto) {
  int p_num;
  struct protoent *pep;
  if ((pep = getprotobyname(proto)) == NULL) {
    perror("Protocol not support");
    return -1;
  }
  p_num = pep->p_proto;
  if (!strcmp(proto, "tcp"))
    return socket(AF_INET, SOCK_STREAM, p_num);
  else if (!strcmp(proto, "udp"))
    return socket(AF_INET, SOCK_DGRAM, p_num);
  else
    return -1;
}


x86 assembly (clang, -O0):
<sock_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <getprotobyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    117b <sock_create+0x3b>
lea    0xe96(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <sock_create+0xc1>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe86(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    11bf <sock_create+0x7f>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1060 <socket@plt>
mov    %eax,-0x4(%rbp)
jmp    1201 <sock_create+0xc1>
mov    -0x10(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    11fa <sock_create+0xba>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1060 <socket@plt>
mov    %eax,-0x4(%rbp)
jmp    1201 <sock_create+0xc1>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_create>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <getprotobyname@plt>
test   %rax,%rax
je     11b5 <sock_create+0x75>
mov    (%rax),%ebp
lea    0xebc(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
je     1184 <sock_create+0x44>
lea    0xead(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
je     1196 <sock_create+0x56>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    0x2e3c(%rip),%rax
jmp    11a6 <sock_create+0x66>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <socket@plt>
lea    0xe44(%rip),%rdi
call   1050 <perror@plt>
jmp    1178 <sock_create+0x38>

LLVM IR (-O0):
%struct.protoent = type { i32 }
@.str = [21 x i8] c"Protocol not support\00"
@.str.1 = [4 x i8] c"tcp\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [4 x i8] c"udp\00"
@SOCK_DGRAM = global i32 0
define i32 @sock_create(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.protoent*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call %struct.protoent* @getprotobyname(i8* %6)
store %struct.protoent* %7, %struct.protoent** %5
%8 = icmp eq %struct.protoent* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %33
11: ; preds = %1
%12 = load %struct.protoent*, %struct.protoent** %5
%13 = getelementptr inbounds %struct.protoent, %struct.protoent* %12, i32 0, i32 0
%14 = load i32, i32* %13
store i32 %14, i32* %4
%15 = load i8*, i8** %3
%16 = call i32 @strcmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp ne i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %11
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = load i32, i32* %4
%22 = call i32 @socket(i32 %19, i32 %20, i32 %21)
store i32 %22, i32* %2
br label %33
23: ; preds = %11
%24 = load i8*, i8** %3
%25 = call i32 @strcmp(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp ne i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = load i32, i32* @AF_INET
%29 = load i32, i32* @SOCK_DGRAM
%30 = load i32, i32* %4
%31 = call i32 @socket(i32 %28, i32 %29, i32 %30)
store i32 %31, i32* %2
br label %33
32: ; preds = %23
store i32 -1, i32* %2
br label %33
33: ; preds = %32, %27, %18, %9
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
%struct.protoent = type { i32 }
@.str = [21 x i8] c"Protocol not support\00"
@.str.1 = [4 x i8] c"tcp\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [4 x i8] c"udp\00"
@SOCK_DGRAM = global i32 0
define i32 @sock_create(i8* %0) {
%2 = tail call %struct.protoent* @getprotobyname(i8* %0)
%3 = icmp eq %struct.protoent* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %22
6: ; preds = %1
%7 = getelementptr inbounds %struct.protoent, %struct.protoent* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %6
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 %8)
br label %22
15: ; preds = %6
%16 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_DGRAM
%21 = tail call i32 @socket(i32 %19, i32 %20, i32 %8)
br label %22
22: ; preds = %15, %18, %11, %4
%23 = phi i32 [ -1, %4 ], [ %21, %18 ], [ %14, %11 ], [ -1, %15 ]
ret i32 %23
}