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

/* Variables and functions */
struct passwd *getpwuid(unsigned int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
void user2str(unsigned uid, char *out) {
  struct passwd *pw = getpwuid(uid);
  if (pw) {
    strcpy(out, pw->pw_name);
  } else {
    sprintf(out, "%d", uid);
  }
}


x86 assembly (clang, -O0):
<user2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     116a <user2str+0x3a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
jmp    117d <user2str+0x4d>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe88(%rip),%rsi
call   1050 <sprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<user2str>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
call   1040 <getpwuid@plt>
test   %rax,%rax
je     1152 <user2str+0x22>
mov    (%rax),%esi
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <strcpy@plt>
lea    0xea7(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <sprintf@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@.str = [3 x i8] c"%d\00"
define void @user2str(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.passwd*
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i32, i32* %3
%7 = call %struct.passwd* @getpwuid(i32 %6)
store %struct.passwd* %7, %struct.passwd** %5
%8 = load %struct.passwd*, %struct.passwd** %5
%9 = icmp ne %struct.passwd* %8, null
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = load %struct.passwd*, %struct.passwd** %5
%13 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @strcpy(i8* %11, i32 %14)
br label %20
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i32, i32* %3
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %16, %10
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@.str = [3 x i8] c"%d\00"
define void @user2str(i32 %0, i8* %1) {
%3 = tail call %struct.passwd* @getpwuid(i32 %0)
%4 = icmp eq %struct.passwd* %3, null
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = tail call i32 @strcpy(i8* %1, i32 %7)
br label %11
9: ; preds = %2
%10 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %11
11: ; preds = %9, %5
ret void
}