#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 EDOM;
int EPIPE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char const *, int /*<<< orphan*/);
int read(int, char *, size_t);
char *strchr(char *, char);
int tb_readport(const char *fname, char *buf, size_t len) {
  int fd;
  char *s;
  int rc;
  if (len < 3)
    return -EDOM;
  if ((fd = open(fname, O_RDONLY)) == -1)
    return -errno;
  rc = read(fd, buf, len - 1);
  close(fd);
  if (rc < 0)
    return -errno;
  if (rc == 0)
    return -EPIPE;
  buf[rc] = 0;
  s = strchr(buf, '\n');
  if (s) {
    *s = 0;
    rc = s - buf;
  }
  return rc;
}


x86 assembly (clang, -O0):
<tb_readport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x3,-0x20(%rbp)
jae    1172 <tb_readport+0x32>
xor    %eax,%eax
mov    0x2e78(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <tb_readport+0x100>
mov    -0x10(%rbp),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11a3 <tb_readport+0x63>
xor    %eax,%eax
mov    0x2e2f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <tb_readport+0x100>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x34(%rbp)
jge    11df <tb_readport+0x9f>
xor    %eax,%eax
mov    0x2df3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <tb_readport+0x100>
cmpl   $0x0,-0x34(%rbp)
jne    11fc <tb_readport+0xbc>
xor    %eax,%eax
mov    0x2dde(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <tb_readport+0x100>
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     123a <tb_readport+0xfa>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tb_readport>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%rdx
ja     1153 <tb_readport+0x13>
mov    0x2e8f(%rip),%rax
jmp    11c1 <tb_readport+0x81>
mov    %rdx,%rbp
mov    %rsi,%r14
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11b1 <tb_readport+0x71>
mov    %eax,%ebx
add    $0xffffffffffffffff,%rbp
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
test   %ebp,%ebp
js     11b1 <tb_readport+0x71>
je     11ba <tb_readport+0x7a>
mov    %ebp,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11c5 <tb_readport+0x85>
movb   $0x0,(%rax)
sub    %r14d,%eax
mov    %eax,%ebp
jmp    11c5 <tb_readport+0x85>
mov    0x2e10(%rip),%rax
jmp    11c1 <tb_readport+0x81>
mov    0x2e0f(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EDOM = global i32 0
@O_RDONLY = global i32 0
@errno = global i32 0
@EPIPE = global i32 0
define i32 @tb_readport(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i64, i64* %7
%12 = icmp ult i64 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @EDOM
%15 = sub nsw i32 0, %14
store i32 %15, i32* %4
br label %62
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
store i32 %23, i32* %4
br label %62
24: ; preds = %16
%25 = load i32, i32* %8
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = sub i64 %27, 1
%29 = call i32 @read(i32 %25, i8* %26, i64 %28)
store i32 %29, i32* %10
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %10
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %24
%35 = load i32, i32* @errno
%36 = sub nsw i32 0, %35
store i32 %36, i32* %4
br label %62
37: ; preds = %24
%38 = load i32, i32* %10
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @EPIPE
%42 = sub nsw i32 0, %41
store i32 %42, i32* %4
br label %62
43: ; preds = %37
%44 = load i8*, i8** %6
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %6
%49 = call i8* @strchr(i8* %48, i8 signext 10)
store i8* %49, i8** %9
%50 = load i8*, i8** %9
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %60
52: ; preds = %43
%53 = load i8*, i8** %9
store i8 0, i8* %53
%54 = load i8*, i8** %9
%55 = load i8*, i8** %6
%56 = ptrtoint i8* %54 to i64
%57 = ptrtoint i8* %55 to i64
%58 = sub i64 %56, %57
%59 = trunc i64 %58 to i32
store i32 %59, i32* %10
br label %60
60: ; preds = %52, %43
%61 = load i32, i32* %10
store i32 %61, i32* %4
br label %62
62: ; preds = %60, %40, %34, %21, %13
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@EDOM = global i32 0
@O_RDONLY = global i32 0
@errno = global i32 0
@EPIPE = global i32 0
define i32 @tb_readport(i8* nocapture %0, i8* %1, i64 %2) {
%4 = icmp ult i64 %2, 3
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = load i32, i32* @EDOM
%7 = sub nsw i32 0, %6
br label %38
8: ; preds = %3
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
br label %38
15: ; preds = %8
%16 = add i64 %2, -1
%17 = tail call i32 @read(i32 %10, i8* %1, i64 %16)
%18 = tail call i32 @close(i32 %10)
%19 = icmp slt i32 %17, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
br label %38
23: ; preds = %15
%24 = icmp eq i32 %17, 0
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = load i32, i32* @EPIPE
%27 = sub nsw i32 0, %26
br label %38
28: ; preds = %23
%29 = zext i32 %17 to i64
%30 = getelementptr inbounds i8, i8* %1, i64 %29
store i8 0, i8* %30
%31 = tail call i8* @strchr(i8* dereferenceable(1) %1, i8 signext 10)
%32 = icmp eq i8* %31, null
br i1 %32, label %38, label %33
33: ; preds = %28
store i8 0, i8* %31
%34 = ptrtoint i8* %31 to i64
%35 = ptrtoint i8* %1 to i64
%36 = sub i64 %34, %35
%37 = trunc i64 %36 to i32
br label %38
38: ; preds = %28, %33, %25, %20, %12, %5
%39 = phi i32 [ %7, %5 ], [ %14, %12 ], [ %22, %20 ], [ %27, %25 ], [ %37, %33 ], [ %17, %28 ]
ret i32 %39
}