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

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ htons(int);
char *strsep(char **, char *);
int strtoul(char *, int /*<<< orphan*/, int);
int sys_read_string(char *, char *, char *, int);
int sys_read_gid(char *dir_name, char *file_name, uint8_t *gid) {
  char buf[64], *str, *s;
  uint16_t *ugid = (uint16_t *)gid;
  int r, i;
  if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
    return r;
  for (s = buf, i = 0; i < 8; i++) {
    if (!(str = strsep(&s, ": \t\n")))
      return -EINVAL;
    ugid[i] = htons(strtoul(str, 0, 16) & 0xffff);
  }
  return 0;
}


x86 assembly (clang, -O0):
<sys_read_gid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x60(%rbp),%rdx
mov    $0x40,%ecx
call   1060 <sys_read_string@plt>
mov    %eax,-0x7c(%rbp)
cmp    $0x0,%eax
jge    118c <sys_read_gid+0x4c>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1215 <sys_read_gid+0xd5>
lea    -0x60(%rbp),%rax
mov    %rax,-0x70(%rbp)
movl   $0x0,-0x80(%rbp)
cmpl   $0x8,-0x80(%rbp)
jge    120e <sys_read_gid+0xce>
lea    -0x70(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1040 <strsep@plt>
mov    %rax,-0x68(%rbp)
cmp    $0x0,%rax
jne    11d6 <sys_read_gid+0x96>
xor    %eax,%eax
mov    0x2e14(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1215 <sys_read_gid+0xd5>
mov    -0x68(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
mov    %eax,%edi
and    $0xffff,%edi
call   1030 <htons@plt>
mov    %eax,%edx
mov    -0x78(%rbp),%rax
movslq -0x80(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x80(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x80(%rbp)
jmp    119b <sys_read_gid+0x5b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_read_gid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rdx,%r14
lea    0x10(%rsp),%rbx
mov    %rbx,%rdx
mov    $0x40,%ecx
call   1060 <sys_read_string@plt>
test   %eax,%eax
js     131a <sys_read_gid+0x1da>
mov    %rbx,0x8(%rsp)
lea    0xe8f(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1040 <strsep@plt>
test   %rax,%rax
je     130d <sys_read_gid+0x1cd>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,(%r14)
lea    0xe5b(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1040 <strsep@plt>
test   %rax,%rax
je     130d <sys_read_gid+0x1cd>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%r14)
lea    0xe26(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1040 <strsep@plt>
test   %rax,%rax
je     130d <sys_read_gid+0x1cd>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%r14)
lea    0xdf1(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1040 <strsep@plt>
test   %rax,%rax
je     130d <sys_read_gid+0x1cd>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%r14)
lea    0xdbc(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1040 <strsep@plt>
test   %rax,%rax
je     130d <sys_read_gid+0x1cd>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%r14)
lea    0xd87(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1040 <strsep@plt>
test   %rax,%rax
je     130d <sys_read_gid+0x1cd>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%r14)
lea    0xd52(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1040 <strsep@plt>
test   %rax,%rax
je     130d <sys_read_gid+0x1cd>
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,0x18(%r14)
lea    0xd1f(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1040 <strsep@plt>
test   %rax,%rax
je     130d <sys_read_gid+0x1cd>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,0x1c(%r14)
jmp    131c <sys_read_gid+0x1dc>
mov    0x2ccc(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    131c <sys_read_gid+0x1dc>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c": \09\0A\00"
@EINVAL = global i32 0
define i32 @sys_read_gid(i8* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [64 x i8]
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%14 = load i32*, i32** %7
store i32* %14, i32** %11
%15 = load i8*, i8** %5
%16 = load i8*, i8** %6
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%18 = call i32 @sys_read_string(i8* %15, i8* %16, i8* %17, i32 64)
store i32 %18, i32* %12
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = load i32, i32* %12
store i32 %21, i32* %4
br label %46
22: ; preds = %3
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
store i8* %23, i8** %10
store i32 0, i32* %13
br label %24
24: ; preds = %42, %22
%25 = load i32, i32* %13
%26 = icmp slt i32 %25, 8
br i1 %26, label %27, label %45
27: ; preds = %24
%28 = call i8* @strsep(i8** %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %28, i8** %9
%29 = icmp ne i8* %28, null
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = load i32, i32* @EINVAL
%32 = sub nsw i32 0, %31
store i32 %32, i32* %4
br label %46
33: ; preds = %27
%34 = load i8*, i8** %9
%35 = call i32 @strtoul(i8* %34, i32 0, i32 16)
%36 = and i32 %35, 65535
%37 = call i32 @htons(i32 %36)
%38 = load i32*, i32** %11
%39 = load i32, i32* %13
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
store i32 %37, i32* %41
br label %42
42: ; preds = %33
%43 = load i32, i32* %13
%44 = add nsw i32 %43, 1
store i32 %44, i32* %13
br label %24
45: ; preds = %24
store i32 0, i32* %4
br label %46
46: ; preds = %45, %30, %20
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@.str = [5 x i8] c": \09\0A\00"
@EINVAL = global i32 0
define i32 @sys_read_gid(i8* %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca [64 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = bitcast i8** %5 to i8*
%8 = call i32 @sys_read_string(i8* %0, i8* %1, i8* %6, i32 64)
%9 = icmp slt i32 %8, 0
br i1 %9, label %69, label %10
10: ; preds = %3
store i8* %6, i8** %5
%11 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %57, %50, %43, %36, %29, %22, %16, %10
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
br label %69
16: ; preds = %10
%17 = call i32 @strtoul(i8* %11, i32 0, i32 16)
%18 = and i32 %17, 65535
%19 = call i32 @htons(i32 %18)
store i32 %19, i32* %2
%20 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %13, label %22
22: ; preds = %16
%23 = call i32 @strtoul(i8* %20, i32 0, i32 16)
%24 = and i32 %23, 65535
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds i32, i32* %2, i64 1
store i32 %25, i32* %26
%27 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%28 = icmp eq i8* %27, null
br i1 %28, label %13, label %29
29: ; preds = %22
%30 = call i32 @strtoul(i8* %27, i32 0, i32 16)
%31 = and i32 %30, 65535
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds i32, i32* %2, i64 2
store i32 %32, i32* %33
%34 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%35 = icmp eq i8* %34, null
br i1 %35, label %13, label %36
36: ; preds = %29
%37 = call i32 @strtoul(i8* %34, i32 0, i32 16)
%38 = and i32 %37, 65535
%39 = call i32 @htons(i32 %38)
%40 = getelementptr inbounds i32, i32* %2, i64 3
store i32 %39, i32* %40
%41 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%42 = icmp eq i8* %41, null
br i1 %42, label %13, label %43
43: ; preds = %36
%44 = call i32 @strtoul(i8* %41, i32 0, i32 16)
%45 = and i32 %44, 65535
%46 = call i32 @htons(i32 %45)
%47 = getelementptr inbounds i32, i32* %2, i64 4
store i32 %46, i32* %47
%48 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%49 = icmp eq i8* %48, null
br i1 %49, label %13, label %50
50: ; preds = %43
%51 = call i32 @strtoul(i8* %48, i32 0, i32 16)
%52 = and i32 %51, 65535
%53 = call i32 @htons(i32 %52)
%54 = getelementptr inbounds i32, i32* %2, i64 5
store i32 %53, i32* %54
%55 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%56 = icmp eq i8* %55, null
br i1 %56, label %13, label %57
57: ; preds = %50
%58 = call i32 @strtoul(i8* %55, i32 0, i32 16)
%59 = and i32 %58, 65535
%60 = call i32 @htons(i32 %59)
%61 = getelementptr inbounds i32, i32* %2, i64 6
store i32 %60, i32* %61
%62 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%63 = icmp eq i8* %62, null
br i1 %63, label %13, label %64
64: ; preds = %57
%65 = call i32 @strtoul(i8* %62, i32 0, i32 16)
%66 = and i32 %65, 65535
%67 = call i32 @htons(i32 %66)
%68 = getelementptr inbounds i32, i32* %2, i64 7
store i32 %67, i32* %68
br label %69
69: ; preds = %64, %3, %13
%70 = phi i32 [ %15, %13 ], [ %8, %3 ], [ 0, %64 ]
ret i32 %70
}