#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ ERANGE;
long LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
int extract_pid(char const *, scalar_t__ *);
long strtol(char const *, char **, int);
int read_pid(const char *name, pid_t *pid) {
  char *endptr;
  errno = 0;
  long int pidtmp = strtol(name, &endptr, 10);
  if ((errno == ERANGE && (pidtmp == LONG_MAX || pidtmp == LONG_MIN)) ||
      (errno != 0 && pidtmp == 0)) {
    return extract_pid(name, pid);
  }
  if (endptr == NULL || endptr[0] != '\0') {
    return extract_pid(name, pid);
  }
  *pid = (pid_t)pidtmp;
  return 0;
}


x86 assembly (clang, -O0):
<read_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e91(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%rax
mov    0x2e7b(%rip),%rcx
cmp    (%rcx),%rax
jne    1196 <read_pid+0x76>
mov    -0x28(%rbp),%rax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%rax
je     11b2 <read_pid+0x92>
mov    -0x28(%rbp),%rax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
je     11b2 <read_pid+0x92>
mov    0x2e2b(%rip),%rax
cmpq   $0x0,(%rax)
je     11c7 <read_pid+0xa7>
cmpq   $0x0,-0x28(%rbp)
jne    11c7 <read_pid+0xa7>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <extract_pid@plt>
mov    %eax,-0x4(%rbp)
jmp    1209 <read_pid+0xe9>
cmpq   $0x0,-0x20(%rbp)
je     11e2 <read_pid+0xc2>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f7 <read_pid+0xd7>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <extract_pid@plt>
mov    %eax,-0x4(%rbp)
jmp    1209 <read_pid+0xe9>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pid>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e92(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    (%r15),%rcx
mov    0x2e8a(%rip),%rdx
cmp    (%rdx),%rcx
jne    1173 <read_pid+0x53>
mov    0x2e6e(%rip),%rdx
cmp    (%rdx),%rax
je     118c <read_pid+0x6c>
mov    0x2e7a(%rip),%rdx
cmp    (%rdx),%rax
je     118c <read_pid+0x6c>
test   %rcx,%rcx
je     117d <read_pid+0x5d>
test   %rax,%rax
je     118c <read_pid+0x6c>
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
je     118c <read_pid+0x6c>
cmpb   $0x0,(%rcx)
je     11a0 <read_pid+0x80>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <extract_pid@plt>
mov    %rax,(%r14)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
define i32 @read_pid(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
store i64 0, i64* @errno
%8 = load i8*, i8** %4
%9 = call i64 @strtol(i8* %8, i8** %6, i32 10)
store i64 %9, i64* %7
%10 = load i64, i64* @errno
%11 = load i64, i64* @ERANGE
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = load i64, i64* %7
%15 = load i64, i64* @LONG_MAX
%16 = icmp eq i64 %14, %15
br i1 %16, label %27, label %17
17: ; preds = %13
%18 = load i64, i64* %7
%19 = load i64, i64* @LONG_MIN
%20 = icmp eq i64 %18, %19
br i1 %20, label %27, label %21
21: ; preds = %17, %2
%22 = load i64, i64* @errno
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = load i64, i64* %7
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24, %17, %13
%28 = load i8*, i8** %4
%29 = load i64*, i64** %5
%30 = call i32 @extract_pid(i8* %28, i64* %29)
store i32 %30, i32* %3
br label %47
31: ; preds = %24, %21
%32 = load i8*, i8** %6
%33 = icmp eq i8* %32, null
br i1 %33, label %40, label %34
34: ; preds = %31
%35 = load i8*, i8** %6
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %34, %31
%41 = load i8*, i8** %4
%42 = load i64*, i64** %5
%43 = call i32 @extract_pid(i8* %41, i64* %42)
store i32 %43, i32* %3
br label %47
44: ; preds = %34
%45 = load i64, i64* %7
%46 = load i64*, i64** %5
store i64 %45, i64* %46
store i32 0, i32* %3
br label %47
47: ; preds = %44, %40, %27
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
define i32 @read_pid(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i64 0, i64* @errno
%5 = call i64 @strtol(i8* %0, i8** %3, i32 10)
%6 = load i64, i64* @errno
%7 = load i64, i64* @ERANGE
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load i64, i64* @LONG_MAX
%11 = icmp eq i64 %5, %10
%12 = load i64, i64* @LONG_MIN
%13 = icmp eq i64 %5, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %19, label %15
15: ; preds = %9, %2
%16 = icmp ne i64 %6, 0
%17 = icmp eq i64 %5, 0
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %19, label %21
19: ; preds = %15, %9
%20 = tail call i32 @extract_pid(i8* %0, i64* %1)
br label %30
21: ; preds = %15
%22 = load i8*, i8** %3
%23 = icmp eq i8* %22, null
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i8, i8* %22
%26 = icmp eq i8 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24, %21
%28 = tail call i32 @extract_pid(i8* %0, i64* %1)
br label %30
29: ; preds = %24
store i64 %5, i64* %1
br label %30
30: ; preds = %29, %27, %19
%31 = phi i32 [ %20, %19 ], [ %28, %27 ], [ 0, %29 ]
ret i32 %31
}