#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 int pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int EINVAL;
int errno;
int read(int, char *, int);
scalar_t__ strtol(char *, char **, int);
pid_t get_pid(int fd, int *err) {
  int bytes_read;
  char buf[32];
  pid_t pid;
  bytes_read = read(fd, buf, sizeof(buf) - 1);
  if (bytes_read > 0) {
    buf[bytes_read] = '\0';
    pid = (pid_t)strtol(buf, (char **)NULL, 10);
  } else {
    pid = -1;
    *err = bytes_read ? errno : EINVAL;
  }
  return pid;
}


x86 assembly (clang, -O0):
<get_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1f,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1170 <get_pid+0x50>
movslq -0x14(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x44(%rbp)
jmp    11a7 <get_pid+0x87>
movl   $0xffffffff,-0x44(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1192 <get_pid+0x72>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
jmp    119e <get_pid+0x7e>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pid>:
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %rsp,%rsi
mov    $0x1f,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    1150 <get_pid+0x30>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
jmp    116b <get_pid+0x4b>
jne    115b <get_pid+0x3b>
mov    0x2e7f(%rip),%rax
jmp    1162 <get_pid+0x42>
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i32 0
@EINVAL = global i32 0
define i32 @get_pid(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [32 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%8 = load i32, i32* %3
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 31)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 %15
store i8 0, i8* %16
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%18 = call i64 @strtol(i8* %17, i8** null, i32 10)
%19 = trunc i64 %18 to i32
store i32 %19, i32* %7
br label %30
20: ; preds = %2
store i32 -1, i32* %7
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = load i32, i32* @errno
br label %27
25: ; preds = %20
%26 = load i32, i32* @EINVAL
br label %27
27: ; preds = %25, %23
%28 = phi i32 [ %24, %23 ], [ %26, %25 ]
%29 = load i32*, i32** %4
store i32 %28, i32* %29
br label %30
30: ; preds = %27, %13
%31 = load i32, i32* %7
ret i32 %31
}

LLVM IR (-O2):
@errno = global i32 0
@EINVAL = global i32 0
define i32 @get_pid(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 31)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = zext i32 %5 to i64
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %8
store i8 0, i8* %9
%10 = call i64 @strtol(i8* nocapture %4, i8** null, i32 10)
%11 = trunc i64 %10 to i32
br label %17
12: ; preds = %2
%13 = icmp eq i32 %5, 0
%14 = load i32, i32* @errno
%15 = load i32, i32* @EINVAL
%16 = select i1 %13, i32 %15, i32 %14
store i32 %16, i32* %1
br label %17
17: ; preds = %12, %7
%18 = phi i32 [ %11, %7 ], [ -1, %12 ]
ret i32 %18
}