#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ LOCK_SH;
int /*<<< orphan*/ LOCK_UN;
char *crypt(char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
scalar_t__ flock(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, char *);
void add_user(char *username, char *passwd) {
  FILE *fd, *fpl;
  fpl = fopen("/var/lock/nh-lock", "r");
  if (!fpl)
    exit(101);
  if (flock(fileno(fpl), LOCK_SH))
    exit(102);
  if ((fd = fopen("/etc/nh-login", "a+")) == NULL)
    exit(103);
  fprintf(fd, "%s,%s\n", username, crypt(passwd, passwd));
  fclose(fd);
  flock(fileno(fpl), LOCK_UN);
  fclose(fpl);
}


x86 assembly (clang, -O0):
<add_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe79(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ac <add_user+0x3c>
mov    $0x65,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
cmp    $0x0,%rax
je     11d9 <add_user+0x69>
mov    $0x66,%edi
call   1090 <exit@plt>
lea    0xe34(%rip),%rdi
lea    0xe3b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1204 <add_user+0x94>
mov    $0x67,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <crypt@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf2(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_user>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe7b(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1228 <add_user+0xb8>
mov    %rax,%r12
mov    %rax,%rdi
call   1060 <fileno@plt>
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1080 <flock@plt>
test   %rax,%rax
jne    1232 <add_user+0xc2>
lea    0xe53(%rip),%rdi
lea    0xe5a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     123c <add_user+0xcc>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r15,%rsi
call   1030 <crypt@plt>
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   1060 <fileno@plt>
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1080 <flock@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>
mov    $0x65,%edi
call   1090 <exit@plt>
mov    $0x66,%edi
call   1090 <exit@plt>
mov    $0x67,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"/var/lock/nh-lock\00"
@.str.1 = [2 x i8] c"r\00"
@LOCK_SH = global i32 0
@.str.2 = [14 x i8] c"/etc/nh-login\00"
@.str.3 = [3 x i8] c"a+\00"
@.str.4 = [7 x i8] c"%s,%s\0A\00"
@LOCK_UN = global i32 0
define void @add_user(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %6
%8 = load i32*, i32** %6
%9 = icmp ne i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 @exit(i32 101)
unreachable
12: ; preds = %2
%13 = load i32*, i32** %6
%14 = call i32 @fileno(i32* %13)
%15 = load i32, i32* @LOCK_SH
%16 = call i64 @flock(i32 %14, i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @exit(i32 102)
unreachable
20: ; preds = %12
%21 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %21, i32** %5
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @exit(i32 103)
unreachable
25: ; preds = %20
%26 = load i32*, i32** %5
%27 = load i8*, i8** %3
%28 = load i8*, i8** %4
%29 = load i8*, i8** %4
%30 = call i8* @crypt(i8* %28, i8* %29)
%31 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %27, i8* %30)
%32 = load i32*, i32** %5
%33 = call i32 @fclose(i32* %32)
%34 = load i32*, i32** %6
%35 = call i32 @fileno(i32* %34)
%36 = load i32, i32* @LOCK_UN
%37 = call i64 @flock(i32 %35, i32 %36)
%38 = load i32*, i32** %6
%39 = call i32 @fclose(i32* %38)
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"/var/lock/nh-lock\00"
@.str.1 = [2 x i8] c"r\00"
@LOCK_SH = global i32 0
@.str.2 = [14 x i8] c"/etc/nh-login\00"
@.str.3 = [3 x i8] c"a+\00"
@.str.4 = [7 x i8] c"%s,%s\0A\00"
@LOCK_UN = global i32 0
define void @add_user(i8* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @exit(i32 101)
unreachable
7: ; preds = %2
%8 = tail call i32 @fileno(i32* %3)
%9 = load i32, i32* @LOCK_SH
%10 = tail call i64 @flock(i32 %8, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %7
%13 = tail call i32 @exit(i32 102)
unreachable
14: ; preds = %7
%15 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @exit(i32 103)
unreachable
19: ; preds = %14
%20 = tail call i8* @crypt(i8* %1, i8* %1)
%21 = tail call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %20)
%22 = tail call i32 @fclose(i32* %15)
%23 = tail call i32 @fileno(i32* %3)
%24 = load i32, i32* @LOCK_UN
%25 = tail call i64 @flock(i32 %23, i32 %24)
%26 = tail call i32 @fclose(i32* %3)
ret void
}