#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;
};

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
struct passwd *getpwnam(char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int username2uid(const char *name) {
  struct passwd *pw;
  errno = 0;
  pw = getpwnam(name);
  if (pw == NULL) {
    if (errno)
      perror("Error: while converting user name to UID");
    else
      fprintf(stderr, "Error: unknown user %s\n", name);
    exit(1);
  }
  return pw->pw_uid;
}


x86 assembly (clang, -O0):
<username2uid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b7 <username2uid+0x77>
mov    0x2e5f(%rip),%rax
cmpq   $0x0,(%rax)
je     1194 <username2uid+0x54>
lea    0xe76(%rip),%rdi
call   1050 <perror@plt>
jmp    11ad <username2uid+0x6d>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe81(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<username2uid>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e8a(%rip),%r14
movq   $0x0,(%r14)
call   1030 <getpwnam@plt>
test   %rax,%rax
je     1169 <username2uid+0x29>
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
cmpq   $0x0,(%r14)
je     1185 <username2uid+0x45>
lea    0xe8a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe94(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@errno = global i64 0
@.str = [41 x i8] c"Error: while converting user name to UID\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Error: unknown user %s\0A\00"
define i32 @username2uid(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.passwd*
store i8* %0, i8** %2
store i64 0, i64* @errno
%4 = load i8*, i8** %2
%5 = call %struct.passwd* @getpwnam(i8* %4)
store %struct.passwd* %5, %struct.passwd** %3
%6 = load %struct.passwd*, %struct.passwd** %3
%7 = icmp eq %struct.passwd* %6, null
br i1 %7, label %8, label %19
8: ; preds = %1
%9 = load i64, i64* @errno
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %17
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %2
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %13, %11
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = load %struct.passwd*, %struct.passwd** %3
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i32 0, i32 0
%22 = load i32, i32* %21
ret i32 %22
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@errno = global i64 0
@.str = [41 x i8] c"Error: while converting user name to UID\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Error: unknown user %s\0A\00"
define i32 @username2uid(i8* %0) {
store i64 0, i64* @errno
%2 = tail call %struct.passwd* @getpwnam(i8* %0)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %4, label %14
4: ; preds = %1
%5 = load i64, i64* @errno
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %12
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %9, %7
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%16 = load i32, i32* %15
ret i32 %16
}