#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 */
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int strtol(char const *, char **, int);
int get_fd_num(const char *fd_str) {
  int sock;
  char *err;
  errno = 0;
  sock = strtol(fd_str, &err, 10);
  if (errno) {
    fprintf(stderr, "Failed in strtol for socket fd, reason: %s\n",
            strerror(errno));
    return -1;
  }
  if (!*fd_str || *err || sock < 0) {
    fprintf(stderr, "bad numerical value for socket fd '%s'\n", fd_str);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<get_fd_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e72(%rip),%rax
cmpq   $0x0,(%rax)
je     11a9 <get_fd_num+0x79>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe68(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <get_fd_num+0xcb>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11d0 <get_fd_num+0xa0>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11d0 <get_fd_num+0xa0>
cmpl   $0x0,-0x14(%rbp)
jge    11f5 <get_fd_num+0xc5>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe48(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <get_fd_num+0xcb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_fd_num>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e9a(%rip),%r14
movq   $0x0,(%r14)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    (%r14),%rdi
test   %rdi,%rdi
je     1176 <get_fd_num+0x46>
mov    0x2e7f(%rip),%rax
mov    (%rax),%ebx
call   1050 <strerror@plt>
lea    0xe91(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
jmp    119b <get_fd_num+0x6b>
cmpb   $0x0,(%rbx)
je     1188 <get_fd_num+0x58>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    1188 <get_fd_num+0x58>
test   %eax,%eax
jns    11a5 <get_fd_num+0x75>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe94(%rip),%rsi
mov    %rbx,%rdx
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
@stderr = global i32 0
@.str = [44 x i8] c"Failed in strtol for socket fd, reason: %s\0A\00"
@.str.1 = [40 x i8] c"bad numerical value for socket fd '%s'\0A\00"
define i32 @get_fd_num(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i64 0, i64* @errno
%6 = load i8*, i8** %3
%7 = call i32 @strtol(i8* %6, i8** %5, i32 10)
store i32 %7, i32* %4
%8 = load i64, i64* @errno
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i64, i64* @errno
%13 = call i8* @strerror(i64 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %2
br label %33
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = load i8, i8* %16
%18 = icmp ne i8 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i8*, i8** %5
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24, %19, %15
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %3
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 -1, i32* %2
br label %33
31: ; preds = %24
%32 = load i32, i32* %4
store i32 %32, i32* %2
br label %33
33: ; preds = %31, %27, %10
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@errno = global i64 0
@stderr = global i32 0
@.str = [44 x i8] c"Failed in strtol for socket fd, reason: %s\0A\00"
@.str.1 = [40 x i8] c"bad numerical value for socket fd '%s'\0A\00"
define i32 @get_fd_num(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i32 @strtol(i8* %0, i8** %2, i32 10)
%5 = load i64, i64* @errno
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i8* @strerror(i64 %5)
%10 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %23
11: ; preds = %1
%12 = load i8, i8* %0
%13 = icmp eq i8 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %11
%15 = load i8*, i8** %2
%16 = load i8, i8* %15
%17 = icmp ne i8 %16, 0
%18 = icmp slt i32 %4, 0
%19 = select i1 %17, i1 true, i1 %18
br i1 %19, label %20, label %23
20: ; preds = %14, %11
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %23
23: ; preds = %14, %20, %7
%24 = phi i32 [ -1, %7 ], [ -1, %20 ], [ %4, %14 ]
ret i32 %24
}