#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 /*<<< orphan*/ uint16_t;
struct ifreq {
  char *ifr_name;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strtol(char *, char **, int);
int get_pkey(struct ifreq *ifreq, uint16_t *pkey) {
  char buf[128], *end;
  FILE *f;
  int ret;
  snprintf(buf, sizeof buf, "//sys//class//net//%s//pkey", ifreq->ifr_name);
  f = fopen(buf, "r");
  if (!f) {
    printf("failed to open %s\n", buf);
    return -1;
  }
  if (fgets(buf, sizeof buf, f)) {
    *pkey = strtol(buf, &end, 16);
    ret = 0;
  } else {
    printf("failed to read pkey\n");
    ret = -1;
  }
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<get_pkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    $0x80,%esi
lea    0xe73(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
jne    11db <get_pkey+0x7b>
lea    -0xa0(%rbp),%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <get_pkey+0xf9>
lea    -0xa0(%rbp),%rdi
mov    -0xb0(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     122c <get_pkey+0xcc>
lea    -0xa0(%rbp),%rdi
lea    -0xa8(%rbp),%rsi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0xb4(%rbp)
jmp    1244 <get_pkey+0xe4>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0xb4(%rbp)
ff ff ff
mov    -0xb0(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xb4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pkey>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %rsi,%r14
mov    (%rdi),%rcx
lea    0xe78(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1060 <snprintf@plt>
lea    0xe7b(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11e3 <get_pkey+0x73>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     11fd <get_pkey+0x8d>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %eax,(%r14)
xor    %ebp,%ebp
jmp    120e <get_pkey+0x9e>
lea    0xe34(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
jmp    1216 <get_pkey+0xa6>
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i8* }
@.str = [28 x i8] c"//sys//class//net//%s//pkey\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"failed to open %s\0A\00"
@.str.3 = [21 x i8] c"failed to read pkey\0A\00"
define i32 @get_pkey(%struct.ifreq* %0, i32* %1) {
%3 = alloca i32
%4 = alloca %struct.ifreq*
%5 = alloca i32*
%6 = alloca [128 x i8]
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
store %struct.ifreq* %0, %struct.ifreq** %4
store i32* %1, i32** %5
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%11 = load %struct.ifreq*, %struct.ifreq** %4
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 0
%13 = load i8*, i8** %12
%14 = call i32 @snprintf(i8* %10, i32 128, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = icmp ne i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %2
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %20)
store i32 -1, i32* %3
br label %37
22: ; preds = %2
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%24 = load i32*, i32** %8
%25 = call i64 @fgets(i8* %23, i32 128, i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%29 = call i32 @strtol(i8* %28, i8** %7, i32 16)
%30 = load i32*, i32** %5
store i32 %29, i32* %30
store i32 0, i32* %9
br label %33
31: ; preds = %22
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %9
br label %33
33: ; preds = %31, %27
%34 = load i32*, i32** %8
%35 = call i32 @fclose(i32* %34)
%36 = load i32, i32* %9
store i32 %36, i32* %3
br label %37
37: ; preds = %33, %19
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.ifreq = type { i8* }
@.str = [28 x i8] c"//sys//class//net//%s//pkey\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"failed to open %s\0A\00"
@str = [20 x i8] c"failed to read pkey\00"
define i32 @get_pkey(%struct.ifreq* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca [128 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = bitcast i8** %4 to i8*
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %0, i64 0, i32 0
%8 = load i8*, i8** %7
%9 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %24
14: ; preds = %2
%15 = call i64 @fgets(i8* %5, i32 128, i32* %10)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 @strtol(i8* %5, i8** %4, i32 16)
store i32 %18, i32* %1
br label %21
19: ; preds = %14
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ 0, %17 ], [ -1, %19 ]
%23 = call i32 @fclose(i32* %10)
br label %24
24: ; preds = %21, %12
%25 = phi i32 [ %22, %21 ], [ -1, %12 ]
ret i32 %25
}