#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_RAW;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_sockfd(void) {
  static int sockfd = -1;
  if (sockfd == -1) {
    if ((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
      perror("user: socket creating failed");
      return (-1);
    }
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<get_sockfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0xffffffff,0x2f01(%rip)
jne    1181 <get_sockfd+0x61>
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
mov    0x2e8a(%rip),%rax
mov    (%rax),%edx
call   1040 <socket@plt>
mov    %eax,0x2ed5(%rip)
cmp    $0xffffffff,%eax
jne    117c <get_sockfd+0x5c>
lea    0xe95(%rip),%rdi
call   1030 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    118a <get_sockfd+0x6a>
jmp    1181 <get_sockfd+0x61>
mov    0x2ea9(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sockfd>:
push   %rax
mov    0x2f09(%rip),%eax
cmp    $0xffffffff,%eax
je     112e <get_sockfd+0xe>
pop    %rcx
ret
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
mov    0x2e91(%rip),%rax
mov    (%rax),%edx
call   1040 <socket@plt>
mov    %eax,0x2edc(%rip)
cmp    $0xffffffff,%eax
jne    112c <get_sockfd+0xc>
lea    0xea0(%rip),%rdi
call   1030 <perror@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@get_sockfd.sockfd = internal global i32 -1
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@.str = [29 x i8] c"user: socket creating failed\00"
define i32 @get_sockfd() {
%1 = alloca i32
%2 = load i32, i32* @get_sockfd.sockfd
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %13
4: ; preds = %0
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @IPPROTO_RAW
%8 = call i32 @socket(i32 %5, i32 %6, i32 %7)
store i32 %8, i32* @get_sockfd.sockfd
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %15
12: ; preds = %4
br label %13
13: ; preds = %12, %0
%14 = load i32, i32* @get_sockfd.sockfd
store i32 %14, i32* %1
br label %15
15: ; preds = %13, %10
%16 = load i32, i32* %1
ret i32 %16
}

LLVM IR (-O2):
@get_sockfd.sockfd = internal unnamed_addr global i32 -1
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@.str = [29 x i8] c"user: socket creating failed\00"
define i32 @get_sockfd() {
%1 = load i32, i32* @get_sockfd.sockfd
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %11
3: ; preds = %0
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @IPPROTO_RAW
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
store i32 %7, i32* @get_sockfd.sockfd
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %0, %3, %9
%12 = phi i32 [ -1, %9 ], [ %7, %3 ], [ %1, %0 ]
ret i32 %12
}