#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*/ uid_t;
struct passwd {
  int /*<<< orphan*/ pw_uid;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
struct passwd *getpwnam(char const *);
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ stderr;
uid_t user_id_from_name(const char *username) {
  errno = 0;
  struct passwd *p;
  p = getpwnam(username);
  if (errno != 0) {
    pexit("getpwnam");
  } else if (p == NULL) {
    fprintf(stderr, "unknown user: %s\n", username);
    exit(EXIT_FAILURE);
  }
  return p->pw_uid;
}


x86 assembly (clang, -O0):
<user_id_from_name>:
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)
mov    0x2e6a(%rip),%rax
cmpq   $0x0,(%rax)
je     1189 <user_id_from_name+0x49>
lea    0xe81(%rip),%rdi
call   1060 <pexit@plt>
jmp    11c0 <user_id_from_name+0x80>
cmpq   $0x0,-0x10(%rbp)
jne    11bb <user_id_from_name+0x7b>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe61(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    11c0 <user_id_from_name+0x80>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<user_id_from_name>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e89(%rip),%r15
movq   $0x0,(%r15)
call   1030 <getpwnam@plt>
mov    %rax,%rbx
cmpq   $0x0,(%r15)
je     1172 <user_id_from_name+0x32>
lea    0xe95(%rip),%rdi
call   1060 <pexit@plt>
jmp    1177 <user_id_from_name+0x37>
test   %rbx,%rbx
je     117f <user_id_from_name+0x3f>
mov    (%rbx),%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@errno = global i64 0
@.str = [9 x i8] c"getpwnam\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"unknown user: %s\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @user_id_from_name(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 i64, i64* @errno
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @pexit(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %20
10: ; preds = %1
%11 = load %struct.passwd*, %struct.passwd** %3
%12 = icmp eq %struct.passwd* %11, null
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %2
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %10
br label %20
20: ; preds = %19, %8
%21 = load %struct.passwd*, %struct.passwd** %3
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %21, i32 0, i32 0
%23 = load i32, i32* %22
ret i32 %23
}

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