#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_STREAM;
struct protoent *getprotobyname(char *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int new_sock() {
  static int p_num = -1;
  struct protoent *pep;
  if (p_num == -1) {
    if ((pep = getprotobyname("tcp")) == NULL) {
      perror("Kernel does not support tcp");
      return (-1);
    }
    p_num = pep->p_proto;
  }
  return (socket(AF_INET, SOCK_STREAM, p_num));
}


x86 assembly (clang, -O0):
<new_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0xffffffff,0x2ef9(%rip)
jne    1183 <new_sock+0x53>
lea    0xecc(%rip),%rdi
call   1030 <getprotobyname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1177 <new_sock+0x47>
lea    0xe9a(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a3 <new_sock+0x73>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,0x2eb5(%rip)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    0x2e9d(%rip),%edx
call   1050 <socket@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_sock>:
push   %rax
mov    0x2f01(%rip),%edx
cmp    $0xffffffff,%edx
jne    1155 <new_sock+0x25>
lea    0xed5(%rip),%rdi
call   1030 <getprotobyname@plt>
test   %rax,%rax
je     116d <new_sock+0x3d>
mov    (%rax),%edx
mov    %edx,0x2ee3(%rip)
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
pop    %rax
jmp    1050 <socket@plt>
lea    0xe8c(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.protoent = type { i32 }
@new_sock.p_num = internal global i32 -1
@.str = [4 x i8] c"tcp\00"
@.str.1 = [28 x i8] c"Kernel does not support tcp\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @new_sock() {
%1 = alloca i32
%2 = alloca %struct.protoent*
%3 = load i32, i32* @new_sock.p_num
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %14
5: ; preds = %0
%6 = call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.protoent* %6, %struct.protoent** %2
%7 = icmp eq %struct.protoent* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %19
10: ; preds = %5
%11 = load %struct.protoent*, %struct.protoent** %2
%12 = getelementptr inbounds %struct.protoent, %struct.protoent* %11, i32 0, i32 0
%13 = load i32, i32* %12
store i32 %13, i32* @new_sock.p_num
br label %14
14: ; preds = %10, %0
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = load i32, i32* @new_sock.p_num
%18 = call i32 @socket(i32 %15, i32 %16, i32 %17)
store i32 %18, i32* %1
br label %19
19: ; preds = %14, %8
%20 = load i32, i32* %1
ret i32 %20
}

LLVM IR (-O2):
%struct.protoent = type { i32 }
@new_sock.p_num = internal unnamed_addr global i32 -1
@.str = [4 x i8] c"tcp\00"
@.str.1 = [28 x i8] c"Kernel does not support tcp\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @new_sock() {
%1 = load i32, i32* @new_sock.p_num
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %11
3: ; preds = %0
%4 = tail call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq %struct.protoent* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %16
8: ; preds = %3
%9 = getelementptr inbounds %struct.protoent, %struct.protoent* %4, i64 0, i32 0
%10 = load i32, i32* %9
store i32 %10, i32* @new_sock.p_num
br label %11
11: ; preds = %8, %0
%12 = phi i32 [ %10, %8 ], [ %1, %0 ]
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 %12)
br label %16
16: ; preds = %11, %6
%17 = phi i32 [ -1, %6 ], [ %15, %11 ]
ret i32 %17
}