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

/* Variables and functions */
int ENOENT;
int getpwnam_r(char const *, struct passwd *, char *, int, struct passwd **);
int get_user_id(const char *username, uid_t *uid) {
  char buf[8192];
  struct passwd pwd, *res;
  int ret;
  ret = getpwnam_r(username, &pwd, buf, sizeof(buf), &res);
  if (res == NULL) {
    if (!ret)
      return -ENOENT;
    return ret;
  }
  *uid = pwd.pw_uid;
  return 0;
}


x86 assembly (clang, -O0):
<get_user_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x2020(%rbp),%rdx
lea    -0x2028(%rbp),%rsi
mov    $0x2000,%ecx
lea    -0x2030(%rbp),%r8
call   1030 <getpwnam_r@plt>
mov    %eax,-0x2034(%rbp)
cmpq   $0x0,-0x2030(%rbp)
jne    1188 <get_user_id+0x78>
cmpl   $0x0,-0x2034(%rbp)
jne    117a <get_user_id+0x6a>
xor    %eax,%eax
mov    0x2e68(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    119b <get_user_id+0x8b>
mov    -0x2034(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119b <get_user_id+0x8b>
mov    -0x2028(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_user_id>:
push   %rbx
sub    $0x2010,%rsp
mov    %rsi,%rbx
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%r8
mov    $0x2000,%ecx
call   1030 <getpwnam_r@plt>
cmpq   $0x0,0x8(%rsp)
je     1143 <get_user_id+0x33>
mov    (%rsp),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    1152 <get_user_id+0x42>
test   %eax,%eax
jne    1152 <get_user_id+0x42>
mov    0x2e8a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x2010,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@ENOENT = global i32 0
define i32 @get_user_id(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [8192 x i8]
%7 = alloca %struct.passwd
%8 = alloca %struct.passwd*
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i8*, i8** %4
%11 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%12 = call i32 @getpwnam_r(i8* %10, %struct.passwd* %7, i8* %11, i32 8192, %struct.passwd** %8)
store i32 %12, i32* %9
%13 = load %struct.passwd*, %struct.passwd** %8
%14 = icmp eq %struct.passwd* %13, null
br i1 %14, label %15, label %23
15: ; preds = %2
%16 = load i32, i32* %9
%17 = icmp ne i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i32, i32* @ENOENT
%20 = sub nsw i32 0, %19
store i32 %20, i32* %3
br label %27
21: ; preds = %15
%22 = load i32, i32* %9
store i32 %22, i32* %3
br label %27
23: ; preds = %2
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32*, i32** %5
store i32 %25, i32* %26
store i32 0, i32* %3
br label %27
27: ; preds = %23, %21, %18
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@ENOENT = global i32 0
define i32 @get_user_id(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [8192 x i8]
%4 = alloca %struct.passwd
%5 = alloca %struct.passwd*
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.passwd* %4 to i8*
%8 = bitcast %struct.passwd** %5 to i8*
%9 = call i32 @getpwnam_r(i8* %0, %struct.passwd* %4, i8* %6, i32 8192, %struct.passwd** %5)
%10 = load %struct.passwd*, %struct.passwd** %5
%11 = icmp eq %struct.passwd* %10, null
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = icmp eq i32 %9, 0
br i1 %13, label %14, label %20
14: ; preds = %12
%15 = load i32, i32* @ENOENT
%16 = sub nsw i32 0, %15
br label %20
17: ; preds = %2
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%19 = load i32, i32* %18
store i32 %19, i32* %1
br label %20
20: ; preds = %12, %17, %14
%21 = phi i32 [ %16, %14 ], [ 0, %17 ], [ %9, %12 ]
ret i32 %21
}