#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 ssize_t;
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int ENODATA;
int FILENAME_MAX;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int errno;
char *getenv(char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *const, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *const);
int gnugol_read_key(char *const key, size_t *const pksize,
                    const char *const keyfile) {
  char path[FILENAME_MAX];
  char *home;
  int fd;
  ssize_t size;
  int err;
  assert(key != NULL);
  assert(pksize != NULL);
  assert(*pksize > 0);
  assert(keyfile != NULL);
  home = getenv("HOME");
  if (home == NULL)
    home = "";
  snprintf(path, sizeof(path), "%s/.%s", home, keyfile);
  fd = open(path, O_RDONLY);
  if (fd == -1) {
    snprintf(path, sizeof(path), "%s/%s", "/etc/gnugol", keyfile);
    fd = open(path, O_RDONLY);
    if (fd == -1)
      return errno;
  }
  size = read(fd, key, *pksize);
  err = errno;
  close(fd);
  if (size == -1)
    return errno;
  if (size == 0)
    return ENODATA;
  while ((size > 0) && ((key[size - 1] == ' ') || (key[size - 1] == '\n')))
    size--;
  if (size == 0)
    return ENODATA;
  key[size] = '\0';
  *pksize = size;
  return 0;
}


x86 assembly (clang, -O0):
<gnugol_read_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    0xe0a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1215 <gnugol_read_key+0xb5>
lea    0xdf3(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xdd7(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x50(%rbp),%rdi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    12af <gnugol_read_key+0x14f>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%r8
lea    0xda5(%rip),%rdx
lea    0xda4(%rip),%rcx
call   1040 <snprintf@plt>
mov    -0x50(%rbp),%rdi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    12aa <gnugol_read_key+0x14a>
mov    0x2d2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    13d5 <gnugol_read_key+0x275>
jmp    12af <gnugol_read_key+0x14f>
mov    -0x3c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
call   1060 <read@plt>
mov    %eax,-0x40(%rbp)
mov    0x2cfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
cmpl   $0xffffffff,-0x40(%rbp)
jne    12fb <gnugol_read_key+0x19b>
mov    0x2cde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    13d5 <gnugol_read_key+0x275>
cmpl   $0x0,-0x40(%rbp)
jne    131d <gnugol_read_key+0x1bd>
mov    0x2ccc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    13d5 <gnugol_read_key+0x275>
jmp    1322 <gnugol_read_key+0x1c2>
xor    %eax,%eax
cmpl   $0x0,-0x40(%rbp)
mov    %al,-0x59(%rbp)
jle    1370 <gnugol_read_key+0x210>
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x5a(%rbp)
je     136a <gnugol_read_key+0x20a>
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
sete   %al
mov    %al,-0x5a(%rbp)
mov    -0x5a(%rbp),%al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    1380 <gnugol_read_key+0x220>
jmp    138e <gnugol_read_key+0x22e>
mov    -0x40(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x40(%rbp)
jmp    1322 <gnugol_read_key+0x1c2>
cmpl   $0x0,-0x40(%rbp)
jne    13b0 <gnugol_read_key+0x250>
mov    0x2c39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    13d5 <gnugol_read_key+0x275>
mov    -0x10(%rbp),%rax
movslq -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movslq -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gnugol_read_key>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e62(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %edi,%edi
test   %r13,%r13
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
cmpq   $0x0,(%r15)
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1070 <assert@plt>
lea    0xe2b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe20(%rip),%rcx
cmovne %rax,%rcx
lea    0xe16(%rip),%rdx
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r14,-0x30(%rbp)
mov    %r14,%r8
call   1040 <snprintf@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    1250 <gnugol_read_key+0xf0>
lea    0xdeb(%rip),%rdx
lea    0xdea(%rip),%rcx
mov    %rbx,%rdi
mov    %r12d,%esi
mov    -0x30(%rbp),%r8
call   1040 <snprintf@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     127c <gnugol_read_key+0x11c>
mov    (%r15),%rdx
mov    %r14d,%edi
mov    %r13,%rsi
call   1060 <read@plt>
mov    %eax,%ebx
mov    %r14d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%ebx
je     127c <gnugol_read_key+0x11c>
test   %ebx,%ebx
jne    1287 <gnugol_read_key+0x127>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
jmp    12c1 <gnugol_read_key+0x161>
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
jmp    12c1 <gnugol_read_key+0x161>
jle    12b3 <gnugol_read_key+0x153>
mov    %ebx,%ebx
jmp    129d <gnugol_read_key+0x13d>
nopl   (%rax)
lea    0x1(%rax),%rcx
mov    %rax,%rbx
cmp    $0x1,%rcx
jbe    1271 <gnugol_read_key+0x111>
lea    -0x1(%rbx),%rax
mov    %eax,%ecx
movzbl 0x0(%r13,%rcx,1),%ecx
cmp    $0x20,%cl
je     1290 <gnugol_read_key+0x130>
cmp    $0xa,%cl
je     1290 <gnugol_read_key+0x130>
movslq %ebx,%rax
movb   $0x0,0x0(%r13,%rax,1)
mov    %rax,(%r15)
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
@.str = [5 x i8] c"HOME\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [7 x i8] c"%s/.%s\00"
@O_RDONLY = global i32 0
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [12 x i8] c"/etc/gnugol\00"
@errno = global i32 0
@ENODATA = global i32 0
define i32 @gnugol_read_key(i8* %0, i64* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
%15 = load i32, i32* @FILENAME_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %8
%18 = alloca i8, i64 %16
store i64 %16, i64* %9
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i64*, i64** %6
%24 = icmp ne i64* %23, null
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i64*, i64** %6
%28 = load i64, i64* %27
%29 = icmp ugt i64 %28, 0
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i8*, i8** %7
%33 = icmp ne i8* %32, null
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %36, i8** %10
%37 = load i8*, i8** %10
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %40
39: ; preds = %3
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %10
br label %40
40: ; preds = %39, %3
%41 = trunc i64 %16 to i32
%42 = load i8*, i8** %10
%43 = load i8*, i8** %7
%44 = call i32 @snprintf(i8* %18, i32 %41, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %42, i8* %43)
%45 = load i32, i32* @O_RDONLY
%46 = call i32 @open(i8* %18, i32 %45)
store i32 %46, i32* %11
%47 = load i32, i32* %11
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %60
49: ; preds = %40
%50 = trunc i64 %16 to i32
%51 = load i8*, i8** %7
%52 = call i32 @snprintf(i8* %18, i32 %50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %51)
%53 = load i32, i32* @O_RDONLY
%54 = call i32 @open(i8* %18, i32 %53)
store i32 %54, i32* %11
%55 = load i32, i32* %11
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = load i32, i32* @errno
store i32 %58, i32* %4
store i32 1, i32* %14
br label %120
59: ; preds = %49
br label %60
60: ; preds = %59, %40
%61 = load i32, i32* %11
%62 = load i8*, i8** %5
%63 = load i64*, i64** %6
%64 = load i64, i64* %63
%65 = call i32 @read(i32 %61, i8* %62, i64 %64)
store i32 %65, i32* %12
%66 = load i32, i32* @errno
store i32 %66, i32* %13
%67 = load i32, i32* %11
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* %12
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %73
71: ; preds = %60
%72 = load i32, i32* @errno
store i32 %72, i32* %4
store i32 1, i32* %14
br label %120
73: ; preds = %60
%74 = load i32, i32* %12
%75 = icmp eq i32 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %73
%77 = load i32, i32* @ENODATA
store i32 %77, i32* %4
store i32 1, i32* %14
br label %120
78: ; preds = %73
br label %79
79: ; preds = %104, %78
%80 = load i32, i32* %12
%81 = icmp sgt i32 %80, 0
br i1 %81, label %82, label %102
82: ; preds = %79
%83 = load i8*, i8** %5
%84 = load i32, i32* %12
%85 = sub nsw i32 %84, 1
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %83, i64 %86
%88 = load i8, i8* %87
%89 = sext i8 %88 to i32
%90 = icmp eq i32 %89, 32
br i1 %90, label %100, label %91
91: ; preds = %82
%92 = load i8*, i8** %5
%93 = load i32, i32* %12
%94 = sub nsw i32 %93, 1
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i8, i8* %92, i64 %95
%97 = load i8, i8* %96
%98 = sext i8 %97 to i32
%99 = icmp eq i32 %98, 10
br label %100
100: ; preds = %91, %82
%101 = phi i1 [ true, %82 ], [ %99, %91 ]
br label %102
102: ; preds = %100, %79
%103 = phi i1 [ false, %79 ], [ %101, %100 ]
br i1 %103, label %104, label %107
104: ; preds = %102
%105 = load i32, i32* %12
%106 = add nsw i32 %105, -1
store i32 %106, i32* %12
br label %79
107: ; preds = %102
%108 = load i32, i32* %12
%109 = icmp eq i32 %108, 0
br i1 %109, label %110, label %112
110: ; preds = %107
%111 = load i32, i32* @ENODATA
store i32 %111, i32* %4
store i32 1, i32* %14
br label %120
112: ; preds = %107
%113 = load i8*, i8** %5
%114 = load i32, i32* %12
%115 = sext i32 %114 to i64
%116 = getelementptr inbounds i8, i8* %113, i64 %115
store i8 0, i8* %116
%117 = load i32, i32* %12
%118 = sext i32 %117 to i64
%119 = load i64*, i64** %6
store i64 %118, i64* %119
store i32 0, i32* %4
store i32 1, i32* %14
br label %120
120: ; preds = %112, %110, %76, %71, %57
%121 = load i8*, i8** %8
%122 = load i32, i32* %4
ret i32 %122
}

LLVM IR (-O2):
@FILENAME_MAX = global i32 0
@.str = [5 x i8] c"HOME\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [7 x i8] c"%s/.%s\00"
@O_RDONLY = global i32 0
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [12 x i8] c"/etc/gnugol\00"
@errno = global i32 0
@ENODATA = global i32 0
define i32 @gnugol_read_key(i8* %0, i64* %1, i8* %2) {
%4 = load i32, i32* @FILENAME_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = icmp ne i8* %0, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i64* %1, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = load i64, i64* %1
%14 = icmp ne i64 %13, 0
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = icmp ne i8* %2, null
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i8* %20, null
%22 = select i1 %21, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %20
%23 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %22, i8* %2)
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %6, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %34
27: ; preds = %3
%28 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%29 = load i32, i32* @O_RDONLY
%30 = call i32 @open(i8* %6, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = load i32, i32* @errno
br label %63
34: ; preds = %27, %3
%35 = phi i32 [ %30, %27 ], [ %25, %3 ]
%36 = load i64, i64* %1
%37 = call i32 @read(i32 %35, i8* %0, i64 %36)
%38 = call i32 @close(i32 %35)
switch i32 %37, label %39 [
i32 -1, label %43
i32 0, label %45
]
39: ; preds = %34
%40 = icmp sgt i32 %37, 0
br i1 %40, label %41, label %59
41: ; preds = %39
%42 = zext i32 %37 to i64
br label %47
43: ; preds = %34
%44 = load i32, i32* @errno
br label %63
45: ; preds = %34
%46 = load i32, i32* @ENODATA
br label %63
47: ; preds = %41, %53
%48 = phi i64 [ %42, %41 ], [ %49, %53 ]
%49 = add nsw i64 %48, -1
%50 = and i64 %49, 4294967295
%51 = getelementptr inbounds i8, i8* %0, i64 %50
%52 = load i8, i8* %51
switch i8 %52, label %57 [
i8 32, label %53
i8 10, label %53
]
53: ; preds = %47, %47
%54 = icmp ugt i64 %48, 1
br i1 %54, label %47, label %55
55: ; preds = %53
%56 = load i32, i32* @ENODATA
br label %63
57: ; preds = %47
%58 = trunc i64 %48 to i32
br label %59
59: ; preds = %39, %57
%60 = phi i32 [ %58, %57 ], [ %37, %39 ]
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %0, i64 %61
store i8 0, i8* %62
store i64 %61, i64* %1
br label %63
63: ; preds = %59, %55, %45, %43, %32
%64 = phi i32 [ %33, %32 ], [ %44, %43 ], [ %46, %45 ], [ %56, %55 ], [ 0, %59 ]
ret i32 %64
}