#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 {
  int pw_uid;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ _SC_GETPW_R_SIZE_MAX;
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpwnam_r(char *, struct passwd *, char *, size_t, struct passwd **);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
size_t sysconf(int /*<<< orphan*/);
int useruid(char *user) {
  int ret;
  size_t bufsize;
  char *buf;
  struct passwd pwd;
  struct passwd *result;
  if (0) {
    printf("search for %s\n", user);
  }
  bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  if (bufsize == -1) {
    bufsize = 16384;
  }
  buf = malloc(bufsize);
  if (buf == NULL) {
    perror("malloc");
    exit(EXIT_FAILURE);
  }
  ret = getpwnam_r(user, &pwd, buf, bufsize, &result);
  if (result == NULL) {
    if (ret == 0) {
      printf("sres: error: user not found, err = %d\n", ret);
      return -1;
    } else {
      errno = ret;
      perror("getpwnam_r()");
      return -1;
    }
  }
  return pwd.pw_uid;
}


x86 assembly (clang, -O0):
<useruid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1191 <useruid+0x31>
movq   $0x4000,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c3 <useruid+0x63>
lea    0xe50(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%r8
call   1050 <getpwnam_r@plt>
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1235 <useruid+0xd5>
cmpl   $0x0,-0x14(%rbp)
jne    1211 <useruid+0xb1>
mov    -0x14(%rbp),%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <useruid+0xdb>
mov    -0x14(%rbp),%ecx
mov    0x2dbd(%rip),%rax
mov    %ecx,(%rax)
lea    0xe0a(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <useruid+0xdb>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<useruid>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
cmp    $0xffffffffffffffff,%rax
mov    $0x4000,%ebx
cmovne %rax,%rbx
mov    %rbx,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     11f0 <useruid+0x90>
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%r8
mov    %r14,%rdi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1050 <getpwnam_r@plt>
cmpq   $0x0,0x10(%rsp)
je     11b8 <useruid+0x58>
mov    0x8(%rsp),%eax
jmp    11d1 <useruid+0x71>
test   %eax,%eax
jne    11d9 <useruid+0x79>
lea    0xe44(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2df8(%rip),%rcx
mov    %eax,(%rcx)
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
jmp    11cc <useruid+0x6c>
lea    0xe09(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@.str = [7 x i8] c"malloc\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [39 x i8] c"sres: error: user not found, err = %d\0A\00"
@errno = global i32 0
@.str.2 = [13 x i8] c"getpwnam_r()\00"
define i32 @useruid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca %struct.passwd
%8 = alloca %struct.passwd*
store i8* %0, i8** %3
%9 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%10 = call i64 @sysconf(i32 %9)
store i64 %10, i64* %5
%11 = load i64, i64* %5
%12 = icmp eq i64 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i64 16384, i64* %5
br label %14
14: ; preds = %13, %1
%15 = load i64, i64* %5
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %14
%24 = load i8*, i8** %3
%25 = load i8*, i8** %6
%26 = load i64, i64* %5
%27 = call i32 @getpwnam_r(i8* %24, %struct.passwd* %7, i8* %25, i64 %26, %struct.passwd** %8)
store i32 %27, i32* %4
%28 = load %struct.passwd*, %struct.passwd** %8
%29 = icmp eq %struct.passwd* %28, null
br i1 %29, label %30, label %39
30: ; preds = %23
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* %4
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %34)
store i32 -1, i32* %2
br label %42
36: ; preds = %30
%37 = load i32, i32* %4
store i32 %37, i32* @errno
%38 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %42
39: ; preds = %23
%40 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i32 0, i32 0
%41 = load i32, i32* %40
store i32 %41, i32* %2
br label %42
42: ; preds = %39, %36, %33
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@.str = [7 x i8] c"malloc\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [39 x i8] c"sres: error: user not found, err = %d\0A\00"
@errno = global i32 0
@.str.2 = [13 x i8] c"getpwnam_r()\00"
define i32 @useruid(i8* %0) {
%2 = alloca %struct.passwd
%3 = alloca %struct.passwd*
%4 = bitcast %struct.passwd* %2 to i8*
%5 = bitcast %struct.passwd** %3 to i8*
%6 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%7 = tail call i64 @sysconf(i32 %6)
%8 = icmp eq i64 %7, -1
%9 = select i1 %8, i64 16384, i64 %7
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %1
%17 = call i32 @getpwnam_r(i8* %0, %struct.passwd* %2, i8* %10, i64 %9, %struct.passwd** %3)
%18 = load %struct.passwd*, %struct.passwd** %3
%19 = icmp eq %struct.passwd* %18, null
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = icmp eq i32 %17, 0
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 0)
br label %29
24: ; preds = %20
store i32 %17, i32* @errno
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %29
26: ; preds = %16
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%28 = load i32, i32* %27
br label %29
29: ; preds = %26, %24, %22
%30 = phi i32 [ -1, %22 ], [ -1, %24 ], [ %28, %26 ]
ret i32 %30
}