#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 spwd {
  int /*<<< orphan*/ sp_warn;
  int /*<<< orphan*/ sp_max;
  int /*<<< orphan*/ sp_min;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int cm_replace_entry(char *, char *, char *, char *, char *);
int errno;
struct spwd *getspnam(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, int,
                            int /*<<< orphan*/, int /*<<< orphan*/,
                            int /*<<< orphan*/);
int time(int /*<<< orphan*/ *);
int cm_user_set_passwd(char *user, char *passwd) {
  int i, day;
  static char buf[2048];
  day = time(NULL) / 86400;
  errno = 0;
  struct spwd *s = getspnam(user);
  if (s == NULL) {
    return -errno;
  }
  snprintf(buf, sizeof(buf), "%s:%s:%d:%ld:%ld:%ld:::\n", user, passwd, day,
           s->sp_min, s->sp_max, s->sp_warn);
  if ((i = cm_replace_entry(user, "%[^:]:%*[^:]:%*d:%*d:%*d:%*d:%*d:%*d:%*d",
                            buf, "/etc/shadow", "/etc/.shadow.cm")) != 0) {
    return i;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cm_user_set_passwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    $0x15180,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    0x2e75(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1060 <getspnam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119c <cm_user_set_passwd+0x5c>
xor    %eax,%eax
mov    0x2e4e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1226 <cm_user_set_passwd+0xe6>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9d
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%r11d
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%r10d
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
lea    0x2e8b(%rip),%rdi
mov    $0x800,%esi
lea    0xe2f(%rip),%rdx
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe2b(%rip),%rsi
lea    0x2e5b(%rip),%rdx
lea    0xe46(%rip),%rcx
lea    0xe4b(%rip),%r8
call   1030 <cm_replace_entry@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     121f <cm_user_set_passwd+0xdf>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1226 <cm_user_set_passwd+0xe6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cm_user_set_passwd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,%ebp
mov    0x2e80(%rip),%r12
movl   $0x0,(%r12)
mov    %r15,%rdi
call   1060 <getspnam@plt>
test   %rax,%rax
je     11ef <cm_user_set_passwd+0xaf>
movslq %ebp,%r9
imul   $0xffffffffc22e4507,%r9,%rcx
shr    $0x20,%rcx
add    %ecx,%r9d
mov    %r9d,%ecx
shr    $0x1f,%ecx
sar    $0x10,%r9d
add    %ecx,%r9d
mov    0x8(%rax),%ebp
mov    (%rax),%ebx
mov    0x4(%rax),%eax
sub    $0x8,%rsp
lea    0x2eaa(%rip),%r12
lea    0xe53(%rip),%rdx
mov    %r12,%rdi
mov    $0x800,%esi
mov    %r15,%rcx
mov    %r14,%r8
push   %rbx
push   %rax
push   %rbp
call   1040 <snprintf@plt>
add    $0x20,%rsp
lea    0xe4b(%rip),%rsi
lea    0xe6d(%rip),%rcx
lea    0xe72(%rip),%r8
mov    %r15,%rdi
mov    %r12,%rdx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <cm_replace_entry@plt>
sub    (%r12),%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.spwd = type { i32, i32, i32 }
@cm_user_set_passwd.buf = internal global [2048 x i8] zeroinitializer
@errno = global i32 0
@.str = [25 x i8] c"%s:%s:%d:%ld:%ld:%ld:::\0A\00"
@.str.1 = [41 x i8] c"%[^:]:%*[^:]:%*d:%*d:%*d:%*d:%*d:%*d:%*d\00"
@.str.2 = [12 x i8] c"/etc/shadow\00"
@.str.3 = [16 x i8] c"/etc/.shadow.cm\00"
define i32 @cm_user_set_passwd(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.spwd*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = call i32 @time(i32* null)
%10 = sdiv i32 %9, 86400
store i32 %10, i32* %7
store i32 0, i32* @errno
%11 = load i8*, i8** %4
%12 = call %struct.spwd* @getspnam(i8* %11)
store %struct.spwd* %12, %struct.spwd** %8
%13 = load %struct.spwd*, %struct.spwd** %8
%14 = icmp eq %struct.spwd* %13, null
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
store i32 %17, i32* %3
br label %38
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = load %struct.spwd*, %struct.spwd** %8
%23 = getelementptr inbounds %struct.spwd, %struct.spwd* %22, i32 0, i32 2
%24 = load i32, i32* %23
%25 = load %struct.spwd*, %struct.spwd** %8
%26 = getelementptr inbounds %struct.spwd, %struct.spwd* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = load %struct.spwd*, %struct.spwd** %8
%29 = getelementptr inbounds %struct.spwd, %struct.spwd* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @snprintf(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @cm_user_set_passwd.buf, i64 0, i64 0), i32 2048, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %20, i32 %21, i32 %24, i32 %27, i32 %30)
%32 = load i8*, i8** %4
%33 = call i32 @cm_replace_entry(i8* %32, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @cm_user_set_passwd.buf, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i32 %33, i32* %6
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %18
%36 = load i32, i32* %6
store i32 %36, i32* %3
br label %38
37: ; preds = %18
store i32 0, i32* %3
br label %38
38: ; preds = %37, %35, %15
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.spwd = type { i32, i32, i32 }
@cm_user_set_passwd.buf = internal global [2048 x i8] zeroinitializer
@errno = global i32 0
@.str = [25 x i8] c"%s:%s:%d:%ld:%ld:%ld:::\0A\00"
@.str.1 = [41 x i8] c"%[^:]:%*[^:]:%*d:%*d:%*d:%*d:%*d:%*d:%*d\00"
@.str.2 = [12 x i8] c"/etc/shadow\00"
@.str.3 = [16 x i8] c"/etc/.shadow.cm\00"
define i32 @cm_user_set_passwd(i8* %0, i8* %1) {
%3 = tail call i32 @time(i32* null)
store i32 0, i32* @errno
%4 = tail call %struct.spwd* @getspnam(i8* %0)
%5 = icmp eq %struct.spwd* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = sub nsw i32 0, %7
br label %19
9: ; preds = %2
%10 = sdiv i32 %3, 86400
%11 = getelementptr inbounds %struct.spwd, %struct.spwd* %4, i64 0, i32 2
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.spwd, %struct.spwd* %4, i64 0, i32 1
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.spwd, %struct.spwd* %4, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i32 @snprintf(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @cm_user_set_passwd.buf, i64 0, i64 0), i32 2048, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i32 %10, i32 %12, i32 %14, i32 %16)
%18 = tail call i32 @cm_replace_entry(i8* %0, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @cm_user_set_passwd.buf, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %9, %6
%20 = phi i32 [ %8, %6 ], [ %18, %9 ]
ret i32 %20
}