#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 {
  char *pw_name;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ g_error(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
char *get_username(uid_t uid) {
  struct passwd *pwd;
  errno = 0;
  pwd = getpwuid(uid);
  if (pwd == NULL) {
    if (errno == 0) {
      return "unknown username";
    } else {
      g_error("username lookup failed");
      exit(EXIT_FAILURE);
    }
  }
  return pwd->pw_name;
}


x86 assembly (clang, -O0):
<get_username>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    0x2e9e(%rip),%rax
movq   $0x0,(%rax)
mov    -0xc(%rbp),%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119b <get_username+0x6b>
mov    0x2e79(%rip),%rax
cmpq   $0x0,(%rax)
jne    1181 <get_username+0x51>
lea    0xe88(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a6 <get_username+0x76>
lea    0xe89(%rip),%rdi
call   1040 <g_error@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_username>:
push   %rbx
mov    0x2ea8(%rip),%rbx
movq   $0x0,(%rbx)
call   1030 <getpwuid@plt>
test   %rax,%rax
je     114e <get_username+0x1e>
mov    (%rax),%rax
pop    %rbx
ret
cmpq   $0x0,(%rbx)
jne    115d <get_username+0x2d>
lea    0xea5(%rip),%rax
pop    %rbx
ret
lea    0xead(%rip),%rdi
call   1040 <g_error@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@errno = global i64 0
@.str = [17 x i8] c"unknown username\00"
@.str.1 = [23 x i8] c"username lookup failed\00"
@EXIT_FAILURE = global i32 0
define i8* @get_username(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.passwd*
store i32 %0, i32* %3
store i64 0, i64* @errno
%5 = load i32, i32* %3
%6 = call %struct.passwd* @getpwuid(i32 %5)
store %struct.passwd* %6, %struct.passwd** %4
%7 = load %struct.passwd*, %struct.passwd** %4
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %2
br label %21
13: ; preds = %9
%14 = call i32 @g_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = load %struct.passwd*, %struct.passwd** %4
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i32 0, i32 0
%20 = load i8*, i8** %19
store i8* %20, i8** %2
br label %21
21: ; preds = %17, %12
%22 = load i8*, i8** %2
ret i8* %22
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@errno = global i64 0
@.str = [17 x i8] c"unknown username\00"
@.str.1 = [23 x i8] c"username lookup failed\00"
@EXIT_FAILURE = global i32 0
define i8* @get_username(i32 %0) {
store i64 0, i64* @errno
%2 = tail call %struct.passwd* @getpwuid(i32 %0)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %4, label %11
4: ; preds = %1
%5 = load i64, i64* @errno
%6 = icmp eq i64 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %4
%8 = tail call i32 @g_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%13 = load i8*, i8** %12
br label %14
14: ; preds = %4, %11
%15 = phi i8* [ %13, %11 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), %4 ]
ret i8* %15
}