#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 */
union ibv_gid {
  scalar_t__ *raw;
};
typedef scalar_t__ uint8_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 *);
scalar_t__ strtol(char *, char **, int);
int get_sgid(struct ifreq *ifr, union ibv_gid *sgid) {
  char buf[128], *end;
  FILE *f;
  int i, p, ret;
  snprintf(buf, sizeof buf, "//sys//class//net//%s//address", ifr->ifr_name);
  f = fopen(buf, "r");
  if (!f) {
    printf("failed to open %s\n", buf);
    return -1;
  }
  if (fgets(buf, sizeof buf, f)) {
    for (i = 0, p = 12; i < 16; i++, p += 3) {
      buf[p + 2] = '\0';
      sgid->raw[i] = (uint8_t)strtol(buf + p, &end, 16);
    }
    ret = 0;
  } else {
    printf("failed to read sgid\n");
    ret = -1;
  }
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<get_sgid>:
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    0xe7f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
jne    11db <get_sgid+0x7b>
lea    -0xa0(%rbp),%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c7 <get_sgid+0x167>
lea    -0xa0(%rbp),%rdi
mov    -0xb0(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     129a <get_sgid+0x13a>
movl   $0x0,-0xb4(%rbp)
movl   $0xc,-0xb8(%rbp)
cmpl   $0x10,-0xb4(%rbp)
jge    128b <get_sgid+0x12b>
mov    -0xb8(%rbp),%eax
add    $0x2,%eax
cltq
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    -0xa0(%rbp),%rdi
movslq -0xb8(%rbp),%rax
add    %rax,%rdi
lea    -0xa8(%rbp),%rsi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0xb4(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0xb4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb4(%rbp)
mov    -0xb8(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0xb8(%rbp)
jmp    1211 <get_sgid+0xb1>
movl   $0x0,-0xbc(%rbp)
jmp    12b2 <get_sgid+0x152>
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0xbc(%rbp)
ff ff ff
mov    -0xb0(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xbc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sgid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rsi,%r12
mov    (%rdi),%rcx
lea    0xe72(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1060 <snprintf@plt>
lea    0xe78(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     120c <get_sgid+0x9c>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1227 <get_sgid+0xb7>
lea    0x1c(%rsp),%rbx
xor    %r15d,%r15d
lea    0x8(%rsp),%r13
xor    %ebp,%ebp
nopl   (%rax)
movb   $0x0,0x2(%rbx)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    (%r12),%rcx
mov    %rax,(%rcx,%rbp,8)
add    $0x1,%rbp
add    $0x3,%rbx
cmp    $0x10,%rbp
jne    11e0 <get_sgid+0x70>
jmp    1239 <get_sgid+0xc9>
lea    0xe0e(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%r15d
jmp    1241 <get_sgid+0xd1>
lea    0xe06(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r15d
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i8* }
%union.ibv_gid = type { i64* }
@.str = [31 x i8] c"//sys//class//net//%s//address\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 sgid\0A\00"
define i32 @get_sgid(%struct.ifreq* %0, %union.ibv_gid* %1) {
%3 = alloca i32
%4 = alloca %struct.ifreq*
%5 = alloca %union.ibv_gid*
%6 = alloca [128 x i8]
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store %struct.ifreq* %0, %struct.ifreq** %4
store %union.ibv_gid* %1, %union.ibv_gid** %5
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%13 = load %struct.ifreq*, %struct.ifreq** %4
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %13, i32 0, i32 0
%15 = load i8*, i8** %14
%16 = call i32 @snprintf(i8* %12, i32 128, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp ne i32* %19, null
br i1 %20, label %24, label %21
21: ; preds = %2
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %22)
store i32 -1, i32* %3
br label %61
24: ; preds = %2
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%26 = load i32*, i32** %8
%27 = call i64 @fgets(i8* %25, i32 128, i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %55
29: ; preds = %24
store i32 0, i32* %9
store i32 12, i32* %10
br label %30
30: ; preds = %49, %29
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 16
br i1 %32, label %33, label %54
33: ; preds = %30
%34 = load i32, i32* %10
%35 = add nsw i32 %34, 2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %36
store i8 0, i8* %37
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = call i64 @strtol(i8* %41, i8** %7, i32 16)
%43 = load %union.ibv_gid*, %union.ibv_gid** %5
%44 = bitcast %union.ibv_gid* %43 to i64**
%45 = load i64*, i64** %44
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i64, i64* %45, i64 %47
store i64 %42, i64* %48
br label %49
49: ; preds = %33
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 3
store i32 %53, i32* %10
br label %30
54: ; preds = %30
store i32 0, i32* %11
br label %57
55: ; preds = %24
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %11
br label %57
57: ; preds = %55, %54
%58 = load i32*, i32** %8
%59 = call i32 @fclose(i32* %58)
%60 = load i32, i32* %11
store i32 %60, i32* %3
br label %61
61: ; preds = %57, %21
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.ifreq = type { i8* }
%union.ibv_gid = type { i64* }
@.str = [31 x i8] c"//sys//class//net//%s//address\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 sgid\00"
define i32 @get_sgid(%struct.ifreq* nocapture %0, %union.ibv_gid* nocapture %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 ([31 x i8], [31 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 %36
14: ; preds = %2
%15 = call i64 @fgets(i8* %5, i32 128, i32* %10)
%16 = icmp eq i64 %15, 0
br i1 %16, label %31, label %17
17: ; preds = %14
%18 = getelementptr %union.ibv_gid, %union.ibv_gid* %1, i64 0, i32 0
br label %19
19: ; preds = %17, %19
%20 = phi i64 [ 0, %17 ], [ %28, %19 ]
%21 = phi i64 [ 12, %17 ], [ %29, %19 ]
%22 = add nuw nsw i64 %21, 2
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %22
store i8 0, i8* %23
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %21
%25 = call i64 @strtol(i8* %24, i8** %4, i32 16)
%26 = load i64*, i64** %18
%27 = getelementptr inbounds i64, i64* %26, i64 %20
store i64 %25, i64* %27
%28 = add nuw nsw i64 %20, 1
%29 = add nuw nsw i64 %21, 3
%30 = icmp eq i64 %28, 16
br i1 %30, label %33, label %19
31: ; preds = %14
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %33
33: ; preds = %19, %31
%34 = phi i32 [ -1, %31 ], [ 0, %19 ]
%35 = call i32 @fclose(i32* %10)
br label %36
36: ; preds = %33, %12
%37 = phi i32 [ %34, %33 ], [ -1, %12 ]
ret i32 %37
}