#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*/ ENOENT;
int /*<<< orphan*/ ENOMEM;
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) {
    errno = ENOENT;
    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 = ENOMEM;
    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    1210 <getpw+0xf0>
mov    -0x8(%rbp),%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    118c <getpw+0x6c>
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <getpw+0xf0>
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    0xe32(%rip),%rsi
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1040 <sprintf@plt>
cmp    $0x0,%rax
jge    1209 <getpw+0xe9>
mov    0x2dee(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddd(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <getpw+0xf0>
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     1182 <getpw+0x62>
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    1199 <getpw+0x79>
mov    0x2e69(%rip),%rax
jmp    1189 <getpw+0x69>
mov    0x2e50(%rip),%rax
jmp    1189 <getpw+0x69>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    0x2e46(%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
@ENOENT = global i32 0
@.str = [21 x i8] c"%s:%s:%u:%u:%s:%s:%s\00"
@ENOMEM = 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 %45
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 %17
15: ; preds = %11
%16 = load i32, i32* @ENOENT
store i32 %16, i32* @errno
store i32 -1, i32* %3
br label %45
17: ; preds = %11
%18 = load i8*, i8** %5
%19 = load %struct.passwd*, %struct.passwd** %6
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = load %struct.passwd*, %struct.passwd** %6
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %22, i32 0, i32 1
%24 = load i8*, i8** %23
%25 = load %struct.passwd*, %struct.passwd** %6
%26 = getelementptr inbounds %struct.passwd, %struct.passwd* %25, i32 0, i32 2
%27 = load i32, i32* %26
%28 = load %struct.passwd*, %struct.passwd** %6
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %28, i32 0, i32 3
%30 = load i32, i32* %29
%31 = load %struct.passwd*, %struct.passwd** %6
%32 = getelementptr inbounds %struct.passwd, %struct.passwd* %31, i32 0, i32 4
%33 = load i8*, i8** %32
%34 = load %struct.passwd*, %struct.passwd** %6
%35 = getelementptr inbounds %struct.passwd, %struct.passwd* %34, i32 0, i32 5
%36 = load i8*, i8** %35
%37 = load %struct.passwd*, %struct.passwd** %6
%38 = getelementptr inbounds %struct.passwd, %struct.passwd* %37, i32 0, i32 6
%39 = load i8*, i8** %38
%40 = call i64 @sprintf(i8* %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %24, i32 %27, i32 %30, i8* %33, i8* %36, i8* %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %17
%43 = load i32, i32* @ENOMEM
store i32 %43, i32* @errno
store i32 -1, i32* %3
br label %45
44: ; preds = %17
store i32 0, i32* %3
br label %45
45: ; preds = %44, %42, %15, %9
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.passwd = type { i8*, i8*, i32, i32, i8*, i8*, i8* }
@EINVAL = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
@.str = [21 x i8] c"%s:%s:%u:%u:%s:%s:%s\00"
@ENOMEM = 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 %24, 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, %4, %2
%25 = phi i32* [ @EINVAL, %2 ], [ @ENOENT, %4 ], [ @ENOMEM, %7 ]
%26 = load i32, i32* %25
store i32 %26, i32* @errno
br label %27
27: ; preds = %24, %7
%28 = phi i32 [ 0, %7 ], [ -1, %24 ]
ret i32 %28
}