#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 scalar_t__ uid_t;
struct passwd {
  char *pw_name;
};

/* Variables and functions */
int slurm_getpwuid_r(scalar_t__, struct passwd *, char *, int,
                     struct passwd **);
char *xstrdup(char *);
char *uid_to_string(uid_t uid) {
  struct passwd pwd, *result;
  char buffer[65536], *ustring;
  int rc;
  if (uid == 0)
    return xstrdup("root");
  rc = slurm_getpwuid_r(uid, &pwd, buffer, 65536, &result);
  if (result && (rc == 0))
    ustring = xstrdup(result->pw_name);
  else
    ustring = xstrdup("nobody");
  return ustring;
}


x86 assembly (clang, -O0):
<uid_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114f <uid_to_string+0x2f>
lea    0xebf(%rip),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11c0 <uid_to_string+0xa0>
mov    -0x10(%rbp),%rdi
lea    -0x10020(%rbp),%rdx
lea    -0x18(%rbp),%rsi
mov    $0x10000,%ecx
lea    -0x20(%rbp),%r8
call   1030 <slurm_getpwuid_r@plt>
mov    %eax,-0x1002c(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a2 <uid_to_string+0x82>
cmpl   $0x0,-0x1002c(%rbp)
jne    11a2 <uid_to_string+0x82>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x10028(%rbp)
jmp    11b5 <uid_to_string+0x95>
lea    0xe5c(%rip),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x10028(%rbp)
mov    -0x10028(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uid_to_string>:
test   %rdi,%rdi
je     1155 <uid_to_string+0x35>
sub    $0x10018,%rsp
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rsp,%r8
mov    $0x10000,%ecx
call   1030 <slurm_getpwuid_r@plt>
mov    (%rsp),%rcx
test   %rcx,%rcx
je     1161 <uid_to_string+0x41>
test   %eax,%eax
jne    1161 <uid_to_string+0x41>
mov    (%rcx),%rdi
jmp    1168 <uid_to_string+0x48>
lea    0xea4(%rip),%rdi
jmp    1040 <xstrdup@plt>
lea    0xe9d(%rip),%rdi
call   1040 <xstrdup@plt>
add    $0x10018,%rsp
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"root\00"
@.str.1 = [7 x i8] c"nobody\00"
define i8* @uid_to_string(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.passwd
%5 = alloca %struct.passwd*
%6 = alloca [65536 x i8]
%7 = alloca i8*
%8 = alloca i32
store i64 %0, i64* %3
%9 = load i64, i64* %3
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i8* @xstrdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %2
br label %31
13: ; preds = %1
%14 = load i64, i64* %3
%15 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%16 = call i32 @slurm_getpwuid_r(i64 %14, %struct.passwd* %4, i8* %15, i32 65536, %struct.passwd** %5)
store i32 %16, i32* %8
%17 = load %struct.passwd*, %struct.passwd** %5
%18 = icmp ne %struct.passwd* %17, null
br i1 %18, label %19, label %27
19: ; preds = %13
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load %struct.passwd*, %struct.passwd** %5
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = call i8* @xstrdup(i8* %25)
store i8* %26, i8** %7
br label %29
27: ; preds = %19, %13
%28 = call i8* @xstrdup(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* %28, i8** %7
br label %29
29: ; preds = %27, %22
%30 = load i8*, i8** %7
store i8* %30, i8** %2
br label %31
31: ; preds = %29, %11
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"root\00"
@.str.1 = [7 x i8] c"nobody\00"
define i8* @uid_to_string(i64 %0) {
%2 = alloca %struct.passwd
%3 = alloca %struct.passwd*
%4 = alloca [65536 x i8]
%5 = bitcast %struct.passwd* %2 to i8*
%6 = bitcast %struct.passwd** %3 to i8*
%7 = getelementptr inbounds [65536 x i8], [65536 x i8]* %4, i64 0, i64 0
%8 = icmp eq i64 %0, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i8* @xstrdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %23
11: ; preds = %1
%12 = call i32 @slurm_getpwuid_r(i64 %0, %struct.passwd* %2, i8* %7, i32 65536, %struct.passwd** %3)
%13 = load %struct.passwd*, %struct.passwd** %3
%14 = icmp ne %struct.passwd* %13, null
%15 = icmp eq i32 %12, 0
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i8* @xstrdup(i8* %19)
br label %23
21: ; preds = %11
%22 = call i8* @xstrdup(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %17, %21, %9
%24 = phi i8* [ %10, %9 ], [ %20, %17 ], [ %22, %21 ]
ret i8* %24
}