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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int /*<<< orphan*/ geteuid();
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int who_am_i(void) {
  struct passwd *pw;
  char *user = NULL;
  pw = getpwuid(geteuid());
  if (pw)
    user = pw->pw_name;
  else if ((user = getenv("USER")) == NULL) {
    fprintf(stderr, "I don't know!\n");
    return 1;
  }
  printf("%s\n", user);
  return 0;
}


x86 assembly (clang, -O0):
<who_am_i>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118d <who_am_i+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11cd <who_am_i+0x7d>
lea    0xe6c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11c8 <who_am_i+0x78>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e4 <who_am_i+0x94>
jmp    11cd <who_am_i+0x7d>
mov    -0x18(%rbp),%rsi
lea    0xe3c(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<who_am_i>:
push   %rax
xor    %eax,%eax
call   1060 <geteuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     1169 <who_am_i+0x19>
mov    (%rax),%rsi
jmp    117d <who_am_i+0x2d>
lea    0xe90(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rsi
test   %rax,%rax
je     118d <who_am_i+0x3d>
lea    0xe90(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"USER\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"I don't know!\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @who_am_i() {
%1 = alloca i32
%2 = alloca %struct.passwd*
%3 = alloca i8*
store i8* null, i8** %3
%4 = call i32 (...) @geteuid()
%5 = call %struct.passwd* @getpwuid(i32 %4)
store %struct.passwd* %5, %struct.passwd** %2
%6 = load %struct.passwd*, %struct.passwd** %2
%7 = icmp ne %struct.passwd* %6, null
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load %struct.passwd*, %struct.passwd** %2
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %9, i32 0, i32 0
%11 = load i8*, i8** %10
store i8* %11, i8** %3
br label %19
12: ; preds = %0
%13 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %3
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %22
18: ; preds = %12
br label %19
19: ; preds = %18, %8
%20 = load i8*, i8** %3
%21 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %20)
store i32 0, i32* %1
br label %22
22: ; preds = %19, %15
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"USER\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"I don't know!\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @who_am_i() {
%1 = tail call i32 (...) @geteuid()
%2 = tail call %struct.passwd* @getpwuid(i32 %1)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%6 = load i8*, i8** %5
br label %13
7: ; preds = %0
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %16
13: ; preds = %7, %4
%14 = phi i8* [ %6, %4 ], [ %8, %7 ]
%15 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %10
%17 = phi i32 [ 0, %13 ], [ 1, %10 ]
ret i32 %17
}