#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;
  char *pw_passwd;
  int /*<<< orphan*/ pw_shell;
  int /*<<< orphan*/ pw_dir;
  int /*<<< orphan*/ pw_gecos;
  scalar_t__ pw_gid;
  scalar_t__ pw_uid;
};
typedef int /*<<< orphan*/ __uid_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ _SC_GETPW_R_SIZE_MAX;
scalar_t__ __getpwuid_r(int /*<<< orphan*/, struct passwd *, char *, size_t,
                        struct passwd **);
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
size_t __sysconf(int /*<<< orphan*/);
char *alloca(size_t);
scalar_t__ sprintf(char *, char *, char *, char *, unsigned long, unsigned long,
                   int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int __getpw(__uid_t uid, char *buf) {
  size_t buflen;
  char *tmpbuf;
  struct passwd resbuf, *p;
  if (buf == NULL) {
    __set_errno(EINVAL);
    return -1;
  }
  buflen = __sysconf(_SC_GETPW_R_SIZE_MAX);
  tmpbuf = alloca(buflen);
  if (__getpwuid_r(uid, &resbuf, tmpbuf, buflen, &p) != 0)
    return -1;
  if (p == NULL)
    return -1;
  if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s", p->pw_name, p->pw_passwd,
              (unsigned long int)p->pw_uid, (unsigned long int)p->pw_gid,
              p->pw_gecos, p->pw_dir, p->pw_shell) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<__getpw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1184 <__getpw+0x34>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <__getpw+0x108>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1040 <__sysconf@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <alloca@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x50(%rbp),%rsi
lea    -0x58(%rbp),%r8
call   1070 <__getpwuid_r@plt>
cmp    $0x0,%rax
je     11d1 <__getpw+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <__getpw+0x108>
cmpq   $0x0,-0x58(%rbp)
jne    11e8 <__getpw+0x98>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <__getpw+0x108>
mov    -0x10(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    (%rax),%rdx
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x58(%rbp),%rax
mov    0x28(%rax),%r8
mov    -0x58(%rbp),%rax
mov    0x20(%rax),%r9
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%r11d
mov    -0x58(%rbp),%rax
mov    0x14(%rax),%r10d
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%eax
lea    0xdd7(%rip),%rsi
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
call   1060 <sprintf@plt>
cmp    $0x0,%rax
jge    1251 <__getpw+0x101>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <__getpw+0x108>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__getpw>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
test   %rsi,%rsi
je     11e5 <__getpw+0x95>
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
call   1040 <__sysconf@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <alloca@plt>
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1070 <__getpwuid_r@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    11f8 <__getpw+0xa8>
mov    0x8(%rsp),%rsi
test   %rsi,%rsi
je     11f8 <__getpw+0xa8>
mov    (%rsi),%rdx
mov    0x8(%rsi),%rcx
mov    0x28(%rsi),%r8
mov    0x20(%rsi),%r9
mov    0x18(%rsi),%eax
mov    0x14(%rsi),%ebp
mov    0x10(%rsi),%ebx
sub    $0x8,%rsp
lea    0xe30(%rip),%rsi
mov    %r14,%rdi
push   %rbx
push   %rbp
push   %rax
call   1060 <sprintf@plt>
add    $0x20,%rsp
sar    $0x3f,%rax
jmp    11f8 <__getpw+0xa8>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8*, i8*, i32, i32, i32, i64, i64 }
@EINVAL = global i32 0
@_SC_GETPW_R_SIZE_MAX = global i32 0
@.str = [23 x i8] c"%s:%s:%lu:%lu:%s:%s:%s\00"
define i32 @__getpw(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca %struct.passwd
%9 = alloca %struct.passwd*
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @EINVAL
%14 = call i32 @__set_errno(i32 %13)
store i32 -1, i32* %3
br label %57
15: ; preds = %2
%16 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%17 = call i64 @__sysconf(i32 %16)
store i64 %17, i64* %6
%18 = load i64, i64* %6
%19 = call i8* @alloca(i64 %18)
store i8* %19, i8** %7
%20 = load i32, i32* %4
%21 = load i8*, i8** %7
%22 = load i64, i64* %6
%23 = call i64 @__getpwuid_r(i32 %20, %struct.passwd* %8, i8* %21, i64 %22, %struct.passwd** %9)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %15
store i32 -1, i32* %3
br label %57
26: ; preds = %15
%27 = load %struct.passwd*, %struct.passwd** %9
%28 = icmp eq %struct.passwd* %27, null
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 -1, i32* %3
br label %57
30: ; preds = %26
%31 = load i8*, i8** %5
%32 = load %struct.passwd*, %struct.passwd** %9
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = load %struct.passwd*, %struct.passwd** %9
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %35, i32 0, i32 1
%37 = load i8*, i8** %36
%38 = load %struct.passwd*, %struct.passwd** %9
%39 = getelementptr inbounds %struct.passwd, %struct.passwd* %38, i32 0, i32 6
%40 = load i64, i64* %39
%41 = load %struct.passwd*, %struct.passwd** %9
%42 = getelementptr inbounds %struct.passwd, %struct.passwd* %41, i32 0, i32 5
%43 = load i64, i64* %42
%44 = load %struct.passwd*, %struct.passwd** %9
%45 = getelementptr inbounds %struct.passwd, %struct.passwd* %44, i32 0, i32 4
%46 = load i32, i32* %45
%47 = load %struct.passwd*, %struct.passwd** %9
%48 = getelementptr inbounds %struct.passwd, %struct.passwd* %47, i32 0, i32 3
%49 = load i32, i32* %48
%50 = load %struct.passwd*, %struct.passwd** %9
%51 = getelementptr inbounds %struct.passwd, %struct.passwd* %50, i32 0, i32 2
%52 = load i32, i32* %51
%53 = call i64 @sprintf(i8* %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %34, i8* %37, i64 %40, i64 %43, i32 %46, i32 %49, i32 %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %30
store i32 -1, i32* %3
br label %57
56: ; preds = %30
store i32 0, i32* %3
br label %57
57: ; preds = %56, %55, %29, %25, %12
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.passwd = type { i8*, i8*, i32, i32, i32, i64, i64 }
@EINVAL = global i32 0
@_SC_GETPW_R_SIZE_MAX = global i32 0
@.str = [23 x i8] c"%s:%s:%lu:%lu:%s:%s:%s\00"
define i32 @__getpw(i32 %0, i8* %1) {
%3 = alloca %struct.passwd
%4 = alloca %struct.passwd*
%5 = bitcast %struct.passwd* %3 to i8*
%6 = bitcast %struct.passwd** %4 to i8*
%7 = icmp eq i8* %1, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @EINVAL
%10 = tail call i32 @__set_errno(i32 %9)
br label %38
11: ; preds = %2
%12 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%13 = tail call i64 @__sysconf(i32 %12)
%14 = tail call i8* @alloca(i64 %13)
%15 = call i64 @__getpwuid_r(i32 %0, %struct.passwd* %3, i8* %14, i64 %13, %struct.passwd** %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %38
17: ; preds = %11
%18 = load %struct.passwd*, %struct.passwd** %4
%19 = icmp eq %struct.passwd* %18, null
br i1 %19, label %38, label %20
20: ; preds = %17
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 1
%24 = load i8*, i8** %23
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 6
%26 = load i64, i64* %25
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 5
%28 = load i64, i64* %27
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 3
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 2
%34 = load i32, i32* %33
%35 = call i64 @sprintf(i8* %1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %24, i64 %26, i64 %28, i32 %30, i32 %32, i32 %34)
%36 = ashr i64 %35, 63
%37 = trunc i64 %36 to i32
br label %38
38: ; preds = %20, %17, %11, %8
%39 = phi i32 [ -1, %8 ], [ -1, %11 ], [ -1, %17 ], [ %37, %20 ]
ret i32 %39
}