#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 /*<<< orphan*/ pw_gid;
};
struct group {
  int /*<<< orphan*/ gr_name;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct group *getgrgid(int /*<<< orphan*/);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, int /*<<< orphan*/);
int validate_user_pw(char *username, char *groupname) {
  struct passwd *p;
  struct group *g;
  if ((p = getpwnam(username)) == NULL) {
    fprintf(stderr, "helper: User does not exist '%s'\n", username);
    return 0;
  } else {
    if ((g = getgrgid(p->pw_gid)) != NULL) {
      if ((strcmp(groupname, g->gr_name)) == 0)
        return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<validate_user_pw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    118c <validate_user_pw+0x4c>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe85(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11db <validate_user_pw+0x9b>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1030 <getgrgid@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11cf <validate_user_pw+0x8f>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11ca <validate_user_pw+0x8a>
movl   $0x1,-0x4(%rbp)
jmp    11db <validate_user_pw+0x9b>
jmp    11cf <validate_user_pw+0x8f>
jmp    11d4 <validate_user_pw+0x94>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_user_pw>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <getpwnam@plt>
test   %rax,%rax
je     1176 <validate_user_pw+0x36>
mov    (%rax),%edi
call   1030 <getgrgid@plt>
test   %rax,%rax
je     118e <validate_user_pw+0x4e>
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    118e <validate_user_pw+0x4e>
mov    $0x1,%eax
jmp    1190 <validate_user_pw+0x50>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@stderr = global i32 0
@.str = [34 x i8] c"helper: User does not exist '%s'\0A\00"
define i32 @validate_user_pw(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.passwd*
%7 = alloca %struct.group*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call %struct.passwd* @getpwnam(i8* %8)
store %struct.passwd* %9, %struct.passwd** %6
%10 = icmp eq %struct.passwd* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %4
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 0, i32* %3
br label %32
15: ; preds = %2
%16 = load %struct.passwd*, %struct.passwd** %6
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call %struct.group* @getgrgid(i32 %18)
store %struct.group* %19, %struct.group** %7
%20 = icmp ne %struct.group* %19, null
br i1 %20, label %21, label %30
21: ; preds = %15
%22 = load i8*, i8** %5
%23 = load %struct.group*, %struct.group** %7
%24 = getelementptr inbounds %struct.group, %struct.group* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @strcmp(i8* %22, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 1, i32* %3
br label %32
29: ; preds = %21
br label %30
30: ; preds = %29, %15
br label %31
31: ; preds = %30
store i32 0, i32* %3
br label %32
32: ; preds = %31, %28, %11
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@stderr = global i32 0
@.str = [34 x i8] c"helper: User does not exist '%s'\0A\00"
define i32 @validate_user_pw(i8* %0, i8* %1) {
%3 = tail call %struct.passwd* @getpwnam(i8* %0)
%4 = icmp eq %struct.passwd* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %19
8: ; preds = %2
%9 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call %struct.group* @getgrgid(i32 %10)
%12 = icmp eq %struct.group* %11, null
br i1 %12, label %18, label %13
13: ; preds = %8
%14 = getelementptr inbounds %struct.group, %struct.group* %11, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i64 @strcmp(i8* %1, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %8, %13
br label %19
19: ; preds = %13, %18, %5
%20 = phi i32 [ 0, %5 ], [ 0, %18 ], [ 1, %13 ]
ret i32 %20
}