#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;
scalar_t__ LONG_MAX;
scalar_t__ LONG_MIN;
scalar_t__ errno;
scalar_t__ strtol(char *, char **, int);
int read_pid(char *str, pid_t *pid) {
  char *endptr;
  errno = 0;
  pid_t pidtmp = strtol(str, &endptr, 10);
  if ((errno == ERANGE && (pidtmp == LONG_MAX || pidtmp == LONG_MIN)) ||
      (errno != 0 && pidtmp == 0)) {
    return 1;
  }
  if (endptr == str) {
    return 1;
  }
  *pid = 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    0x2ea1(%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    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e8b(%rip),%rcx
cmp    (%rcx),%rax
jne    1186 <read_pid+0x76>
mov    -0x28(%rbp),%rax
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
je     11a2 <read_pid+0x92>
mov    -0x28(%rbp),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
je     11a2 <read_pid+0x92>
mov    0x2e3b(%rip),%rax
cmpq   $0x0,(%rax)
je     11ae <read_pid+0x9e>
cmpq   $0x0,-0x28(%rbp)
jne    11ae <read_pid+0x9e>
movl   $0x1,-0x4(%rbp)
jmp    11da <read_pid+0xca>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11c8 <read_pid+0xb8>
movl   $0x1,-0x4(%rbp)
jmp    11da <read_pid+0xca>
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   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2ea3(%rip),%r15
movq   $0x0,(%r15)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    (%r15),%rdx
mov    0x2e9d(%rip),%rcx
cmp    (%rcx),%rdx
jne    1165 <read_pid+0x55>
mov    0x2e81(%rip),%rsi
mov    $0x1,%ecx
cmp    (%rsi),%rax
je     1185 <read_pid+0x75>
mov    0x2e88(%rip),%rsi
cmp    (%rsi),%rax
je     1185 <read_pid+0x75>
test   %rdx,%rdx
setne  %dl
test   %rax,%rax
sete   %bl
mov    $0x1,%ecx
test   %bl,%dl
jne    1185 <read_pid+0x75>
cmp    %r12,(%rsp)
je     1185 <read_pid+0x75>
mov    %rax,(%r14)
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
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 %28
24: ; preds = %21
%25 = load i64, i64* %7
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24, %17, %13
store i32 1, i32* %3
br label %36
28: ; preds = %24, %21
%29 = load i8*, i8** %6
%30 = load i8*, i8** %4
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 1, i32* %3
br label %36
33: ; preds = %28
%34 = load i64, i64* %7
%35 = load i64*, i64** %5
store i64 %34, i64* %35
store i32 0, i32* %3
br label %36
36: ; preds = %33, %32, %27
%37 = load i32, i32* %3
ret i32 %37
}

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* nocapture writeonly %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 %23, 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
%19 = load i8*, i8** %3
%20 = icmp eq i8* %19, %0
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %23, label %22
22: ; preds = %15
store i64 %5, i64* %1
br label %23
23: ; preds = %9, %15, %22
%24 = phi i32 [ 0, %22 ], [ 1, %15 ], [ 1, %9 ]
ret i32 %24
}