#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 {
  char *sp_pwdp;
  int /*<<< orphan*/ sp_namp;
};
typedef int mode_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
struct spwd *fgetspent(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ putspent(struct spwd *, int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ system(char *);
int umask(int);
int cmd_smac_adm_user_replace(const char *name, const char *label) {
  struct spwd *spwd;
  FILE *su, *tsu;
  mode_t old_umask;
  old_umask = umask(0077);
  su = fopen("/smac_users", "r");
  if (!su) {
    umask(old_umask);
    return -errno;
  }
  tsu = fopen("/smac_users.tmp", "w");
  if (!tsu) {
    umask(old_umask);
    fclose(su);
    return -errno;
  }
  while ((spwd = fgetspent(su))) {
    if (!strcmp(spwd->sp_namp, name)) {
      spwd->sp_pwdp = (char *)label;
    }
    putspent(spwd, tsu);
  }
  fclose(tsu);
  fclose(su);
  system("rm "
         "/smac_users");
  system("mv -f "
         "/smac_users.tmp"
         " "
         "/smac_users");
  umask(old_umask);
  return 0;
}


x86 assembly (clang, -O0):
<cmd_smac_adm_user_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x3f,%edi
call   1070 <umask@plt>
mov    %eax,-0x34(%rbp)
lea    0xe83(%rip),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ca <cmd_smac_adm_user_replace+0x5a>
mov    -0x34(%rbp),%edi
call   1070 <umask@plt>
xor    %eax,%eax
mov    0x2e20(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    129b <cmd_smac_adm_user_replace+0x12b>
lea    0xe31(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1210 <cmd_smac_adm_user_replace+0xa0>
mov    -0x34(%rbp),%edi
call   1070 <umask@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
mov    0x2dda(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    129b <cmd_smac_adm_user_replace+0x12b>
jmp    1215 <cmd_smac_adm_user_replace+0xa5>
mov    -0x28(%rbp),%rdi
call   1080 <fgetspent@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1262 <cmd_smac_adm_user_replace+0xf2>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1250 <cmd_smac_adm_user_replace+0xe0>
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <putspent@plt>
jmp    1215 <cmd_smac_adm_user_replace+0xa5>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd99(%rip),%rdi
call   1050 <system@plt>
lea    0xd9c(%rip),%rdi
call   1050 <system@plt>
mov    -0x34(%rbp),%edi
call   1070 <umask@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_smac_adm_user_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r12
mov    $0x3f,%edi
call   1070 <umask@plt>
mov    %eax,%r14d
lea    0xe82(%rip),%rdi
lea    0xe64(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1243 <cmd_smac_adm_user_replace+0xd3>
mov    %rax,%r13
lea    0xe4e(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     124d <cmd_smac_adm_user_replace+0xdd>
mov    %rax,%rbp
mov    %r13,%rdi
call   1080 <fgetspent@plt>
test   %rax,%rax
je     120f <cmd_smac_adm_user_replace+0x9f>
mov    %rax,%rbx
jmp    11fb <cmd_smac_adm_user_replace+0x8b>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <putspent@plt>
mov    %r13,%rdi
call   1080 <fgetspent@plt>
mov    %rax,%rbx
test   %rax,%rax
je     120f <cmd_smac_adm_user_replace+0x9f>
mov    0x8(%rbx),%edi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    11e0 <cmd_smac_adm_user_replace+0x70>
mov    %r15,(%rbx)
jmp    11e0 <cmd_smac_adm_user_replace+0x70>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
lea    0xdee(%rip),%rdi
call   1050 <system@plt>
lea    0xdf1(%rip),%rdi
call   1050 <system@plt>
mov    %r14d,%edi
call   1070 <umask@plt>
xor    %eax,%eax
jmp    1268 <cmd_smac_adm_user_replace+0xf8>
mov    %r14d,%edi
call   1070 <umask@plt>
jmp    125d <cmd_smac_adm_user_replace+0xed>
mov    %r14d,%edi
call   1070 <umask@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    0x2d7c(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.spwd = type { i8*, i32 }
@.str = [12 x i8] c"/smac_users\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [16 x i8] c"/smac_users.tmp\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [15 x i8] c"rm /smac_users\00"
@.str.5 = [34 x i8] c"mv -f /smac_users.tmp /smac_users\00"
define i32 @cmd_smac_adm_user_replace(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.spwd*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = call i32 @umask(i32 63)
store i32 %10, i32* %9
%11 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %19, label %14
14: ; preds = %2
%15 = load i32, i32* %9
%16 = call i32 @umask(i32 %15)
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
store i32 %18, i32* %3
br label %59
19: ; preds = %2
%20 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %20, i32** %8
%21 = load i32*, i32** %8
%22 = icmp ne i32* %21, null
br i1 %22, label %30, label %23
23: ; preds = %19
%24 = load i32, i32* %9
%25 = call i32 @umask(i32 %24)
%26 = load i32*, i32** %7
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* @errno
%29 = sub nsw i32 0, %28
store i32 %29, i32* %3
br label %59
30: ; preds = %19
br label %31
31: ; preds = %46, %30
%32 = load i32*, i32** %7
%33 = call %struct.spwd* @fgetspent(i32* %32)
store %struct.spwd* %33, %struct.spwd** %6
%34 = icmp ne %struct.spwd* %33, null
br i1 %34, label %35, label %50
35: ; preds = %31
%36 = load %struct.spwd*, %struct.spwd** %6
%37 = getelementptr inbounds %struct.spwd, %struct.spwd* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = load i8*, i8** %4
%40 = call i32 @strcmp(i32 %38, i8* %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %35
%43 = load i8*, i8** %5
%44 = load %struct.spwd*, %struct.spwd** %6
%45 = getelementptr inbounds %struct.spwd, %struct.spwd* %44, i32 0, i32 0
store i8* %43, i8** %45
br label %46
46: ; preds = %42, %35
%47 = load %struct.spwd*, %struct.spwd** %6
%48 = load i32*, i32** %8
%49 = call i32 @putspent(%struct.spwd* %47, i32* %48)
br label %31
50: ; preds = %31
%51 = load i32*, i32** %8
%52 = call i32 @fclose(i32* %51)
%53 = load i32*, i32** %7
%54 = call i32 @fclose(i32* %53)
%55 = call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @system(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
%57 = load i32, i32* %9
%58 = call i32 @umask(i32 %57)
store i32 0, i32* %3
br label %59
59: ; preds = %50, %23, %14
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.spwd = type { i8*, i32 }
@.str = [12 x i8] c"/smac_users\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [16 x i8] c"/smac_users.tmp\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [15 x i8] c"rm /smac_users\00"
@.str.5 = [34 x i8] c"mv -f /smac_users.tmp /smac_users\00"
define i32 @cmd_smac_adm_user_replace(i8* %0, i8* %1) {
%3 = tail call i32 @umask(i32 63)
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @umask(i32 %3)
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
br label %39
10: ; preds = %2
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call %struct.spwd* @fgetspent(i32* %4)
%15 = icmp eq %struct.spwd* %14, null
br i1 %15, label %33, label %21
16: ; preds = %10
%17 = tail call i32 @umask(i32 %3)
%18 = tail call i32 @fclose(i32* %4)
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
br label %39
21: ; preds = %13, %29
%22 = phi %struct.spwd* [ %31, %29 ], [ %14, %13 ]
%23 = getelementptr inbounds %struct.spwd, %struct.spwd* %22, i64 0, i32 1
%24 = load i32, i32* %23
%25 = tail call i32 @strcmp(i32 %24, i8* %0)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = getelementptr inbounds %struct.spwd, %struct.spwd* %22, i64 0, i32 0
store i8* %1, i8** %28
br label %29
29: ; preds = %27, %21
%30 = tail call i32 @putspent(%struct.spwd* %22, i32* %11)
%31 = tail call %struct.spwd* @fgetspent(i32* %4)
%32 = icmp eq %struct.spwd* %31, null
br i1 %32, label %33, label %21
33: ; preds = %29, %13
%34 = tail call i32 @fclose(i32* %11)
%35 = tail call i32 @fclose(i32* %4)
%36 = tail call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%37 = tail call i32 @system(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
%38 = tail call i32 @umask(i32 %3)
br label %39
39: ; preds = %33, %16, %6
%40 = phi i32 [ 0, %33 ], [ %20, %16 ], [ %9, %6 ]
ret i32 %40
}