#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 int /*<<< orphan*/ uid_t;
struct passwd {
  char *pw_name;
  char *pw_passwd;
  int pw_gid;
  int pw_uid;
  char *pw_gecos;
  char *pw_dir;
  char *pw_shell;
};

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOBUFS;
int /*<<< orphan*/ errno;
struct passwd *getpwuid(int /*<<< orphan*/);
scalar_t__ sprintf(char *, char *, char *, char *, int, int, char *, char *,
                   char *);
int getpw(uid_t uid, char *buf) {
  struct passwd *passwd;
  if (buf == NULL) {
    errno = EINVAL;
    return -1;
  }
  if ((passwd = getpwuid(uid)) == NULL)
    return -1;
  if (sprintf(buf, "%s:%s:%u:%u:%s:%s:%s", passwd->pw_name, passwd->pw_passwd,
              passwd->pw_gid, passwd->pw_uid, passwd->pw_gecos, passwd->pw_dir,
              passwd->pw_shell) < 0) {
    errno = ENOBUFS;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<getpw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1158 <getpw+0x38>
mov    0x2e8f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e8e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <getpw+0xde>
mov    -0x8(%rbp),%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    117a <getpw+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <getpw+0xde>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%r8d
mov    -0x18(%rbp),%rax
mov    0x14(%rax),%r9d
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%r11
mov    -0x18(%rbp),%rax
mov    0x20(%rax),%r10
mov    -0x18(%rbp),%rax
mov    0x28(%rax),%rax
lea    0xe44(%rip),%rsi
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1040 <sprintf@plt>
cmp    $0x0,%rax
jge    11f7 <getpw+0xd7>
mov    0x2e18(%rip),%rax
mov    (%rax),%ecx
mov    0x2def(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <getpw+0xde>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpw>:
push   %rbx
sub    $0x20,%rsp
test   %rsi,%rsi
je     1179 <getpw+0x59>
mov    %rsi,%rbx
call   1030 <getpwuid@plt>
test   %rax,%rax
je     118b <getpw+0x6b>
mov    (%rax),%rdx
mov    0x8(%rax),%rcx
mov    0x10(%rax),%r8d
mov    0x14(%rax),%r9d
movups 0x18(%rax),%xmm0
mov    0x28(%rax),%rax
mov    %rax,0x10(%rsp)
movups %xmm0,(%rsp)
lea    0xea2(%rip),%rsi
mov    %rbx,%rdi
call   1040 <sprintf@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    1190 <getpw+0x70>
mov    0x2e81(%rip),%rax
jmp    1180 <getpw+0x60>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    0x2e4f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i8*, i8*, i32, i32, i8*, i8*, i8* }
@EINVAL = global i32 0
@errno = global i32 0
@.str = [21 x i8] c"%s:%s:%u:%u:%s:%s:%s\00"
@ENOBUFS = global i32 0
define i32 @getpw(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.passwd*
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
store i32 -1, i32* %3
br label %44
11: ; preds = %2
%12 = load i32, i32* %4
%13 = call %struct.passwd* @getpwuid(i32 %12)
store %struct.passwd* %13, %struct.passwd** %6
%14 = icmp eq %struct.passwd* %13, null
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 -1, i32* %3
br label %44
16: ; preds = %11
%17 = load i8*, i8** %5
%18 = load %struct.passwd*, %struct.passwd** %6
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = load %struct.passwd*, %struct.passwd** %6
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %21, i32 0, i32 1
%23 = load i8*, i8** %22
%24 = load %struct.passwd*, %struct.passwd** %6
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i32 0, i32 2
%26 = load i32, i32* %25
%27 = load %struct.passwd*, %struct.passwd** %6
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %27, i32 0, i32 3
%29 = load i32, i32* %28
%30 = load %struct.passwd*, %struct.passwd** %6
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %30, i32 0, i32 4
%32 = load i8*, i8** %31
%33 = load %struct.passwd*, %struct.passwd** %6
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 5
%35 = load i8*, i8** %34
%36 = load %struct.passwd*, %struct.passwd** %6
%37 = getelementptr inbounds %struct.passwd, %struct.passwd* %36, i32 0, i32 6
%38 = load i8*, i8** %37
%39 = call i64 @sprintf(i8* %17, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %23, i32 %26, i32 %29, i8* %32, i8* %35, i8* %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %16
%42 = load i32, i32* @ENOBUFS
store i32 %42, i32* @errno
store i32 -1, i32* %3
br label %44
43: ; preds = %16
store i32 0, i32* %3
br label %44
44: ; preds = %43, %41, %15, %9
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.passwd = type { i8*, i8*, i32, i32, i8*, i8*, i8* }
@EINVAL = global i32 0
@errno = global i32 0
@.str = [21 x i8] c"%s:%s:%u:%u:%s:%s:%s\00"
@ENOBUFS = global i32 0
define i32 @getpw(i32 %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %24, label %4
4: ; preds = %2
%5 = tail call %struct.passwd* @getpwuid(i32 %0)
%6 = icmp eq %struct.passwd* %5, null
br i1 %6, label %27, label %7
7: ; preds = %4
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 2
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 3
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 4
%17 = load i8*, i8** %16
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 5
%19 = load i8*, i8** %18
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 6
%21 = load i8*, i8** %20
%22 = tail call i64 @sprintf(i8* %1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %11, i32 %13, i32 %15, i8* %17, i8* %19, i8* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %7, %2
%25 = phi i32* [ @EINVAL, %2 ], [ @ENOBUFS, %7 ]
%26 = load i32, i32* %25
store i32 %26, i32* @errno
br label %27
27: ; preds = %24, %7, %4
%28 = phi i32 [ -1, %4 ], [ 0, %7 ], [ -1, %24 ]
ret i32 %28
}