#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;
};
typedef int /*<<< orphan*/ id;

/* Variables and functions */
struct passwd *getpwuid(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
char *get_uid_str(int uid) {
  struct passwd *pwd;
  static char id[20];
  snprintf(id, sizeof(id), "%i", uid);
  if ((pwd = getpwuid(uid)) != NULL)
    strncpy(id, pwd->pw_name, sizeof(id));
  id[sizeof(id) - 1] = '\0';
  return id;
}


x86 assembly (clang, -O0):
<get_uid_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%ecx
lea    0x2f0b(%rip),%rdi
mov    $0x14,%esi
lea    0xeaf(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x4(%rbp),%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1183 <get_uid_str+0x53>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0x2ed7(%rip),%rdi
mov    $0x14,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x2ed9(%rip)
lea    0x2ebf(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_uid_str>:
push   %rbx
mov    %edi,%ebx
lea    0x2f16(%rip),%rdi
lea    0xebf(%rip),%rdx
mov    $0x14,%esi
mov    %ebx,%ecx
call   1050 <snprintf@plt>
mov    %ebx,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     116c <get_uid_str+0x3c>
mov    (%rax),%esi
lea    0x2eee(%rip),%rdi
mov    $0x14,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x2ef0(%rip)
lea    0x2ed6(%rip),%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@get_uid_str.id = internal global [20 x i8] zeroinitializer
@.str = [3 x i8] c"%i\00"
define i8* @get_uid_str(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.passwd*
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i32 @snprintf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @get_uid_str.id, i64 0, i64 0), i32 20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* %2
%7 = call %struct.passwd* @getpwuid(i32 %6)
store %struct.passwd* %7, %struct.passwd** %3
%8 = icmp ne %struct.passwd* %7, null
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load %struct.passwd*, %struct.passwd** %3
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @strncpy(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @get_uid_str.id, i64 0, i64 0), i32 %12, i32 20)
br label %14
14: ; preds = %9, %1
store i8 0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @get_uid_str.id, i64 0, i64 19)
ret i8* getelementptr inbounds ([20 x i8], [20 x i8]* @get_uid_str.id, i64 0, i64 0)
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@get_uid_str.id = internal global [20 x i8] zeroinitializer
@.str = [3 x i8] c"%i\00"
define i8* @get_uid_str(i32 %0) {
%2 = tail call i32 @snprintf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @get_uid_str.id, i64 0, i64 0), i32 20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = tail call %struct.passwd* @getpwuid(i32 %0)
%4 = icmp eq %struct.passwd* %3, null
br i1 %4, label %9, label %5
5: ; preds = %1
%6 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = tail call i32 @strncpy(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @get_uid_str.id, i64 0, i64 0), i32 %7, i32 20)
br label %9
9: ; preds = %5, %1
store i8 0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @get_uid_str.id, i64 0, i64 19)
ret i8* getelementptr inbounds ([20 x i8], [20 x i8]* @get_uid_str.id, i64 0, i64 0)
}