#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 */
struct passwd {
  int pw_uid;
  int pw_gid;
};
struct group {
  int gr_gid;
};

/* Variables and functions */
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
int sscanf(char *, char *, int *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
int resolve_uidgid(const char *uidgid, int *uid, int *gid) {
  struct passwd *pwd = NULL;
  struct group *grp = NULL;
  char uname[256] = "";
  char *gname = NULL;
  strcpy(uname, uidgid);
  gname = strchr(uname, ':');
  if (gname != NULL)
    *gname++ = 0;
  if (sscanf(uname, "%d", uid) < 1) {
    pwd = getpwnam(uname);
    if (pwd == NULL)
      return -1;
    *uid = pwd->pw_uid;
    *gid = pwd->pw_gid;
  }
  if (gname == NULL) {
    if (pwd == NULL)
      *gid = *uid;
    return 0;
  }
  if (sscanf(gname, "%d", gid) < 1) {
    grp = getgrnam(gname);
    if (grp == NULL)
      return -1;
    *gid = grp->gr_gid;
  }
  return 0;
}


x86 assembly (clang, -O0):
<resolve_uidgid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
movq   $0x0,-0x138(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x130(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
je     11f3 <resolve_uidgid+0x93>
mov    -0x138(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x138(%rbp)
movb   $0x0,(%rax)
lea    -0x130(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdfb(%rip),%rsi
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jge    1253 <resolve_uidgid+0xf3>
lea    -0x130(%rbp),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123a <resolve_uidgid+0xda>
movl   $0xffffffff,-0x4(%rbp)
jmp    12de <resolve_uidgid+0x17e>
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x138(%rbp)
jne    1284 <resolve_uidgid+0x124>
cmpq   $0x0,-0x28(%rbp)
jne    1278 <resolve_uidgid+0x118>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12de <resolve_uidgid+0x17e>
mov    -0x138(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd6a(%rip),%rsi
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jge    12d7 <resolve_uidgid+0x177>
mov    -0x138(%rbp),%rdi
call   1070 <getgrnam@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12cb <resolve_uidgid+0x16b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12de <resolve_uidgid+0x17e>
mov    -0x30(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_uidgid>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rsi
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     120e <resolve_uidgid+0xbe>
mov    %rax,%rbx
add    $0x1,%rbx
movb   $0x0,(%rax)
lea    0xe03(%rip),%rsi
mov    %rsp,%rdi
mov    %r15,%rdx
call   1070 <sscanf@plt>
test   %eax,%eax
jg     1243 <resolve_uidgid+0xf3>
jmp    1226 <resolve_uidgid+0xd6>
lea    0xdeb(%rip),%rsi
mov    %rsp,%rdi
mov    %r15,%rdx
call   1070 <sscanf@plt>
test   %eax,%eax
jg     126d <resolve_uidgid+0x11d>
xor    %ebx,%ebx
mov    %rsp,%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
je     1277 <resolve_uidgid+0x127>
mov    (%rax),%ecx
mov    %ecx,(%r15)
mov    0x4(%rax),%eax
mov    %eax,(%r14)
test   %rbx,%rbx
je     1273 <resolve_uidgid+0x123>
lea    0xdb6(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1070 <sscanf@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jg     127c <resolve_uidgid+0x12c>
mov    %rbx,%rdi
call   1060 <getgrnam@plt>
test   %rax,%rax
je     1277 <resolve_uidgid+0x127>
mov    %rax,%r15
mov    (%r15),%eax
mov    %eax,(%r14)
xor    %eax,%eax
jmp    127c <resolve_uidgid+0x12c>
mov    $0xffffffff,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
%struct.group = type { i32 }
@.str = [3 x i8] c"%d\00"
define i32 @resolve_uidgid(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.passwd*
%9 = alloca %struct.group*
%10 = alloca [256 x i8]
%11 = alloca i8*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store %struct.passwd* null, %struct.passwd** %8
store %struct.group* null, %struct.group** %9
%12 = bitcast [256 x i8]* %10 to i8*
store i8* null, i8** %11
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%14 = load i8*, i8** %5
%15 = call i32 @strcpy(i8* %13, i8* %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%17 = call i8* @strchr(i8* %16, i8 signext 58)
store i8* %17, i8** %11
%18 = load i8*, i8** %11
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %3
%21 = load i8*, i8** %11
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %11
store i8 0, i8* %21
br label %23
23: ; preds = %20, %3
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%25 = load i32*, i32** %6
%26 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %25)
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %43
28: ; preds = %23
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%30 = call %struct.passwd* @getpwnam(i8* %29)
store %struct.passwd* %30, %struct.passwd** %8
%31 = load %struct.passwd*, %struct.passwd** %8
%32 = icmp eq %struct.passwd* %31, null
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 -1, i32* %4
br label %71
34: ; preds = %28
%35 = load %struct.passwd*, %struct.passwd** %8
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32*, i32** %6
store i32 %37, i32* %38
%39 = load %struct.passwd*, %struct.passwd** %8
%40 = getelementptr inbounds %struct.passwd, %struct.passwd* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = load i32*, i32** %7
store i32 %41, i32* %42
br label %43
43: ; preds = %34, %23
%44 = load i8*, i8** %11
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %54
46: ; preds = %43
%47 = load %struct.passwd*, %struct.passwd** %8
%48 = icmp eq %struct.passwd* %47, null
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i32*, i32** %6
%51 = load i32, i32* %50
%52 = load i32*, i32** %7
store i32 %51, i32* %52
br label %53
53: ; preds = %49, %46
store i32 0, i32* %4
br label %71
54: ; preds = %43
%55 = load i8*, i8** %11
%56 = load i32*, i32** %7
%57 = call i32 @sscanf(i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %56)
%58 = icmp slt i32 %57, 1
br i1 %58, label %59, label %70
59: ; preds = %54
%60 = load i8*, i8** %11
%61 = call %struct.group* @getgrnam(i8* %60)
store %struct.group* %61, %struct.group** %9
%62 = load %struct.group*, %struct.group** %9
%63 = icmp eq %struct.group* %62, null
br i1 %63, label %64, label %65
64: ; preds = %59
store i32 -1, i32* %4
br label %71
65: ; preds = %59
%66 = load %struct.group*, %struct.group** %9
%67 = getelementptr inbounds %struct.group, %struct.group* %66, i32 0, i32 0
%68 = load i32, i32* %67
%69 = load i32*, i32** %7
store i32 %68, i32* %69
br label %70
70: ; preds = %65, %54
store i32 0, i32* %4
br label %71
71: ; preds = %70, %64, %53, %33
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
%struct.group = type { i32 }
@.str = [3 x i8] c"%d\00"
define i32 @resolve_uidgid(i8* %0, i32* %1, i32* %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = call i32 @strcpy(i8* %5, i8* %0)
%7 = call i8* @strchr(i8* %5, i8 signext 58)
%8 = icmp eq i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = getelementptr inbounds i8, i8* %7, i64 1
store i8 0, i8* %7
%11 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%12 = icmp slt i32 %11, 1
br i1 %12, label %16, label %26
13: ; preds = %3
%14 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %35
16: ; preds = %13, %9
%17 = phi i8* [ null, %13 ], [ %10, %9 ]
%18 = call %struct.passwd* @getpwnam(i8* %5)
%19 = icmp eq %struct.passwd* %18, null
br i1 %19, label %38, label %20
20: ; preds = %16
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 0
%22 = load i32, i32* %21
store i32 %22, i32* %1
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 1
%24 = load i32, i32* %23
store i32 %24, i32* %2
%25 = icmp eq i8* %17, null
br i1 %25, label %38, label %26
26: ; preds = %9, %20
%27 = phi i8* [ %17, %20 ], [ %10, %9 ]
%28 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%29 = icmp slt i32 %28, 1
br i1 %29, label %30, label %38
30: ; preds = %26
%31 = call %struct.group* @getgrnam(i8* %27)
%32 = icmp eq %struct.group* %31, null
br i1 %32, label %38, label %33
33: ; preds = %30
%34 = getelementptr inbounds %struct.group, %struct.group* %31, i64 0, i32 0
br label %35
35: ; preds = %13, %33
%36 = phi i32* [ %34, %33 ], [ %1, %13 ]
%37 = load i32, i32* %36
store i32 %37, i32* %2
br label %38
38: ; preds = %35, %20, %26, %30, %16
%39 = phi i32 [ -1, %16 ], [ -1, %30 ], [ 0, %26 ], [ 0, %20 ], [ 0, %35 ]
ret i32 %39
}